二叉树几种遍历算法

来源:互联网 发布:python的正则表达式 编辑:程序博客网 时间:2024/06/05 02:01
  1. <span style="font-size:14px;">/*二叉树的遍历*/  
  2.   
  3. #include <iostream>  
  4. #include <cstring>  
  5. #include <stack>  
  6. using namespace std;  
  7.   
  8. typedef struct node  
  9. {  
  10.     char data;  
  11.     struct node *lchild,*rchild;  
  12. }BinTree;  
  13.   
  14. typedef struct node1  
  15. {  
  16.     BinTree *btnode;  
  17.     bool isFirst;  
  18. }BTNode;  
  19.   
  20. void creatBinTree1(BinTree* &root) //前序遍历创建树,形如 A,B,C,#,#,D,#,#,E,#,#  
  21. {  
  22.     char value;  
  23.     cin>>value;  
  24.     if(value == '#') root = NULL; //递归结束条件  
  25.     else  
  26.     {  
  27.         root = (BinTree *)malloc(sizeof(BinTree));  
  28.         root->data = value;  
  29.         creatBinTree1(root->lchild);  
  30.         creatBinTree1(root->rchild);  
  31.     }  
  32. }  
  33.   
  34. void creatBinTree2(char *s,BinTree *&root)  //创建二叉树,s为形如A(B,C(D,E))形式的字符串  
  35. {  
  36.     int i;  
  37.     bool isRight=false;  
  38.     stack<BinTree*> s1;          //存放结点  
  39.     stack<char> s2;              //存放分隔符  
  40.     BinTree *p,*temp;  
  41.     root->data=s[0];  
  42.     root->lchild=NULL;  
  43.     root->rchild=NULL;  
  44.     s1.push(root);  
  45.     i=1;  
  46.     while(i<strlen(s))  
  47.     {  
  48.         if(s[i]=='(')  
  49.         {  
  50.             s2.push(s[i]);  
  51.             isRight=false;  
  52.         }  
  53.         else if(s[i]==',')  
  54.         {  
  55.             isRight=true;  
  56.         }  
  57.         else if(s[i]==')')  
  58.         {  
  59.             s1.pop();  
  60.             s2.pop();  
  61.         }  
  62.         else if(isalpha(s[i]))  
  63.         {  
  64.             p=(BinTree *)malloc(sizeof(BinTree));  
  65.             p->data=s[i];  
  66.             p->lchild=NULL;  
  67.             p->rchild=NULL;  
  68.             temp=s1.top();  
  69.             if(isRight==true)  
  70.             {  
  71.                 temp->rchild=p;  
  72.                 cout<<temp->data<<"的右孩子是"<<s[i]<<endl;  
  73.             }  
  74.             else  
  75.             {  
  76.                 temp->lchild=p;  
  77.                 cout<<temp->data<<"的左孩子是"<<s[i]<<endl;  
  78.             }  
  79.             if(s[i+1]=='(')  
  80.                 s1.push(p);  
  81.         }  
  82.         i++;  
  83.     }  
  84. }  
  85.   
  86. void display(BinTree *root)        //显示树形结构  
  87. {  
  88.     if(root!=NULL)  
  89.     {  
  90.         cout<<root->data;  
  91.         if(root->lchild!=NULL)  
  92.         {  
  93.             cout<<'(';  
  94.             display(root->lchild);  
  95.         }  
  96.         if(root->rchild!=NULL)  
  97.         {  
  98.             cout<<',';  
  99.             display(root->rchild);  
  100.             cout<<')';  
  101.         }  
  102.     }  
  103. }  
  104.   
  105. void preOrder1(BinTree *root)     //递归前序遍历  
  106. {  
  107.     if(root!=NULL)  
  108.     {  
  109.         cout<<root->data<<" ";  
  110.         preOrder1(root->lchild);  
  111.         preOrder1(root->rchild);  
  112.     }  
  113. }  
  114.   
  115. void inOrder1(BinTree *root)      //递归中序遍历  
  116. {  
  117.     if(root!=NULL)  
  118.     {  
  119.         inOrder1(root->lchild);  
  120.         cout<<root->data<<" ";  
  121.         inOrder1(root->rchild);  
  122.     }  
  123. }  
  124.   
  125. void postOrder1(BinTree *root)    //递归后序遍历  
  126. {  
  127.     if(root!=NULL)  
  128.     {  
  129.         postOrder1(root->lchild);  
  130.         postOrder1(root->rchild);  
  131.         cout<<root->data<<" ";  
  132.     }  
  133. }  
  134.   
  135. void preOrder2(BinTree *root)     //非递归前序遍历  
  136. {  
  137.     stack<BinTree*> s;  
  138.     BinTree *p=root;  
  139.     while(p!=NULL||!s.empty())  
  140.     {  
  141.         while(p!=NULL)  
  142.         {  
  143.             cout<<p->data<<" ";  
  144.             s.push(p);  
  145.             p=p->lchild;  
  146.         }  
  147.         if(!s.empty())  
  148.         {  
  149.             p=s.top();  
  150.             s.pop();  
  151.             p=p->rchild;  
  152.         }  
  153.     }  
  154. }  
  155.   
  156. void inOrder2(BinTree *root)      //非递归中序遍历  
  157. {  
  158.     stack<BinTree*> s;  
  159.     BinTree *p=root;  
  160.     while(p!=NULL||!s.empty())  
  161.     {  
  162.         while(p!=NULL)  
  163.         {  
  164.             s.push(p);  
  165.             p=p->lchild;  
  166.         }  
  167.         if(!s.empty())  
  168.         {  
  169.             p=s.top();  
  170.             cout<<p->data<<" ";  
  171.             s.pop();  
  172.             p=p->rchild;  
  173.         }  
  174.     }  
  175. }  
  176.   
  177. void postOrder2(BinTree *root)    //非递归后序遍历  
  178. {  
  179.     stack<BTNode*> s;  
  180.     BinTree *p=root;  
  181.     BTNode *temp;  
  182.     while(p!=NULL||!s.empty())  
  183.     {  
  184.         while(p!=NULL)              //沿左子树一直往下搜索,直至出现没有左子树的结点  
  185.          {  
  186.             BTNode *btn=(BTNode *)malloc(sizeof(BTNode));  
  187.             btn->btnode=p;  
  188.             btn->isFirst=true;  
  189.             s.push(btn);  
  190.             p=p->lchild;  
  191.         }  
  192.         if(!s.empty())  
  193.         {  
  194.             temp=s.top();  
  195.             s.pop();  
  196.             if(temp->isFirst==true)     //表示是第一次出现在栈顶  
  197.              {  
  198.                 temp->isFirst=false;  
  199.                 s.push(temp);  
  200.                 p=temp->btnode->rchild;  
  201.             }  
  202.             else                        //第二次出现在栈顶  
  203.              {  
  204.                 cout<<temp->btnode->data<<" ";  
  205.                 p=NULL;  
  206.             }  
  207.         }  
  208.     }  
  209. }  
  210.   
  211. void postOrder3(BinTree *root)     //非递归后序遍历  
  212. {  
  213.     stack<BinTree*> s;  
  214.     BinTree *cur;                      //当前结点  
  215.     BinTree *pre=NULL;                 //前一次访问的结点  
  216.     s.push(root);  
  217.     while(!s.empty())  
  218.     {  
  219.         cur=s.top();  
  220.         if((cur->lchild==NULL&&cur->rchild==NULL)||  
  221.            (pre!=NULL&&(pre==cur->lchild||pre==cur->rchild)))  
  222.         {  
  223.             cout<<cur->data<<" ";  //如果当前结点没有孩子结点或者孩子节点都已被访问过  
  224.               s.pop();  
  225.             pre=cur;  
  226.         }  
  227.         else  
  228.         {  
  229.             if(cur->rchild!=NULL)  
  230.                 s.push(cur->rchild);  
  231.             if(cur->lchild!=NULL)  
  232.                 s.push(cur->lchild);  
  233.         }  
  234.     }  
  235. }  
  236.   
  237.   
  238. int main(int argc, char *argv[])  
  239. {  
  240.     char s[100];  
  241.     while(scanf("%s",s)==1)  
  242.     {  
  243.         BinTree *root=(BinTree *)malloc(sizeof(BinTree));  
  244.         creatBinTree2(s,root);  
  245.         display(root);  
  246.         cout<<endl;  
  247.         preOrder2(root);  
  248.         cout<<endl;  
  249.         inOrder2(root);  
  250.         cout<<endl;  
  251.         postOrder2(root);  
  252.         cout<<endl;  
  253.         postOrder3(root);  
  254.         cout<<endl;  
  255.     }  
  256.     return 0;  
  257. }  
  258. </span>  
0 0
原创粉丝点击