/* File: bintree.H Copyright glh 8/15/94 */ #ifndef BINTREEH #define BINTREEH #include #include typedef enum {false, true} Boolean; typedef enum {left, right} Child; // forward references for template classes template class BinaryTree; template class BST; template class Bnode { friend class BinaryTree; friend class BST; friend ostream& operator<<(ostream&, const BinaryTree&); protected: T data; Bnode* parent; Bnode* lchild; Bnode* rchild; public: Bnode() {} Bnode(const T& elm) : data(elm), parent(0), lchild(0), rchild(0) {} friend ostream& operator<<(ostream& os, const Bnode& v); }; template class BinaryTree { protected: Bnode* root; public: BinaryTree() : root(0) {} BinaryTree(const T& elm); inline ~BinaryTree(); inline Bnode* Root(); inline Bnode* LeftChild(Bnode* nd); inline Bnode* RightChild(Bnode* nd); Boolean InsertLeaf(Bnode* leaf, Bnode* prnt, Child lr); inline Boolean Empty(); void DeleteSubtree(Bnode*); void MakeEmpty(); void InorderSubtreePrint(ostream& os, Bnode*) const; friend ostream& operator<<(ostream& os, const BinaryTree& tree); }; template inline BinaryTree::~BinaryTree() {MakeEmpty();} template inline Bnode* BinaryTree::LeftChild(Bnode* nd) {return nd->lchild;} template inline Bnode* BinaryTree::RightChild(Bnode* nd) {return nd->rchild;} template inline Bnode* BinaryTree::Root() {return root;} template inline Boolean BinaryTree::Empty() {return root == 0 ? true : false;} #endif