1、2、3



#include <stdio.h>
#include <malloc.h>
#define ERROR 0
#define OK 1
typedef char TElemType;
typedef enum PointerTag{Link,Thread};
typedef struct BiThrNode{
      TElemType data;
      struct BiThrNode *lchild,*rchild;
      PointerTag LTag,RTag;
}BiThrNode,*BiThrTree;
BiThrTree pre;
void InOrderTraverse_Thr(BiThrTree T)
{
    BiThrNode *p;
    p=T->lchild;
    while(p!=T){
        while (p->LTag==Link) p=p->lchild;
        printf("%c",p->data);
        while (p->RTag==Thread && p->rchild!=T){
            p=p->rchild;printf("%c",p->data);
        }
        p=p->rchild;
    }
}
void InThreading(BiThrTree p)
{
  if(p)
    {InThreading(p->lchild);
     if(!p->lchild)
     {p->LTag=Thread;p->lchild=pre;}
     else p->LTag=Link;
     if(!p->rchild) p->RTag=Thread; else p->RTag=Link;
     if(!pre->rchild)
     {pre->RTag=Thread;pre->rchild=p;}
     else p->RTag=Link;
     pre=p;
     InThreading(p->rchild);
    }
}
int InOrderThreading(BiThrTree &Thrt,BiThrTree T)
{
    if(!(Thrt=(BiThrTree)malloc(sizeof(BiThrNode)))) return ERROR;
    Thrt->LTag=Link;  Thrt->rchild=Thrt;
    if(!T) {Thrt->lchild=Thrt;Thrt->RTag=Thread;}
    else
    {Thrt->lchild=T;   pre=Thrt;
     InThreading(T);
     pre->rchild=Thrt;
     pre->RTag=Thread;Thrt->rchild=pre;
    }
    return OK;
}
void CreateBiThrTree(BiThrTree &T)
{
    char x;
    scanf("%c",&x);
    if(x=='*')T=NULL;
    else
    {
        T=(BiThrNode *)malloc(sizeof(BiThrNode));
        T->data=x;
        CreateBiThrTree(T->lchild);
        CreateBiThrTree(T->rchild);
    }
}

int main(int argc,char*argv[])
{
    BiThrTree ThrTree,h;
    printf("按先序次序输入字符序列,空格表示空树:\n");
    CreateBiThrTree(ThrTree);
    InOrderThreading(h,ThrTree);
    printf("二叉线索链表构建完成!\n");
    printf("二叉线索树中序遍历结果:\n");
    InOrderTraverse_Thr(h);
    printf("\n");
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值