zoukankan      html  css  js  c++  java
  • 二叉树遍历-递归算法

    一、先序

    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
    ";
            }
            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
  • 相关阅读:
    2.DI依赖注入
    1.Spring框架入门案例
    5.通过参数注解多个接口参数的用法
    4.update更新和delete删除用法
    3.insert添加用法
    git基础操作
    2.mongodb可视化工具
    12.Scrapy与mongodb交互和设置中间键
    WPF 学习系列汇总
    WPF 4.0 DatePicker 快速录入
  • 原文地址:https://www.cnblogs.com/shenyuling/p/10028481.html
Copyright © 2011-2022 走看看