二叉排序树AVL如何实现动态平衡

什么是avl树 大家好,我是bigsai,好久不见,甚是想念,今天给大家讲讲avl树。
对于树这种数据结构,想必大家也已经不再陌生,我们简单回顾一下。
在树的种类中,通常分成二叉树和多叉树,我们熟悉的二叉树种类有二叉搜索(排序、查找)树、二叉平衡树、伸展树、红黑树等等。而熟悉的多叉树像b树、字典树都是经典多叉树。
普通的二叉树,我们研究其遍历方式,因为其没啥规则约束查找和插入都很随意所以很少有研究价值。
但是二叉树结构上很有特点:左孩子和右孩子,两个不同方向的孩子对应二进制的01,判断的对错,比较的大小 ,所以根据这个结构所有树左侧节点比父节点小,右侧节点比父节点大,这时候就诞生了二叉搜索(排序)树。二叉搜索(排序)树的一大特点就是查找效率提高了,因为查找一个元素位置或者查看元素是否存在通过每遇到一个节点直接进行比较就可以一步步逼近结果的位置。
但二叉搜索(排序树)有个很大的问题就是当插入节点很有序,很可能成为一棵斜树或者深度很高,那么这样的一个查找效率还是趋近于线性o(n)级别,所以这种情况二叉搜索(排序)树的效率是比较低的。
所以,人们有个期望:对一棵树来说插入节点,小的还在左面,大的还在右面方便查找,但是能不能不要出现那么斜的情况?
这不,平衡二叉搜索(avl)树就是这么干的,avl在插入的时候每次都会旋转自平衡,让整个树一直处于平衡状态,让整个树的查询更加稳定(logn)。我们首先来看一下什么是avl树:
avl树是带有平衡条件的二叉搜索树,这个平衡条件必须要容易保持,而且要保证它的深度是o(logn)。
avl的左右子树的高度差(平衡因子)不大于1,并且它的每个子树也都是平衡二叉树。
对于平衡二叉树的最小个数,n0=0;n1=1;nk=n(k-1)+n(k-2)+1;(求法可以类比斐波那契)
难点:avl是一颗二叉排序树,用什么样的规则或者规律让它能够在复杂度不太高的情况下实现动态平衡呢?
  不平衡情况 如果从简单情况模型看,其实四种不平衡情况很简单,分别是rr,ll,rl,lr四种不平衡情况。
然后将其平衡的结果也很容易(不考虑其附带节点只看结果),将中间大小数值移动最上方,其他相对位置不变即可:
当然,这个仅仅是针对三个节点情况太过于理想化了,很多时候让你找不平衡的点,或者我们在解决不平衡的时候,我们需要的就是找到第一个不平衡(从底往上)的点将其平衡即可,下面列举两个不平衡的例子:
上述四种不平衡条件情况,可能出现在底部,也可能出现在头,也可能出现在某个中间节点导致不平衡, 而我们只需要研究其首次不平衡点,解决之后整棵树即继续平衡,在具体的处理上我们使用递归的方式解决问题。
四种不平衡情况处理 针对四种不平衡的情况,这里对每种情况进行详细的讲解。
rr平衡旋转(左单旋转) 这里的rr指的是节点模型的样子,其含义是需要左单旋转(记忆时候需要注意一下rr不是右旋转)!
出现这种情况的原因是节点的右侧的右侧较深这时候不平衡节点需要左旋,再细看过程。
在左旋的过程中,root(oldroot)节点下沉,中间节点(newroot)上浮.而其中中间节点(newroot)的右侧依然不变。
它上浮左侧所以需要指向根节点(oldroot)(毕竟一棵树)。但是这样newroot原来左侧节点h空缺。而我们需要仍然让整个树完整并且满足二叉排序树的规则。
而刚好本来oldroot右侧指向newroot现在结构改变oldroot右侧空缺,刚好这个位置满足在oldroot的右侧,在newroot的左侧,所以我们将h插入在这个位置。
其中h可能为null,不过不影响操作!
其更详细流程为:
而左旋的代码可以表示为:
private node getrrbanlance(node oldroot) {//右右深,需要左旋    // todo auto-generated method stub    node newroot=oldroot.right;    oldroot.right=newroot.left;    newroot.left=oldroot;    oldroot.height=math.max(getheight(oldroot.left),getheight(oldroot.right))+1;    newroot.height=math.max(getheight(newroot.left),getheight(newroot.right))+1;//原来的root的高度需要从新计算    return newroot;     } ll平衡旋转(右单旋转) 而右旋和左旋相反,但是思路相同,根据上述进行替换即可!
代码:
private node getllbanlance(node oldroot) {//ll小,需要右旋转    // todo auto-generated method stub    node newroot=oldroot.left;    oldroot.left=newroot.right;    newroot.right=oldroot;    oldroot.height=math.max(getheight(oldroot.left),getheight(oldroot.right))+1;    newroot.height=math.max(getheight(newroot.left),getheight(newroot.right))+1;//原来的root的高度需要从新金酸      return newroot; } rl平衡旋转(先右后左双旋转) 这个rl你可能有点懵圈,为啥rr叫左旋,ll叫右旋,这个rl怎么就叫先右后左旋转了?
别急别急,这个之所以先后后左,是因为具体需要中间节点右旋一次,然后上面节点左旋一次才能平衡,具体可以下面慢慢看。
首先产生这种不平衡的条件原因是:root节点右侧左侧节点的深度高些,使得与左侧的差大于1,这个与我们前面看到的左旋右旋不同因为旋转一次无法达到平衡!
对于右左结构,中间(r)的最大,两侧(root,r.l)的最小,但是下面(r.l)的比上面(root)大(r.l在root右侧)所以如果平衡的话,那么r.l应该在中间,而r应该在右侧,原来的root在左侧。
这个过程节点的变化浮动比较大,需要妥善处理各个子节点的移动使其满足二叉排序树的性质!
这种双旋转具体实现其实也不难,不要被外表唬住,这里面双旋转我提供两种解答方法。
思路(标准答案)1:两次旋转rr,ll
这个处理起来非常容易,因为前面已经解决rr(左旋),ll(右旋)的问题,所以这里面在上面基础上可以直接解决,首先对r节点进行一次ll右旋,旋转一次之后r在最右侧,这就转化成rr不平衡旋转的问题了,所以这个时候以root开始一次rr左旋即可完成平衡,具体流程可以参考下面这张图。
思路(个人方法)2:直接分析
根据初始和结果的状态,然后分析各个节点变化顺序=,手动操作这些节点即可。其实不管你怎么操作,只要能满足最后结构一致就行啦!
首先根据root,r,r.l三个节点变化,r.l肯定要在最顶层,左右分别指向root和r,那么这其中r.left,root.right发生变化(原来分别是r.l和r)暂时为空。而刚好根据左右大小关系可以补上r.l原来的孩子节点a,b。
代码为:(注释部分为方案1)
private node getrlbanlance(node oldroot) {//右左深    //        node newroot=oldroot.right.left;//        oldroot.right.left=newroot.right;//        newroot.right=oldroot.right;//        oldroot.right=newroot.left; //        newroot.left=oldroot;//        oldroot.height=math.max(getheight(oldroot.left),getheight(oldroot.right))+1;//        newroot.right.height=math.max(getheight(newroot.right.left),getheight(newroot.right.right))+1;//        newroot.height=math.max(getheight(oldroot.left),getheight(newroot.right))+1;//原来的root的高度需要从新金酸      oldroot.right =getllbanlance(oldroot.right);    oldroot.height=math.max(getheight(oldroot.left), getheight(oldroot.right))+1;    return getrrbanlance(oldroot);    } lr平衡旋转(先左后右双旋转) 这个情况和rl情况相似,采取相同操作即可。
根据上述rl修改即可
这部分代码为  
private node getlrbanlance(node oldroot) {    oldroot.left =getrrbanlance(oldroot.left);    oldroot.height=math.max(getheight(oldroot.left), getheight(oldroot.right))+1;    return getllbanlance(oldroot);    } 代码实现 首先对于节点多个height属性。用于计算高度(平衡因子)
插入是递归插入,递归是一个来回的过程,去的过程进行插入,回的过程进行高度更新,和检查是否平衡。推荐不要写全局递归计算高度,效率太低下,事实上高度变化只和插入和平衡有关,仔细考虑即不会有疏漏!
代码写的比较早,如有命名不规范的情况,还请勿喷,如果有疏漏还请指出!
import java.util.arraydeque;import java.util.queue;public class avltree {    class node    {        int value;        node left;        node right;        int height;        public node() {        }        public node(int value)        {            this.value=value;            this.height=0;        }        public node(int value,node left,node right)        {            this.value=value;            this.left=left;this.right=right;            this.height=0;        }    }    node root;// 根    public avltree() {        this.root = null;    }    public boolean iscontains(int x)// 是否存在    {        node current = root;        if (root == null) {            return false;        }        while (current.value != x && current != null) {            if (x  current.value) {                current = current.right;            }            if (current == null) {                return false;            } // 在里面判断如果超直接返回        }        // 如果在这个位置判断是否为空会导致current.value不存在报错        if (current.value == x) {            return true;        }        return false;    }    public int getheight(node t)    {        if(t==null) {return -1;}//        return t.height;        //return 1+math.max(getheight(t.left), getheight(t.right));这种效率太低    }    public void cengxu(node t) {//层序遍历        queue q1 = new arraydeque();        if (t == null)            return;        if (t != null) {            q1.add(t);        }        while (!q1.isempty()) {            node t1 = q1.poll();            if (t1.left != null)                q1.add(t1.left);            if (t1.right != null)                q1.add(t1.right);            system.out.print(t1.value +  );        }        system.out.println();    }    public void zhongxu(node t)//中序遍历 中序遍历:左子树---> 根结点 ---> 右子树    {//为了测试改成中后都行        if(t!=null)        {            zhongxu(t.left);            system.out.print(t.value+ );//访问完左节点访问当前节点            zhongxu(t.right);            //system.out.print(t.value+ );//访问完左节点访问当前节点        }    }    public void qianxu(node t)//前序递归 前序遍历:根结点 ---> 左子树 ---> 右子树    {        if(t!=null) {            system.out.print(t.value+ );//当前节点            qianxu(t.left );            qianxu(t.right);}    }    public void insert(int value) {        root=insert(value, root);    }    public node insert(int x,node t)//插入   t是root的引用    {        node a1=new node(x);        //if(root==null) {root=a1;return root;}                if(t==null)    {return a1;}        //插入操作。递归实现        else if(t!=null)        {            if(x   newroot     *       /                                  /         *      b     newroot(平衡因子为1)        oldroot   d     *             /                          /            *            c      d                    b   c      e     *                         *                     e     */    private node getrrbanlance(node oldroot) {//右右深,需要左旋        // todo auto-generated method stub        node newroot=oldroot.right;        oldroot.right=newroot.left;        newroot.left=oldroot;        oldroot.height=math.max(getheight(oldroot.left),getheight(oldroot.right))+1;        newroot.height=math.max(getheight(newroot.left),getheight(newroot.right))+1;//原来的root的高度需要从新计算        return newroot;    }    /*     * 右旋同理     */    private node getllbanlance(node oldroot) {//ll小,需要右旋转        // todo auto-generated method stub        node newroot=oldroot.left;        oldroot.left=newroot.right;        newroot.right=oldroot;        oldroot.height=math.max(getheight(oldroot.left),getheight(oldroot.right))+1;        newroot.height=math.max(getheight(newroot.left),getheight(newroot.right))+1;//原来的root的高度需要从新金酸            return newroot;    }    private node getlrbanlance(node oldroot) {        oldroot.left =getrrbanlance(oldroot.left);        oldroot.height=math.max(getheight(oldroot.left), getheight(oldroot.right))+1;        return getllbanlance(oldroot);    }    /*          (不平衡出现在右左节点)     *         oldroot       ==>          newroot     *        /                         /            *       a          b             oldroot     b     *                /              /         /       *           newroot   d         a      e    f   d     *            /        *           e     f     */    private node getrlbanlance(node oldroot) {//右左深    //        node newroot=oldroot.right.left;//        oldroot.right.left=newroot.right;//        newroot.right=oldroot.right;//        oldroot.right=newroot.left; //        newroot.left=oldroot;//        oldroot.height=math.max(getheight(oldroot.left),getheight(oldroot.right))+1;//        newroot.right.height=math.max(getheight(newroot.right.left),getheight(newroot.right.right))+1;//        newroot.height=math.max(getheight(oldroot.left),getheight(newroot.right))+1;//原来的root的高度需要从新金酸          oldroot.right =getllbanlance(oldroot.right);        oldroot.height=math.max(getheight(oldroot.left), getheight(oldroot.right))+1;        return getrrbanlance(oldroot);    }} 测试情况:
avl的理解需要时间,当然笔者的avl自己写的可能有些疏漏,如果有问题还请各位一起探讨!
当然,除了插入,avl还有删除等其他操作,(原理相似。删除后平衡)有兴趣可以一起研究。

定华雷达知识讲堂:浅析两种类型雷达物位计
以太坊2.0发布最终规范是怎样的
焊接接头形式分类
一款可以测定面粉的物理特性包含面团的拉伸阻力
关于步进电机减速机丢步和失步的分析
二叉排序树AVL如何实现动态平衡
聊聊在手机上开启快速swap的可能性
2012工业与汽车应用开发者论坛压轴大戏11月1日上演
PDP等离子电视维修经验
2020山东科学技术奖颁奖啦!海信7个项目获奖
带平均电压反馈的张弛振荡器详解
中国软件定义计算软件市场到2023年市场规模将达到20.3亿美元
文晔科技以38亿美元收购富昌电子
哪些情况下不得使用等离子抛光机?
爱立信已与韩国三家移动运营商全部签订了5G RAN商用合同
新品速递 | EMC对策产品:TDK公司推出业内首款用于汽车以太网10BASE-T1S的共模滤波器
中国新能源汽车市场销量影响到了全球走势
首款搭载前置升降双摄的vivo V17 Pro曝光,将于9月20日在印度首发
喜报|启英泰伦连续四年上榜 “成都硬科技扑克牌榜单”
谈谈S7-1200/1500系列PLC优化块访问的功能