//**************二叉树****************
//题目要求:求其先序,和顺序(即层序)和层次数
// 数据描述为:
// typedef struct BiTNode{
// TElemType data;
// Struct BiTNode *lchild ,*rchild;
// }BiTNode,*BiTree;
#include"stdio.h"
#include"stdlib.h"
#define MAX 20
#define NULL 0
typedef char TElemType;
typedef int Status;
typedef struct BiTNode{
TElemType data;
struct BiTNode*lchild ,*rchild;
}BiTNode,*BiTree;
Status CreateBiTree(BiTree *T)
{ char ch;
ch=getchar();
if(ch=='#')
(*T)=NULL;
else
{ (*T)=(BiTree)malloc(sizeof(BiTNode));
(*T)->data=ch;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
return 1;
}
void PreOrder(BiTree T)
{ if(T)
{ printf("%2c",T->data);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
void LevelOrder(BiTree T)
{ BiTree Queue[MAX],b;
int front,rear;
front=rear=0;
if(T)
{ Queue[rear++]=T;
while(front!=rear)
{ b=Queue[front++];
printf("%2c",b->data);
if(b->lchild!=NULL)
Queue[rear++]=b->lchild;
if(b->rchild!=NULL)
Queue[rear++]=b->rchild;
}
}
}
int depth(BiTree T)
{ int dep1,dep2;
if(T==NULL)
return 0;
else
{dep1=depth(T->lchild);
dep2=depth(T->rchild);
return dep1>dep2?dep1+1:dep2+1;
}
}
void main()
{ BiTree T=NULL;
printf("*****************二叉树****************");
printf("\nCreate a Binary Tree\n");
CreateBiTree(&T);
printf("\n The preorder is: \n");
PreOrder(T);
printf("\n The level order is:\n");
LevelOrder(T);
printf("\n The depth is:%d\n",depth(T));
}