数据结构复习代码——基于线索二叉树实现下相关方法的实现

1、基于线索二叉树实现下相关方法的实现

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);
/////////////////////////////
//2         基于线索二叉树实现下关于寻找当前中序线索化下第一个节点、最后一个节点、下一个节点、前一个结点、中序遍历、查找结点、查找特定节点的父节点等方法的实现
//各个函数的声明---------------具体的相关注释还未完成
BinTreeNode* First(BinTree *bt);
BinTreeNode* First(BinTreeNode *t);

BinTreeNode* Last(BinTree *bt);
BinTreeNode* Last(BinTreeNode *t);

BinTreeNode* Next(BinTree *bt,BinTreeNode *cur);
BinTreeNode* Next(BinTreeNode *t,BinTreeNode *cur);

BinTreeNode* Prio(BinTree *bt,BinTreeNode *cur);
BinTreeNode* Prio(BinTreeNode *t,BinTreeNode *cur);

void InOrder(BinTree *bt);
void InOrder(BinTreeNode *t);

void inOrder(BinTree *bt);
void inOrder(BinTreeNode *t);

BinTreeNode* Search(BinTree *bt,ElemType key);
BinTreeNode* Search(BinTreeNode *t,ElemType key);

BinTreeNode* Parent(BinTree *bt,BinTreeNode *cur);
BinTreeNode* Parent(BinTreeNode *t,BinTreeNode *cur);


// 各个方法的实现
BinTreeNode* Parent(BinTree *bt,BinTreeNode *cur)           //查询父节点----接口
{
    return Parent(bt->root,cur);
}
BinTreeNode* Parent(BinTreeNode *t,BinTreeNode *cur)        //查询父节点实现
{
    if(t == NULL || cur == NULL)                            //判空
        return NULL;
    if(t == cur)                                            //当查找节点为根节点时,无父节点
        return NULL;
    BinTreeNode *p;                                         //定义新节点作为返回值
    if(cur->ltag == THREAD)
    {
         p = cur->leftChild;
         if(p->rightChild == cur)
            return p;
    }
    if(cur->rtag == THREAD)
    {
        p = cur->rightChild;
        if(p->leftChild == cur)
            return p;
    }
    p = First(cur->leftChild);
    p = p->leftChild;
    if(p!=NULL && p->rightChild == cur)
        return p;

    p = Last(cur->rightChild);
    return p->rightChild;

}

BinTreeNode* Search(BinTree *bt,ElemType key)
{
    return Search(bt->root,key);
}
BinTreeNode* Search(BinTreeNode *t,ElemType key)
{
    if(t == NULL)
        return NULL;
    if(t->data == key)
        return t;
    BinTreeNode *p;
    for( p=First(t); p!=NULL; p=Next(t,p))
    {
        if(p->data == key)
            return p;
    }
    return NULL;
}

void InOrder(BinTree *bt)
{
    InOrder(bt->root);
}
void InOrder(BinTreeNode *t)
{
    BinTreeNode *p;
    for( p=First(t); p!=NULL; p=Next(t,p))
    {
        printf("%c ",p->data);
    }
    printf("\n");
}

void inOrder(BinTree *bt)
{
    inOrder(bt->root);
}
void inOrder(BinTreeNode *t)
{
    if(t != NULL)
    {
        inOrder(t->leftChild);
        printf("%c ",t->data);
        inOrder(t->rightChild);
    }
}

BinTreeNode* Next(BinTree *bt,BinTreeNode *cur)
{
    return Next(bt->root,cur);
}
BinTreeNode* Next(BinTreeNode *t,BinTreeNode *cur)
{
    if(t==NULL||cur==NULL)
        return NULL;
    if(cur->rtag == THREAD)
        return cur->rightChild;
    return First(cur->rightChild);
}

BinTreeNode* Prio(BinTree *bt,BinTreeNode *cur)
{
    return Prio(bt->root,cur);
}
BinTreeNode* Prio(BinTreeNode *t,BinTreeNode *cur)
{
    if(t==NULL||cur==NULL)
        return NULL;
    if(cur->ltag == THREAD)
        return cur->leftChild;
    return Last(cur->leftChild);
}

BinTreeNode* First(BinTree *bt)
{
    return First(bt->root);
}
BinTreeNode* First(BinTreeNode *t)
{
    if(t == NULL)
        return NULL;
    BinTreeNode *p = t;
    while(p->ltag == LINK)
        p = p->leftChild;
    //printf("%c" , p->data);
    return p;
}

BinTreeNode* Last(BinTree *bt)
{
    return Last(bt->root);
}
BinTreeNode* Last(BinTreeNode *t)
{
    if(t == NULL)
        return NULL;
    BinTreeNode *p = t;
    while(p->rtag == LINK)
        p = p->rightChild;
    return p;
}

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);
        //printf("%c",t->data);
    }

}

 

posted @ 2022-07-08 20:11  往心。  阅读(17)  评论(0编辑  收藏  举报