// Computer Science 367, Section 3, Fall 1997 // Instructor: Michael Siff (siff@cs.wisc.edu) // // // FILE: bst.h // // // TEMPLATE CLASS PROVIDED: BST (a container template class // for a binary search tree of Items indexed by their Keys) // // The template parameter Item is the data type of the items stored in // the binary search tree. It may be any of the C++ built-in types (int, // char, etc.), or a class with a default constructor, a copy // constructor, and an assignment operator. // The template parameter Key is the data type of the priority key // associated with each item. It may be any C++ built-in type that has a // comparison operator (<), or a class with a copy constructor, // assignment operator, equality(==), inequality(!=), and comparison(<). // // // CONSTRUCTORS for the BST template class: // BST() // Postcondition: The binary search tree has been initialized as an // empty tree. // BST(const BST& source) // Postcondition: The binary search tree has been initialized as a // complete copy of source. (This is the copy constructor.) // // // DESTRUCTOR for the BST template class: // ~BST() // Postcondition: The memory allocated to store the binary search tree // has been freed. // // // MODIFICATION MEMBER FUNCTIONS for the BST template class: // void operator=(const BST& source) // Postcondition: This tree has been assigned as a complete copy of // source. // bool insert(const Item& v, const Key& k) // Postcondition: If an item of key k is not already in the tree, v has // been inserted into the tree with key k and true is // returned. Otherwise, the tree remains the same and false is returned. // bool remove(const Key& v) // Postcondition: If an item of key k was in the tree, it has been // deleted from the tree and true is returned. Otherwise, the tree // remains the same and false is returned. // // // CONSTANT MEMBER FUNCTIONS for the BST template class: // bool isEmpty() const // Postcondition: true if and only if the tree is empty. // bool search(const Key& k, Item& returnVal) const // Postcondition: If an item of key k is in the tree, returnVal is // assigned that item and true is returned. Otherwise, returnVal // remains the same and false is returned. // size_t size() const // Postcondition: the return value is the number of nodes in the binary // search tree. // size_t height() const // Precondition: the tree is not empty. // Postcondition: the return value is the height of the binary search tree. // bool hasBSTP() const // Postcondition: true iff the BST satisfies the binary search tree property. // bool hasAVLP() const // Postcondition: true iff the BST satisfies the AVL property. // void displayTree(ostream& (*outputItem)(ostream&,const Item&), // ostream& (*outputKey)(ostream&,const Key&)) const // Postcondition: the contents of the tree is displayed in reverse in // order with node contents indented according to depth (and according // to private constant SPACES_PER_INDENT), and the item and key values // displayed with outputItem and outputKey. #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include #include #include #include "binary-tree.h" template class BST { public: // Constructors BST(); BST(const BST& source); // copy constructor // Destructor ~BST(); // Constant member functions bool isEmpty() const; size_t size() const; size_t height() const; bool search(const Key& k, Item& returnVal) const; bool hasBSTP() const; bool hasAVLP() const; void displayTree(ostream& (*outputItem)(ostream&,const Item&), ostream& (*outputKey)(ostream&,const Key&)) const; // Modification member functions void operator=(const BST& source); bool insert(const Item& v, const Key& k); bool remove(const Key& v); private: typedef BinaryTree > BT; // utilities to test if bst has certain properties bool _hasBSTP(BT *t) const; bool _hasAVLP(BT *t, size_t&) const; // output utilities and parameters static const size_t SPACES_PER_INDENT=2; ostream& printNode(ostream& os, BT *t, ostream& (*outputItem)(ostream&,const Item&), ostream& (*outputKey)(ostream&,const Key&)) const; void indentedPrint(BT *t, size_t n, ostream& (*outputItem)(ostream&,const Item&), ostream& (*outputKey)(ostream&,const Key&)) const; protected: BT *root; // protected member functions Item itemOf(const pair& ikp) const {return ikp.first;} Key keyOf(const pair& ikp) const {return ikp.second;} bool isRightChild(BT *node) const; BT *removeNode(BT *node); BT *successor(BT *node) const; BT *_search(BT *t, const Key& k) const; BT *_insert(const pair& ikp); void _remove(BT *node); }; #include "bst.C" #endif