// BST.cpp // // Chris Nevison // March 13, 1997 // // Modified, Chris Nevison // May 2, 2000 // // Modified, G. Volger // March 13, 2001 // // Modification adds a function that returns contents in order // in an apvector. 5-2-2000 // // This file provides the implementation for a map or directory // structure which stores items which can be looked up using one // part of the item stored, the key. When the item is stored, the key // under which it is stored is specified separately. // Each item in the directory must have a unique key. If // an item is inserted with the same key as an item already in the // directory, then the old item is removed (overwritten) by the new. // // The directory is written as a template for two types, the type of // the key and the type of the items stored. The key type must have the // operators ==, !=, <, and >, and assignment (=) defined. template Directory::Directory() : myRoot(NULL) {} template Directory::~Directory() { FreeTree(myRoot); } //////////////// Accessors //////////////////// template bool Directory::Lookup(const KeyType & key, ItemType & item) const // post: if there is an item which matches the key in the directory, // Lookup returns it as item and returns true, otherwise // item is unchanged and Lookup returns false { return LookHelp(key, myRoot, item); } template apqueue Directory::InorderList() const { apqueue itemList; InorderHelp(itemList, myRoot); return itemList; } template void Directory::Display() const // post: Outputs the contents of the directory in order { } ////////// Modifiers //////////// template void Directory::Insert(const ItemType & item, const KeyType & key) // post: item has been inserted into the directory { InsHelp(myRoot, item, key); } template void Directory::Remove(const KeyType & key) // post: key has removed from the directory if possible { RemHelp(myRoot, key); } //////////////////// Helper functions ///////////////// template void Directory::FreeTree(TNode * T) // pre: T is a binary search tree // post: the memory for all nodes below T in the tree has been deallocated // and the memory for the node pointed to by T has been deallocated { } template void Directory::InsHelp(TNode * & T, const ItemType & item, const KeyType & key) // pre: T is a non-empty binary search tree // post: if any item in the tree had the same key as the given item, // that item has been replaced by the given item, otherwise a // node has been added to the tree containing the given item // so as to preserve the binary search tree ordering. { } template bool Directory::LookHelp(const KeyType & key, TNode * T, ItemType & item) const // pre: T is a binary search tree // post: if any item in the tree matches the given key, // that item is returned as item and LookHelp returns true, // otherwise LookHelp returns false. { } template void Directory::RemHelp(TNode * & root, const KeyType & key) // pre: T is a binary search tree // post: The key node is removed from the tree if it is found // If key is not found then the tree is not altered { } template void Directory::InorderHelp(apqueue & list, TNode * T) const // pre: T is a binary search tree // post: Stores in the nodes in the BST in the queue using an Inorder traversal { } template void Directory::DisplayHelp(TNode * T) const // Preorder traversal for debugging only { }