二叉树程序如何的退出输入?
#include "stdafx.h"#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "malloc.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef struct BiTNode
{
int data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
Status CreateBiTree(BiTree &T)//创建二叉树
{
char ch;
scanf("%c",&ch);
if (ch=='#')
T=NULL;
else
{
if (!(T=(BiTNode*)malloc(sizeof(BiTNode)) ))
exit (OVERFLOW);
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return OK;
}//CreateBiTree
Status PreOrderTraverse(BiTree &t)//先序遍历二叉树 ,例如实现先序的输入输出,我该如何输入结点的值然后在输出结点的值?
{
if(t!=NULL)
{
printf("%c",t->data);
PreOrderTraverse(t->lchild);
PreOrderTraverse(t->rchild);
}
return OK;
}
Status InOrderTraverse(BiTree &t)
{
//中序遍历二叉树
if(t!=NULL)
{
InOrderTraverse(t->lchild);
printf("%c",t->data);
InOrderTraverse(t->rchild);
}
return OK;
}
Status PostOrderTraverse(BiTree &t){
//后序遍历二叉树
if(t!=NULL)
{
PostOrderTraverse(t->lchild);
PostOrderTraverse(t->rchild);
printf("%c",t->data);
}
return OK;
}
int main(int argc, char* argv[])
{
BiTree T;
CreateBiTree(T);
printf("PreOrderTraverse:");
PreOrderTraverse(T);
printf("\n");
printf("InOrderTraverse:");
InOrderTraverse(T);
printf("\n");
printf("PostOrderTraverse:");
PostOrderTraverse(T);
printf("\n");
return 0;
}