平衡二叉树 实例

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

《平衡二叉树 实例》

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

Adelson-Velskii 和 Landis 提出了一个动态地保持二叉排序树平衡的方法,其基本思想是:
  在构造二叉排序树的过程中,每当插入一个结点时,首先检查是否因插入而破坏了树的平衡性,如果是因插入结点而破坏了树的平衡性,则找出其中最小不平衡子树,在保持排序树特性的前提下,调整最小不平衡子树中各结点之间的连接关系,以达到新的平衡。通常将这样得到的平衡二叉排序树简称为 AVL 树
2.最小不平衡子树
  以离插入结点最近、且平衡因子绝对值大于 1 的结点作根结点的子树。为了简化讨论,不妨假设二叉排序树的最小不平衡子树的根结点为 A ,则调整该子树的规律可归纳为下列四种情况:
(1) LL 型:
  新结点 X 插在 A 的左孩子的左子树里。调整方法见图 8.5(a) 。图中以 B 为轴心,将 A 结点从 B 的右上方转到 B 的右下侧,使 A 成为 B 的右孩子。

《平衡二叉树 实例》
  图8.5 平衡调整的4种基本类型(结点旁的数字是平衡因子)
(2)RR 型:
  新结点 X 插在 A 的右孩子的右子树里。调整方法见图 8.5(b) 。图中以 B 为轴心,将 A 结点从 B 的左上方转到 B 的左下侧,使 A 成为 B 的左孩子。
(3)LR 型:
  新结点 X 插在 A 的左孩子的右子树里。调整方法见图 8.5(c) 。分为两步进行:第一步以 X 为轴心,将 B 从 X 的左上方转到 X 的左下侧,使 B 成为 X 的左孩子, X 成为 A 的左孩子。第二步跟 LL 型一样处理 ( 应以 X 为轴心 ) 。 
(4)RL 型:
  新结点 X 插在 A 的右孩子的左子树里。调整方法见图 8.5(d) 。分为两步进行:第一步以 X 为轴心,将 B 从 X 的右上方转到 X 的右下侧,使 B 成为 X 的右孩子, X 成为 A 的右孩子。第二步跟 RR 型一样处理 ( 应以 X 为轴心 ) 。
【例】
实际的插入情况,可能比图 8.5 要复杂。因为 A 、 B 结点可能还会有子树。现举一例说明,设一组记录的关键字按以下次序进行插入: 4 、 5 、 7 , 2 、 1 、 3 、 6 ,其生成及调整成二叉平衡树的过程示于图 8.6 。 
  在图 8.6 中,当插入关键字为 3 的结点后,由于离结点 3 最近的平衡因子为 2 的祖先是根结点 5 。所以,第一次旋转应以结点 4 为轴心,把结点 2 从结点 4 的左上方转到左下侧,从而结点 5 的左孩子是结点 4 ,结点 4 的左孩子是结点 2 ,原结点 4 的左孩子变成了结点 2 的右孩子。第二步再以结点 4 为轴心,按 LL 类型进行转换。这种插入与调整平衡的方法可以编成算法和程序,这里就不再讨论了。 

《平衡二叉树 实例》
 
   图 8.6 二叉平衡树插入结点 ( 结点旁的数字为其平衡因子 )

代码实现:

utl.h
#ifndef UTL_H_
#define UTL_H_
/*
 *整理了一些常用的功能,如内存管理
 */
#include<stdio.h>
#include<stdlib.h>

/*申请内存*/
inline void*xalloc(int size)
{
    void *p;
    p = (void *)malloc(size);
    /*申请失败*/
    if(p==NULL)
    {
        printf("alloc error\n");
        exit(1);
    }
    return p;
}
/*内存释放*/
#define xfree(p)free(p)

#endif
 
avl.h
#ifndef AVL_H__
#define AVL_H__
/*
 *avl树数据结构及相关操作
 */
#include<stdio.h>
#include<stdlib.h>

struct AVLTree
{
    unsigned int nData;    /*存储数据*/
    struct AVLTree* pLeft;    /*指向左子树*/
    struct AVLTree* pRight;    /*指向右子树*/
    int nHeight;    /*树的平衡度*/
};

/*插入操作*/
struct AVLTree* insert_tree(unsignedint nData,struct AVLTree* pNode);

/*查找操作,找到返回1,否则,返回0*/
int find_tree(unsignedint data,struct AVLTree* pRoot);

/*删除操作,删除所有节点*/
void delete_tree(struct AVLTree** ppRoot);

/*打印操作*/
void print_tree(struct AVLTree* pRoot);

#endif
 
avl.c
#include"avl.h"
#include "utl.h"

static intMax(int a,int b);
static int Height(struct AVLTree* pNode);

/*旋转操作*/
static struct AVLTree* SingleRotateWithLeft(struct AVLTree* pNode);
static struct AVLTree* SingleRotateWithRight(struct AVLTree* pNode);
static struct AVLTree* DoubleRotateWithLeft(struct AVLTree* pNode);
static struct AVLTree* DoubleRotateWithRight(struct AVLTree* pNode);

struct AVLTree* insert_tree(unsignedint nData,struct AVLTree* pNode)
{
    if (NULL== pNode)
    {
        pNode = (struct AVLTree*)xalloc(sizeof(struct AVLTree));
        pNode->nData= nData;
        pNode->nHeight= 0;
        pNode->pLeft= pNode->pRight= NULL;
    }
    else if (nData < pNode->nData)    /*插入到左子树中*/
    {
        pNode->pLeft= insert_tree(nData, pNode->pLeft);
        if (Height(pNode->pLeft)- Height(pNode->pRight)== 2)    /*AVL树不平衡*/
        {
            if (nData < pNode->pLeft->nData)
            {
                /*插入到了左子树左边, 做单旋转*/
                pNode = SingleRotateWithLeft(pNode);
            }
            else 
            {
                /*插入到了左子树右边, 做双旋转*/
                pNode = DoubleRotateWithLeft(pNode);
            }
        }
    }
    else if (nData > pNode->nData)    /*插入到右子树中*/
    {
        pNode->pRight= insert_tree(nData, pNode->pRight);
        if (Height(pNode->pRight)- Height(pNode->pLeft)== 2)    /*AVL树不平衡*/
        {
            if (nData > pNode->pRight->nData)
            {
                /*插入到了右子树右边, 做单旋转*/
                pNode = SingleRotateWithRight(pNode);
            }
            else 
            {
                /*插入到了右子树左边, 做双旋转*/
                pNode = DoubleRotateWithRight(pNode);
            }
        }
    }

    pNode->nHeight= Max(Height(pNode->pLeft), Height(pNode->pRight))+ 1;

    return pNode;
}


/*删除树*/
void delete_tree(struct AVLTree** ppRoot)
{
    if (NULL== ppRoot||NULL ==*ppRoot)
        return;

    delete_tree(&((*ppRoot)->pLeft));
    delete_tree(&((*ppRoot)->pRight));
    xfree(*ppRoot);
    *ppRoot = NULL;
}

/*中序遍历打印树的所有结点, 因为左结点 < 父结点 < 右结点, 因此打印出来数据的大小是递增的*/
void print_tree(struct AVLTree* pRoot)
{
    if (NULL== pRoot)
        return;

    static int n = 0;


    print_tree(pRoot->pLeft);
    printf("[%d]nData = %u\n",++n, pRoot->nData);
    print_tree(pRoot->pRight);
}

/*
 *查找操作,找到返回1,否则,返回0
 *data是待查找的数据
 *pRoot:avl树的指针
 */
int find_tree(unsignedint data,struct AVLTree* pRoot)
{
    static int k=1;    /*查找次数*/
    if (NULL== pRoot)
    {
        printf("not find %d times\n", k);
        return 0;
    }

    if(data== pRoot->nData)
    {
        printf("find:%d times\n", k);
        return 1;
    }
    else if(data < pRoot->nData)
    {
        ++k;
        return find_tree(data, pRoot->pLeft);
    }
    else if(data > pRoot->nData)
    {
        ++k;
        return find_tree(data, pRoot->pRight);
    }
}

static intMax(int a,int b)
{
    return (a > b ? a : b);
}

/*返回节点的平衡度*/
static int Height(struct AVLTree* pNode)
{
    if (NULL== pNode)
        return -1;

    return pNode->nHeight;
}

/********************************************************************
      pNode pNode->pLeft 
      / \
  pNode->pLeft ==> pNode
      \     /
  pNode->pLeft->pRight pNode->pLeft->pRight
 *********************************************************************/
static struct AVLTree* SingleRotateWithLeft(struct AVLTree* pNode)
{
    struct AVLTree* pNode1;

    pNode1 = pNode->pLeft;
    pNode->pLeft= pNode1->pRight;
    pNode1->pRight= pNode;

    /*结点的位置变了, 要更新结点的高度值*/
    pNode->nHeight= Max(Height(pNode->pLeft), Height(pNode->pRight))+ 1;
    pNode1->nHeight= Max(Height(pNode1->pLeft), pNode->nHeight)+ 1;

    return pNode1;
}

/********************************************************************
  pNode pNode->pRight
  \ /
  pNode->pRight ==> pNode 
  / \
  pNode->pRight->pLeft pNode->pRight->pLeft
 *********************************************************************/
static struct AVLTree* SingleRotateWithRight(struct AVLTree* pNode)
{
    struct AVLTree* pNode1;

    pNode1 = pNode->pRight;
    pNode->pRight= pNode1->pLeft;
    pNode1->pLeft= pNode;

    /*结点的位置变了, 要更新结点的高度值*/
    pNode->nHeight= Max(Height(pNode->pLeft), Height(pNode->pRight))+ 1;
    pNode1->nHeight= Max(Height(pNode1->pRight), pNode->nHeight)+ 1;

    return pNode1;
}

static struct AVLTree* DoubleRotateWithLeft(struct AVLTree* pNode)
{
    pNode->pLeft= SingleRotateWithRight(pNode->pLeft);

    return SingleRotateWithLeft(pNode);
}

static struct AVLTree* DoubleRotateWithRight(struct AVLTree* pNode)
{
    pNode->pRight= SingleRotateWithLeft(pNode->pRight);

    return SingleRotateWithRight(pNode);
}

测试:
#include <stdio.h>
#include <time.h>
#include "avl.h"
int main()
{
    int i,j;
    AVLTree* pRoot = NULL;

    srand((unsignedint)time(NULL));
    
    for (i = 0; i< 10;++i)
    {
            j = rand();
            printf("%d\n", j);
        pRoot = Insert(j, pRoot);
    }

    PrintTree(pRoot);

    DeleteTree(&pRoot);

    return 0;
}
    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/luhaoting110/article/details/18803641
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞