求大神帮忙 怎样将二叉树输出 明天急用 我的源代码输出的不对

流烟默 2012-05-24 05:35:36
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define EQ(a,b) ((a)==(b))
#define LT(a,b) ((a)< (b))
#define LQ(a,b) ((a)<=(b))
#define TRUE 1
#define FALSE 0
#define OVERFLOW -2

typedef int KeyType;
typedef int Status;

typedef struct{
KeyType key; /*关键字域*/
}SElemType;
typedef struct BitNode{
SElemType data; /*存储空间基址,建表时按实际长度分配,0号单元留空*/
struct BitNode *lchild,*rchild;
}BitNode,* BiTree;
//函数声明
void CreateBST(BiTree &T);
void InOrderTraverse(BiTree T);
Status PrintTree(BiTree T,int n);
Status Delete(BiTree &p);
BiTree SearchBST(BiTree T,KeyType key);
Status InsertBST(BiTree &T,KeyType key);
Status DeleteBST(BiTree &T,KeyType key);
void Choose( int choice ,BiTree &T);
void Print(BiTree T);

/*创建二叉排序树*/
void CreateBST(BiTree &T){
KeyType key;
T=NULL;
scanf("%d",&key);
while(key!=0){
InsertBST(T,key);
scanf("%d",&key);
}
}
/*中序遍历*/
void InOrderTraverse(BiTree T)
{
if(T)
{
InOrderTraverse(T->lchild);
printf("%4d",T->data.key);
InOrderTraverse(T->rchild);
}
}
/*打印二叉树*/
Status PrintTree(BiTree T,int n)
{
if(T==NULL)return FALSE;
PrintTree(T->rchild,n+1);
for(int i=0;i<n;i++)
printf(" ");
printf("%d\n",T->data.key);
PrintTree(T->lchild,n+1);
return TRUE;
}

/*二叉排序树的插入*/
Status InsertBST(BiTree &T,KeyType key)
{
BiTree s;
if(!T){
s=(BiTree)malloc(sizeof(BitNode));
s->data.key=key;
s->lchild=s->rchild=NULL;
T=s;
}
else if LT(key,T->data.key)
InsertBST(T->lchild,key);
else InsertBST(T->rchild,key);
return TRUE;
}
/*二叉排序树的查找*/
BiTree SearchBST(BiTree T,KeyType key)
{
if(!T){printf("Can not find!!\n");return T;}
else if EQ(key,T->data.key){return T;}
else if LT(key,T->data.key) return SearchBST(T->lchild,key);
else return SearchBST(T->rchild,key);
}


/*二叉排序树的删除*/

Status Delete(BiTree &p)
{
BiTree q,s;
if(!p->rchild)//没有右孩子
{
q=p;
p=p->lchild;
free(q);
}
else if(!p->lchild)//没有左孩子
{
q=p;
p=p->rchild;
free(q);
}
else//两个孩子都有
{
q=p;
s=p->lchild;
while(s->rchild){q=s;s=s->rchild;}
p->data=s->data;
if(q!=p) q->rchild=s->lchild;
else q->lchild=s->lchild;
delete s;
}
return TRUE;
}

Status DeleteBST(BiTree &T,KeyType key)
{
if(!T)return FALSE;
else{
if (EQ(key,T->data.key))return Delete(T);
else
if(LT(key,T->data.key))return DeleteBST(T->lchild,key);
else return DeleteBST(T->rchild,key);
}
}

int main()
{
BiTree T;
//BiTree b1,b2;
KeyType key;
printf("创建二叉排序树\n");
printf("Input every key(0 to quit):");
CreateBST(T);
Print(T);
while(true)
{
printf("Press enter to continue.........");
getchar();
getchar();
system("cls");
Print(T);
}
return 0;
}
void Print(BiTree T)
{
int choice;
printf("1:打印排序树\n");
printf("2:查找结点\n");
printf("3:中序遍历\n");
printf("4:插入结点\n");
printf("5:删除结点\n");
printf("0:退出\n");
printf("请选择要进行的操作:");
scanf("%d",&choice);
Choose(choice,T);

}
void Choose( int choice ,BiTree &T)
{
BiTree b1,b2;
KeyType key;
int t;
switch(choice)
{
case 1:
PrintTree(T,0);break;
case 2:
printf("Input the key to search:");
scanf("%d",&key);
if(key!=0){
b2=SearchBST(T,key);//把 key 为根的子树打印出来
PrintTree(b2,0);
printf("\nThe root is the key to search!!\n\n");
}
else printf("Can not find!!\n");
break;
case 3:InOrderTraverse(T);break;
case 4:
printf("输入要插入的数据:");
scanf("%d",&key);
if(InsertBST(T, key))printf("\n插入完毕!\n");
else printf("插入失败\n"); break;
case 5:
printf("输入要删除的数据:");
scanf("%d",&key);
if(DeleteBST(T, key))printf("\n删除完毕!\n");
else printf("删除失败\n");break;
case 0: exit (0);break;
default: printf("输入错误\n");
}
printf("\n谢谢使用!\n");
}
...全文
81 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
proorck6 2012-05-24
  • 打赏
  • 举报
回复
所有这些问题应该用结构化的数据结构来处理,否则同样的问题会一次又一次地出现。
W170532934 2012-05-24
  • 打赏
  • 举报
回复
为什么是先打印右子树后根节点然后才是左子树的?

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧