平衡二叉树AVL - 插入节点后旋转方法分析
平衡二叉树 AVL( 发明者为Adel'son-Vel'skii 和 Landis)是一种二叉排序树,其中每一个节点的左子树和右子树的高度差至多等于1。
首先我们知道,当插入一个节点,从此插入点到树根节点路径上的所有节点的平衡都可能被打破,如何解决这个问题呢?
这里不讲大多数书上提的什么平衡因子,什么最小不平衡子树,实际上让人(me)更加费解。实际上你首要做的就是先找到第一个出现不平衡的节点,也就是从插入点到root节点的路径上第一个出现不平衡的节点,即深度最深的那个节点A,对以它为根的子树做一次旋转或者两次旋转,此时这个节点的平衡问题解决了,整个往上路径经过的节点平衡问题也随之解决。
注:AVL 树也是一种二叉查找树,故删除策略可以参照前面文章来实现,只是删除节点后,如果平衡被打破,则也需要进行旋转以保持平衡。
After
deletion, retrace the path back up the tree (parent of the replacement)
to the root, adjusting the balance factors as needed.
下面先来分析下不平衡发生的四种情况:
1、An insertion into the left subtree of the left child of A; (LL)
2、An insertion into the right
subtree of the left child of A;(RL)
3、An
insertion into the left subtree of the
right child of A;(LR)
4、An
insertion into theright child of A;(RR)
旋转方法:
1、A 和 A's child 顺时针旋转 singlerotateLL()
4、A 和 A's child 逆时针旋转 singlerotateRR()
2、A's child 和 A's grandchild 逆时针旋转,A 和 A's new child 顺时针旋转 doublerotateRL()
3、A's child 和 A's grandchild 顺时针旋转,A 和 A's
new child 逆时针旋转 doublerotateLR()
可以看出1,4对称;2,3对称,实际在实现 rotate 函数的时候实现1和4 即可,2和3直接调用1&4的实现。在实现1&4时需要传递需要旋转的子树的root
node 作为参数,如 nodePtr doublerotateRL(A) { A->left = singlerotateRR(A->left); return singlerotateLL(A);}
当然,这样说还是对应不上,下面上图分析。
第一种情况举例:
现在想要插入的点是6,请看是否符合第一种情况的描述。8是不是深度最深的发生不平衡的点?6是不是插入在A的左孩子的左子树?符合是吧,那就直接按上述方法顺时针旋转7和8,效果是右图。当然这只是逻辑上的视图而已,真正函数实现也不难,就是修改两个指针指向,待会再谈。第4种情况是对称的,就不说了。
下面来看第三种情况示例:
现在要插入的点是14,请看是否符合第3种情况的描述。6是不是深度最深的发生不平衡的点?14是不是插入在A的右孩子的左子树?符合是吧,那就先顺时针旋转7和15,中间结果如下图所示:
现在7是A的new child了是吧,那就逆时针旋转6和7就可以了。
接着来分析singlerotateLL() 和doublerotateRL() 的实现,剩下两个函数就是对称的了。
首先是singlerotateLL(),看下图:
改动其实很简单,先把Y解绑当k2的左孩子,接着k2成为k1的右孩子,代码如下:
|
1
2 3 4 5 6 7 8 9 10 11 |
/* return pointer to the new root */
static AVLNodePtr singlerotateLL(AVLNodePtr k2) { AVLNodePtr k1 = k2->left; k2->left = k1->right; k1->right = k2; k2->height = Max(height(k2->left), height(k2->right)) + 1; k1->height = Max(height(k1->left), k2->height) + 1; return k1; } |
接着是doublerotateRL()的实现,看下图:
很明显B或者C的插入使得k3(A)不平衡了,那么首先应该是k1和k2逆时针旋转,所以调用
k3->left = singlerotateRR(k3->left);
接着是k3和new child 顺时针旋转,调用singlerotateLL(k3);
so easy, 代码如下:
|
1
2 3 4 5 |
static AVLNodePtr doublerotateRL(AVLNodePtr k3)
{ k3->left = singlerotateRR(k3->left); return singlerotateLL(k3); } |
完整的测试代码如下:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
#include <stdio.h>
#include <stdlib.h> struct AVLNode; struct AVLNode void makeempty(AVLNodePtr T) static int height(AVLNodePtr p) static int Max(int ln, int rn) /* return pointer to the new root */ static AVLNodePtr singlerotateRR(AVLNodePtr k1) static AVLNodePtr doublerotateRL(AVLNodePtr k3) static AVLNodePtr doublerotateLR(AVLNodePtr k3) AVLNodePtr insert(int X, AVLNodePtr T) else if (X < T->element) else if (X > T->element) void inorder(AVLNodePtr T) int main(void) inorder(T); |
代码将数组元素插入后,中序遍历后输出,即1~16的顺序排列。
注意:输入数组元素就不要搞成有序的了,如果代码中没有调整的实现,整个树就是个右斜树,但即使实现了调整,也会使得每插入一次就调整一次,何必内耗啊。很显然,平衡二叉树的优势在于不会出现普通二叉查找树的最差情况。其查找的时间复杂度为O(logN)。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
注:理解旋转函数的实现关键在于理解实现时的限制:
The
key to understanding how a rotation functions
is to understand its constraints. In particular the order of the leaves
of the tree (when read left to right for example) cannot change
(another way to think of it is that the order that the leaves would be
visited in a depth first search must be the same
after the operation as before ).
Another constraint is the main property of
a binary search tree, namely that the right child is greater than the parent and the left child is lesser than the parent.
Detailed illustration:
Using the terminology of Root for
the parent node of the subtrees to rotate, Pivot for the node which will become the new parent node,RS for rotation side upon to rotate and OS for opposite side of rotation. In the above diagram for the root
Q, the RS is C and the OS is P. The pseudo code for the rotation is:
参考:
《data structure and algorithm analysis in c》
《Data Structures》
平衡二叉树AVL - 插入节点后旋转方法分析的更多相关文章
- 平衡二叉树AVL插入
平衡二叉树(Balancedbinary tree)是由阿德尔森-维尔斯和兰迪斯(Adelson-Velskiiand Landis)于1962年首先提出的,所以又称为AVL树. 定义:平衡二叉树或为 ...
- JQ 添加节点和插入节点的方法总结
转载来源:http://blog.csdn.net/ss1106404013/article/details/49274345 添加节点的jQuery方法: append().prepend().ap ...
- K:平衡二叉树(AVL)
相关介绍: 二叉查找树的查找效率与二叉树的形状有关,对于按给定序列建立的二叉排序树,若其左.右子树均匀分布,则查找过程类似于有序表的二分查找,时间复杂度变为O(log2n).当若给定序列原来有序,则 ...
- 平衡二叉树,AVL树之图解篇
学习过了二叉查找树,想必大家有遇到一个问题.例如,将一个数组{1,2,3,4}依次插入树的时候,形成了图1的情况.有建立树与没建立树对于数据的增删查改已经没有了任何帮助,反而增添了维护的成本.而只有建 ...
- 图解:平衡二叉树,AVL树
学习过了二叉查找树,想必大家有遇到一个问题.例如,将一个数组{1,2,3,4}依次插入树的时候,形成了图1的情况.有建立树与没建立树对于数据的增删查改已经没有了任何帮助,反而增添了维护的成本.而只有建 ...
- 插入节点insertBefore()
http://www.imooc.com/code/1699 插入节点insertBefore() insertBefore() 方法可在已有的子节点前插入一个新的子节点. 语法: insertBef ...
- js进阶 11-9/10/11 jquery创建和插入节点
js进阶 11-9/10/11 jquery创建和插入节点 一.总结 一句话总结: 1.jquery插入节点8个方法? 内部之前,内部之后,之前,之后:各两个 append()和appendTo() ...
- 【算法】论平衡二叉树(AVL)的正确种植方法
参考资料 <算法(java)> — — Robert Sedgewick, Kevin Wayne <数据结构> ...
- AVL树的插入操作(旋转)图解
=================================================================== AVL树的概念 在说AVL树的概念之前,我们需要清楚 ...
随机推荐
- java.net.URI 简介 文档 API
URI 简介 文档地址:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh public final class java.net.URI extend ...
- 利用WebClient实现对Http协议的Post和Get对网站进行模拟登陆和浏览
我们在一些场合经常需要模拟浏览器进行一些操作,比如模拟投票,或者模拟点击,或者Web游戏外挂.而C#中封装好的WebClient可以在某些要求不算搞的场景实现Http的Post和Get.具体请见代码: ...
- 体绘制(Volume Rendering)概述之3:光线投射算法(Ray Casting)原理和注意要点(强烈推荐呀,讲的很好)
转自:http://blog.csdn.net/liu_lin_xm/article/details/4850609 摘抄“GPU Programming And Cg Language Primer ...
- Pinger2
import java.io.IOException;import java.io.InputStreamReader;import java.io.LineNumberReader;import j ...
- asp.net网站项目调用page,或者ashx页面不能用反射
public class TestHandler : System.Web.IHttpHandler { public bool IsReusable { get { return false; } ...
- 关于ASP.NET MVC中Form Authentication与Windows Authentication的简单理解
一般互联网应用,如人人网,微博,都是需要用户登录的,如果用户不登陆,就不能使用此网站.所以,这里都是用FormAuthentication,要求用户填写用户名与密码,然后登录成功后,FormAuthe ...
- [置顶] Zend Optimizer 和 Zend Debugger 同时安装
下载地址: Zend Optimizer: http://download.csdn.net/detail/wf120355/6479947 Zend Debugger: http://downlo ...
- Mysql(一)安装
一.下载 下载地址:http://www.mysql.com/downloads/ 二.安装 解压 双击安装 下一步,选择NO, 下一步,选择, 按需求选择,这时选择developer default ...
- PyQt5教程——菜单和工具栏(3)
PyQt5中的菜单和工具栏 在这部分的PyQt5教程中,我们将创建菜单和工具栏.菜单式位于菜单栏的一组命令操作.工具栏是应用窗体中由按钮和一些常规命令操作组成的组件. 主窗口 QMainWindow类 ...
- UVa 10642 - Can You Solve It?
题目:二维平面上的整数点.用路径链接起来(0,0)->(1.0)->(0.1)->(2,0)->.. 给你两点坐标.求两点间步长(在路径上的距离). 分析:简单题. 我们发现点 ...