• 中序线索化二叉树


    #include<stdio.h>//A R # T # # Y E # # #
    #include<iostream>
    using namespace std;
    
    struct threadnode
    {
        char v;
        int ltag,rtag;
        threadnode*ls,*rs;
    
    };
    // 定义pre为全局变量可以
    class threadbitree
    {
        private:
            threadnode*root; //指向中序化线索二叉树根节点的指针
            threadnode*pre;  //指向上一次访问的节点
        public:
            threadbitree()//构造函数
            {
                pre=NULL;
                root=creat();//建立一棵二叉树
                cout<<"/中序线索化遍历:"<<endl;
                inthread(root);//把root指针指向的树中序线索化
                inorder();//把中序线索树遍历一遍,输出结果和二叉树的中序遍历结果相同
            }
            ~threadbitree()//析构函数
            {
    
            }
            threadnode*next(threadnode*p)//查找p的后继
            {
                struct threadnode*q;
                if(p->rtag==1)
                    q=p->rs;
                else
                {
                    q=p->rs;
                    while(q->ltag==0)
                        q=q->ls;
                }
                return q;
    
            }
    
            void inorder()
            {
                struct threadnode*p;
                if(root==NULL) return ;
                p=root;
                while(p->ltag==0)
                    p=p->ls;
                cout<<p->v<<" ";
                while(p->rs!=NULL)
                {
                    p=next(p);
                    cout<<p->v<<" ";
                }
    
    
            }
    
    
        private:
                threadnode*creat()
                {
                    char ch;
                    cin>>ch;
                    if(ch=='#')
                        return NULL;
                    struct threadnode*p=new threadnode;
                    p->v=ch;
                    p->ltag=p->rtag=0;
                    p->ls=creat();
                    p->rs=creat();
                    return p;
                }
                void inthread(threadnode*now)
                {
                    if(now==NULL) return ;
                    inthread(now->ls);
                    if(now->ls==NULL)
                    {
                        now->ltag=1;
                        now->ls=pre;
                    }
                    if(now->rs==NULL)
                    {
                        now->rtag=1;
                    }
                    if(pre!=NULL&&pre->rtag==1)
                    {
                        pre->rs=now;
                    }
                    pre=now;
                    inthread(pre->rs);
    
                }
    };
    
    int main()
    {
        threadbitree t;
        return 0;
    
    }
    

  • 相关阅读:
    裸裸的spfa~嘿嘿嘿!
    睡前1小时数学系列之-整除
    拓扑排序1.奖金
    拓扑排序
    SCU 1095运送物资(最短路)
    POJ1158 城市交通Traffic lights IOI 1999 (最短路)
    POI0109 POD (最短路)
    HN0I2000最优乘车 (最短路变形)
    FOJ1205 小鼠迷宫问题 (BFD+递推)
    CJOI 05新年好 (最短路+枚举)
  • 原文地址:https://www.cnblogs.com/eason9906/p/11755125.html
Copyright © 2020-2023  润新知