[Data Structure] What is the AVL tree

What is the AVL tree


Binary search tree is a limitation that is likely to degenerate into a linked list, in this case the efficiency of a binary search tree will become sharp decline in 0 (n). The AVL tree may be a good solution to this dilemma of BST. This blog will introduce the basic characteristics and related operations AVL tree.

Reference article from blog: Binary Tree - you may need to know the knowledge


1. What is the AVL tree

Height difference between any two of the largest sub-tree is 1, so the binary tree is called AVL tree.

First to determine several concepts:

Balance factor: The left subtree of a node binary tree height minus the value of the balance factor BF right subtree of the node is referred to as a height (Balance Factor).

Minimum unbalanced subtree: node nearest node is inserted from the absolute value, and the balance factor is greater than one subtree rooted.

After BF left node of the binary tree 45 = 1, the insertion node 43, the node 45 = BF 2.
Node 45 is inserted from the node point 43 within the scope of BF is not in the most recent [-1, 1], so the node 45 is the smallest subtree rooted subtree imbalance.

2. achieve node

public class AVLTreeNode<T extends Comparable> {

    // 存储的数据-用于排序
    T key;

    // 节点高度-用于计算父节点的BF
    int height;

    // 左儿子 & 右儿子
    AVLTreeNode<T> left;
    AVLTreeNode<T> right;
    
    public AVLTreeNode() {
    }

    public AVLTreeNode(T key, AVLTreeNode<T> left, AVLTreeNode<T> right) {
        this.key = key;
        this.left = left;
        this.right = right;
        this.height = 0;
    }

}

The definition of the node is relatively simple, previously defined with respect to a plurality of BF for calculating a height attribute of the parent node.

The definition of the tree

public class AVLTree<T extends Comparable> {
    // 定义树的根节点
    AVLTreeNode<T> root;

    public AVLTree() {
        root = null;
    }
}

Gets the height of the tree

    public int height() {
        return height(root);
    }

    private int height(AVLTreeNode<T> tree) {
        if (tree != null){
            return tree.height;
        }
        return 0;
    }

This article will define the height of the empty tree is zero, whichever is highly hierarchical tree, the root of the height of 1, and so on.

Adjust 3. AVL tree

If the insert or delete nodes in the AVL tree AVL tree may lead to loss of balance. This imbalance may occur in the following four situations:

  • Left subtree left a son once inserted. (LL)
  • To the right subtree of the left son of a once inserted. (LR)
  • On the left and right sub-tree of a son once inserted. (RL)
  • On the right subtree a right son once inserted. (RR)
    where 1,4 is the point on a mirror-symmetrical, 2 and 3 are mirror-symmetrical about a point.

The first case (4) a need for a single rotation of the tree to complete the adjustment.
The second case (2,3) need to complete the adjustment of the tree by a double rotation.

3.1 LL rotation

In the left into the left child subtree lead AVL tree imbalances, "the height of the right subtree of the root" "height left subtree rooted" larger than 2. For this case, we need to complete the single right rotation adjustments to the tree.

The figure on the left is a tree before rotation, the right is the tree after rotation. It can be found after the rotation, it is a tree AVL tree, and the only need to complete one rotation.
For LL rotation, you can understand as: LL rotation is about "out of balance AVL Root" performed, that is node 4; and because LL is the case, it will conduct a 4 node clockwise.

Code:

    /**
     * 进行一次单右旋转
     *
     * @param node 最小失衡树根节点
     */
    private AVLTreeNode<T> rightRotation(AVLTreeNode<T> node) {
        AVLTreeNode<T> left = node.left;
        node.left = left.right;
        left.right = node;
        // 更新高度
        node.height = Math.max(height(node.left), height(node.right)) + 1;
        left.height = Math.max(height(left.left), height(left.right)) + 1;
        return left;
    }

3.2 RR rotation

When the right sub-tree into the right child AVL lead to imbalances, we need to make a single left-adjusted. Rotating around the smallest imbalance sub-tree root node.

    /**
     * 进行一次单左旋转
     *
     * @param node 最小失衡树根节点
     */
    private AVLTreeNode<T> leftRotation(AVLTreeNode<T> node) {
        AVLTreeNode<T> right = node.right;
        node.right = right.left;
        right.left = node;

        // 更新高度
        node.height = Math.max(height(node.left), height(node.right)) + 1;
        right.height = Math.max(height(right.left), height(right.right)) + 1;

        return right;

    }

3.3 RL rotation

"Insert left the child in the right sub-tree AVL tree resulting in an imbalance," At this point we need to make adjustments after the first left-handed right-handed.


    /**
     * 先右旋后左旋
     *
     * @param node 失衡树根节点
     * @return 旋转后的根节点
     */
    private AVLTreeNode<T> rightLeftRotation(AVLTreeNode<T> node) {
        node.right = rightRoation(node.right);

        return leftRoation(node);

    }

3.4 LR rotation

"Insert right child of the left subtree lead AVL tree imbalances", then we need to be adjusted after the first left-handed right-handed.


    /**
     * 先左旋后右旋
     *
     * @param node 失衡树根节点
     * @return 旋转后的根节点
     */
    private AVLTreeNode<T> leftRightRotation(AVLTreeNode<T> node) {
        node.left = leftRoation(node.left);

        return rightLeftRoation(node);

    }


Guess you like

Origin www.cnblogs.com/54chensongxia/p/11575467.html