主函数文件

 
  
  1. #pragma once 
  2. #include "BinaryTree.h" 
  3.  
  4. int main(){ 
  5.     BinaryTree<int> a; 
  6.     a.build(  a.getDRoot()); 
  7.     cout<<"a.levelOrder(a.getRoot() );"<<endl; 
  8.     a.levelOrder(a.getRoot() );   //right! 
  9.     cout<<"a.postOrder(a.getRoot());"<<endl; 
  10.     a.postOrder(a.getRoot()); 
  11.     cout<<endl; 
  12.     cout<<"a.inOrder(a.getRoot());"<<endl; 
  13.     a.inOrder(a.getRoot()); 
  14.     cout<<"a.preOrder(a.getRoot());"<<endl; 
  15.     a.preOrder(a.getRoot()); 
  16.     cout<<"a.deleteBinaryTree(a.getRoot()->getLeftChild());"<<endl; 
  17.     a.deleteBinaryTree(a.getRoot()->getLeftChild()); 
  18.     a.levelOrder(a.getRoot() ); 
  19.     exit(0); 

BinaryTreeNote.h

 

 
  
  1. BinaryTreeNote<T>* BinaryTreeNote<T>::getLeftChild(){ 
  2.     return this->left; 
  3. template<class T> 
  4. BinaryTreeNote<T>* BinaryTreeNote<T>::getRightChile(){ 
  5.     return this->right; 
  6. template<class T> 
  7. T BinaryTreeNote<T>::getValue()const
  8.     return this->data; 
  9. template<class T> 
  10. bool BinaryTreeNote<T>::isLeaf()const { 
  11.     return ! (this->left||this->right); 
  12. template<class T>  
  13. void BinaryTreeNote<T>::setLeftChild(BinaryTreeNote* l ){ 
  14.     this->left=l; 
  15. template<class T> 
  16. void BinaryTreeNote<T>::setRightChild(BinaryTreeNote* r ){ 
  17.     this->right=r; 
  18. template<class T> 
  19. BinaryTreeNote<T>::~BinaryTreeNote(){ 
  20.      

BinaryTree.h

 

 
  
  1. #pragma once 
  2. #include<iostream> 
  3. #include<queue> 
  4. #include<stack> 
  5. #include"BinaryTreeNote.h" 
  6. #include<string> 
  7. using namespace std; 
  8.  
  9. template<class T> 
  10. class BinaryTree{ 
  11.     BinaryTreeNote<T> *rt; 
  12.     int num; 
  13. public
  14.     BinaryTree(); 
  15.     ~BinaryTree(); 
  16.     bool isEmpty( ) const
  17.     BinaryTreeNote<T>* getRoot()const;  //return root note 
  18.     BinaryTreeNote<T>* getParent(BinaryTreeNote<T>* current )const;   //return parent note 
  19.     BinaryTreeNote<T>* getLeftSibing(BinaryTreeNote<T>* current ) const;  //return left brother 
  20.     BinaryTreeNote<T>* getRightSibing(BinaryTreeNote<T>* current ) const;  //return right brother 
  21.     void breadFristOrder(BinaryTreeNote<T>* );  //广度优先遍历 
  22.     void preOrder(BinaryTreeNote<T>* );  //  前序 
  23.     void inOrder(BinaryTreeNote<T>* );  //中序 
  24.     void postOrder(BinaryTreeNote<T>* );   //后序 
  25.     void levelOrder(BinaryTreeNote<T>* );   // 
  26.     void deleteBinaryTree(BinaryTreeNote<T>* root);  //delete the tree which is the charge of root 
  27.     void build(BinaryTreeNote<T>** ); 
  28.     BinaryTreeNote<T>** getDRoot(){ 
  29.         return &rt; 
  30.     }  
  31. }; 
  32. template<class T> 
  33. BinaryTree<T>::BinaryTree( ):rt(new BinaryTreeNote<T>),num(0){ } 
  34. template<class T> 
  35. bool BinaryTree<T>::isEmpty( )const
  36.     return !rt; 
  37. template<class T> 
  38. BinaryTreeNote<T>* BinaryTree<T>::getRoot() const
  39.     return rt; 
  40. template<class T> 
  41. BinaryTreeNote<T>* BinaryTree<T>::getParent(BinaryTreeNote<T>* current ) const { 
  42.     queue<BinaryTreeNote<T>*> n; 
  43.     BinaryTreeNote<T>* cur; 
  44.     n.push(this->getRoot()); 
  45.     while(!n.empty() ){ 
  46.         cur=n.front(); 
  47.         n.pop(); 
  48.         if(cur->getLeftChild()==current ||cur->getRightChile()==current  ) 
  49.             return cur; 
  50.         if( cur->getLeftChild()!=0) 
  51.             n.push(cur->getLeftChild); 
  52.         else if(cur->getRightChile()) 
  53.             n.push(cur->getRightChile()); 
  54.     } 
  55. template<class T> 
  56. BinaryTreeNote<T>* BinaryTree<T>::getLeftSibing(BinaryTreeNote<T>* current ) const
  57.     ifthis->getParent()==NULL) 
  58.         return NULL; 
  59.     return this->getParent(current)->getLeftChild(); 
  60. template<class T> 
  61. BinaryTreeNote<T>* BinaryTree<T>::getRightSibing(BinaryTreeNote<T>* current ) const
  62.     ifthis->getParent()==NULL) 
  63.         return NULL; 
  64.     return this->getParent(current)->getRightChile(); 
  65. template<class T> 
  66. void  BinaryTree<T>::breadFristOrder( BinaryTreeNote<T>* root ){ 
  67.     queue<BinaryTreeNote<T>*> local; 
  68.     local.push(root ); 
  69.     BinaryTreeNote<T>* index=local.front(); 
  70.     while( !local.empty() ){ 
  71.         if( index ){ 
  72.             local.push(index->getLeftChild()); 
  73.             local.push(index->getRightChile()); 
  74.             cout<<index->getValue()<<" "
  75.         } 
  76.         local.pop(); 
  77.     } 
  78.     cout<<endl; 
  79. template<class T> 
  80. void  BinaryTree<T>::preOrder(BinaryTreeNote<T>* root) { 
  81.     BinaryTreeNote<T>* cur=root; 
  82.     stack<BinaryTreeNote<T>*> n; 
  83.     while( cur || !n.empty() ){ 
  84.         if( cur ){ 
  85.             cout<<cur->data<<" "
  86.             if(cur->getRightChile() ) 
  87.                 n.push(cur->getRightChile()); 
  88.             cur=cur->getLeftChild(); 
  89.         } 
  90.         else
  91.             cur=n.top(); 
  92.             n.pop(); 
  93.         } 
  94.     } 
  95.     cout<<endl; 
  96. template<class T> 
  97. void  BinaryTree<T>::inOrder(BinaryTreeNote<T>* root){ 
  98.     //关键部分就是把一个过程抽象成一步一步执行相同的过程! 
  99.     BinaryTreeNote<T>* cur=root; 
  100.     stack<BinaryTreeNote<T>* > n; 
  101.     while( !n.empty() || cur!=0  ){ 
  102.         if(cur ){ 
  103.             n.push(cur); 
  104.             cur=cur->getLeftChild(); 
  105.         } 
  106.         else
  107.             cur=n.top(); 
  108.             cout<<cur->getValue()<<" "
  109.             cur=cur->getRightChile(); 
  110.             n.pop(); 
  111.         } 
  112.     } 
  113.     cout<<endl; 
  114. template<class T> 
  115. void  BinaryTree<T>::postOrder(BinaryTreeNote<T>* root){ 
  116.     stack<BinaryTreeNote<T>* > n; 
  117.     BinaryTreeNote<T>* cur=root; 
  118.     BinaryTreeNote<T>* pre=root; 
  119.     while( cur ){ 
  120.         while( cur->getLeftChild() ){ 
  121.             n.push(cur); 
  122.             cur=cur->getLeftChild(); 
  123.         } 
  124.         while( cur &&( cur->getRightChile()==0 || cur->getRightChile()==pre ) ){ 
  125.             cout<<cur->getValue()<<" "
  126.             pre=cur; 
  127.             if(n.empty()) 
  128.                 return
  129.             cur=n.top(); 
  130.             n.pop(); 
  131.         } 
  132.         n.push(cur); 
  133.         cur=cur->getRightChile(); 
  134.     } 
  135.     cout<<endl; 
  136. template<class T> 
  137. void  BinaryTree<T>::levelOrder(BinaryTreeNote<T>* root){ 
  138.     if(!root) 
  139.         return ; 
  140.     queue<BinaryTreeNote<T>* > n; 
  141.     BinaryTreeNote<T>* cur; 
  142.     n.push(root); 
  143.     while(!n.empty() ){ 
  144.         cur=n.front(); 
  145.         n.pop(); 
  146.         cout<<cur->getValue()<<" "
  147.         if( cur->getLeftChild()!=0) 
  148.             n.push(cur->getLeftChild()); 
  149.         if(cur->getRightChile()!=0) 
  150.             n.push(cur->getRightChile()); 
  151.     } 
  152.     cout<<endl; 
  153. template<class T> 
  154. void  BinaryTree<T>::deleteBinaryTree(BinaryTreeNote<T>* root){ 
  155.     if(!root) 
  156.         return ; 
  157.     queue<BinaryTreeNote<T>* > n; 
  158.     BinaryTreeNote<T>* cur; 
  159.     n.push(rt); 
  160.     if(root==rt) 
  161.         rt=0; 
  162.     while(!n.empty() ){ 
  163.         cur=n.front(); 
  164.         n.pop(); 
  165.         if(cur->getLeftChild()==root ){ 
  166.             cur->left=0; 
  167.             break
  168.         } 
  169.         else if(cur->getRightChile()==root ){ 
  170.             cur->right=0; 
  171.             break
  172.         } 
  173.         if( cur->getLeftChild()!=0) 
  174.             n.push(cur->getLeftChild()); 
  175.         if(cur->getRightChile()!=0) 
  176.             n.push(cur->getRightChile()); 
  177.     } 
  178. template<class T> 
  179. BinaryTree<T>::~BinaryTree(){ 
  180.     this->deleteBinaryTree (this->rt); 
  181. //下面是一段2重的指针代码....... 
  182. template<class T> 
  183. void BinaryTree<T>::build(BinaryTreeNote<T>** root){ 
  184.     T h; 
  185.     static int ll=0; 
  186.     if(ll++==0) 
  187.         cout<<"root:"
  188.     if(cin>>h){ 
  189.         *root=new BinaryTreeNote<T>; 
  190.         num++; 
  191.         (*root)->data=h; 
  192.         cout<<h<<" left:";       
  193.         build(&((*root)->left)); 
  194.         cout<<h<<" right:"
  195.         build(&((*root)->right)); 
  196.     } 
  197.     else
  198.         cin.clear(); 
  199.         return ; 
  200.     }