二叉树的创建及遍历

这里创建二叉树运用了即遍历,都进行了函数的重载,并且遍历的时候,只是顺序的改变;

难点:层次遍历的方法,在这里进行了一下总结。其中包括vector , queue, deque, 等多种方法:

[cpp]  view plain copy
  1. #include "iostream"  
  2. #include "vector"  
  3. #include "malloc.h"  
  4. #include "queue"  
  5. using namespace std;  
  6.   
  7. //二叉树   //先序顺序进行创建  
  8. template<class Object>  
  9. class BinaryNode;  
  10.   
  11. template<class Object>  
  12. class BinaryTree  
  13. {  
  14. private:  
  15.     BinaryNode<Object> *root;   //想要对private进行创建,建立一个重载的函数  
  16. public:  
  17.     BinaryTree()  
  18.     {  
  19.         root = 0;  
  20.     }  
  21.     ~BinaryTree()  
  22.     {  
  23.         delete root;  
  24.     }  
  25.     void create();    
  26.     bool create(BinaryNode<Object> *&T);  //define 构造二叉树  //递归创建  
  27.       
  28.     //先序,中序,后序,只是输出顺序不同,在操作中递归排列输出顺序。  
  29.     void preOrderTraverse();  
  30.     void inOrderTraverse();  
  31.     void postOrderTraverse();  
  32.     void levelOrderTraverse();  
  33.     void pirntBFS();  
  34.     void printBFS_();  
  35.     void printNodeByLevel();  
  36.     void printNodeByLevel_();  
  37.     bool preOrderTraverse(BinaryNode<Object> *T);   //还可以将遍历完的结点加入到队列中,最后输出。这样就遍历完毕  
  38.     bool inOrderTraverse(BinaryNode<Object> *T);  
  39.     bool postOrderTraverse(BinaryNode<Object> *T);  
  40.     bool levelOrderTraverse(BinaryNode<Object> *T);  
  41.     void pirntBFS(BinaryNode<Object> *root);  
  42.     void printBFS_(BinaryNode<Object> *root);  
  43.     void printNodeByLevel(BinaryNode<Object> *root);  
  44.     void printNodeByLevel_(BinaryNode<Object> *root);  
  45.   
  46.     void *operator new(size_t size);  
  47.     void operator delete(void *p);  
  48. };  
  49.   
  50. template<class Object>  
  51. void* BinaryTree<Object>::operator new(size_t size)  
  52. {  
  53.     void *p = malloc(size);  
  54.     return p;  
  55. }  
  56.   
  57. template<class Object>  
  58. void BinaryTree<Object>::operator delete(void *p)  
  59. {  
  60.     free(p);  
  61. }  
  62.   
  63. //typedef char Object;  
  64. template<class Object>  
  65. class BinaryNode  
  66. {  
  67.     friend class BinaryTree<Object>;  
  68. private:  
  69.     Object elem;  
  70.     BinaryNode *leftChild;  
  71.     BinaryNode * rightChild;  
  72. };  
  73.   
  74. template<class Object>  
  75. void BinaryTree<Object>::create()  
  76. {  
  77.       
  78.     create(root);  
  79. }  
  80.   
  81. template<class Object>  
  82. bool BinaryTree<Object>::create(BinaryNode<Object> *&T)   //指针申请的内存,所以用指向指针的指针或引用  
  83. {  
  84.     cout << "enter the value of the elem:";  
  85.     Object x;  
  86.     cin >> x;  
  87.     if(x == '#') {T = NULL; delete T;}  
  88.     else  
  89.     {  
  90.         T = new BinaryNode<Object>;   //对外不起作用, //指针参数是如何传递内存的。除非使用指向指针的引用  
  91.         T->elem = x;  
  92.         create(T->leftChild);  
  93.         create(T->rightChild);  
  94.     }  
  95.     return true;  
  96. }  
  97.   
  98. template<class Object>  
  99. void BinaryTree<Object>::preOrderTraverse()  
  100. {  
  101.     cout << "the preOrder: ";  
  102.     preOrderTraverse(root);  
  103.     cout << endl;  
  104. }  
  105. template<class Object>  
  106. bool BinaryTree<Object>::preOrderTraverse(BinaryNode<Object> *T)  
  107. {  
  108.     if(T != NULL)  
  109.     {  
  110.         cout << T->elem << " ";  
  111.         preOrderTraverse(T->leftChild);  
  112.         preOrderTraverse(T->rightChild);  
  113.     }  
  114.     return true;  
  115. }  
  116.   
  117. template<class Object>  
  118. void BinaryTree<Object>::inOrderTraverse()  
  119. {  
  120.     cout << "the inOrder: ";  
  121.     inOrderTraverse(root);  
  122.     cout << endl;  
  123. }  
  124. template<class Object>  
  125. bool BinaryTree<Object>::inOrderTraverse(BinaryNode<Object> *T)  
  126. {  
  127.     if(T != NULL)  
  128.     {  
  129.         inOrderTraverse(T->leftChild);  
  130.         cout << T->elem << " ";  
  131.         inOrderTraverse(T->rightChild);  
  132.     }  
  133.     return true;  
  134. }  
  135.   
  136. template<class Object>  
  137. void BinaryTree<Object>::postOrderTraverse()  
  138. {  
  139.     cout << "the postOrder: ";  
  140.     postOrderTraverse(root);  
  141.     cout << endl;  
  142. }  
  143. template<class Object>  
  144. bool BinaryTree<Object>::postOrderTraverse(BinaryNode<Object> *T)  
  145. {  
  146.     if(T != NULL)  
  147.     {  
  148.         postOrderTraverse(T->leftChild);  
  149.         postOrderTraverse(T->rightChild);  
  150.         cout << T->elem << " ";  
  151.     }  
  152.     return true;  
  153. }  
  154.   
  155. /*************************************************************************************************************/  
  156. //按层次遍历的几种方法  
  157. //method1:  
  158. //层次遍历的vector存储模型  
  159. template<class Object>  
  160. void BinaryTree<Object>::levelOrderTraverse()  
  161. {  
  162.     cout <<  "the levelOrder: ";  
  163.     levelOrderTraverse(root);  
  164. }  
  165.   
  166. template<class Object>  
  167. bool BinaryTree<Object>::levelOrderTraverse(BinaryNode<Object> *T)  
  168. {  
  169.     vector<BinaryNode<Object>*> Node;   //注意定义是结点指针  
  170.     if(T != NULL)  
  171.     {     
  172.         Node.push_back(T);  
  173.         int cur = 0;  
  174.         int last = 1;  
  175.         while(cur < Node.size())  
  176.         {  
  177.             last = Node.size();  
  178.             while(cur < last)  
  179.             {  
  180.                 cout << Node[cur]->elem << " ";  
  181.                 if(Node[cur]->leftChild)  
  182.                     Node.push_back(Node[cur]->leftChild);  
  183.                 if(Node[cur]->rightChild)  
  184.                     Node.push_back(Node[cur]->rightChild);  
  185.                 cur++;  
  186.             }  
  187.         }  
  188.         cout << endl;  
  189.     }  
  190.     return true;  
  191. }  
  192.   
  193. //method2:  
  194. //利用queue双向队列进行实现,  
  195. template<class Object>  
  196. void BinaryTree<Object>::printBFS_()  
  197. {  
  198.     cout << "the level order: ";  
  199.     printBFS_(root);  
  200. }  
  201. template<class Object>  
  202. void BinaryTree<Object>::printBFS_(BinaryNode<Object> *root)  
  203. {  
  204.     queue<BinaryNode<Object> *> Node;  
  205.     Node.push(root);  
  206.     do  
  207.     {  
  208.         BinaryNode<Object> *node = Node.front(); //第一个结点  
  209.         Node.pop();  
  210.         cout << node->elem << " ";  
  211.         if(node->leftChild)  
  212.             Node.push(node->leftChild);  
  213.         if(node->rightChild)  
  214.             Node.push(node->rightChild);  
  215.     }  
  216.     while(!Node.empty());  
  217. }  
  218.   
  219. //mothod3:  
  220. //换行  
  221. template<class Object>  
  222. void BinaryTree<Object>::printNodeByLevel_()  
  223. {  
  224.     cout << "the level order: ";  
  225.     printNodeByLevel_(root);  
  226. }  
  227. template<class Object>  
  228. void BinaryTree<Object>::printNodeByLevel_(BinaryNode<Object> *root)  
  229. {  
  230.     deque<BinaryNode<Object> *>Q1, Q2;  
  231.     Q1.push_back(root);  
  232.     do  
  233.     {  
  234.         do  
  235.         {  
  236.             BinaryNode<Object>* node = Q1.front();  
  237.             Q1.pop_front();  
  238.             cout << node->elem << " ";  
  239.             if(node->leftChild)  
  240.                 Q2.push_back(node->leftChild);  
  241.             if(node->rightChild)  
  242.                 Q2.push_back(node->rightChild);  
  243.         }while(!Q1.empty());  
  244.         cout << endl;  
  245.         Q1.swap(Q2);   //exchange the node  
  246.     }while(!Q1.empty());  
  247. }  
  248.   
  249.   
  250. //method 4:  
  251. template<class Object>  
  252. void BinaryTree<Object>::printNodeByLevel()  
  253. {  
  254.     cout << "the level order: ";  
  255.     printNodeByLevel(root);  
  256. }  
  257. template<class Object>  
  258. void BinaryTree<Object>::printNodeByLevel(BinaryNode<Object> *root)  
  259. {  
  260.     queue<BinaryNode<Object> *> Q;  
  261.     Q.push(root);  
  262.     Q.push(0);  
  263.     do  
  264.     {  
  265.         BinaryNode<Object> *node = Q.front();  
  266.         Q.pop();  
  267.         if(node)  
  268.         {  
  269.             cout << node->elem << " ";  
  270.             if(node->leftChild)  
  271.                 Q.push(node->leftChild);   //左压入,没有递归哦  
  272.             if(node->rightChild)  
  273.                 Q.push(node->rightChild);  //右压入  
  274.         }  
  275.         else if(!Q.empty())  //当发现空指针(结束信号)时,要检查队列内是否还有节点,如果没有的话还插入新的结束信号,则会做成死循环  
  276.         {  
  277.             Q.push(0);  
  278.             cout << endl;  
  279.         }  
  280.     }while(!Q.empty());  
  281. }  
  282.   
  283. //method5:  
  284. //多加一个level 成员  
  285. template<class Object>  
  286. void BinaryTree<Object>::pirntBFS()  
  287. {  
  288.     cout << "the level order: ";  
  289.     pirntBFS(root);  
  290. }  
  291. template<class Object>  
  292. void BinaryTree<Object>::pirntBFS(BinaryNode<Object> *root)  
  293. {  
  294.     queue<BinaryNode<Object> *> Q;  
  295.     Q.push(root);  
  296.     int curlevel = 0;  
  297.     do  
  298.     {  
  299.         BinaryNode<Object> *node = Q.front();  
  300.         int level = node->level;  
  301.         Q.pop();  
  302.         cout << node->elem << " ";  
  303.         if(node->leftChild)  
  304.         {  
  305.             node->leftChild.level = level + 1;  
  306.             Q.push(node->leftChild);  
  307.         }  
  308.         if(node->rightChild)  
  309.         {  
  310.             node->rightChild.level = level + 1;  
  311.             Q.push(node->rightChild);  
  312.         }  
  313.     }  
  314.     while(!Q.empty());  
  315. }  
  316. /****************************************************************************************************************/  
  317.   
  318. int main()  
  319. {  
  320.     BinaryTree<char> tree;  
  321.     tree.create();  
  322.   
  323.     tree.preOrderTraverse();  //先序遍历  
  324.     tree.inOrderTraverse();   //中序遍历  
  325.     tree.postOrderTraverse(); //后序遍历  
  326.     tree.levelOrderTraverse();//层次遍历  
  327.     tree.printNodeByLevel();  
  328.   
  329.     cin.get();  
  330.     cin.get();  
  331.     return 0;  
  332. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值