/*二叉树子系统的实现*/
#include<stdio.h>
#include<stdlib.h>
#define MAXLEN 100
typedef struct BT
{
char data;
struct BT *Lchild;
struct BT *Rchild;
}BT;
int count = 0;
BT *Create_BT() /*创建二叉树*/
{
BT *T;
char x;
scanf("%c",&x);
//getchar();
if(' '==x)
T = NULL;
else
{
T=(BT*)malloc(sizeof(BT));
T->data = x;
printf("\n\t\tLchild Node:",T->data);
T->Lchild = Create_BT();
printf("\n\t\tRchild Node:",T->data);
T->Rchild = Create_BT();
}
return T;
}
void PreOrder (BT *T) /*DLR*/
{
if(T)
{
printf("%3c",T->data);
PreOrder(T->Lchild);
PreOrder(T->Rchild);
}
}
void InOrder (BT *T) /*LDR*/
{
if(T)
{
InOrder(T->Lchild);
printf("%3c",T->data);
InOrder(T->Rchild);
}
}
void PostOrder (BT *T) /*LRD*/
{
if(T)
{
PostOrder(T->Lchild);
PostOrder(T->Rchild);
printf("%3c",T->data);
}
}
void LevelOrder (BT *T)
{
int i,j;
BT *q[100],*p;
p=T;
if(p!=NULL)
{
i=1;q[i]=p;j=2;}
while(i!=j)
{
p=q[i];printf("%3c",p->data);
if(p->Lchild!=NULL)
{
q[j]=p->Lchild;j++;
}
if(p->Rchild!=NULL)
{
q[j]=p->Rchild;j++;
}
i++;
}
}
void LeafNum (BT *T)
{
if(T)
{
if(T->Lchild==NULL&&T->Rchild==NULL)
count++;
LeafNum(T->Lchild);
LeafNum(T->Rchild);
}
}
void NodeNum (BT *T)
{
if(T)
{
count++;
NodeNum(T->Lchild);
NodeNum(T->Rchild);
}
}
int TreeDepth (BT *T)
{
int ldep,rdep;
if(T==NULL)
return 0;
else
{
ldep=TreeDepth(T->Lchild);
rdep=TreeDepth(T->Rchild);
if(ldep>rdep)
return ldep+1;
else return rdep+1;
}
}
void ShowTree (BT *T)
{
BT *stack[MAXLEN],*p;
int level[MAXLEN][2],top,n,i,width=4;
if(T)
{
printf("\n\t\tShowTree:\n\t\t");
top=1;
stack[top]=T;
level[top][0]=width;
while(top>0)
{
p=stack[top];
n=level[top][0];
for(i=1;i<=n;i++)
printf(" ");
printf("%c",p->data);
for(i=n+1;i<50;i+=2)
printf(" ");
top--;
if(p->Rchild!=NULL)
{
top++;
stack[top]=p->Rchild;
level[top][0]=n+width;
level[top][1]=2;
}
if(p->Lchild!=NULL)
{
top++;
stack[top]=p->Lchild;
level[top][0]=n+width;
level[top][1]=1;
}
}
}
}
void main() /*主函数*/
{
BT *T=NULL;
char ch1,ch2,a;
ch1='y';
while(ch1=='y'||ch1=='Y')
{
printf("\n\n\n\n");
printf("\n\t\t\tBT System");
printf("\n\t\t**************************");
printf("\n\t\t 1.Creater a BT");
printf("\n\t\t 2.ShowTree");
printf("\n\t\t 3.PreOrder");
printf("\n\t\t 4.InOrder");
printf("\n\t\t 5.PostOrder");
printf("\n\t\t 6.LevelOrder");
printf("\n\t\t 7.LeafNum");
printf("\n\t\t 8.NodeNum");
printf("\n\t\t 9.TreeDepth");
printf("\n\t\t 0.Exit");
printf("\n\t\t**************************");
printf("\n\t\t Please Select A Num(0~9):");
scanf("%c",&ch2);
getchar();
printf("\n");
switch(ch2)
{
case '1': printf("\n\t\tInput BT Node By PreOrder:" );
printf("\n\t\tInput root Node:");
T=Create_BT();
printf("\n\t\tCreate BT successfully!");
break;
case '2': ShowTree(T);break;
case '3': printf("\n\t\tDLR: ");
PreOrder(T);
break;
case '4': printf("\n\t\tLDR:");
InOrder(T);
break;
case '5': printf("\n\t\tLRD:");
PostOrder(T);
break;
case '6': printf("\n\t\tLevel:");
LevelOrder(T);
break;
case '7': count=0;
LeafNum(T);
printf("\n\t\tThe BT have %d Leaf.\n",count);
break;
case '8': count=0;
NodeNum(T);
printf("\n\t\tThe BT have %d Node.\n",count);
break;
case '9': printf("\n\t\tThe TreeDepth is %d.\n",TreeDepth(T));
break;
case '0': ch1='n';
break;
default: printf("\n\t\t******Input Wrong !");
}
if(ch2!='0')
{
printf("\n\t\tPress Enter countinue Or press any key Exit\n");
a=getchar();
if(a!='\xA')
{
getchar();ch1='n';
}
}
}
}