其他分享
首页 > 其他分享> > 数据结构复习代码——线索二叉树的实现

数据结构复习代码——线索二叉树的实现

作者:互联网

1、线索二叉树的实现

#include<stdio.h>
#include<assert.h>
#include<malloc.h>


#define ElemType char

typedef enum{LINK,THREAD}Tag_Type;

typedef struct BinTreeNode
{
    ElemType data;
    struct BinTreeNode *leftChild;
    struct BinTreeNode *rightChild;
    Tag_Type ltag;
    Tag_Type rtag;
}BinTreeNode;

typedef struct BinTree
{
    BinTreeNode *root;
    ElemType    refvalue;
}BinTree;

void InitBinTree(BinTree *bt,ElemType ref);
void CreateBinTree(BinTree *bt, char *str);
void CreateBinTree(BinTree *bt,BinTreeNode *&t,char *str);
BinTreeNode* _Buynode(ElemType x);
void CreateInTread(BinTree *bt);
void CreateInTread(BinTreeNode *&t,BinTreeNode *&pre);

void CreateInTread(BinTree *bt)
{
    BinTreeNode *pre = NULL;
    CreateInTread(bt->root,pre);
    pre->rightChild = NULL;
    pre->rtag = THREAD;
}
void CreateInTread(BinTreeNode *&t,BinTreeNode *&pre)
{
    if(t == NULL)
        return;
    CreateInTread(t->leftChild,pre);
    if(t->leftChild == NULL)
    {
        t->ltag = THREAD;
        t->leftChild = pre;
    }
    if(pre!=NULL&&pre->rightChild==NULL)
    {
        pre->rtag = THREAD;
        pre->rightChild = t;
    }
    pre = t;
    CreateInTread(t->rightChild,pre);
}

BinTreeNode* _Buynode(ElemType x)
{
    BinTreeNode *s = (BinTreeNode*)malloc(sizeof(BinTreeNode));
    assert(s != NULL);
    s->data = x;
    s->leftChild = s->rightChild = NULL;
    s->ltag = s->rtag = LINK;
    return s;
}

void InitBinTree(BinTree *bt,ElemType ref)
{
    bt->root = NULL;
    bt->refvalue = ref;
}



void CreateBinTree(BinTree *bt, char *str)
{
    CreateBinTree(bt,bt->root,str);
}
void CreateBinTree(BinTree *bt,BinTreeNode *&t,char *str)
{
    if(*str == bt->refvalue)
        t = NULL;
    else
    {
        t = _Buynode(*str);
        CreateBinTree(bt,t->leftChild,++str);
        CreateBinTree(bt,t->rightChild,++str);
    }
}

int main()
{
    char *str = "ABC##DE##F##G#H##";
    BinTree mytree;
    InitBinTree(&mytree,'#');
    CreateBinTree(&mytree,str);

    CreateInTread(&mytree);
    return 0;
}

 

标签:pre,NULL,复习,bt,二叉树,str,BinTreeNode,数据结构,BinTree
来源: https://www.cnblogs.com/lx06/p/16456016.html