二叉树的遍历

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值