数据结构:浅谈平衡二叉树

前言碎语

记得第一次读到关于二叉树的插入与平衡的操作,是在《大话数据结构》里,当然觉得好像有那么一回事,但毕竟纸上得来终觉浅,绝知此事要躬行。看懂了,不代表自己就真的会了。当时算是有一个感性认识吧,因为没有自己动手实践过,所以理解的并不深刻。

今天是重新学习,并且是自己动手实现了一遍,才算有了一点浅显的认识。

一点浅薄的认识

对于那些术语:左单旋、右单旋、左右双旋、右左双旋什么的,刚接触,确实有一种“哇,好厉害的样子~”的感觉,然而没有自己动手之前,一切都显得很虚无。

在自己动手理清楚了一遍后,我是这么来理解这四种操作的:

首先,这个左、右,说的都是结点的位置。
左单旋,即表示:左边的结点要发生旋转,那要旋到哪呢?旋到上一层的父结点那
左右单旋,即表示:左结点的结点要发生旋转,旋到哪呢?先从右孩子的位置旋到它的父结点那,然后在作为左孩子再往上旋到上一层父结点。

所以,结点的实际旋转方向和这四个名词的左右是相反的。并且,每一次发生旋转,都是相对与最上层(也就是最终要旋转到的那一次父结点)为“相对点”,其实也就是根结点,最终返回的也是这个结点的位置。

具体的思路

具体的实现,结合代码来理解会好很多,所以,全文贴在这里:

/* AVL */
#include <cstdio>
#include <cstdlib>

typedef int ElementType;
typedef struct AVLNode *Position;
typedef Position AVLTree;
struct AVLNode{
    ElementType Data;
    AVLTree Left;
    AVLTree Right;
    int Height;
};

AVLTree SingleLeftRotation ( AVLTree A );
AVLTree DoubleLeftRightRotation ( AVLTree A);
AVLTree SingleRightRotation ( AVLTree A);
AVLTree DoubleRightLeftRotation ( AVLTree A);
AVLTree Insert( AVLTree T, ElementType X );

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

//获取树的高度
//只有一个结点,高度为0
int GetHeight( AVLTree A )
{
    if ( A )
        return Max(GetHeight(A->Left), GetHeight(A->Right)) + 1;
    else
        return 0;
}

//左单旋
//麻烦结点在发现者的左子树的左边
//根结点A,左子树结点B,BL插了两个
//解决:把B作为根结点,BR接到A左边,A接到B右边

AVLTree SingleLeftRotation( AVLTree A)
{
    AVLTree B = A->Left;
    A->Left = B->Right;
    B->Right = A;
    //还要记得更新各自结点的高度
    A->Height = Max(GetHeight(A->Left), GetHeight(A->Right)) + 1;
    B->Height = Max(GetHeight(B->Left), A->Height) + 1;

    //此时B为根结点,返回B
    return B;
}

//右单旋
AVLTree SingleRightRotation( AVLTree A)
{
    AVLTree B = A->Right;
    A->Right = B->Left;
    B->Left = A;
    A->Height = Max(GetHeight(A->Left), GetHeight(A->Right)) + 1;
    B->Height = Max(A->Height, GetHeight(B->Right)) + 1;

    return B;
}

//左单旋:把左边的子结点旋上来,左是指左边的结点(方向是相反的)
//右单旋:把右边的子结点旋上来
//且左单旋、右单旋都是“相对于”根结点而言的

//左右双旋(左结点的右结点要旋上来)
//麻烦结点在发现者左子树的右边(的左孩子或右孩子)
//即A现在是根,B是左子,C是B的右子
//先把C右旋上一层,再左旋上一层
AVLTree DoubleLeftRightRotation( AVLTree A )
{
    A->Left = SingleRightRotation(A->Left);
    return SingleLeftRotation(A);
    //左单旋和右单旋返回的都是“根结点”的位置
}

//右左双旋(右结点的左结点要旋上来)
AVLTree DoubleRightLeftRotation( AVLTree A )
{
    A->Right = SingleLeftRotation(A->Right);
    return SingleRightRotation(A);
}

//建立树的过程(如何插入)
AVLTree Insert( AVLTree T, ElementType X)
{
    //先插入再调整,空树的情况下要申请一个空间
    if ( !T ) {
        T = (AVLTree)malloc(sizeof(struct AVLNode));
        T->Data = X;
        T->Height = 0;
        T->Left = T->Right = NULL;
    }
    else if ( X < T->Data ) {
        T->Left = Insert(T->Left, X);
        //判断是否需要旋转的情况
        //如果只是通过T的高度,无法判断要怎么旋转
        //也不需要大于等于2
        if ( GetHeight(T->Left) - GetHeight(T->Right) == 2 ) {
            if ( X < T->Left->Data )
                //注意一点,不管单旋,还是双旋,都是以相对于发现者而言的的
                T = SingleLeftRotation(T);
            else
                T = DoubleLeftRightRotation(T);
        }
    }
    else if ( X > T->Data ) {
        //先把结点插进去,然后再进行判断
        T->Right = Insert(T->Right, X);
        if ( GetHeight(T->Left) - GetHeight(T->Right) == -2 ) {
            if ( X > T->Right->Data )
                T = SingleRightRotation(T);
            else
                T = DoubleRightLeftRotation(T);
        }
    }
    //X == T->Data时不需要插入,说明重复了,二叉树里不考虑重复的结点
    //最后,更新结点的高度
    T->Height = Max(GetHeight(T->Left), GetHeight(T->Right)) + 1;

    return T;
}

这里有对应的一道实战题,也贴上:

Root of AVL Tree (25 point(s))
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.
Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print the root of the resulting AVL tree in one line.

Sample Input 1:
5
88 70 61 96 120
Sample Output 1:
70
Sample Input 2:
7
88 70 61 96 120 90 65
Sample Output 2:
88

最后,作为小白的嵌套循环,唯一能做的,就是耐心、细致,一点点进步。

转载于:https://www.cnblogs.com/ZealYoung/p/10849950.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值