二叉树遍历-递归算法

一、先序

void pre_order(const Btnode *b1)//先序 
{
        if(b1==NULL) return ;
        cout<<b1->data;
        pre_order(b1->lchild);
        pre_order(b1->rchild);
}

二、中序

    void in_order(const Btnode *b1)//中序 
   {
            if(b1==NULL) return ;
            in_order(b1->lchild);
            cout<<b1->data;
            in_order(b1->rchild);
    }

 

三、后序

    void post_order(const Btnode *b1)//后序 
   {
          if(b1==NULL) return ;
              post_order(b1->lchild);
          post_order(b1->rchild);
          cout<<b1->data;
    }

四、层次

  void level_order()
    {
        queue<Btnode*> q;
        q.push(b);
        while(!q.empty())
        {
            Btnode* p;
            p=q.front();
            q.pop();
            cout<<p->data;
            if(p->lchild!=NULL)
               q.push(p->lchild);
            if(p->rchild!=NULL)
               q.push(p->rchild); 
        }
    }
//直接插到下面代码class中最后就OK

 

五、试例

#include<iostream>
#include<string>
using namespace std;
const int max_size=100;
struct Btnode
{
    char data;
    Btnode *lchild;
    Btnode *rchild;
};
class Btree
{ 
    Btnode *b;
    public:
        Btree():b(NULL) {}
        ~Btree()
        {
            destroy(b);
            cout<<"hello,is ok\n";
        }
        void destroy(Btnode *&b1)
        {
            if(b1!=NULL)
            {
                destroy(b1->lchild);
                destroy(b1->rchild);
                delete b1;
            }
        }
         void make_Btree()
        {
            Btnode *st[max_size],*p;
            string str;
            int k,j=0,top=-1;
            b=NULL;
            cout<<"请输入括号表示的二叉树:";
            cin>>str;
            for(int i=0;i<str.size();i++)
            {
                switch(str[i])
                {
                    case '(':st[++top]=p;k=1;break;
                    case ')':top--;break;
                    case ',':k=2;break;
                    default :
                             p=new Btnode;
                             p->data=str[i];
                             p->lchild=p->rchild=NULL;
                             if(b==NULL)
                                 b=p;
                             else
                             {
                                 switch(k)
                                 {
                                     case 1:st[top]->lchild=p;break;
                                     case 2:st[top]->rchild=p;break;
                                 }
                             }             
                }
            }
        }
        void pre_order(const Btnode *b1)//先序 
        {
            if(b1==NULL) return ;
            cout<<b1->data;
            pre_order(b1->lchild);
            pre_order(b1->rchild);
        }
        void in_order(const Btnode *b1)//中序 
        {
            if(b1==NULL) return ;
            in_order(b1->lchild);
            cout<<b1->data;
            in_order(b1->rchild);
        }
        void post_order(const Btnode *b1)//后序 
        {
            if(b1==NULL) return ;
            post_order(b1->lchild);
            post_order(b1->rchild);
            cout<<b1->data;
        }
    friend int main();//友元,可以使主函数有权访问该类的私有成员
};
int main()
{
    Btree t;
    t.make_Btree();
    cout<<"先序:";
    t.pre_order(t.b);
    cout<<endl<<"中序:";
    t.in_order(t.b);
    cout<<endl<<"后序:"; 
    t.post_order(t.b); 
    cout<<endl;
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/shenyuling/p/10028481.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值