2017-4-18 9:22
#include<iostream>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
BiTree Creat()
{
BiTree T;
char ch;
scanf("%c",&ch);
if(ch == '#')
T = NULL;
else
{
T = (BiTree)malloc(sizeof(BiTNode));
T->data = ch;
T->lchild = Creat();
T->rchild = Creat();
}
return T;
}
int BianLi1(BiTree T) //先序遍历
{
if(T == NULL)
return 0;
else
{
printf("%c ",T->data);
if(T->lchild != NULL)
BianLi1(T->lchild);
if(T->rchild != NULL)
BianLi1(T->rchild);
}
return 1;
}
int BianLi2(BiTree T) //中序遍历
{
if(T == NULL)
return 0;
else
{
if(T->lchild != NULL)
BianLi2(T->lchild);
printf("%c ",T->data);
if(T->rchild != NULL)
BianLi2(T->rchild);
}
return 1;
}
int BianLi3(BiTree T) //后序遍历
{
if(T==NULL)
return 0;
else
{
if(T->lchild != NULL)
BianLi3(T->lchild);
if(T->rchild != NULL)
BianLi3(T->rchild);
printf("%c ",T->data);
}
return 1;
}
int depth(BiTree T) //求二叉树的深度
{
int depth1,depth2;
if(T==NULL)
return 0;
else
{
depth1 = depth(T->lchild);
depth2 = depth(T->rchild);
}
if(depth1 > depth2)
return (depth1+1);
else
return (depth2+1);
}
int main()
{
BiTree T;
int depth_number;
T = Creat();
BianLi1(T);
printf("\n");
BianLi2(T);
printf("\n");
BianLi3(T);
printf("\n");
depth_number =depth(T);
printf("树的深度为:%d\n",depth_number);
return 0;
}