// tree_I.h template class Binary_Tree_Node { T Element; Binary_Tree_Node *Left; Binary_Tree_Node *Right; Binary_Tree_Node() { Left = NULL; Right= NULL; } Binary_Tree_Node(T value) { Element = value; Left = NULL; Right= NULL; } friend class Binary_Tree; }; template class Binary_Tree { private: Binary_Tree_Node *Root; public: Binary_Tree() { Root=NULL; } ~Binary_Tree() { if ( Root == NULL ) return; Empty_Subtree(Root); delete Root; Root = NULL; } void Binary_Tree::Empty_Subtree(Binary_Tree_Node * ptr); void Binary_Tree::Print_Inorder(Binary_Tree_Node * ptr); void Binary_Tree::Print_Inorder() { Print_Inorder(Root); }; void Binary_Tree::Insert(const T & Value ); Binary_Tree_Node * Binary_Tree::Find(const T & Value); Binary_Tree_Node * Binary_Tree:: Find_Recursive(Binary_Tree_Node * ptr, const T & Value); Binary_Tree_Node * Binary_Tree:: Find_Recursive(const T & Value) { return(Find_Recursive(Root,Value));}; Binary_Tree_Node * Binary_Tree::Find_Parent(const T & Value); int Binary_Tree::Depth() { return(Depth(Root)); }; int Binary_Tree::Depth(Binary_Tree_Node * ptr); void Binary_Tree:: Print_Fancy(int flags[],int depth,Binary_Tree_Node * ptr); void Binary_Tree:: Print_Fancy(int flags[],int depth) { Print_Fancy(flags,depth,Root);}; }; // End of the class template void Binary_Tree::Print_Inorder(Binary_Tree_Node * ptr) { if ( ptr == NULL ) return; Print_Inorder(ptr->Left); cout << ptr->Element << "\n"; Print_Inorder(ptr->Right); } template Binary_Tree_Node * Binary_Tree::Find(const T & Value) { Binary_Tree_Node *ptr; ptr = Root; while(ptr != NULL ) { if (ptr->Element == Value ) break; if (Value < ptr->Element) { ptr = ptr->Left; } else { ptr = ptr->Right; } } return(ptr); } template Binary_Tree_Node * Binary_Tree:: Find_Recursive(Binary_Tree_Node * ptr, const T & Value) { Binary_Tree_Node * retval; if ( ptr == NULL ) { retval = NULL; } else if (ptr->Element == Value ) { retval = ptr; } else if (Value < ptr->Element) { retval = Find_Recursive(ptr->Left,Value); } else { retval = Find_Recursive(ptr->Right,Value); } return(retval); } template Binary_Tree_Node * Binary_Tree::Find_Parent(const T & Value) { Binary_Tree_Node *ptr; Binary_Tree_Node *savptr; ptr = Root; savptr = Root; while(ptr != NULL ) { savptr = ptr; if (ptr->Element == Value ) break; if (Value < ptr->Element) { ptr = ptr->Left; } else { ptr = ptr->Right; } } return(savptr); } template void Binary_Tree::Insert(const T & Value) { Binary_Tree_Node * parent; Binary_Tree_Node * tmp; parent = Find_Parent(Value); if ( parent != NULL ) { // Check for equality if ( parent->Element == Value ) return; } tmp = new Binary_Tree_Node; tmp->Element = Value; if ( parent == NULL ) { cout << "Inserting as the Root"; Root = tmp; } else if ( Value < parent->Element ) { cout << "Inserting to the Left of " << parent->Element << "\n"; parent->Left = tmp; } else { cout << "Inserting to the right of " << parent->Element << "\n"; parent->Right = tmp; } } template int Binary_Tree::Depth(Binary_Tree_Node * ptr) { int dl,dr; if ( ptr == NULL ) return 0 ; dl = Depth(ptr->Left); dr = Depth(ptr->Right); if ( dl > dr ) return(dl); return(dr); } template void Binary_Tree::Empty_Subtree(Binary_Tree_Node * ptr) { if ( ptr == NULL ) return; Empty_Subtree(ptr->Left); if ( ptr->Left != NULL ) delete ptr->Left; Empty_Subtree(ptr->Right); if ( ptr->Right != NULL ) delete ptr->Right; } template void Binary_Tree:: Print_Fancy(int flags[],int depth,Binary_Tree_Node * ptr) { int i; if ( ptr == NULL ) return; cout << "- " << ptr->Element << " -" ; if ( ptr->Right == NULL ) { cout << "\n"; } flags[depth] = ptr->Left != NULL; depth++; if (ptr->Right != NULL ) { Print_Fancy(flags,depth,ptr->Right); } if ( ptr->Left == NULL ) return; cout << " "; for(i=0;i 0 ) cout << "|--"; if ( depth > 0 ) flags[depth-1] = 0; Print_Fancy(flags,depth,ptr->Left); }