二叉树 建立成功 指令无法输出
#include<stdio.h>#include<stdlib.h>
typedef char DateType;
typedef struct Node
{
DateType date;
struct Node *lchild;
struct Node *rchild;
}BiTreeNode;
void PrintDate(DateType x)
{
printf("%c",x);
}
void CreatBiTree(BiTreeNode *T)
{
DateType ch;
scanf("%c",&ch);
if(ch==' ') {T=NULL;return;}
else
{
if(!(T=(BiTreeNode*)malloc(sizeof(BiTreeNode)))){printf("Overflow.\n");exit(0);}
(T)->date=ch;
CreatBiTree(T->lchild);
CreatBiTree(T->rchild);
}
}
void PreOrder(BiTreeNode *T,void(*Visit)(DateType item))
{
if(T!=NULL)
{
Visit(T->date);
PreOrder(T->lchild,Visit);
PreOrder(T->rchild,Visit);
}
}
void InOrder(BiTreeNode *T,void(*Visit)(DateType item))
{
if(T!=NULL)
{
InOrder(T->lchild,Visit);
Visit(T->date);
InOrder(T->rchild,Visit);
}
}
void PostOrder(BiTreeNode *T,void(*Visit)(DateType item))
{
if(T!=NULL)
{
PostOrder(T->lchild,Visit);
PostOrder(T->rchild,Visit);
Visit(T->date);
}
}
void main()
{
int choice;
BiTreeNode *root;
printf("下面建立一颗二叉树,结点数据输入顺序按先序次序\n");
printf("输入数据请注意,每一个非空结点的所有还走都要给出\n");
printf("如孩子为空,请用空格表示。\n");
printf("请输入二叉树结点数据,这里设定为字符\n");
CreatBiTree(root);
printf("******************************\n");
printf(" 1.先序序列:\n");
printf(" 2.中序序列:\n");
printf(" 3.后序序列:\n");
printf(" 4.其他值则退出:\n");
printf("******************************\n");
do
{
printf("请输入对应的操作码:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n先序序列为:");
PreOrder(root,PrintDate);printf("\n");break;
case 2:printf("\n中序序列:");
InOrder(root,PrintDate);printf("\n");break;
case 3:printf("后序序列:");
PostOrder(root,PrintDate);printf("\n");break;
}
}while(choice>0&&choice<4);
}