二叉树相关操作

来源:互联网 发布:java怎么调试错误 编辑:程序博客网 时间:2024/04/20 18:47
    //1.定义、封装二叉树(构造、遍历(递归、非递归))
    //2.部分树的操作
//1.定义、封装二叉树(构造、遍历(递归、非递归))    //2.部分树的操作    #include <iostream>#include<queue>#include<stack>#include<string>using namespace std;typedef char intT;class node                                      //树结点类{private:    intT elem;    node *lchild;    node *rchild;public:    friend class binarytree;    node()                                           //构造函数1    {        elem=0;        lchild=rchild=NULL;    }    node(const intT& t)                             //构造函数2    {        elem=t;        lchild=rchild=NULL;    }    node(const intT& t,node* l,node* r)               //构造函数3    {        elem=t;        lchild=l;        rchild=r;    }    node* get_left_child()    {        return lchild;    }    node* get_right_child()    {        return rchild;    }    void set_left_child(node* l)    {        lchild=l;    }    void set_right_child(node* r)    {        rchild=r;    }    intT get_value()    {        return elem;    }    void set_value(const intT& t)    {        elem=t;    }    bool isleaf()    {        if(lchild==NULL&&rchild==NULL)      return true;        else        return false;    }    node * pi_construct_binarytree(string pre_str,string in_str)    //前序中序构造二叉树    {        char root_value=pre_str[0];        node * rot=new node;        rot->elem=root_value;        rot->lchild=rot->rchild=NULL;        int llen=in_str.find(root_value),rlen=in_str.length()-llen-1;        if(llen==0)     rot->lchild=NULL;        else if(llen>0)        {            rot->lchild=pi_construct_binarytree(pre_str.substr(1,llen),in_str.substr(0,llen));        }        else    return NULL;        if(rlen==0)         rot->rchild=NULL;        else if(rlen>0)        {            rot->rchild=pi_construct_binarytree(pre_str.substr(llen+1,rlen),in_str.substr(llen+1,rlen));        }        else     return NULL;    }    node * ip_construct_binarytree(string in_str,string post_str)   //中序后序构造二叉树    {        char root_value=post_str[post_str.length()-1];        node *rot=new node;        rot->elem=root_value;        rot->lchild=rot->rchild=NULL;        int llen=in_str.find(root_value),rlen=in_str.length()-llen-1;        if(llen==0)     rot->lchild=NULL;        else if(llen>0)        {            rot->lchild=ip_construct_binarytree(in_str.substr(0,llen),post_str.substr(0,llen));        }        else    return NULL;        if(rlen==0)         rot->rchild=NULL;        else if(rlen>0)        {            rot->rchild=ip_construct_binarytree(in_str.substr(llen+1,rlen),post_str.substr(llen,rlen));        }        else     return NULL;    }    void exchange_node(node *p)                 //交换左右结点    {        node *temp=p->lchild;        p->lchild=p->rchild;        p->rchild=temp;    }};void visit(node* p);class binarytree                                //树类{private:    node * root;public:    binarytree(node* p)    {        root=p;    }    ~binarytree()    {        delete root;    }    bool isempty()    {        if(root)    return false;        else    return true;    }    int Total()                                             //统计结点个数    {        int total=0;        node *p=root;        stack<node*> s;        if(p)   s.push(p);        while(!s.empty())        {            node* p=s.top();            s.pop();            if(p)           total++;            if(p->rchild)   s.push(p->rchild);            if(p->lchild)   s.push(p->lchild);        }        return total;    }    node* get_root()    {        return root;    }    node* get_father(node* cnode)                               //返回cnode的父结点    {        queue<node*> que;        node* p=root;        if(p)   que.push(p);        while(!que.empty())        {            p=que.front();            if(p->lchild==cnode||p->rchild==cnode)      return p;            que.pop();            if(p->lchild)       que.push(p->lchild);            if(p->rchild)       que.push(p->rchild);        }    }    node* get_left_brother(node* cnode)    {        queue<node*> que;        node* p=root;        if(p)   que.push(p);        while(!que.empty())        {            p=que.front();            if(p->lchild!=NULL&&p->rchild==cnode)      return p->lchild;            que.pop();            if(p->lchild)       que.push(p->lchild);            if(p->rchild)       que.push(p->rchild);        }    }    node* get_right_brother(node* cnode)    {        queue<node*> que;        node* p=root;        if(p)   que.push(p);        while(!que.empty())        {            p=que.front();            if(p->rchild!=NULL&&p->lchild==cnode)      return p->rchild;            que.pop();            if(p->lchild)       que.push(p->lchild);            if(p->rchild)       que.push(p->rchild);        }    }    node* get_left_child()    {        return root->lchild;    }    node* get_right_child()    {        return root->rchild;    }    queue<node*> breadth_find()                         //广度遍历,返回队列    {        queue<node*> que,qnode;        node* p=root;        if(p)   que.push(p);        while(!que.empty())        {            p=que.front();            visit(p);            qnode.push(p);            que.pop();            if(p->lchild)       que.push(p->lchild);            if(p->rchild)       que.push(p->rchild);        }        return qnode;    }    void pre_find(node* p)                           //前序遍历    {        //递归实现://        if(p)//        {//            visit(p);//            pre_find(p->lchild);//            pre_find(p->rchild);//        }        //非递归实现:        stack<node*> s;        if(p)   s.push(p);        while(!s.empty())        {            node* p=s.top();            s.pop();            if(p)           visit(p);            if(p->rchild)   s.push(p->rchild);            if(p->lchild)   s.push(p->lchild);        }    }    void in_find(node* pnode)                        //中序遍历    {//        //递归实现://        if(p)//        {//            in_find(p->lchild);//            visit(p);//            in_find(p->rchild);//        }//        //非递归实现:        stack<node*> s;        node *p=pnode;        while(!s.empty()||p)        {            if(p)            {             s.push(p);             p=p->lchild;            }            else            {                p=s.top();                visit(p);                p=p->rchild;                s.pop();            }        }    }    void post_find(node* rot)                        //后序遍历    {        //递归实现://        if(p)//        {//            post_find(p->lchild);//            post_find(p->rchild);//            visit(p);//        }        //非递归实现:        stack<node*> s;        node *p=rot;        node *pre=rot;        while(p)        {            for(;p->lchild!=NULL;p=p->lchild)                s.push(p);            while(p&&(p->rchild==NULL||p->rchild==pre))            {                visit(p);                pre=p;                if(s.empty())       return;                p=s.top();                s.pop();            }            s.push(p);            p=p->rchild;        }    }    void level_find()                               //广度遍历    {        queue<node*> que,qnode;        node* p=root;        if(p)   que.push(p);        while(!que.empty())        {            p=que.front();            visit(p);            qnode.push(p);            que.pop();            if(p->lchild)       que.push(p->lchild);            if(p->rchild)       que.push(p->rchild);        }    }    void delete_node()    {    }    int num_0()                    //返回度为0的结点个数    {        node *p=root;        int total=0;        stack<node*> s;        if(p)   s.push(p);        while(!s.empty())        {            node* p=s.top();            s.pop();            if(p)            {                if(p->isleaf())     total++;            }            if(p->rchild)   s.push(p->rchild);            if(p->lchild)   s.push(p->lchild);        }        return total;    }    int num_1()                    //返回度为1的结点个数    {        node *p=root;        int total=0;        stack<node*> s;        if(p)   s.push(p);        while(!s.empty())        {            node* p=s.top();            s.pop();            if(p)            {                if((p->lchild&&!p->rchild)||(p->rchild&&!p->lchild))     total++;            }            if(p->rchild)   s.push(p->rchild);            if(p->lchild)   s.push(p->lchild);        }        return total;    }    int num_2()                    //返回度为2的结点个数    {        node *p=root;        int total=0;        stack<node*> s;        if(p)   s.push(p);        while(!s.empty())        {            node* p=s.top();            s.pop();            if(p)            {                if(p->lchild&&p->rchild)     total++;            }            if(p->rchild)   s.push(p->rchild);            if(p->lchild)   s.push(p->lchild);        }        return total;    }    int countheight(node *p)        //返回高度    {        int height=0;        if(p)        {            int lheight=countheight(p->lchild);            int rheight=countheight(p->rchild);            height=lheight>rheight?lheight+1:rheight+1;        }        return height;    }    int countweight()               //返回广度(每层结点的最大值)    {        queue<node*> que;        node *p=root,*level=new node('1');        int height=countheight(root),num[height],i=0,weight=0;        for(int i=0;i<height;i++)        {            num[i]=0;        }        if(p)        {            que.push(p);            que.push(level);        }        while(!que.empty())        {            p=que.front();            if(p->elem=='1')            {                i++;                que.pop();                if(!que.empty())    que.push(p);                continue;            }            else    num[i]++;            que.pop();            if(p->lchild)       que.push(p->lchild);            if(p->rchild)       que.push(p->rchild);        }        delete level;        for(int i=0;i<height;i++)        {            if(num[i]>=weight)      weight=num[i];        }        return weight;    }    intT maxvalue()                 //返回最大值    {        node *p=root;        intT m='\0';        stack<node*> s;        if(p)   s.push(p);        while(!s.empty())        {            node* p=s.top();            s.pop();            if(p)           m=p->elem>m?p->elem:m;            if(p->rchild)   s.push(p->rchild);            if(p->lchild)   s.push(p->lchild);        }        return m;    }    void exchange_all_child()                            //交换所有结点的左右结点    {        node *p=root;        stack<node*> s;        if(p)   s.push(p);        while(!s.empty())        {            node* p=s.top();            s.pop();            if(p)            {                p->exchange_node(p);            }            if(p->rchild)   s.push(p->rchild);            if(p->lchild)   s.push(p->lchild);        }    }    void deleteleaf()    {        node *p=root;        stack<node*> s;        if(p)   s.push(p);        while(!s.empty())        {            node* p=s.top();            s.pop();            if(p)            {                if(p->lchild)                if(p->lchild->isleaf())                {                    delete p->lchild;                    p->lchild=NULL;                }                if(p->rchild)                if(p->rchild->isleaf())                {                    delete p->rchild;                    p->rchild=NULL;                }            }            if(p->rchild)   s.push(p->rchild);            if(p->lchild)   s.push(p->lchild);        }    }    node * get_last_node()                            //返回最后一个结点(按广度遍历)    {        node *p=root;        if(p->isleaf())     return  p;        else        {            while(p->lchild||p->rchild)            {                if(!p->lchild&&p->rchild)           p=p->rchild;                else if(p->lchild&&!p->rchild)      p=p->lchild;                else if(p->lchild&&p->rchild)      p=p->rchild;            }            return p;        }    }    bool is_complete_bt()                            //判断是否为完全二叉树    {        queue<node*> que;        node* p=root;        if(p)   que.push(p);        while(!que.empty())        {            p=que.front();            if(p!=get_last_node())            {                if(p->isleaf())                {                    que.pop();                    continue;                }                if(!p->lchild||!p->rchild)      return  false;            }            else    return true;            que.pop();            if(p->lchild)       que.push(p->lchild);            if(p->rchild)       que.push(p->rchild);        }    }};void visit(node* p){    cout<<p->get_value()<<"  ";}void binarytree_create(node* p)                             //普通粗暴的二叉树初始化,见P93 图3-13{    node* n1=new node;      n1->set_value('b');     p->set_left_child(n1);    node* n2=new node;      n2->set_value('c');     p->set_right_child(n2);    node* n3=new node;      n3->set_value('d');     n1->set_right_child(n3);    node* n4=new node;      n4->set_value('e');     n2->set_right_child(n4);    node* n5=new node;      n5->set_value('f');     n3->set_left_child(n5);    node* n6=new node;      n6->set_value('g');     n3->set_right_child(n6);    node* n7=new node;      n7->set_value('h');     n4->set_right_child(n7);}int main(){    cout<<"前序为:abdfgceh  中序为:bfdgaceh  构造二叉树:"<<endl;    node *p=new node;    p=p->pi_construct_binarytree("abdfgceh","bfdgaceh");    binarytree bt1(p);    cout<<"广度遍历:"<<endl;    bt1.breadth_find();    cout<<endl;    cout<<"前序遍历:"<<endl;    bt1.pre_find(bt1.get_root());    cout<<endl;    cout<<"中序遍历:"<<endl;    bt1.in_find(bt1.get_root());    cout<<endl;    cout<<"后序遍历:"<<endl;    bt1.post_find(bt1.get_root());    cout<<endl;    cout<<"后序为:fgdbheca  中序为:bfdgaceh  构造二叉树:"<<endl;    node *p1=new node;    p1=p1->ip_construct_binarytree("BFDGACEH","FGDBHECA");    binarytree bt(p1);    cout<<"广度遍历:"<<endl;    bt.breadth_find();    cout<<endl;    cout<<"前序遍历:"<<endl;    bt.pre_find(bt.get_root());    cout<<endl;    cout<<"中序遍历:"<<endl;    bt.in_find(bt.get_root());    cout<<endl;    cout<<"后序遍历:"<<endl;    bt.post_find(bt.get_root());    cout<<endl;    cout<<"结点一共有: "<<bt.Total()<<endl;    cout<<"度为0的结点有: "<<bt.num_0()<<endl;    cout<<"度为1的结点有: "<<bt.num_1()<<endl;    cout<<"度为2的结点有: "<<bt.num_2()<<endl;    cout<<"  高度为: "<<bt.countheight(bt.get_root())<<endl;    cout<<"  宽度为: "<<bt.countweight()<<endl;    cout<<"结点最大值为: "<<bt.maxvalue()<<endl;    cout<<"此树是否为完全二叉树:  "<<bt.is_complete_bt()<<endl;    cout<<"交换左右结点后前序遍历: "<<endl;    bt.exchange_all_child();    bt.pre_find(bt.get_root());    cout<<endl;    cout<<"删除所有叶子结点后前序遍历: "<<endl;    bt.deleteleaf();    bt.pre_find(bt.get_root());    cout<<endl;    cout << "Hello world!" << endl;    return 0;}


//修改关于判断是否二叉树的代码:

node * get_last_node()                            //返回最后一个结点(按广度遍历)    {        node *p=root;        if(p->isleaf())     return  p;        else        {            queue<node*> que,qnode;            if(p)   que.push(p);            while(!que.empty())            {                p=que.front();                qnode.push(p);                que.pop();                if(p->lchild)       que.push(p->lchild);                if(p->rchild)       que.push(p->rchild);            }            return p;        }    }    bool is_complete_bt()                            //判断是否为完全二叉树    {        queue<node*> que;        node* p=root;        int i=0;                        //i用来判断后续是否全是叶子结点,如果遍历时发现一个结点是叶子结点,那么之后若不全为叶子结点则返回false        if(p)   que.push(p);        while(!que.empty())        {            p=que.front();            if(p!=get_last_node())            {                if(p->isleaf())                {                    i=1;                    que.pop();                    continue;                }                else                {                    if(i==1)        return false;                }                if(!p->lchild||!p->rchild)      return  false;            }            que.pop();            if(p->lchild)       que.push(p->lchild);            if(p->rchild)       que.push(p->rchild);        }        return true;    }
修改原因:未能正确返回广度遍历下的最后一个结点

原创粉丝点击