主题:[原创]关于线索二叉树!!!!求助!~~~
我写了个程序:
用前序输入建立一个二叉树,然后再把此树线索化,最后利用线索来遍历此二叉树。。。但是编译没错,就是运行不出结果!!
希望各位高手帮帮小妹我,看看是哪里出了问题~~~~~~~~万分感激!!!!
#include <stdio.h>
#include <malloc.h>
typedef struct node
{
char data;
struct node *lchild,*rchild;
int lbit,rbit;
}btnode,*btree;
//创建一个二叉树(前序输入)
void buildbt(btree &T)
{
char ch;
ch = getchar();
if(ch == ' ')
T = NULL;
else
{
T = (btree)malloc(sizeof(btnode));
T->data = ch;
buildbt(T->lchild);
buildbt(T->rchild);
}
}
//二叉树线索化(中序)
void threadbt(btree &T)
{
btree pre;
if(T)
{
threadbt(T->lchild);
if(T->rchild == NULL)
T->rbit = 0;
if(T->lchild == NULL)
{
T->lbit = 0;
T->lchild = pre;
}
if(pre->rbit == 0)
pre->rchild = T;
pre = T;
threadbt(T->rchild);
}
}
//在中序线索二叉树中确定结点的直接后继结点
btree insucc(btree T)
{
btree s;
s = T->rchild;
if(T->rbit == 1)
{
while(s->lbit == 1)
s = s->lchild;
}
return s;
}
//利用线索遍历二叉树
void blbt(btree T)
{
btree p = T;
while(1)
{
p = insucc(p);
if(p == T)
break;
printf(" %c ",p->data);
}
}
void main()
{
btree T;
printf("按前序输入一个二叉树:\n"); //输入单个字符
buildbt(T);
threadbt(T);
printf("\n将其线索化并利用线索遍历二叉树:\n");
blbt(T);
}
用前序输入建立一个二叉树,然后再把此树线索化,最后利用线索来遍历此二叉树。。。但是编译没错,就是运行不出结果!!
希望各位高手帮帮小妹我,看看是哪里出了问题~~~~~~~~万分感激!!!!
#include <stdio.h>
#include <malloc.h>
typedef struct node
{
char data;
struct node *lchild,*rchild;
int lbit,rbit;
}btnode,*btree;
//创建一个二叉树(前序输入)
void buildbt(btree &T)
{
char ch;
ch = getchar();
if(ch == ' ')
T = NULL;
else
{
T = (btree)malloc(sizeof(btnode));
T->data = ch;
buildbt(T->lchild);
buildbt(T->rchild);
}
}
//二叉树线索化(中序)
void threadbt(btree &T)
{
btree pre;
if(T)
{
threadbt(T->lchild);
if(T->rchild == NULL)
T->rbit = 0;
if(T->lchild == NULL)
{
T->lbit = 0;
T->lchild = pre;
}
if(pre->rbit == 0)
pre->rchild = T;
pre = T;
threadbt(T->rchild);
}
}
//在中序线索二叉树中确定结点的直接后继结点
btree insucc(btree T)
{
btree s;
s = T->rchild;
if(T->rbit == 1)
{
while(s->lbit == 1)
s = s->lchild;
}
return s;
}
//利用线索遍历二叉树
void blbt(btree T)
{
btree p = T;
while(1)
{
p = insucc(p);
if(p == T)
break;
printf(" %c ",p->data);
}
}
void main()
{
btree T;
printf("按前序输入一个二叉树:\n"); //输入单个字符
buildbt(T);
threadbt(T);
printf("\n将其线索化并利用线索遍历二叉树:\n");
blbt(T);
}