二叉树_线索化二叉树

#include<assert.h>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>

using namespace std;

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值