[soft test] data structure - tree structure - balanced binary search tree (AVL tree)

1. What is an AVL tree?

The AVL tree is a binary search tree with self-balancing properties.
The name of the AVL tree comes from its inventors: GM Adelson-Velsky and EM Landis.

Although the AVL tree is a self-balancing binary search tree, it is not the only self-balancing binary search tree.
Other common self-balancing binary search trees include red-black trees, Splay trees, etc.

Second, the characteristics of the AVL tree

The AVL tree requires that the absolute value of the height difference between the left subtree and the right subtree of each node in the tree does not exceed 1.
In an AVL tree, the maximum difference in height between two subtrees of any node is 1, so it is also called a height-balanced tree.
Such a tree is often called a balanced tree because it guarantees a time complexity of O(log n) for the search operation.
insert image description here

2.1 Advantages of AVL tree

  • The AVL tree can ensure the balance of the tree, thus ensuring that the time complexity of the search operation is O(log n).
  • The self-balancing characteristics of the AVL tree make it have better performance in operations such as insertion and deletion, and the time complexity is O(log n).
  • The time complexity of insertion and deletion operations of an AVL tree is O(log n), where n is the number of nodes in the tree.
  • The AVL tree maintains a balance factor and rotates, and the balance of the AVL tree is achieved by maintaining a balance factor.
  • The AVL tree can maintain the balance of the tree in operations such as insertion and deletion, and has better performance.

2.2 Disadvantages of AVL tree

Since AVL trees are inserted and deleted (created and maintained),

  • Balance adjustment needed
  • Need more time and space
  • there will be a certain cost

Therefore, in practical applications, other balanced binary search trees, such as red-black trees, may be chosen.

Three, AVL tree insertion and deletion

3.1 AVL tree insertion operation

The insertion operation of the AVL tree is relatively simple.
The creation and insertion operations of an AVL tree are similar to ordinary binary search trees.
After inserting a new node in the AVL tree, the AVL tree may become unbalanced, and the AVL tree needs to rotate the tree to maintain balance.
The AVL tree can adjust the position of the nodes through four kinds of rotation operations to restore the balance of the AVL tree.

The rotation operation of the AVL tree includes four cases: right-left rotation, left-right rotation, left-left rotation, and right-right rotation.

At this point, starting from the root node, perform balance adjustment (rotation operation) according to the following steps:
(1) Single rotation:
If the balance factor of the current node (the height of the left subtree minus the height of the right subtree) is -1 or 1, perform Single rotation operation.
Specifically, if the left subtree of the current node is higher than the right subtree, perform a right-rotation operation; otherwise, perform a left-rotation operation.

(2) Double rotation:
If the balance factor of the current node is -2 or 2, perform double rotation.
Specifically, if the left subtree of the current node is 2 higher than the right subtree, perform a left-right rotation operation; otherwise, perform a right-left rotation operation.

3.2 AVL tree deletion operation

The deletion operation of the AVL tree is slightly more complicated.
The deletion operation of the AVL tree needs to be processed in three cases.
After deleting a node in an AVL tree, the tree may need to be rotated to maintain balance.

Case 1: The deleted node has no children:

Just delete the node directly.

Case 2: The deleted node has a child node:

Delete the node and connect its children to its parent.

Case 3: The deleted node has two children:

Find the node's predecessor node (that is, the maximum value in its left subtree) or successor node (that is, the minimum value in its right subtree), and replace the node to be deleted with this node. A single or double rotation is then performed on the replaced nodes to keep the tree balanced.

Also, if the deleted node is the root node of the tree, a new root node needs to be reselected.

Guess you like

Origin blog.csdn.net/wstever/article/details/129978049