// Computer Science 367, Section 3, Fall 1997 // Instructor: Michael Siff (siff@cs.wisc.edu) // // // FILE: binary-tree.h // // // CLASS TEMPLATE PROVIDED: // // BinaryTree - a binary tree of Items. BinaryTree is derived // from AbstractBinaryTree. The template parameter, Item, is the // data type of the items in the binary 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. // // // CONSTRUCTORS for the BinaryTree template class: // BinaryTree(Item v) // Postcondition: The binary tree has been initialized as a leaf node // with value v. // BinaryTree(Item v, BinaryTree *L, BinaryTree *R) // Precondition: L and R are non-overlapping binary trees. // Postcondition: A binary tree has been constructed with a root of // value v and a left child as subtree L and right child as subtree R. // BinaryTree(const BinaryTree& source) // Postcondition: The binary tree has been initialized as a complete copy of // source. (This is the copy constructor.) // // // DESTRUCTOR for the BinaryTree template class: // ~BinaryTree() // Postcondition: The memory allocated to store the binary tree has // been freed. // // // MODIFICATION MEMBER FUNCTIONS for the BinaryTree template class: // void operator=(const BinaryTree& source) // Postcondition: This tree has been assigned as a complete copy of // source. // void setVal(const Item& v) // Postcondition: the value of the (node of) this tree is now v. // BinaryTree *setLeft(BinaryTree *T) // Precondition: The right subtree has no overlap with T. // Postcondition: the left child of this tree is now the subtree T. The // return value is a pointer to the former left subtree of this tree. // BinaryTree *setRight(BinaryTree *T) // Precondition: the current left subtree has no overlap with T. // Postcondition: the right child of this tree is now the subtree T. The // return value is a pointer to the former right subtree of this tree. // // // CONSTANT MEMBER FUNCTIONS for the BinaryTree template class: // Item valOf() const // Postcondition: the return value is the value of the (node of) this tree. // BinaryTree *leftOf() const // Postcondition: the return value is a pointer to the left child of // this tree or NULL if this node has no left child. // BinaryTree *rightOf() const // Postcondition: the return value is a pointer to the right child of // this tree or NULL if this node has no right child. // BinaryTree *parentOf() const // Postcondition: the return value is a pointer to the parent of this // node or NULL if this node is the root. // size_t size() const // Postcondition: the return value is the number of nodes in the tree // rooted at this node. // size_t height() const // Postcondition: the return value is the height of the tree rooted at // this node. // size_t depth() const // Postcondition: the return value is the depth of this node. #ifndef BINARY_TREE_H #define BINARY_TREE_H template class BinaryTree { public: // Constructors BinaryTree(Item v); // create a leaf consisting of v BinaryTree(Item v, BinaryTree *L, BinaryTree *R); // merge BinaryTree(const BinaryTree& source); // copy constructor // Destructor ~BinaryTree(); // Constant member functions size_t depth() const; size_t height() const; size_t size() const; Item valOf() const; BinaryTree *leftOf() const; BinaryTree *rightOf() const; BinaryTree *parentOf() const; // Modification member functions void operator=(const BinaryTree& source); // assignment op void setVal(const Item& v); BinaryTree *setLeft(BinaryTree *T); BinaryTree *setRight(BinaryTree *T); private: Item val; BinaryTree *left; BinaryTree *right; BinaryTree *parent; // private member functions: void deleteTree(); void copyTree(const BinaryTree *source); }; #include "binary-tree.C" #endif