#include <stdio.h>
typedef struct bitode
{
char data;
struct bitode *lchild,*rchild;
} binode,*bitree;
void creattree(bitree &t)
{
char a;
//printf("请输入结点值\n");
scanf("%c",&a);
if(a=='#')
t=NULL;
else
{
t=new bitode;
t->data=a;
creattree(t->lchild);
creattree(t->rchild);
}
}
void Front(bitree t)//前序遍历
{
if(t)
{
printf("%c ",t->data);
Front(t->lchild);
Front(t->rchild);
}
}
void order(bitree t)//中序遍历
{
if(t)
{
order(t->lchild);
printf("%c ",t->data);
order(t->rchild);
}
}
void b3(bitree t)//后序遍历
{
if(t)
{
b3(t->lchild);
b3(t->rchild);
printf("%c ",t->data);
}
}
int Count(bitree t)
{
if(t==NULL)
return 0;
else
return Count(t->lchild)+Count(t->rchild)+1;
}
int main()
{
bitree t;
creattree(t);
Front(t);
b3(t);
printf("%d",Count(t));
return 0;
}
输入样例:ABC##D##EF####
结果:ABCDEF 5
二叉树的性质
(1)二叉树的第i层上至多有2的i-1次方个结点
证明:归纳法:每层最多的
第一层 1
第二层 2
第三层 4
第四层 8
…
(2)深度为k的二叉树至多有2的k次方减一个结点
证明:由性质1
两个综合来记:总的结点数比一层的节点数的次方大。
(3)n0结点比n2结点多一个
常用式子:
n总=n0+n1+n2
n0=n2+1