Data structure study notes - tree search in search algorithm (balanced binary tree)

1. Definition of balanced binary tree

The balanced binary tree is based on the binary sorting tree. If it is in the binary sorting tree 左、右子树的高度之差的绝对值不超过1, it is called a balanced binary tree (AVL tree). Its left and right subtrees are also a balanced binary tree, and its average search length is O(log 2 n ),as follows:
Insert image description here

  • A balanced binary tree (AVL) with m levels has at most 2 m -1 nodes. According to the Fibonacci sequence, it can be obtained that at least f(m) = f(m-1) + f( m-2) + 1 node, where f(1) = 1, f(2) = 2, f(3) = 4.

For example, the number of nodes of a balanced binary tree with 5 levels of nodes is at least f(5) = f(5-1) + f(5-2) + 1= f(4) + f(3) + 1= [f(3) + f(2) + 1]+[f(2) + f(1) + 1]+1=4+2+1+2+1+1+1=12 nodes.

2. Balance factor

In a binary tree 左子树的深度减去其右子树深度, the node is called a node 平衡因子. The balance factors of a node in a balanced binary tree can only be 1, -1or 0three, and 叶子结点the balance factors are all 0. For example, the following binary tree is a balanced binary tree:

The balance factors of leaf nodes 0, 1, and 6 are all 0,
the depth of the left subtree of node 4 is 1, and the depth of the right subtree is 0, so the balance factor is 1-0=1, and the
left subtree of node 9 The depth is 1, the depth of the right subtree is 0, so the balance factor is 1-0=1, the
depth of the left subtree of node 2 is 1, and the depth of the right subtree is 2, so the balance factor is 1-2=-1,
The depth of the left subtree of node 5 is 3 and the depth of the right subtree is 2, so the balance factor is 3-2=1.
Insert image description here
However, the binary tree in the figure below is an unbalanced binary tree:
Insert image description here
the balance factors of leaf nodes 0, 1, 4, and 9 are all 0,
the depth of the left subtree of node 3 is 1, and the depth of the right subtree is 1, so the balance factor is 1-1=0,
the depth of the left subtree of node 2 is 1, and the depth of the right subtree is 2, so the balance factor is 1-2=-1, the depth
of the left subtree of node 5 is 3, and the depth of the right subtree is 3 is 1, so the balance factor is 3-1=2, which does not meet the requirements of a balanced binary tree.
Insert image description here

3. Insertion and construction of balanced binary tree

The nature of the insertion operation of a balanced binary tree is the same as that of a binary sorted tree. The insertion operation must be performed under certain conditions. If the conditions are not met, adjustments need to be made. If an imbalance occurs when an element is inserted into a balanced binary tree, the positional relationship of the elements needs to be adjusted to achieve balance. The principle followed by the adjustment is that the object of each adjustment is the balance factor closest to the inserted node 最小不平衡子树. The node whose absolute value is greater than 1 serves as the subtree of the root.
There are four situations in which inserting new nodes leads to imbalance:

category operate
LL type rotation (right single rotation) Inserting a new node into the left subtree of the node's left subtree causes imbalance
LR type rotation (first left and then right rotation) Inserting a new node into the right subtree of the node's left subtree causes imbalance
RR type rotation (left single rotation) Inserting a new node into the right subtree of the node's right subtree causes imbalance
RL type rotation (first right and then left rotation) Inserting a new node into the left subtree of the node's right subtree causes imbalance

(1) LL type rotation

左子树的左子树If inserting a new node into a node causes the balanced binary tree to become unbalanced, LL型旋转a right single rotation is performed. [Rotate clockwise to the right]
Insert image description here
After inserting the new node 6, the balanced binary tree is unbalanced. The insertion position is the left subtree of the left subtree of node 9, so an LL-type rotation is performed, as follows: The adjusted binary tree is a balanced binary
Insert image description here
tree .

(2) LR type rotation

This is done if inserting a new node into a node 左子树的右子树causes the balanced binary tree to become unbalanced LR型旋转. [First rotate left and then rotate right]
Insert image description here
After inserting new node 7, the balanced binary tree is unbalanced. The insertion position is the right subtree of the left subtree of node 9, so an LR-type rotation is performed, as follows: The adjusted binary tree is
Insert image description here
balanced Binary tree.

(3) RR type rotation

右子树的右子树If inserting a new node into a node causes the balanced binary tree to become unbalanced, RR型旋转a left-hand rotation is performed. [Rotate counterclockwise to the left]
Insert image description here
After inserting the new node 10, the balanced binary tree is unbalanced. The insertion position is the right subtree of the right subtree of node 7, so an RR-type rotation is performed, as follows: The adjusted binary tree is a balanced binary
Insert image description here
tree .

(4) RL type rotation

This is done if inserting a new node into a node 右子树的左子树causes the balanced binary tree to become unbalanced RL型旋转. [First rotate right and then rotate left]
Insert image description here
After inserting the new node 6, the balanced binary tree is unbalanced. The insertion position is the left subtree of the right subtree of node 5, so an RL-type rotation is performed, as follows: The adjusted binary tree is
Insert image description here
balanced Binary tree.

4. Deletion of balanced binary tree

(1) Leaf nodes

In a balanced binary tree, if the nodes to be deleted are only leaf nodes, they can be deleted directly without affecting the balanced binary tree.
For example, delete node 9 in the balanced binary tree. Since this node is a leaf node, it is deleted directly, as follows:
Insert image description here

(2) Only left/right subtree, no right/left subtree

In a balanced binary tree, the node to be deleted only has the left/right subtree and no right/left subtree. At this time, the node is deleted and the subsequent child nodes can be connected, so as not to affect the balanced binary tree.
Insert image description here

(3) There is both a left subtree and a right subtree

In a balanced binary tree, the node to be deleted has both a left subtree and a right subtree. At this time, you can follow the right (left) pointer of the left (right) subtree to the right (left) subtree. For the rightmost (left) node, replace [ 找到交换的结点] with this node and the node to be deleted. Then, it can be converted into the situation in (1) and (2). If replaced, the node If it is a leaf node, delete it according to (1). If it is not a leaf node, delete it according to (2).
For example, to delete node 11 in the balanced binary tree, first find the node to be exchanged, that is, node 9, so node 11 is exchanged with node 9, as follows: Then, since node 11 is a leaf node at this time,
Insert image description here
this When converted to the first situation, it can be deleted directly, as follows:
Insert image description here

5. Search and average search length of balanced binary trees

The search for a balanced binary tree is the same as a binary sorting tree. A balanced binary tree containing n nodes 最大深度is h=⌈log 2 (n+1)⌉, so it 平均查找长度is O(log 2 n). In addition, a balanced binary tree 最小深度is h= ⌈log 2 (n+1)⌉.

Guess you like

Origin blog.csdn.net/qq_43085848/article/details/133277930