Algorithm Notes: Balanced Binary Tree

1 Introduction

  • An Balanced Binary Tree (AVL Tree) is a special type of Binary Search Tree (BST) that automatically ensures that the tree height is kept low for efficient performance of various basic operations such as add, delete, and lookup.
    • ——>The time is maintained at O(logN)
  • It is an empty tree, or the absolute value of the height difference between its left and right subtrees does not exceed 1, and both left and right subtrees are balanced binary trees.

  • Most operations of a balanced binary tree are similar to those of a binary search tree. The main difference is that the balance of a balanced binary tree may be changed during insertion and deletion.

2 insert

  • Call the node that needs to be rebalanced α (6 in the figure below)
  • Since any two nodes have at most two sons, when the height is unbalanced, the height difference between the two subtrees of the α node is 2.
  • It is easy to see that this imbalance may occur in the following four situations:
    • 1. Perform an insertion into the left subtree of the left son of α

      2. Perform an insertion into the right subtree of the left son of α

      3. Perform an insertion into the left subtree of the right son of α

      4. Perform an insertion into the right subtree of the right son of α

    • Cases 1 and 4 are mirror symmetries about α, and cases 2 and 3 are also mirror symmetries about α.
      • ——> So theoretically there are only two situations
        • Outside: Zuozuo (the length of the left subtree of the left child), right and right (the length of the right subtree of the right child)
        • Inside: left and right, right and left

2.1 Single rotation

2.1.1 Left rotation

The basic steps for left rotation are as follows:

  1. First create a new node, and set the value of this node to the value of the root node;
  2. The left pointer of the new node points to the left subtree of the root node;
  3. The right pointer of the new node points to the left subtree of the right child node of the root node;
  4. Set the value of the root node to the value of the root node's right child node;
  5. The left pointer of the root node points to the new node;
  6. The right pointer of the root node points to the right subtree of its right child node.

For example, we insert 8:

Obviously this is not a balanced binary tree, we perform left rotation adjustment

So we got a binary tree

2.1.2 Right rotation

The basic steps for right rotation are as follows:

  1. First create a new node, and set the value of the new node to the value of the root node;
  2. The right pointer of the new node points to the right subtree of the root node;
  3. The left pointer of the new node points to the right subtree of the left child node of the root node;
  4. Set the value of the root node to the value of its left child node;
  5. The left pointer of the root node points to the left subtree of its left child node;
  6. The right pointer of the root node points to the new node.

For example, we insert a 1

Obviously this is not a balanced binary tree, we perform right rotation adjustment

2.2 Double rotation

For left and right and right and left situations, a single rotation cannot solve the problem, and two rotations are required.

First, take K1 as the root and perform a left rotation;

Then take K3 as the root, do a right rotation

2.2.1 Judging whether double rotation is required

The core idea of ​​the double rotation code is: in the process of creating a binary tree, every time a new node is added, it is necessary to determine whether the binary tree needs to be rotated.

  • If the binary tree requires left rotation, first determine whether the height of the left subtree of the right subtree of the root node is greater than the height of the right subtree. If it is greater, first perform a right rotation on the right subtree of the root node, and finally perform a left rotation on the original binary tree;
  • If the binary tree requires right rotation, first determine whether the height of the right subtree of the left subtree of the root node is greater than the height of the left subtree. If it is greater, first perform a left rotation on the left subtree of the root node, and finally perform a right rotation on the original binary tree. .

The right subtree of the left subtree is larger - rotate the left subtree first, and then rotate the whole

The left subtree of the right subtree is larger - rotate the right subtree first, and then rotate the whole

3 Delete

Just like the insertion operation, deleting a node may also destroy the balance. This requires us to judge whether to make balance adjustments when deleting.

3.1 Delete the root node

  • If both the left and right subtrees are non-empty. Implement delete operations in subtrees with larger heights
    • If the height of the left subtree is greater than the height of the right subtree, assign the largest element in the left subtree to the current root node, and then delete the node with the largest element value in the left subtree.

    • If the height of the left subtree is less than the height of the right subtree, assign the smallest element in the right subtree to the current root node, and then delete the node with the smallest element value in the right subtree.

3.2 Rotate and adjust when deleting

Reference content: Detailed explanation of balanced binary trees - zhangbaochong - Blog Park (cnblogs.com)

Balanced Binary Tree (AVL Tree)_RonzL's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/qq_40206371/article/details/132667394