当前位置:   article > 正文

平衡二叉树(AVL)插入结点后的再平衡思路_avl树再平衡的方法

avl树再平衡的方法

理解平衡二叉树

在解决平衡二叉树动平衡问题,我们先来明确什么是平衡二叉树:

平衡二叉树是二叉搜索树的一种特殊情况,所以在二叉搜索树的基础上加上了如下定义:

平衡因子:我们将二叉树中各个结点的左右子树的高度差称为该节点的平衡因子。

平衡二叉树:就是在二叉搜索树的基础上,所有结点的平衡因子都小于等于一。则称该树为一颗平衡二叉树。

当然平衡二叉树有很多实现方案,例如:AVL、红黑树等。这篇文章还是以最基础的AVL方式实现一个自平衡二叉树。

例如:

在上图中10节点左子树高度为2,10的右子树高度为1,同样的我们查看5号结点的平衡因子等于0,15号结点的平衡因子为0,4号结点.............所以这棵树为平衡二叉树。

规律:当我们插入节点时,我们可以发现这么一个规律,被破坏平衡的节点最近也是插入节点的爷爷结点

例:这棵树本来是一个平衡二叉树,如果我们插入一个值为6的结点,那么10号结点的平衡因子就会大于1,那么

这树就变得不平衡了。不平衡的节点为10,是6的爷爷结点。这是最极端的情况,不可能找到插入节点使得父结点不平衡的情况。(这句话自己好好体会)


插入节点的再平衡的解决思路

在AVL中采用节点的旋转使得树再平衡,二叉搜索树中节点的旋转不会破坏二叉搜索树的规则。

而平衡二叉树插入节点后导致平衡性被破坏,分为下面几种情况:(红色为不平衡节点,蓝色为刚刚插入的节点

左左型:

 

右右型:

右左型:

左右型:

上面四种情况的再平衡需要做不同的讨论:

 

左左型的再平衡:

对于左左型,我们需要以被破坏平衡的节点为中心顺时针旋转,这样既没有破坏二叉搜索树的规则还,使得树再次平衡了。

这里我们举个栗子:

我们给定一个平衡二叉树:

现在我们插入一个值为1的结点:

此时导致节点为10的结点不平衡,而此时3、5、10节点正好形成左左型不平衡。按照上面的思路,我们需要以被破坏平衡的结点的位置为中心,旋转元素。

此时我们需要注意6号结点,该节点原先在5号结点的右孩子,但是由于旋转后原先的父节点肯定是5好节点的右孩子,占了5号结点的位置,但是又与10号结点被旋转后左孩子为空,我们刚好可以把它加入10结点的左孩子上(符合二叉搜索树的左小右大原则)。这样二叉树再次平衡了。

同样的右右型原理与左左型相同,此处不再赘述。

 

左右型的再平衡:

   对于左右型,我们需要将破坏平衡结点的孩子节点和孙子节点进行逆时针旋转,这样就将问题又转换成了左左型。

此时我们只需要重复上面的左左型操作即可。

我们还是来举个栗子:

还是上面给出的那个平衡二叉树:

现在我们想要插入一个值为7的节点:

此时由于7号结点加入,10号几点变得不平衡了,而5、9、10节点也形成了“左右型”。此时我们只需要以10的孩子结点5的位置为中心,逆时针旋转。

此时问题转换成“左左型”的问题了(注意),那我们此时已10号结点为中心顺时针旋转即可解决这个问题

 

思路总结

在插入节点后我们需要以插入的节点为起点,向上寻找第一个不平衡节点,然后判断这个不平衡节点在不平衡路径上是哪种状态,然后根据不同的状态进行调整,如果是左左型或是右右型只需经过一次旋转即可,如果是左右型或者右左型需要先旋转一次将其转换成左左型或者右右型,然后再旋转一次即可使得二叉树平衡。

代码实现

二叉搜索树的实现:

  1. public class MyBinarySearchTree {
  2. public static class Node {
  3. public int value;
  4. public Node lchild;
  5. public Node rchild;
  6. public Node parent;
  7. public Node(int value) {
  8. this.value = value;
  9. }
  10. }
  11. protected Node root;
  12. private int size = 0;
  13. public void insert(int value) {
  14. Node insertNode = new Node(value);
  15. if (root == null) {
  16. root = insertNode;
  17. } else {
  18. Node node = root;
  19. while (true) {
  20. if (value < node.value) {
  21. if (node.lchild == null) {
  22. node.lchild = insertNode;
  23. insertNode.parent = node;
  24. break;
  25. }
  26. node = node.lchild;
  27. } else {
  28. if (node.rchild == null) {
  29. node.rchild = insertNode;
  30. insertNode.parent = node;
  31. break;
  32. }
  33. node = node.rchild;
  34. }
  35. }
  36. }
  37. size++;
  38. }
  39. /**
  40. * 中序遍历
  41. */
  42. public List<Integer> inorderTree() {
  43. List<Integer> list = new LinkedList<>();
  44. inorderTree(list, root);
  45. return list;
  46. }
  47. private void inorderTree(List<Integer> list, Node node) {
  48. if (node != null) {
  49. inorderTree(list, node.lchild);
  50. list.add(node.value);
  51. inorderTree(list, node.rchild);
  52. }
  53. }
  54. public Node getNode(int value) {
  55. Node node = root;
  56. Node result = null;
  57. while (node != null) {
  58. if (node.value == value) {
  59. result = node;
  60. break;
  61. } else {
  62. if (value < node.value) {
  63. node = node.lchild;
  64. } else {
  65. node = node.rchild;
  66. }
  67. }
  68. }
  69. return result;
  70. }
  71. public Integer min() {
  72. return min(root).value;
  73. }
  74. public Node min(Node node) {
  75. if (node != null) {
  76. while (node.lchild != null) {
  77. node = node.lchild;
  78. }
  79. return node;
  80. }
  81. return null;
  82. }
  83. public Integer max() {
  84. Node node = root;
  85. if (node != null) {
  86. while (node.rchild != null) {
  87. node = node.rchild;
  88. }
  89. return node.value;
  90. }
  91. return null;
  92. }
  93. /**
  94. * 删除节点
  95. *
  96. * @param value
  97. */
  98. public void remove(int value) {
  99. /**
  100. * 解题思路:删除节点要分为三种情况
  101. *
  102. * 情况一:删除叶子节点 好解决
  103. * 情况二:删除单分支节点 由后面的元素顶替要删除的元素即可
  104. * 情况三:删除双分支节点,这种情况是最不好想的
  105. * 这种情况需要找到左子树最大的节点或右子树最小的节点顶上去。
  106. */
  107. Node node = this.getNode(value);
  108. if (node != null) {
  109. //当节点为叶子节点
  110. if (node.lchild == null && node.rchild == null) {
  111. //要删除的节点为根节点
  112. if (node.parent == null) {
  113. root = null;
  114. }else{
  115. //判断当前节点是父节点的左孩子还是右孩子
  116. if (node.parent.rchild == node) {
  117. node.parent.rchild = null;
  118. } else {
  119. node.parent.lchild = null;
  120. }
  121. node.parent = null;
  122. }
  123. } else if (node.lchild == null || node.rchild == null) {
  124. //当该节点为单分支节点
  125. //找到在单分支情况下,唯一的那个孩子节点
  126. Node childen = null;
  127. if (node.lchild != null) {
  128. childen = node.lchild;
  129. node.lchild = null;
  130. } else {
  131. childen = node.rchild;
  132. node.rchild = null;
  133. }
  134. //如果要删除的节点为根节点
  135. if (node.parent == null) {
  136. root = childen;
  137. }else {
  138. childen.parent = node.parent;
  139. //同样的需要先判断当前节点是父节点的左孩子还是右孩子
  140. if (node.parent.rchild == node) {
  141. node.parent.rchild = childen;
  142. } else {
  143. node.parent.lchild = childen;
  144. }
  145. }
  146. } else {
  147. //双分支情况
  148. //获取右分支最小的节点
  149. Node minNode = min(node.rchild);
  150. //采用递归法删除最小的那个节点,为什么可以使用递归删除呢,因为前面获得的右分支中的最小节点,
  151. //一定是叶子节点,或者向右倾斜的单分支节点,所以采用递归删除即可(前面的步骤不就用于删除这两种情况的吗)。
  152. this.remove(minNode.value);
  153. //将已经被删除的“右子树最小值”的结点的值附到需要删除的值的节点上,这样就从表象上完成了对指定节点的删除
  154. node.value = minNode.value;
  155. }
  156. }
  157. }
  158. /**
  159. * 获取当前Node的后继元素,即比当前Node的值大,但最接近当前Node值的结点(或者说是获得中序遍历的下一个元素)
  160. * @param node
  161. * @return
  162. */
  163. public Node nextValue(Node node){
  164. /**
  165. * 解题思路:在通常情况下后继结点就是右子树中最大的元素,但是如果没有右子树那就复杂了,
  166. * 那后继结点就是依次遍历父结点,将自己作为左子树的第一个父结点就是我们找的后继结点。
  167. */
  168. if (node != null) {
  169. if (node.rchild != null) {
  170. return min(node.rchild);
  171. }else{
  172. while (node !=null && node.parent.rchild == node){
  173. node = node.parent;
  174. }
  175. return node.parent;
  176. }
  177. }
  178. return null;
  179. }
  180. public boolean isBalance() {
  181. return false;
  182. }
  183. public int size() {
  184. return size;
  185. }
  186. public int getHeight(Node node) {
  187. if(node != null){
  188. if(node.lchild == null && node.rchild == null){
  189. return 0;
  190. }else{
  191. return 1+Math.max(getHeight(node.lchild),getHeight(node.rchild));
  192. }
  193. }
  194. return -1;
  195. }
  196. public int getHeight() {
  197. return this.getHeight(root);
  198. }
  199. /**
  200. * 判断当前节点是否是父节点的右孩子,如果没有父节点则返回null
  201. * @param node
  202. * @return
  203. */
  204. public static Boolean isRchild(Node node){
  205. if(node != null && node.parent != null){
  206. if(node.parent.rchild == node){
  207. return true;
  208. }
  209. return false;
  210. }else{
  211. return null;
  212. }
  213. }
  214. /**
  215. * 判断当前节点是否是父节点的左孩子,如果没有父节点则返回null
  216. * @param node
  217. * @return
  218. */
  219. public static Boolean isLchild(Node node){
  220. if(node != null && node.parent != null){
  221. if(node.parent.lchild == node){
  222. return true;
  223. }
  224. return false;
  225. }else{
  226. return null;
  227. }
  228. }
  229. }

 

平衡二叉树的实现:(继承于二叉搜索树)

  1. public class MyBalanceTree extends MyBinarySearchTree {
  2. /**
  3. * 解题思路:平衡二叉树我们需要先插入节点,然后从插入节点往上找,找到第一个不平衡的节点,看这个不平衡的树是什么类型的
  4. * 左左型 右右型
  5. * 左右型 右左型
  6. * <p>
  7. * 我们将不平衡节点设为g 不平衡节点的孩子节点设置p p的孩子节点设为s
  8. *
  9. * @param value
  10. */
  11. @Override
  12. public void insert(int value) {
  13. //先插入节点,然后在调整树的平衡
  14. super.insert(value);
  15. //获得刚刚插入的节点
  16. Node node = this.getNode(value);
  17. Node[] unBalance = this.getFirstUnbalanceNode(node);
  18. if (unBalance != null) {
  19. this.reBalance(unBalance);
  20. }
  21. }
  22. /**
  23. * 根据插入节点向上找到第一个未平衡节点
  24. *
  25. * @param node
  26. * @return 返回的是一个数组,node[0]就是未平衡结点,node[1]是node[0]的孩子节点,node[2]是node[3]的孩子节点
  27. * 这样做是为了还原未平衡节点与插入节点之间的路径,方便后面的旋转
  28. */
  29. private Node[] getFirstUnbalanceNode(Node node) {
  30. Node g, p, s;
  31. if (node != null && node.parent != null && node.parent.parent != null) {
  32. s = node;
  33. p = s.parent;
  34. g = p.parent;
  35. if (Math.abs(this.getHeight(g.lchild) - this.getHeight(g.rchild)) > 1) {
  36. return new Node[]{g, p, s};
  37. } else {
  38. return getFirstUnbalanceNode(s.parent);
  39. }
  40. }
  41. return null;
  42. }
  43. private void reBalance(Node[] unBalance) {
  44. if (unBalance == null || unBalance.length != 3) {
  45. return;
  46. }
  47. Node g = unBalance[0];
  48. Node p = unBalance[1];
  49. Node s = unBalance[2];
  50. if (MyBinarySearchTree.isLchild(p) && MyBinarySearchTree.isLchild(s)) {//左左型
  51. this.rightRevolve(g, p);
  52. } else if (MyBinarySearchTree.isRchild(p) && MyBinarySearchTree.isRchild(s)) {//右右型
  53. this.leftResolve(g, p);
  54. } else if (MyBinarySearchTree.isLchild(p)) {//左右型
  55. this.leftResolve(p, s);
  56. this.rightRevolve(g, s);
  57. } else {//右左型
  58. this.rightRevolve(p, s);
  59. this.leftResolve(g, s);
  60. }
  61. }
  62. /**
  63. * 以center结点位置为中心右旋,旋转后,原先的child作为中心结点,原先的中心结点作为child右孩子
  64. *
  65. * @param center 旋转的中心节点
  66. * @param child 中心节点的左孩子
  67. */
  68. private void rightRevolve(Node center, Node child) {
  69. if (center == null) {
  70. return;
  71. }
  72. if (child == null) {
  73. return;
  74. }
  75. Node parent = center.parent;
  76. if (parent == null) {//证明此时旋转的中心结点是root
  77. root = child;
  78. child.parent = null;
  79. Node rchild = child.rchild;
  80. child.rchild = center;
  81. center.parent = child;
  82. center.lchild = rchild;
  83. if(rchild !=null){
  84. rchild.parent = center;
  85. }
  86. } else {
  87. if (MyBinarySearchTree.isRchild(center)) {//中心结点是父节点的右孩子
  88. parent.rchild = child;
  89. } else {
  90. parent.lchild = child;
  91. }
  92. child.parent = parent;
  93. Node rchild = child.rchild;//需要将旋转前child的右孩子放到旋转后center的左节点中
  94. child.rchild = center;
  95. center.parent = child;
  96. center.lchild = rchild;
  97. if(rchild !=null){
  98. rchild.parent = center;
  99. }
  100. }
  101. }
  102. /**
  103. * 以center结点位置为中心左旋,旋转后,原先的child作为中心结点,原先的中心结点作为child左孩子
  104. *
  105. * @param center 旋转的中心节点
  106. * @param child 中心节点的右孩子
  107. */
  108. private void leftResolve(Node center, Node child) {
  109. if (center == null) {
  110. return;
  111. }
  112. if (child == null) {
  113. return;
  114. }
  115. Node parent = center.parent;
  116. if (parent == null) {//证明此时旋转的中心结点是root
  117. root = child;
  118. child.parent = null;
  119. Node lchild = child.lchild;
  120. child.lchild = center;
  121. center.parent = child;
  122. center.rchild = lchild;
  123. if(lchild!=null) {
  124. lchild.parent = center;
  125. }
  126. } else {
  127. if (MyBinarySearchTree.isRchild(center)) {//中心结点是父节点的右孩子
  128. parent.rchild = child;
  129. } else {
  130. parent.lchild = child;
  131. }
  132. child.parent = parent;
  133. Node lchild = child.lchild;//需要将旋转前child的右孩子放到旋转后center的左节点中
  134. child.lchild = center;
  135. center.parent = child;
  136. center.rchild = lchild;
  137. if(lchild!=null) {
  138. lchild.parent = center;
  139. }
  140. }
  141. }
  142. }

各位大佬,原创不易呀,路过的点个赞!!!文章有错误欢迎大佬指正!!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号