数据结构: 平衡二叉树

一、定义

        平衡二叉树(Balanced Binary Tree)又被称为AVL树(有别于AVL算法),且具有以下性质:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。平衡二叉树的常用算法有红黑树、AVL、Treap、伸展树等。

     平衡二叉树是在二叉排序树(BST)上引入的,就是为了解决二叉排序树的不平衡性导致时间复杂度大大下降,那么AVL就保持住了(BST)的最好时间复杂度O(logn),所以每次的插入和删除都要确保二叉树的平衡。平衡二叉树见图1所示。

《数据结构: 平衡二叉树》

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

二、作用

        对于一般的二叉搜索树(Binary Search Tree),其期望高度(即为一棵平衡树时)为log2n,其各操作的时间复杂度(O(log2n))同时也由此而决定。但是,在某些极端的情况下(如在插入的序列是有序的时),二叉搜索树将退化成近似链或链,此时,其操作的时间复杂度将退化成线性的,即O(n)。我们可以通过随机化建立二叉搜索树来尽量的避免这种情况,但是在进行了多次的操作之后,由于在删除时,我们总是选择将待删除节点的后继代替它本身,这样就会造成总是右边的节点数目减少,以至于树向左偏沉。这同时也会造成树的平衡性受到破坏,降低它的操作的时间复杂度。
    平衡二叉搜索树(Balanced Binary Tree)具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。常用算法有红黑树、AVL、Treap、伸展树等。在平衡二叉搜索树中,我们可以看到,其高度一般都良好地维持在O(log2n),大大降低了操作的时间复杂度。

三、动态平衡技术

1.动态平衡技术

        Adelson-Velskii 和 Landis 提出了一个动态地保持二叉1.动态平衡技术
        Adelson-Velskii 和 Landis 提出了一个动态地保持二叉排序树平衡的方法,其基本思想是:在构造二叉排序树的过程中,每当插入一个结点时,首先检查是否因插入而破坏了树的平衡性,如果是因插入结点而破坏了树的平衡性,则找出其中最小不平衡子树,在保持排序树特性的前提下,调整最小不平衡子树中各结点之间的连接关系,以达到新的平衡。通常将这样得到的平衡二叉排序树简称为 AVL 树。

2.最小不平衡子树

        以离插入结点最近、且平衡因子绝对值大于 1 的结点作根结点的子树。为了简化讨论,不妨假设二叉排序树的最小不平衡子树的根结点为 A ,则调整该子树的规律可归纳为下列四种情况:

(1) LL 型:
  新结点 X 插在 A 的左孩子的左子树里。调整方法见图 2(a) 。图中以 B 为轴心,将 A 结点从 B 的右上方转到 B 的右下侧,使 A 成为 B 的右孩子。

《数据结构: 平衡二叉树》

图2

(2)RR 型:
  新结点 X 插在 A 的右孩子的右子树里。调整方法见图 2(b) 。图中以 B 为轴心,将 A 结点从 B 的左上方转到 B 的左下侧,使 A 成为 B 的左孩子。
(3)LR 型:
  新结点 X 插在 A 的左孩子的右子树里。调整方法见图 2(c) 。分为两步进行:第一步以 X 为轴心,将 B 从 X 的左上方转到 X 的左下侧,使 B 成为 X 的左孩子, X 成为 A 的左孩子。第二步跟 LL 型一样处理 ( 应以 X 为轴心 ) 。
(4)RL 型:
  新结点 X 插在 A 的右孩子的左子树里。调整方法见图 2(d) 。分为两步进行:第一步以 X 为轴心,将 B 从 X 的右上方转到 X 的右下侧,使 B 成为 X 的右孩子, X 成为 A 的右孩子。第二步跟 RR 型一样处理 ( 应以 X 为轴心 ) 。

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

《数据结构: 平衡二叉树》

图3 平衡二叉树的建立

3.代码实现

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(unsigned int nData, struct AVLTree* pNode);

/*查找操作,找到返回1,否则,返回0*/
int find_tree(unsigned int 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 int Max(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(unsigned int 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(unsigned int 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 int Max(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((unsigned int)time(NULL));
    
    for (i = 0; i < 10; ++i)
    {
            j = rand();
            printf("%d\n", j);
        pRoot = Insert(j, pRoot);
    }

    PrintTree(pRoot);

    DeleteTree(&pRoot);

    return 0;
}

四、参考文献

1、http://sjjg.js.zwu.edu.cn/SFXX/chazhao/chazhao7.3.2.2.html

2、baike.baidu.com/view/593144.htm

3、http://caoruntao.iteye.com/blog/1013550

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