Advanced programmers lessons - Architect of the road (9) - balanced binary tree (AVL trees)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m0_37609579/article/details/99690222

First, the definition of balanced binary tree

Of a search tree (search tree) query / add / delete action, h is proportional to the time spent height of the tree, the tree is not the capacity is proportional to n. If you can make a tree maintain chunky good shape, so that h is maintained at about O (lg n), completion of the work it is to save time. We have been able to maintain a good figure, not because of the new and long crooked delete the search tree, called a balanced search tree (balanced tree).

Baidu Encyclopedia balanced binary tree [] (Balanced Binary Tree) having the following properties: it is an empty tree height difference or absolute value of its left and right subtrees of no more than 1, and the left and right sub-trees are a balanced binary tree. The method used to achieve a balanced binary tree has a red-black tree , AVL , scapegoat tree , Treap , splay trees and the like.

Usually balanced binary tree is an ordered tree, binary tree having all of the properties, which the same binary tree traversal and traversal operations. But because it imposes additional restrictions on the binary tree, so add, delete operations must ensure a balanced binary tree factor is maintained.

Second, the left and right hand tree is how to play?

Experience the action figure: reference from the easiest to understand black tree

to · |æè¿éåå¾çæè¿ °

L Code:

D-Code:

Third, the imbalance adjustment node

平衡二叉树中引入了一个概念:平衡二叉树节点的平衡因子,它指的是该节点的两个子树,即左子树和右子树的高度差,即用左子树的高度减去右子树的高度,如果该节点的某个子树不存在,则该子树的高度为0,如果高度差的绝对值超过1就要根据情况进行调整。

平衡的调整共有四种情况:分别为LL,LR,RR,RL。

下面我们通过不断插入数据来说明几种不同的旋转方式:

注意:橘黄色的结点为旋转中心,黑色结点的为离插入结点最近的失衡结点。

(1)LR型

简单说明:最开始插入数据16,3,7后的结构如上图所示,结点16失去了平衡,3为16的左孩子,7为失衡结点的左孩子的右孩子,所以为LR型,接下来通过两次旋转操作复衡,先通过以3为旋转中心,进行左旋转,结果如图所示,然后再以7为旋转中心进行右旋转,旋转后恢复平衡了。

(2)LL型

简单说明:在上面恢复平衡后我们再次插入数据11和9,发现又失去平衡了,这次失衡结点是16,11是其左孩子,9为其失衡结点的左孩子的左孩子,所以是LL型,以失衡结点的左孩子为旋转中心进行一次右旋转即可。

(3)RR型

简单说明:进一步插入数据26后又再次失衡了,失衡结点为7,很明显这是RR型,以失衡结点的右孩子为旋转中心左旋转一次即可。

(4)RL型

再插入18后又再次失衡了,失衡结点为16,26为其右孩子,18为其右孩子的左孩子,为RL型,以失衡结点的右孩子为旋转中心,进行一次右旋转,然后再次已失衡结点的右孩子为旋转中心进行一次左旋转变恢复了平衡。

小总结:

这就是4种失衡后调整的旋转方式,其实只有两种,RR和LL,RL和LR本质上是一样的。下面我们再次插入数据14,15,完成我们最后数据的插入操作:

又是一次LR型,按前面操作就可以了。

四、应用场景

这个数据结构类似于双向链表,任意插入元素时都会自动排序,红黑树和平衡二叉树都使二叉树尽量平衡,从而使查询时和二分法类似。它适合的场合主要是:

  1. 需要时刻保证列表元素的有序排列;
  2. 需要频繁的增删和查询操作;
  3. Belonging to the bidirectional iterator, not random access to any element;

V. Summary

Personally I think that this chapter is quite difficult to understand, how to rotate the picture also requires a combination of feel for the imbalance.

The main advantage of balanced binary focused on fast lookup.


My micro-channel public number: architecture Scriptures (id: gentoo666), shared Java dry, high concurrency programming, popular technical tutorials, and distributed micro-services technology, architecture, design, block chain technology, artificial intelligence, big data, Java interview questions, as well as cutting-edge information and so popular. Updated daily Oh!

References:

  1. https://www.cnblogs.com/shixiangwan/p/7530015.html
  2. https://blog.csdn.net/wannuoge4766/article/details/83998377
  3. https://blog.csdn.net/qq_25940921/article/details/82183093
  4. https://blog.csdn.net/u014634338/article/details/42465089
  5. https://blog.csdn.net/sun_tttt/article/details/65445754
  6. https://blog.csdn.net/lemon_tree12138/article/details/50393548
  7. https://www.jianshu.com/p/4f3c8f134833
  8. https://blog.csdn.net/zxzxzx0119/article/details/80012812
  9. https://blog.csdn.net/buffoon1900/article/details/51330990

Guess you like

Origin www.cnblogs.com/anymk/p/11470512.html