二叉树 基本运算

一、括号表示法建二叉树

   核心代码

 void make_Btree()
 {
            Btnode *st[max_size],*p;
            string str;
            int k,j=0,top=-1;
            b=NULL;
            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;
                                 }
                             }             
                }
            }
            print(b);
 }
View Code

二、查找节点

   核心代码

     Btnode *find_node(Btnode *b1,char x)//查找节点数值等于X的节点 
        {
            if(b1==NULL)
               return NULL;
            else if(b1->data==x)
               return b1;
            else
            {
                Btnode *p=find_node(b1->lchild,x);
                if(p==NULL)
                   return find_node(b1->rchild,x);
                else
                   return p;
             } 
        }
View Code

三、求树高

   核心代码

 int get_high(const Btnode *b1)
    {
        if(b1==NULL)
           return 0;
        else
        {
            int hl=get_high(b1->lchild);
            int hr=get_high(b1->rchild);
            return hl>hr?hl+1:hr+1;
        }
    }

四、范例

#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);
        }
        void destroy(Btnode *&b1)
        {
            if(b1!=NULL)
            {
                destroy(b1->lchild);
                destroy(b1->rchild);
                delete b1;
            }
        }
        void print(Btnode *&b1)
        {
            if(b1!=NULL)
            {
                cout<<b1->data;
                if(b1->lchild!=NULL||b1->rchild!=NULL)
                {
                    cout<<"(";
                    print(b1->lchild);
                    if(b1->rchild!=NULL)
                    {
                        cout<<",";
                        print(b1->rchild);
                    }
                    cout<<")";
                }
            }
        }
         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;
                                 }
                             }             
                }
            }
        }
    int get_high(const Btnode *b1)
    {
        if(b1==NULL)
           return 0;
        else
        {
            int hl=get_high(b1->lchild);
            int hr=get_high(b1->rchild);
            return hl>hr?hl+1:hr+1;
        }
    }
     Btnode *find_node(Btnode *b1,char x)//查找节点数值等于X的节点 
        {
            if(b1==NULL)
               return NULL;
            else if(b1->data==x)
               return b1;
            else
            {
                Btnode *p=find_node(b1->lchild,x);
                if(p==NULL)
                   return find_node(b1->rchild,x);
                else
                   return p;
             } 
        }
    friend int main();//友元,可以使主函数有权访问该类的私有成员
};
int main()
{
    Btree t;
    t.make_Btree();
    cout<<"输入的二叉树为:";
    t.print(t.b);
    cout<<endl<<"树高:";
    cout<<t.get_high(t.b); 
    cout<<endl<<"请输入要查找的节点的值(x):";
    char x;
    cin>>x;
    Btnode *p=t.find_node(t.b,x);
    if(p!=NULL)
       cout<<"find and x="<<p->data<<endl;
    else
       cout<<"no so node\n";
    
    return 0;
}
View Code

 

posted @ 2018-11-27 14:19  shenyuli  阅读(504)  评论(0编辑  收藏  举报
Live2D