各位高手帮忙改错。。
这个代码好像有问题。。执行一半会退出。。请问怎么改。。。。。#include <iostream>
using namespace std;
enum PointerTag {Link, Thread};
typedef struct BiThrNode
{
char data;
struct BiThrNode *lchild, *rchild;
PointerTag LTag, RTag;
}BiThrNode, *BiThrTree;
BiThrTree T, pre = NULL;
void CreateBiTree(BiThrTree &T)
{
//先序建立二叉树
char ch;
scanf("%c", &ch);
if (ch == '*')
T = NULL;
else
{
if (!(T = (BiThrNode *)malloc(sizeof(BiThrNode))))
exit(-1);
T->data = ch;
T->LTag = Link;
T->RTag = Link;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void PreThreading(BiThrTree P)
{
//先序遍历线索化二叉树
if (P)
{
printf("%c ", P->data);
if (!P->lchild)
{
P->LTag = Thread;
P->lchild = pre;
}
if (!P->rchild)
P->RTag = Thread;
if (pre && pre->RTag == Thread)
pre->rchild = P;
pre = P;
if (P->LTag == Link)
PreThreading(P->lchild);
if (P->RTag == Link)
PreThreading(P->rchild);
}
}
void preOrderThr(BiThrTree T)
{
//遍历先序线索二叉树
BiThrTree p = T;
printf("%c ", p->data);
while (p->rchild)
{
if (p->LTag == Link)
p = p->lchild;
else
p = p->rchild;
printf("%c ", p->data);
}
}
void InThreading(BiThrTree P)
{
//中序遍历线索化二叉树
if (P)
{
InThreading(P->lchild);
printf("%c ", P->data);
if (!P->lchild)
{
P->LTag = Thread;
P->lchild = pre;
}
if (!P->rchild)
P->RTag = Thread;
if (pre && pre->RTag == Thread)
pre->rchild = P;
pre = P;
InThreading(P->rchild);
}
}
void inOrderThr(BiThrTree T)
{
//遍历中序线索二叉树
BiThrTree p = T;
while (p->LTag == Link)
p = p->lchild;
printf("%c ", p->data);
while (p->rchild)
{
if (p->RTag == Link)
{
p = p->rchild;
while (p->LTag == Link)
p = p->lchild;
}
else
p = p->rchild;
printf("%c ", p->data);
}
}
void PostThreading(BiThrTree P)
{
//后序遍历线索化二叉树
if (P)
{
PostThreading(P->lchild);
PostThreading(P->rchild);
printf("%c ", P->data);
if (!P->lchild)
{
P->LTag = Thread;
P->lchild = pre;
}
if (!P->rchild)
P->RTag = Thread;
if (pre && pre->RTag == Thread)
pre->rchild = P;
pre = P;
}
}
int main()
{
//建立二叉树
cout << "请按先序输入二叉树各元素值,"
<< "其中*代表空:\n";
CreateBiTree(T);
//先序遍历线索化二叉树
cout << "先序遍历线索化二叉树:\n";
PreThreading(T);
cout << endl;
//遍历先序线索二叉树
cout << "遍历先序线索二叉树:\n";
preOrderThr(T);
cout << endl;
//中序遍历线索化二叉树
cout << "中序遍历线索化二叉树:\n";
InThreading(T);
cout << endl;
//遍历中序线索二叉树
cout << "遍历中序线索二叉树:\n";
inOrderThr(T);
cout << endl;
//后序遍历线索化二叉树
cout << "后序遍历线索化二叉树:\n";
pre = NULL;
PostThreading(T);
cout << endl;
return 0;
}
[ 本帖最后由 Vi_No 于 2010-4-27 22:02 编辑 ]