二叉树的三种遍历方法(递归和非递归)(转载)

 

二叉树的三种遍历方法(递归和非递归)(转载)

分类: 数据结构与算法   120人阅读  评论(0)  收藏  举报

[cpp:firstline[1]]  view plain copy
  1. #include<iostream>  
  2. #include<stack> //STL  
  3. #include<queue>  
  4. using namespace std;  
  5. class Tree  
  6. {  
  7. public:  
  8.     Tree *Left;  
  9.     Tree *Right;  
  10.     char data;  
  11.     Tree();  
  12.   
  13.     //成员函数  
  14.     void CreateTree(Tree* &node);  
  15.     //递归的遍历二叉树  
  16.     void PreOrderVisit(Tree *T);  
  17.     void InOrderVisit(Tree *T);  
  18.     void LastOrderVisit(Tree *T);  
  19.     void LevelOrderVisit(Tree *T);  
  20.     //非递归的调用二叉树  
  21.     void NoPreRecursiveVist(Tree *T);  
  22.     void NoInRecursiveVist(Tree *T);  
  23.     void NoLastRecursiveVist(Tree *T);  
  24. };  
  25.   
  26. Tree::Tree()  
  27. {  
  28. }  
  29. //构造树  
  30. void Tree::CreateTree(Tree* &node)  
  31. {  
  32.     char temp;  
  33.     cout<<"输入改节点的数据"<<endl;  
  34.     cin>>temp;  
  35.     if(temp=='c')  
  36.     {  
  37.         node=NULL;  
  38.     }  
  39.     else  
  40.     {  
  41.         node=new Tree;  
  42.         node->data=temp;  
  43.         CreateTree(node->Left);  
  44.         CreateTree(node->Right);  
  45.     }  
  46. }  
  47. //先序遍历树  
  48. void Tree::PreOrderVisit(Tree *T)  
  49. {  
  50.   
  51.     if(T)  
  52.     {  
  53.         cout<<T->data<<'/t';  
  54.         PreOrderVisit(T->Left);  
  55.         PreOrderVisit(T->Right);  
  56.     }  
  57. }  
  58. //中序遍历树  
  59. void Tree::InOrderVisit(Tree *T)  
  60. {  
  61.   
  62.     if(T)  
  63.     {  
  64.         InOrderVisit(T->Left);  
  65.         cout<<T->data<<'/t';  
  66.         InOrderVisit(T->Right);  
  67.     }  
  68. }  
  69. //后序遍历树  
  70. void Tree::LastOrderVisit(Tree *T)  
  71. {  
  72.   
  73.     if(T)  
  74.     {  
  75.         LastOrderVisit(T->Left);  
  76.         LastOrderVisit(T->Right);  
  77.         cout<<T->data<<'/t';  
  78.     }  
  79.   
  80. }  
  81. //层序遍历  
  82. void Tree::LevelOrderVisit(Tree *T)  
  83. {  
  84.     queue<Tree *>Q;  
  85.     if(!T)  
  86.         return;  
  87.     Q.push(T);  
  88.     Tree *temp=NULL;  
  89.     while(!Q.empty())  
  90.     {  
  91.         temp=Q.front();  
  92.         cout<<temp->data<<'/t';  
  93.         Q.pop();  
  94.         if(temp->Left)  
  95.             Q.push(temp->Left);  
  96.         if(temp->Right)  
  97.             Q.push(temp->Right);  
  98.     }  
  99. }  
  100. //非递归的遍历树的三种方法  
  101. void Tree::NoInRecursiveVist(Tree *T)  
  102. {  
  103.     stack<Tree *>S; //用来保存的节点的栈  
  104.     Tree *p;  
  105.     p=T;  
  106.     while(!S.empty()||p)  
  107.     {  
  108.         if(p) //p非空  
  109.         {  
  110.             S.push(p);  
  111.             p=p->Left;  
  112.         }  
  113.         else  
  114.         {  
  115.             p=S.top();  
  116.             S.pop();  
  117.             cout<<p->data<<'/t';  
  118.             p=p->Right;  
  119.         }  
  120.     }  
  121. }  
  122.   
  123. void Tree::NoPreRecursiveVist(Tree *T)  
  124. {  
  125.     stack<Tree *>S; //用来保存的节点的栈  
  126.     Tree *p;  
  127.     p=T;  
  128.     while(!S.empty()||p)  
  129.     {  
  130.         if(p) //p非空  
  131.         {  
  132.             S.push(p);  
  133.             cout<<p->data<<'/t';  
  134.             p=p->Left;  
  135.         }  
  136.         else  
  137.         {  
  138.             p=S.top();  
  139.             S.pop();  
  140.             p=p->Right;  
  141.         }  
  142.     }  
  143. }  
  144. void Tree::NoLastRecursiveVist(Tree *T)  
  145. {  
  146.     stack<Tree *>S;  
  147.     Tree *p;  
  148.     Tree *pre=NULL;  
  149.     p=T;  
  150.     while(!S.empty()||p)  
  151.     {  
  152.         while(p)  
  153.         {  
  154.             S.push(p);  
  155.             p=p->Left;  
  156.         }  
  157.         p=S.top();  
  158.         if(p->Right==NULL||p->Right==pre)  
  159.         {  
  160.             cout<<p->data<<'/t';  
  161.             pre=p;  
  162.             p=NULL;  
  163.             S.pop();  
  164.         }  
  165.         else  
  166.             p=p->Right;  
  167.     }  
  168. }  
  169. void main()  
  170. {  
  171.     Tree *T=NULL;  
  172.     T->CreateTree(T);  
  173.     cout<<"先序遍历"<<endl;  
  174.     T->PreOrderVisit(T);  
  175.     cout<<endl;  
  176.     cout<<"中序遍历"<<endl;  
  177.     T->InOrderVisit(T);  
  178.     cout<<endl;  
  179.     cout<<"后序遍历"<<endl;  
  180.     T->LastOrderVisit(T);  
  181.     cout<<endl;  
  182.     cout<<"层序遍历"<<endl;  
  183.     T->LevelOrderVisit(T);  
  184.     cout<<endl;  
  185.     cout<<"非递归先序遍历二叉树"<<endl;  
  186.     T->NoPreRecursiveVist(T);  
  187.     cout<<endl;  
  188.     cout<<"非递归中序遍历二叉树"<<endl;  
  189.     T->NoInRecursiveVist(T);  
  190.     cout<<endl;  
  191.     cout<<"非递归后序遍历二叉树"<<endl;  
  192.     T->NoLastRecursiveVist(T);  
  193.     cout<<endl;  
  194.     system("pause");  
  195. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值