#2
azzbcc2014-10-22 11:01
|
要求:采用二叉链表作为存储结构,以加入虚结点的先序序列输入该二叉树,并设置选单,依据选单项分别输出该二叉树的先序、中序、后序序列。二叉树子结点的数据域可采用字符类型。
测试数据:建立如图所示的二叉树,建立时的输入序列为:ABD000CE00F00
程序代码:
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
char data;
struct node * LChild;
struct node * RChild;
}biTnode,*biTree;
void preorder(biTree root)
{
if(root!=NULL)
{
printf("%c",root->data);
preorder(root->LChild);
preorder(root->RChild);
}
}
void inorder(biTree root)
{
if(root!=NULL)
{
inorder(root->LChild);
printf("%c",root->data);
inorder(root->RChild);
}
}
void postorder(biTree root)
{
if(root!=NULL)
{
postorder(root->LChild);
postorder(root->RChild);
printf("%c",root->data);
}
}
void GreatebiTree(biTree * bt)
{
char ch;
ch = getchar();
if(ch == '#')
*bt = NULL;
else
{
*bt = (biTree)malloc(sizeof(biTnode));
(*bt)->data = ch;
GreatebiTree(&((*bt)->LChild));
GreatebiTree(&((*bt)->RChild));
}
}
int main()
{
biTnode * bt;
int num;
printf("please enter this tree:\n");
GreatebiTree(&bt);
printf("┌────────────────────┐\n");
printf("│ please choose one input the tree: │\n");
printf("│ 1.preorder │\n");
printf("│ 2.inorder │\n");
printf("│ 3.postorder │\n");
printf("└────────────────────┘\n");
scanf("%d",&num);
switch(num)
{
case 1:
preorder(bt);
case 2:
inorder(bt);
case 3:
postorder(bt);
default:
break;
}
return 0;
}
#include<malloc.h>
typedef struct node
{
char data;
struct node * LChild;
struct node * RChild;
}biTnode,*biTree;
void preorder(biTree root)
{
if(root!=NULL)
{
printf("%c",root->data);
preorder(root->LChild);
preorder(root->RChild);
}
}
void inorder(biTree root)
{
if(root!=NULL)
{
inorder(root->LChild);
printf("%c",root->data);
inorder(root->RChild);
}
}
void postorder(biTree root)
{
if(root!=NULL)
{
postorder(root->LChild);
postorder(root->RChild);
printf("%c",root->data);
}
}
void GreatebiTree(biTree * bt)
{
char ch;
ch = getchar();
if(ch == '#')
*bt = NULL;
else
{
*bt = (biTree)malloc(sizeof(biTnode));
(*bt)->data = ch;
GreatebiTree(&((*bt)->LChild));
GreatebiTree(&((*bt)->RChild));
}
}
int main()
{
biTnode * bt;
int num;
printf("please enter this tree:\n");
GreatebiTree(&bt);
printf("┌────────────────────┐\n");
printf("│ please choose one input the tree: │\n");
printf("│ 1.preorder │\n");
printf("│ 2.inorder │\n");
printf("│ 3.postorder │\n");
printf("└────────────────────┘\n");
scanf("%d",&num);
switch(num)
{
case 1:
preorder(bt);
case 2:
inorder(bt);
case 3:
postorder(bt);
default:
break;
}
return 0;
}
编译也没有错,就是结果出不来,求教。