Advertisement
MaskerQwQ

二叉链表遍历

Nov 28th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include<stack>
  3. #include<queue>
  4.  
  5. using namespace std;
  6.  
  7. #define Elemtype char
  8.  
  9. typedef struct Bitnode{
  10.     Elemtype data;
  11.     struct Bitnode *lchild;
  12.     struct Bitnode *rchild;
  13. }Bitnode,*Bitree;
  14.  
  15. void CreateTree(Bitree &T){
  16.     Elemtype x;
  17.     cin>>x;
  18.     if(x=='*'){
  19.         T=NULL;
  20.     }else{
  21.         T=(Bitree)malloc(sizeof(Bitnode));
  22.         T->data=x;
  23.         CreateTree(T->lchild);
  24.         CreateTree(T->rchild);
  25.     }
  26. }
  27.  
  28. void PreOrder(Bitree T){
  29.     if(T==NULL){
  30.         return;
  31.     }else{
  32.         cout<<T->data;
  33.         PreOrder(T->lchild);
  34.         PreOrder(T->rchild);
  35.     }
  36. }
  37.  
  38. void NC_PreOrder(Bitree T){
  39.     stack<Bitree> s;
  40.     s.push(T);
  41.     Bitree p;
  42.     while(!s.empty()){
  43.         while(s.top()){
  44.             p=s.top();
  45.             cout<<p->data;
  46.             s.push(p->lchild);
  47.         }
  48.         s.pop();
  49.         if(!s.empty()){
  50.             p=s.top();
  51.             s.pop();
  52.             s.push(p->rchild);
  53.         }
  54.     }
  55. }
  56.  
  57.  
  58.  
  59. void InOrder(Bitree T){
  60.     if(T==NULL){
  61.         return;
  62.     }else{
  63.         InOrder(T->lchild);
  64.         cout<<T->data;
  65.         InOrder(T->rchild);
  66.     }
  67. }
  68.  
  69. void NC_InOrder(Bitree T){
  70.     stack<Bitree> s;
  71.     s.push(T);
  72.     while(!s.empty()){
  73.         while(s.top()){
  74.             s.push(s.top()->lchild);
  75.         }
  76.         s.pop();
  77.         if(!s.empty()){
  78.             Bitree t=s.top();
  79.             s.pop();
  80.             cout<<t->data;
  81.             s.push(t->rchild);
  82.         }
  83.     }
  84. }
  85.  
  86. void PostOrder(Bitree T){
  87.     if(T==NULL){
  88.         return;
  89.     }else{
  90.         PostOrder(T->lchild);
  91.         PostOrder(T->rchild);
  92.         cout<<T->data;
  93.     }
  94. }
  95.  
  96. void NC_PostOrder(Bitree T){
  97.     stack<Bitree> s;
  98.     Bitree p=T,r=NULL;
  99.     while(p!=NULL||!s.empty()){
  100.         if(p!=NULL){
  101.             s.push(p);
  102.             p=p->lchild;
  103.         }else{
  104.             p=s.top();
  105.             if(p->rchild&&p->rchild!=r){
  106.                 p=p->rchild;
  107.                 s.push(p);
  108.                 p=p->lchild;
  109.             }else{
  110.                 s.pop();
  111.                 cout<<p->data;
  112.                 r=p;
  113.                 p=NULL;
  114.             }
  115.         }
  116.     }
  117.    
  118. }
  119.  
  120. int main(){
  121.     Bitree S;
  122.     cout<<"按先序创建二叉树:";
  123.     CreateTree(S);
  124.    
  125.     cout<<"先序遍历为:"<<endl;    
  126.     cout<<"非递归遍历:";
  127.     PreOrder(S);
  128.     cout<<endl;
  129.     cout<<"递归遍历:  ";
  130.     NC_PreOrder(S);
  131.     cout<<endl;
  132.    
  133.    
  134.     cout<<"中序遍历为:"<<endl;
  135.     cout<<"非递归遍历:";
  136.     InOrder(S);
  137.     cout<<endl;
  138.     cout<<"递归遍历:  ";
  139.     NC_InOrder(S);  
  140.     cout<<endl;
  141.    
  142.     cout<<"后序遍历为:"<<endl;
  143.     cout<<"非递归遍历:";
  144.     PostOrder(S);
  145.     cout<<endl;
  146.     cout<<"递归遍历:  ";
  147.     NC_PostOrder(S);
  148.     cout<<endl;    
  149.     return 0;
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement