# Ifndef mybinarytree_h # define mybinarytree_htemplate <class T> class binarytree {protected: struct tnode {T val; tnode * parent; tnode * left; tnode * right; tnode (t, tnode * p = 0, tnode * l = 0, tnode * r = 0): Val (t), parent (P), left (L), right (r) {If (! Parent) parent = This ;}}; PRIVATE: tnode * _ root; public: Class titerator {binarytree * tree; tnode * node; public: Friend class binarytree; titerator (): tree (0), node (0) {} titerator (const titerator & IT): tree (it. tree), node (it. node) {} titerator (binarytree * t, tnode * n = 0): tree (t), node (n) {} titerator & operator = (const titerator & it) {tree = it. tree; node = it. node; return * This;} bool operator = (const titerator & IT) {return (tree = It. Tree & node = It. node);} bool Operator! = (Const titerator & IT) {return! (* This = It);} titerator & operator ++ () {node = preordersuccessor (node); return * This;} titerator operator ++ (INT) {titerator it (* This); node = preordersuccessor (node); return it;} T & operator * () {return node-> val;} tnode * operator-> () {return node;} const T & operator * () const {return node-> val;} const tnode * operator-> () const {return node;} bool Operator! () {Return node = 0;} protected:}; binarytree (): _ root (0) {} binarytree (const binarytree & B) {_ root = clone (B. _ root, 0);} binarytree (const T & T): _ root (New tnode (t) {_ root-> parent = _ root ;} binarytree (const T & T, const binarytree & L, const binarytree & R );~ Binarytree (); binarytree & operator = (const binarytree &); static void clear (tnode *); // clear static tnode * preordersuccessor (tnode *) by recursive post-traversal *); // returns the static tnode * clone (tnode *, tnode *) of the next node in the forward order. // traverses and copies titerator begin () in the recursive forward order (); titerator end (); bool empty () const; int size () const; // returns the tree size static int getsize (tnode *); // recursively calculates the size of int leaves () const; // returns the number of leaf columns. Static int getleaf (tnode *); // recursively calculates the number of leaf columns. Int height () const; // returns the tree height S. Tatic int getheight (tnode *); // recursively calculates the height of int level (titerator it) const; // returns the current layer static int getlevel (tnode *); // recursively calculate the layer void reflect (); // switch the Left and Right subnodes of all nodes void defoliate (); // fallen leaves, delete all leaf nodes static void deletenode (tnode * n); // recursively Delete leaf node T & getroot () const; static bool isroot (titerator ); // determine whether the current iterator is the root node static bool isleaf (titerator); // determine whether the current iterator is the leaf node static titerator getparent (titerator ); // return the parent node static titerator leftch of the current iterator ILD (titerator); // returns the static titerator rightchild (titerator) on the left subnode of the current iterator; // returns the static titerator find (titerator, titerator, const T &); // search for friend class titerator;}; Template <class T> binarytree <t >:: binarytree (const T & T, const binarytree & ltree, const binarytree & rtree) {_ root = new tnode (t); _ root-> left = clone (ltree. _ root, _ root); _ root-> right = clone (rtree. _ root, _ root);} template <class T> binarytree <t> ::~ Binarytree () {If (_ root! = 0) delete _ root;} template <class T> binarytree <t> & binarytree <t>: Operator = (const binarytree & T) {clear (_ root ); binarytree * temp = new binarytree (t); _ root = temp-> _ root; return * This;} template <class T> void binarytree <t> :: clear (tnode * n) // recursive post-order traversal {If (n = 0) return; clear (n-> left); clear (n-> right ); delete n ;}template <class T> typename binarytree <t >:: tnode * binarytree <t >:: preordersuccessor (tnode * n )// Previous traversal {If (n = 0) return N; If (n-> left) return N-> left; If (n-> right) return N-> right; while (n-> parent! = N & (n-> parent-> right = n | n-> parent-> right = 0) n = N-> parent; if (n-> parent = N) return 0; return N-> parent-> right;} template <class T> typename binarytree <t> :: tnode * binarytree <t>: Clone (tnode * root, tnode * parent) // recursively traversing the previous {If (! Root) return 0; tnode * temp = new tnode (root-> Val, parent); temp-> left = clone (root-> left, temp ); temp-> right = clone (root-> right, temp); Return temp;} template <class T> typename binarytree <t >:: titerator binarytree <t> :: begin () {return titerator (this, _ root);} template <class T> typename binarytree <t >:: titerator binarytree <t >:: end () {return titerator (this, 0);} template <class T> bool binarytree <t>: Empty () con St {return _ root = 0;} template <class T> int binarytree <t>: getsize (tnode * n) // recursively calculates the tree size {If (! N) return 0; int sizel = getsize (n-> left); int sizer = getsize (n-> right); return 1 + sizel + sizer ;} template <class T> int binarytree <t>: size () const {return getsize (_ root);} template <class T> int binarytree <t> :: getleaf (tnode * n) // recursively calculates the number of leaves {If (! N) return 0; If (n-> left = 0 & N-> right = 0) return 1; return getleaf (n-> left) + getleaf (n-> right);} template <class T> int binarytree <t >:: leaves () const {return getleaf (_ root );} template <class T> int binarytree <t>: getheight (tnode * node) // recursively calculates the height {If (! Node) Return-1; int HL = getheight (node-> left); int hR = getheight (node-> right); int sum = (HL> HR? Hl: HR); return 1 + sum;} template <class T> int binarytree <t>: height () const {return getheight (_ root );} template <class T> int binarytree <t>: getlevel (tnode * node) // recursively calculates the number of layers {If (node = node-> parent) return 0; int sum = getlevel (node-> parent); Return sum + 1 ;}template <class T> int binarytree <t >:: level (titerator it) const {return getlevel (it. node);} template <class T> void binarytree <t>: reflect () // exchange the Left and Right subnodes of all nodes {For (titerator it = begin (); it! = End (); It ++) {If (it. node-> left & it. node-> right) {T temp = it. node-> left-> val; it. node-> left-> val = it. node-> right-> val; it. node-> right-> val = temp; }}template <class T> void binarytree <t>: deletenode (tnode * n) // recursively Delete the leaf node {tnode * nodel = N-> left; If (nodel & (nodel-> left | nodel-> right) deletenode (nodel ); else {Delete nodel; n-> left = 0;} tnode * noder = N-> right; if (noder & (noder-> left | noder-> righ T) deletenode (noder); else {Delete noder; n-> right = 0 ;}} template <class T> void binarytree <t >:: defoliate () // fallen leaves, delete all leaf nodes {If (! _ Root) return; If (_ root-> left | _ root-> right) deletenode (_ root); else clear (_ root );} template <class T> T & binarytree <t>: getroot () const {If (! _ Root) return T (); Return _ root-> val;} template <class T> bool binarytree <t >:: isroot (titerator it) // determine whether the current iterator is the root node {return it. node = It. node-> parent;} template <class T> bool binarytree <t>: isleaf (titerator it) // judge whether the current iterator is a leaf node {return! (It. node-> left | it. node-> right);} template <class T> typename binarytree <t >:: titerator binarytree <t >:: getparent (titerator it) // return the parent node {If (! It. node-> parent) return titerator (it. tree, 0); Return titerator (it. tree, it. node-> parent);} template <class T> typename binarytree <t >:: titerator binarytree <t >:: leftchild (titerator it) // return the left subnode {If (! It. node-> left) return titerator (it. tree, 0); Return titerator (it. tree, it. node-> left);} template <class T> typename binarytree <t >:: titerator binarytree <t >:: rightchild (titerator it) // return the right subnode {If (! It. node-> right) return titerator (it. tree, 0); Return titerator (it. tree, it. node-> right);} template <class T> typename binarytree <t >:: titerator binarytree <t >:: find (titerator first, titerator last, const T & T) // search for {for (titerator it (first); it within the range of the front and back iterators! = Last; it ++) {If (t = It. node-> Val) {return it;} return titerator (first. Tree, 0);} # endif