[Data Structure] Binary Sorting Tree - Adjustment of Balanced Binary Tree


Reference video:
Teacher Lazy Cat - Data Structure - (59) Balanced Binary Tree [Interactive Video]

pre-concept

(1) What is a balanced binary tree

Balanced Binary Tree (Balanced Binary Tree) is a special binary search tree. Its purpose is to keep the height of the tree as balanced as possible to ensure that the time complexity of operations such as search, insertion, and deletion is O(log n).

Common balanced binary tree algorithms include AVL tree, red-black tree, splay tree, etc. These algorithms keep the tree balanced by recording the balance factor or color of each node and adjusting the tree's morphology by rotating or recoloring if necessary. Balanced binary trees are widely used to implement efficient data structures such as dictionaries, sets, etc.

(2) How to judge whether a tree is a balanced binary tree

There are many ways to judge whether a tree is a balanced binary tree, and the specific method depends on the specific balanced binary tree algorithm.
Taking the AVL tree as an example, the method to judge whether a tree is an AVL tree is:

  • Define the height of a node: For any node, its height is equal to the maximum height of its left and right subtrees plus 1.
  • Define the balance factor of a node: For any node, its balance factor is equal to the height of its left subtree minus the height of its right subtree.
  • Judging whether the tree is balanced: For any node, the absolute value of its balance factor is not greater than 1. If all nodes in the tree satisfy the absolute value of the balance factor not greater than 1, then the tree is an AVL tree.

For other balanced binary tree algorithms, such as red-black tree and splay tree, the judgment method is also different, but the basic idea is the same: by checking the relevant information (such as color or balance factor) of each node in the tree, to judge whether the tree is balance.

(3) Minimum unbalanced subtree

Minimal unbalanced subtree : In the process of constructing a balanced binary tree, the subtree rooted at the node 距离插入结点最近of and is .平衡因子的绝对值大于1

insert image description here

For example, in the figure above, here 4 is the root node of the smallest unbalanced subtree.

1. The basic idea of ​​constructing a balanced binary tree

Each time a node is inserted,

  • Calculate the balance factor of each node from the insertion node upwards. If the absolute value of the balance factor of a node is greater than 1, it means that the insertion operation has destroyed the balance of the binary sorting tree, and a balance adjustment is required; otherwise, continue to perform the insertion operation .
  • If the binary sorting tree is unbalanced, find out the root node of the smallest unbalanced subtree , and judge the adjustment type according to the relationship between the newly inserted node and the root node of the smallest unbalanced subtree .
  • Make corresponding adjustments according to the adjustment type, making it a new balanced subtree.

2. An example

insert image description here

3. The adjustment details of the balanced binary tree

Assuming node A is the root node of the least unbalanced subtree, there are the following four situations for the balance adjustment of the subtree:

  1. LL type
  2. RR type
  3. LR type
  4. RL type

(1) LL type (clockwise)

After inserting node X, the binary tree becomes unbalanced. The balance factor of node B becomes 1(h+1-h), and the balance factor of node A becomes 2(h+2-h). Here we call node X as the originator of the problem ; node A as the discoverer of the problem . From the problem discoverer node A to the problem generator, the left subtree of the left subtree (ie LL) must be passed .

Now that A finds that the binary tree is unbalanced, the binary tree needs to be adjusted.

Rotation: pole principle; Conflict: rotation priority

insert image description here

Using the pole principle, the left and right subtrees of node A are unbalanced: the left subtree is "heavy" and the right subtree is "light". Then we lift node B up, and node A pushes down (a clockwise rotation ), node A becomes the right subtree of node B, and the original right subtree of node B is adjusted to A The left subtree of the node (all nodes on the right subtree of node B must be smaller than node A, so it is most appropriate to adjust the original right subtree of B to the left subtree of node A).

example

insert image description here

insert image description here

(2) RR type (counterclockwise)

insert image description here

(3) LR type (first counterclockwise and then clockwise)

insert image description here

Understanding memory: Imagine that we are carrying a pole and find that the left side is heavy, but for the left side, the right side of the left side is heavier, so it is more complicated to adjust this LR type to a balanced binary tree. We first need to make a good adjustment to the left side, adjust it (rotate counterclockwise), adjust it into an LL shape, and let all the weight be completely and completely pressed to the left side. Then make a one-time adjustment to the right (rotate clockwise) for the obtained LL type.

example

insert image description here

(4) RL type (first clockwise and then counterclockwise)

insert image description here

(5) Summary of four adjustment types

insert image description here

4. Examples

insert image description here

problem solving process

insert image description here

  • Find the root node of the smallest unbalanced subtree: 5

  • Judgment type: From the person who discovered the problem to the person who caused the problem, first left and then right, the circled one is the RL type unbalanced tree.注意:下面对画圈的部分独立操作。

  • Rotate the unbalanced tree of type RL clockwise into type RR
    insert image description here

  • Insert node 9 and find that the binary tree is unbalanced again, find the root node of the smallest unbalanced subtree: 4

insert image description here

  • Judgment type: RR type unbalanced tree
  • Counterclockwise rotation of RR-type unbalanced tree

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/verse_armour/article/details/128975417