二叉树


/*二叉树的遍历* 2011.8.25*/ 

#include 
  
  
   
   
#include
   
   
    
    
#include
    
    
     
      
using namespace std;

typedef struct node
{
    char data;
    struct node *lchild,*rchild;
}BinTree;

typedef struct node1
{
    BinTree *btnode;
    bool isFirst;
}BTNode;


void creatBinTree(char *s,BinTree *&root)  //创建二叉树,s为形如A(B,C(D,E))形式的字符串 
{
    int i;
    bool isRight=false;
    stack
     
     
      
       s1;          //存放结点 
    stack
      
      
        s2; //存放分隔符 BinTree *p,*temp; root->data=s[0]; root->lchild=NULL; root->rchild=NULL; s1.push(root); i=1; while(i 
       
         data=s[i]; p->lchild=NULL; p->rchild=NULL; temp=s1.top(); if(isRight==true) { temp->rchild=p; cout< 
        
          data<<"的右孩子是"<<s[i]< 
         
           lchild=p; cout< 
          
            data<<"的左孩子是"<<s[i]<<endl; } if(s[i+1]=='(') s1.push(p); } i++; } } void display(BinTree *root) //显示树形结构 { if(root!=NULL) { cout< 
           
             data; if(root->lchild!=NULL) { cout<<'('; display(root->lchild); } if(root->rchild!=NULL) { cout<<','; display(root->rchild); cout<<')'; } } } void preOrder1(BinTree *root) //递归前序遍历 { if(root!=NULL) { cout< 
            
              data<<" "; preOrder1(root->lchild); preOrder1(root->rchild); } } void inOrder1(BinTree *root) //递归中序遍历 { if(root!=NULL) { inOrder1(root->lchild); cout< 
             
               data<<" "; inOrder1(root->rchild); } } void postOrder1(BinTree *root) //递归后序遍历 { if(root!=NULL) { postOrder1(root->lchild); postOrder1(root->rchild); cout< 
              
                data<<" "; } } void preOrder2(BinTree *root) //非递归前序遍历 { stack 
               
                 s; BinTree *p=root; while(p!=NULL||!s.empty()) { while(p!=NULL) { cout< 
                
                  data<<" "; s.push(p); p=p->lchild; } if(!s.empty()) { p=s.top(); s.pop(); p=p->rchild; } } } void inOrder2(BinTree *root) //非递归中序遍历 { stack 
                 
                   s; BinTree *p=root; while(p!=NULL||!s.empty()) { while(p!=NULL) { s.push(p); p=p->lchild; } if(!s.empty()) { p=s.top(); cout< 
                  
                    data<<" "; s.pop(); p=p->rchild; } } } void postOrder2(BinTree *root) //非递归后序遍历 { stack 
                   
                     s; BinTree *p=root; BTNode *temp; while(p!=NULL||!s.empty()) { while(p!=NULL) //沿左子树一直往下搜索,直至出现没有左子树的结点 { BTNode *btn=(BTNode *)malloc(sizeof(BTNode)); btn->btnode=p; btn->isFirst=true; s.push(btn); p=p->lchild; } if(!s.empty()) { temp=s.top(); s.pop(); if(temp->isFirst==true) //表示是第一次出现在栈顶 { temp->isFirst=false; s.push(temp); p=temp->btnode->rchild; } else //第二次出现在栈顶 { cout< 
                    
                      btnode->data<<" "; p=NULL; } } } } /**要保证根结点在左孩子和右孩子访问之后才能访问,因此对于任一结点P,先将其入栈。如果P不存在左孩子和右孩子,则可以直接访问它; *或者P存在左孩子或者右孩子,但是其左孩子和右孩子都已被访问过了,则同样可以直接访问该结点。若非上述两种情况,则将P的右孩子 *和左孩子依次入栈,这样就保证了每次取栈顶元素的时候,左孩子在右孩子前面被访问,左孩子和右孩子都在根结点前面被访问。 */ void postOrder3(BinTree *root) //非递归后序遍历 { stack 
                     
                       s; BinTree *cur; //当前结点 BinTree *pre=NULL; //前一次访问的结点 s.push(root); while(!s.empty()) { cur=s.top(); if((cur->lchild==NULL&&cur->rchild==NULL) || (pre!=NULL&&(pre==cur->lchild||pre==cur->rchild))) { cout< 
                      
                        data<<" "; //如果当前结点没有孩子结点或者孩子节点都已被访问过 s.pop(); pre=cur; } else { if(cur->rchild!=NULL) s.push(cur->rchild); if(cur->lchild!=NULL) s.push(cur->lchild); } } } int main(int argc, char *argv[]) { char s[100]; while(scanf("%s",s)==1) { BinTree *root=(BinTree *)malloc(sizeof(BinTree)); creatBinTree(s,root); display(root); cout<<endl; preOrder2(root); cout<<endl; inOrder2(root); cout<<endl; postOrder2(root); cout<<endl; postOrder3(root); cout<<endl; } return 0; } /************************************************************************/ /* 先序 */ /************************************************************************// void preOrder(Node *p) { if(!p) return; stack 
                       
                         s; Node *t; s.push(p); while(!s.empty()) { t=s.top(); printf("%d\n",t->data); s.pop(); if(t->right) s.push(t->right); if(t->left) s.push(t->left); } } /************************************************************************/ /* 中序 */ /************************************************************************// void inOrder(Node *p) { if(!p) return; stack< pair 
                        
                          > s; Node *t; int unUsed; s.push(make_pair(p,1)); while(!s.empty()) { t=s.top().first; unUsed = s.top().second; s.pop(); if(unUsed) { if(t->right) s.push( make_pair(t->right,1) ); s.push( make_pair(t,0) ); if(t->left) s.push( make_pair(t->left,1)); } else printf("%d\n",t->data); } } /************************************************************************/ /* 后序 */ /************************************************************************/ void postOrder(Node *p) { if(!p) return; stack<pair 
                         
                           > s; Node *t; int unUsed; s.push(make_pair(p,1)); while(!s.empty()) { t=s.top().first; unUsed=s.top().second; s.pop(); if(unUsed) { s.push(make_pair(t,0); if(t->right) s.push(make_pair(t->right,1)); if(t->left) s.push(make_pair(t->left,1)); } else printf("%d\n",t->data); } } /*自己的code 非递归 中序遍历 */ void inOrder2(Tree *t) { push(t); t = t->left; while(!stackEmpty()){ if(t){ push(t); t = t->left; } else{ t = pop(); cout << t->data << endl; t = t->right; } } } 
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
      
     
     
    
    
   
   
  
  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值