您好,欢迎来到爱够旅游网。
搜索
您的当前位置:首页二叉树_线索化二叉树

二叉树_线索化二叉树

来源:爱够旅游网

typedef char  ElemType;
#define END  '#'
typedef enum  //存在子节点为0,不存在子节点为1
{
    LINK = 0,
    THREAD = 1

} PointerTag;

typedef struct BiThrNode
{
    BiThrNode *leftchild;
    BiThrNode *rightchild;
    PointerTag  Ltag;
    PointerTag  Rtag;
    ElemType data;
}BiThrNode,*BinaryThreadTree;

BiThrNode * Buynode()
{
    BiThrNode *s = (BiThrNode*)malloc(sizeof(BiThrNode));
    if(NULL == s) exit(1);
    memset(s,0,sizeof(BiThrNode));
    return s;
}

void Freenode(BiThrNode *p)
{
    free(p);
}

BiThrNode * CreateTree(ElemType *&str)    //创建二叉树
{
    BiThrNode *s = NULL;
    if(str != NULL  && *str != END)
    {
        s = Buynode();
        s->data = *str;
        s->Ltag = s->Rtag = LINK;
        s->leftchild = CreateTree(++str);
        s->rightchild = CreateTree(++str);
    }
    return s;
}

void InOrder(BiThrNode *p)      //中序遍历
{
    if(p != NULL)
    {
        InOrder(p->leftchild);
        cout<<p->data<<" ";
        InOrder(p->rightchild);
    }
}

void NiceInOrder(BiThrNode *ptr)
{
    for(BiThrNode *p = First(ptr); p != NULL; p = Next(p))
    {
        cout<<p->data<<" ";
    }
    cout<<endl;
}

void ResNiceInOrder(BiThrNode *ptr)
{
    for(BiThrNode *p = Last(ptr); p != NULL; p = Prev(p))
    {
        cout<<p->data<<" ";
    }
    cout<<endl;
}

void MakeThread(BiThrNode *p,BiThrNode *&ptr)
{
    if(p != NULL)
    {
        MakeThread(p->leftchild,ptr);

        if(p->leftchild == NULL)    
        {
            p->leftchild = ptr;        
            p->Ltag = THREAD;      
        }
        if(ptr != NULL && ptr->rightchild == NULL)  
        {
            ptr->rightchild = p;                              
            ptr->Rtag = THREAD;                        
        }
        ptr = p;                                                      

        MakeThread(p->rightchild,ptr);
    }
}

void MakeThreadTree(BiThrNode *p)     //二叉树线索化
{
    BiThrNode *ptr = NULL;
    MakeThread(p,ptr);
    ptr->Rtag = THREAD;
}

BiThrNode * First(BiThrNode *ptr)       //线索化后的第一个线索结点
{   
    while(ptr != NULL && ptr->Ltag != THREAD)
    {
        ptr = ptr->leftchild;
    }
    return ptr;
}
BiThrNode *Next(BiThrNode *ptr)
{
    if(ptr == NULL) return NULL;
    if(ptr->Rtag == THREAD)
    {
        return ptr->rightchild;
    }
    else
    {
        return First(ptr->rightchild);
    }
}

BiThrNode *Last(BiThrNode *ptr)
{
    while(ptr != NULL && ptr->Rtag != THREAD)
    {
        ptr = ptr->rightchild;
    }
    return ptr;
}

BiThrNode * Prev(BiThrNode *ptr)
{
    if(NULL == ptr) return NULL;
    if(ptr->Ltag == THREAD)
    {
        return ptr->leftchild;
    }
    else
    {
        return Last(ptr->leftchild);
    }
}

int main()
{
    char *str = "ABC##DE##F##G#H##";
    BinaryThreadTree root = CreateTree(str);
    InOrder(root);
    cout<<endl;
    MakeThreadTree(root);
    NiceInOrder(root);
    ResNiceInOrder(root);
    return 0;
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igbc.cn 版权所有 湘ICP备2023023988号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务