二叉树遍历-递归算法

一、先序

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

四、层次

五、试例

#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

猜你喜欢

转载自www.cnblogs.com/shenyuling/p/10028481.html