这是我参考网上编写的一道数据结构关于二叉树求其子树是否指针或者线索,及其对应得值,但是我运行结果却是有点问题,希望高手帮指教下!

这是我参考网上编写的一道数据结构关于二叉树求其子树是否指针或者线索,及其对应得值,但是我运行结果却是有点问题,希望高手帮指教下!谢谢!

#include"stdio.h"
#include "malloc.h"


/*  ++a##*b##c##*+*d##e##f##g## */
typedef char  datatype;

typedef struct node
{
    char data;
    int ltag;
    int rtag;
    struct node *lchild,*rchild;

 

}TBTNode,*TBTREE;
TBTREE p;
TBTREE prior;/*全局变量*/

TBTREE CreateTree(TBTREE T)/*从上之下构造树*/
{
    char ch;

    ch=getchar();
    if(ch=='#')
        T=NULL;
    else
    {
        T=(TBTNode *)malloc(sizeof(TBTNode));
        T->data=ch;
        T->ltag=1;
        T->rtag=1;
        T->lchild=CreateTree(T->lchild);
        T->rchild=CreateTree(T->rchild);

    }
    return T;

 

}

void PrintTreeInOrder(TBTREE T)/*中序*/
{
    if(T!=NULL)
    {

        if(T->lchild!=NULL)
            printf("(");
        PrintTreeInOrder(T->lchild);

        printf("%c", T->data);
        PrintTreeInOrder(T->rchild);
        if(T->rchild!=NULL )
            printf(")");


    }
    return;

}
void PrintTreePostOrder(TBTREE T)/*后序*/
{

    if(T!=NULL)
    {
        PrintTreePostOrder(T->lchild);
        PrintTreePostOrder(T->rchild);
        printf("%c",T->data);
    }
    return ;

}


void InThreading(TBTREE T)/*线索遍历*/
{

 

    if(T!=NULL)
    {

        InThreading(T->lchild);/*对左子数进行线索化*/
        if(T->rchild==NULL)
        {
            T->rtag=0;
            prior->rchild=T;/*即prior 指向其后继T*/
        }
        if(T->lchild==NULL)/*当前左子树为空*/
        {
            T->lchild=prior;/*指向前驱*/
            T->ltag=0;          /*ltag==0 , 说明它是线索 */

        }
      
        prior=T;/*T是相对于prior的下一个节点*/
        InThreading(T->rchild);

    }
    return ;

}


TBTREE InOrderThreading(TBTREE T,TBTREE Head)/*中序线索遍历*/
{

    Head=(TBTNode *)malloc(sizeof(TBTNode));
    if(!Head)
    {
        printf("OVERFLOW ");
        return ;
    }
    Head->ltag=1;
    Head->data='@';  /* @  头结点的符号*/
    Head->rtag=1;
    Head->rchild=Head;/*刚开始设其右子树指向其头结点*/

    if(!T)
    {
        Head->lchild=Head;/*为空树的情况*/
    }
    else
    {
        Head->lchild=T;
        prior=Head;

        InThreading(T);/*中序遍历对二叉树进行中序线索化*/
        prior->rchild=Head;/*线索遍历最后让最后一个右指针指向头结点*/
        prior->rtag=0;

    }

    return Head;
}

TBTREE FinInPrior(TBTREE T)
{
    TBTREE s;
    s=T->lchild;
                        /*两种情况*/
    if(T->ltag==1)
    {
        printf("leftchild is pointer/n");/*非线索*/
        while(s->rtag==1)
        {
            s=s->rchild;/*指向其后继,即s的前驱*/
        }
        printf(" its prior is %c/n",s->data);
    }
    else if(T->ltag==0)
    {
        printf(" leftChild is thread:  its prior is %c/n",s->data);
    }
    return s;


}

TBTREE FinInSucc(TBTREE T)
{
    TBTREE s;
    s=T->rchild;      /*两种情况*/
    if(T->rtag==1)
    {
        printf(" rightChild is pointer/n");
        while(s->ltag==1)
        {
            s=s->lchild;/**/
        }
        printf("its successor is %c/n",s->data);
    }
    else if(T->ltag==0)
    {
        printf("  RightChild is thread:  its successor is %c/n",s->data);

    }
    return s;


}
void TreeSearch(TBTREE T)
{
    char ch;

    ch=getchar();

    if(ch=='$')
    {

        printf("the data your want is :%c/n",T->data);
        FinInPrior(T);
        FinInSucc(T);
    }
    else
    {
        if(ch=='l')
        {

            TreeSearch(T->lchild);

        }
        else
        {
            TreeSearch(T->rchild);

        }

    }
    return;
}


void main()
{

    TBTREE T,Head,T1,T2,Head1;

    T1=CreateTree(T);

    PrintTreeInOrder(T1);
    printf("/n");
    PrintTreePostOrder(T1);

    Head1=InOrderThreading(T1,Head);

    printf("/nsearch beginning:/n");
    printf("input instrution*like rs$ rl$* :/n");
    T2=Head1->lchild;/*即root*/


    TreeSearch(T2);
    getch();
    return;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值