数据结构二叉树遍历问题
#include<stdio.h>#include<malloc.h>#include<stdlib.h>typedefstructNode{chardata;struc...
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct Node
{
char data;
struct Node *LChild;
struct Node *RChild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree T)
{
char ch;
ch=getchar();
if(ch=='#')
{
T=NULL;
}
else
{
T=(BiTNode *)malloc(sizeof(BiTNode));
if(!T) exit(-1);
T->data=ch;
CreateBiTree(T->LChild);
CreateBiTree(T->RChild);
}
}
void InOrder(BiTree root)
{
if(root!=NULL)
{
InOrder(root->LChild);
printf("%c",root->data);
InOrder(root->RChild);
}
}
void main()
{
BiTree T;
T=(BiTNode *)malloc(sizeof(BiTNode));
printf("Builing bitree:\n");
CreateBiTree(T);
InOrder(T);
getchar();
getchar();
}
程序运行不出结果,检查没有错误。请帮忙看一下,要怎么改,或者怎么运行?步骤写一下,谢谢!急用!!! 展开
#include<malloc.h>
#include<stdlib.h>
typedef struct Node
{
char data;
struct Node *LChild;
struct Node *RChild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree T)
{
char ch;
ch=getchar();
if(ch=='#')
{
T=NULL;
}
else
{
T=(BiTNode *)malloc(sizeof(BiTNode));
if(!T) exit(-1);
T->data=ch;
CreateBiTree(T->LChild);
CreateBiTree(T->RChild);
}
}
void InOrder(BiTree root)
{
if(root!=NULL)
{
InOrder(root->LChild);
printf("%c",root->data);
InOrder(root->RChild);
}
}
void main()
{
BiTree T;
T=(BiTNode *)malloc(sizeof(BiTNode));
printf("Builing bitree:\n");
CreateBiTree(T);
InOrder(T);
getchar();
getchar();
}
程序运行不出结果,检查没有错误。请帮忙看一下,要怎么改,或者怎么运行?步骤写一下,谢谢!急用!!! 展开
展开全部
#include<stdio.h>
#include<malloc.h>
typedef struct Node
{
char data;
struct Node *LChild;
struct Node *RChild;
}BiTNode,*BiTree;
void create(BiTree *bt)
{
char ch;
ch=getchar();
if(ch=='.') *bt=NULL;
else
{
*bt=(BiTree)malloc(sizeof(BiTNode));
(*bt)->data=ch;
create(&((*bt)->LChild));
create(&((*bt)->RChild));
}
}
void preorder(BiTree root)
{
if(root!=NULL)
{
printf("%2c",root->data);
preorder(root->LChild);
preorder(root->RChild);
}
}
void inorder(BiTree root)
{
if(root!=NULL)
{
inorder(root->LChild);
printf("%2c",root->data);
inorder(root->RChild);
}
}
void postorder(BiTree root)
{
if(root!=NULL)
{
postorder(root->LChild);
postorder(root->RChild);
printf("%2c",root->data);
}
}
void PrintTree(BiTree root,int nlayer)
{
int i;
if(root==NULL) return;
PrintTree(root->RChild,nlayer+1);
for(i=0;i<nlayer;i++)
{
printf(" ");
}
printf("%c\n",root->data);
PrintTree(root->LChild,nlayer+1);
}
void main()
{
BiTree *bt;
int i=0,nlayer=0;
// clrscr();
printf("\ninsert preorder number:\n");
create(bt);
while(i!=4)
{
printf("please choose what do you want?\n");
printf("1.PreOrder\n");
printf("2.InOrder\n");
printf("3.PostOrder\n");
printf("4.Exit\n");
scanf("%d",&i);
switch(i)
{
case 1:preorder(*bt);printf("\n");
PrintTree(*bt,nlayer);printf("\n");
break;
case 2:inorder(*bt);printf("\n");
PrintTree(*bt,nlayer);printf("\n");
break;
case 3:postorder(*bt);printf("\n");
PrintTree(*bt,nlayer);printf("\n");
break;
case 4:break;
}
}
}
#include<malloc.h>
typedef struct Node
{
char data;
struct Node *LChild;
struct Node *RChild;
}BiTNode,*BiTree;
void create(BiTree *bt)
{
char ch;
ch=getchar();
if(ch=='.') *bt=NULL;
else
{
*bt=(BiTree)malloc(sizeof(BiTNode));
(*bt)->data=ch;
create(&((*bt)->LChild));
create(&((*bt)->RChild));
}
}
void preorder(BiTree root)
{
if(root!=NULL)
{
printf("%2c",root->data);
preorder(root->LChild);
preorder(root->RChild);
}
}
void inorder(BiTree root)
{
if(root!=NULL)
{
inorder(root->LChild);
printf("%2c",root->data);
inorder(root->RChild);
}
}
void postorder(BiTree root)
{
if(root!=NULL)
{
postorder(root->LChild);
postorder(root->RChild);
printf("%2c",root->data);
}
}
void PrintTree(BiTree root,int nlayer)
{
int i;
if(root==NULL) return;
PrintTree(root->RChild,nlayer+1);
for(i=0;i<nlayer;i++)
{
printf(" ");
}
printf("%c\n",root->data);
PrintTree(root->LChild,nlayer+1);
}
void main()
{
BiTree *bt;
int i=0,nlayer=0;
// clrscr();
printf("\ninsert preorder number:\n");
create(bt);
while(i!=4)
{
printf("please choose what do you want?\n");
printf("1.PreOrder\n");
printf("2.InOrder\n");
printf("3.PostOrder\n");
printf("4.Exit\n");
scanf("%d",&i);
switch(i)
{
case 1:preorder(*bt);printf("\n");
PrintTree(*bt,nlayer);printf("\n");
break;
case 2:inorder(*bt);printf("\n");
PrintTree(*bt,nlayer);printf("\n");
break;
case 3:postorder(*bt);printf("\n");
PrintTree(*bt,nlayer);printf("\n");
break;
case 4:break;
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询