二叉树学习笔记(待补充)

一 二叉树的概念,建立,遍历,求深度

实例程序

#include<stdio.h>
#include<stdlib.h>

typedef struct tree
{
    char data;
    struct tree* tleft;
    struct tree* tright;
}*TREE;

static int num = 0;

TREE creattree(TREE &H)
{
    char da;
    printf("input the data to creat tree:/n");
    da = getchar();
    printf("da=%c/n",da);
    if(da == '#')
    {
        H = NULL;
    }
    else
    {
        H = (TREE)malloc(sizeof(TREE));
        //printf("sizeof(TREE)=%d,sizeof(struct tree)=%d/n",sizeof(TREE),sizeof(struct tree));
        if(H == NULL)
        {
            printf("malloc error!/n");
            return NULL;
        }
        H->data = da;
        creattree(H->tleft);
        creattree(H->tright);
    }
    return H;
}

void printtreexian(TREE H)
{
    if(H != NULL)
    {
        num++;
        printf("num = %d ,data = %c/n",num,H->data);
        printtreexian(H->tleft);
        printtreexian(H->tright);
    }
}

void printtreezhong(TREE H)
{
    if(H != NULL)
    {
        printtreezhong(H->tleft);
        num++;
        printf("num = %d ,data = %c/n",num,H->data);
        printtreezhong(H->tright);
    }
}

void printtreehou(TREE H)
{
    if(H != NULL)
    {
        printtreehou(H->tleft);
        printtreehou(H->tright);
        num++;
        printf("num = %d ,data = %c/n",num,H->data);
    }
}

void destroy(TREE H)
{
    if(H != NULL)
    {
        destroy(H->tleft);
        destroy(H->tright);
        free(H);
    }
}

int deepth(TREE H)
{
    if(H == NULL)
    {
        return 0;
    }
    return ((deepth(H->tleft) > deepth(H->tright) ? deepth(H->tleft):deepth(H->tright))+1);
}

void main()
{
    TREE head;
    printf("now creat the tree:/n");
    creattree(head);

    printf("先序遍历:/n");
    printtreexian(head);

    printf("中序遍历:/n");
    printtreezhong(head);

    printf("后序遍历:/n");
    printtreehou(head);

    int deep;
    deep = deepth(head);
    printf("deep = %d/n",deep);

    printf("销毁二叉树:/n");
    //destroy(head);

}

二 平衡二叉树

形态匀称的二叉树称为平衡二叉树 (Balanced binary tree) ,其严格定义是:
一棵空树是平衡二叉树;若 T 是一棵非空二叉树,其左、右子树为 TL 和 TR ,令 hl 和 hr 分别为左、右子树的深度。当且仅当
①TL 、 TR 都是平衡二叉树;
② | hl - hr |≤ 1;
时,则 T 是平衡二叉树。

image

(a)平衡二叉树           (b)非平衡二叉树

相应地定义 hl - hr 为二叉平衡树的平衡因子 (balance factor) 。因此,平衡二叉树上所有结点的平衡因子可能是 -1 , 0 , 1 。换言之,若一棵二叉树上任一结点的平衡因子的绝对值都不大于 1 ,则该树是就平衡二叉树。

所以判断是否是平衡二叉树的算法是:

首先检查当前树是否是空树,如果是空树,则是平衡二叉树,返回真;

若不是空树,则检查其是否满足两个条件:

  (1)左右子树深度差值不超过1;

  (2)左右子树也是平衡二叉树;

分别求的当前树的左右深度,判断差值是否超过1;

    ---如果超过1,说明此树不是二叉树,返回false;

    ---如果没有超过1,再判断左右子树是否都是平衡二叉树(递归实现);

       ---如果都是,说明当前树是平衡二叉树,返回true;

       ---如果不都是,说明当前树不是平衡二叉树,返回false;

程序:

bool balance(TREE H)
{
    if(H == NULL)
    {
        return true;
    }
    int dl = deepth(H->tleft);
    int dr = deepth(H->tright);
    if((dl-dr<=1)&&(dl-dr>=-1))
    {
        return balance(H->tleft)&&balance(H->tright);
    }
    else
    {
        return false;
    }
}

参考: http://blog.chinaunix.net/u3/105029/showart_2320608.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值