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


    二叉树类的头文件“树.h”

    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. };  

    类源文件“树.cpp”

    1. #include"树.h"  
    2.   
    3. Tree::Tree()  
    4. {  
    5. }  
    6. //构造树  
    7. void Tree::CreateTree(Tree* &node)  
    8. {  
    9.     char temp;  
    10.     cout<<"输入改节点的数据"<<endl;  
    11.     cin>>temp;  
    12.     if(temp=='c')  
    13.     {  
    14.         node=NULL;  
    15.     }  
    16.     else  
    17.     {  
    18.         node=new Tree;  
    19.         node->data=temp;  
    20.         CreateTree(node->Left);  
    21.         CreateTree(node->Right);  
    22.     }  
    23. }  
    24. //先序遍历树  
    25. void Tree::PreOrderVisit(Tree *T)  
    26. {  
    27.       
    28.     if(T)  
    29.     {  
    30.         cout<<T->data<<'/t';  
    31.         PreOrderVisit(T->Left);  
    32.         PreOrderVisit(T->Right);  
    33.     }  
    34. }  
    35. //中序遍历树  
    36. void Tree::InOrderVisit(Tree *T)  
    37. {  
    38.       
    39.     if(T)  
    40.     {  
    41.         InOrderVisit(T->Left);  
    42.         cout<<T->data<<'/t';  
    43.         InOrderVisit(T->Right);  
    44.     }  
    45. }  
    46. //后序遍历树  
    47. void Tree::LastOrderVisit(Tree *T)  
    48. {  
    49.       
    50.     if(T)  
    51.     {  
    52.         LastOrderVisit(T->Left);  
    53.         LastOrderVisit(T->Right);  
    54.         cout<<T->data<<'/t';  
    55.     }  
    56.       
    57. }  
    58. //层序遍历  
    59. void Tree::LevelOrderVisit(Tree *T)  
    60. {  
    61.     queue<Tree *>Q;  
    62.     if(!T)  
    63.         return;  
    64.     Q.push(T);  
    65.     Tree *temp=NULL;  
    66.     while(!Q.empty())  
    67.     {  
    68.         temp=Q.front();  
    69.         cout<<temp->data<<'/t';  
    70.         Q.pop();  
    71.         if(temp->Left)  
    72.             Q.push(temp->Left);  
    73.         if(temp->Right)  
    74.             Q.push(temp->Right);  
    75.     }  
    76. }  
    77. //非递归的遍历树的三种方法  
    78. void Tree::NoInRecursiveVist(Tree *T)  
    79. {  
    80.     stack<Tree *>S;  //用来保存的节点的栈  
    81.     Tree *p;  
    82.     p=T;  
    83.     while(!S.empty()||p)  
    84.     {  
    85.         if(p)  //p非空  
    86.         {  
    87.             S.push(p);  
    88.             p=p->Left;  
    89.         }  
    90.         else  
    91.         {  
    92.             p=S.top();  
    93.             S.pop();  
    94.             cout<<p->data<<'/t';  
    95.             p=p->Right;  
    96.         }  
    97.     }  
    98. }  
    99.   
    100. void Tree::NoPreRecursiveVist(Tree *T)  
    101. {  
    102.     stack<Tree *>S;  //用来保存的节点的栈  
    103.     Tree *p;  
    104.     p=T;  
    105.     while(!S.empty()||p)  
    106.     {  
    107.         if(p)  //p非空  
    108.         {  
    109.             S.push(p);  
    110.             cout<<p->data<<'/t';  
    111.             p=p->Left;  
    112.         }  
    113.         else  
    114.         {  
    115.             p=S.top();  
    116.             S.pop();  
    117.             p=p->Right;  
    118.         }  
    119.     }  
    120. }  
    121. void Tree::NoLastRecursiveVist(Tree *T)  
    122. {  
    123.     stack<Tree *>S;  
    124.     Tree *p;  
    125.     Tree *pre=NULL;  
    126.     p=T;  
    127.     while(!S.empty()||p)  
    128.     {  
    129.         while(p)  
    130.         {  
    131.             S.push(p);  
    132.             p=p->Left;  
    133.         }  
    134.         p=S.top();  
    135.         if(p->Right==NULL||p->Right==pre)  
    136.         {  
    137.             cout<<p->data<<'/t';  
    138.             pre=p;  
    139.             p=NULL;  
    140.             S.pop();  
    141.         }  
    142.         else  
    143.             p=p->Right;  
    144.     }  
    145. }  

    main.cpp

    1. #include"树.h"  
    2. void main()  
    3. {  
    4.     Tree *T=NULL;  
    5.     T->CreateTree(T);  
    6.     cout<<"先序遍历"<<endl;  
    7.     T->PreOrderVisit(T);  
    8.     cout<<endl;  
    9.     cout<<"中序遍历"<<endl;  
    10.     T->InOrderVisit(T);  
    11.     cout<<endl;  
    12.     cout<<"后序遍历"<<endl;  
    13.     T->LastOrderVisit(T);  
    14.     cout<<endl;  
    15.     cout<<"层序遍历"<<endl;  
    16.     T->LevelOrderVisit(T);  
    17.     cout<<endl;  
    18.     cout<<"非递归先序遍历二叉树"<<endl;  
    19.     T->NoPreRecursiveVist(T);  
    20.     cout<<endl;  
    21.     cout<<"非递归中序遍历二叉树"<<endl;  
    22.     T->NoInRecursiveVist(T);  
    23.     cout<<endl;  
    24.     cout<<"非递归后序遍历二叉树"<<endl;  
    25.     T->NoLastRecursiveVist(T);  
    26.     cout<<endl;  
    27.     system("pause");  
    28. }  

    运行结果

  • 相关阅读:
    Excel导入导出DataGridView
    博客开通第一天
    windows10 VM12 安装Mac OS X 10.11
    js判断IE浏览器及版本
    C# MD5 加密
    WindowsErrorCode
    localStorage,sessionStorage的使用
    js实现页面锚点定位动画滚动
    纯js实现页面返回顶部的动画
    HTML table固定表头
  • 原文地址:https://www.cnblogs.com/10jschen/p/2637941.html
Copyright © 2020-2023  润新知