|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
#define MAXCSIZE 100
typedef int TElemType;
typedef struct BitNode {
TElemType data;
struct BitNode *lchild,*rchild;
}BitNode,*BitTree; /*二叉树的构建*/
BitTree CreateBiTree(void) {
BitTree bt;
TElemType x;
scanf("%d",&x);
if(x==-1)
bt=NULL;
else {
bt=(BitTree)malloc(sizeof(BitNode));
bt->data=x;
bt->lchild=CreateBiTree();
bt->rchild=CreateBiTree();
} return bt; } /*二叉树的中序遍历*/
void InOrderTraverse(BitTree bt) {
if(bt!=NULL) {
InOrderTraverse(bt->lchild);
printf("%d",bt->data);
InOrderTraverse(bt->rchild);
} } /*二叉树的后序遍历*/
void PostOrderTraverse(BitTree bt)
{ if(bt!=NULL)
{ PostOrderTraverse(bt->lchild);
PostOrderTraverse(bt->rchild);
printf("%d",bt->data);
} } /*二叉树的先序遍历*/
void PreOrderTraverse(BitTree bt) {
if(bt!=NULL) {
printf("%d",bt->data);
PreOrderTraverse(bt->lchild);
PreOrderTraverse(bt->rchild);
} }
int main()
{ BitTree bt;
bt=CreateBiTree();
printf("\n中序遍历");
InOrderTraverse(bt);
printf("\n后序遍历");
PostOrderTraverse(bt);
printf("\n先序遍历");
PreOrderTraverse(bt);
printf("\n");
}
|
|