关于“数据结构”,线索二叉树的建立和实现

最近在学严蔚敏老师的《数据结构》为加深映像,自己线索二叉树那一节的内容自己敲了一遍,本想在VC++6.0环境下运行一下,可是没有办法运行,我自己调试了两天,但还是没有办法。想恳求各位高手和大师,帮帮解决这个问题。源代如下

#include "stdafx.h"


#include <stdio.h>
#include "math.h"
#include <stdlib.h>
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 CreateBiTree(BiThrTree T)//先序递归遍历创建二叉树
{
    char ch;
    scanf("%c",&ch);
    if(ch==' ')
    {
      T=NULL;
    }
    else
    {
        if(!(T=(BiThrNode*)malloc(sizeof(BiThrNode)))) 
        {
            exit(OVERFLOW);
         }
        T->data=ch;
        CreateBiTree(T->lchild);
        CreateBiTree(T->rchild);
     }
}


void InThreading(BiThrTree p)//中序遍历进行中序线索化
{
if(p)
{
InThreading(p->lchild);
if(!p->lchild)
{
p->LTag=Thread;
p->lchild=pre;
}
if(!pre->rchild)
{
pre->RTag=Thread;
pre->rchild=p;
}
pre=p;
InThreading(p->rchild);
}
}


void InOrderThreading(BiThrTree &Thrt,BiThrTree T)//建立头结点,中序线索化二叉树
{
if(!(Thrt=(BiThrTree)malloc(sizeof(BiThrNode)))) exit(OVERFLOW);
Thrt->LTag=Link;
  Thrt->RTag=Thread;
Thrt->rchild=Thrt;
if(!T) T->lchild=Thrt;
else
   {
Thrt->lchild=T;
pre=Thrt;
InThreading(T);
pre->rchild=Thrt;
pre->RTag=Thread;
Thrt->rchild=pre;
     }
}


int InOrderTraverse_Thr(BiThrTree T)//中序遍历线索二叉树
{
  BiThrTree p;
  p=T->lchild;
  while(p!=T)
  {
while(p) p=p->lchild;
        if(p->data) return 0;
        while(p->RTag==Thread&&p->rchild!=T)
        {
p=p->rchild;
printf("%c",p->data);
}
        p=p->rchild;
  }
  return 1;
}


int main(int argc, char* argv[])
{


   BiThrTree T;
   BiThrTree Thr;
   printf("请输入前序二叉树的内容;\n");
   CreateBiTree(T);                              //建立二叉树
   InOrderThreading(T,T);                        //加入头结点,并线索化
   printf("输出中序二叉树的内容:\n");
   InOrderTraverse_Thr(T);
   printf("\n");
   return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值