Balanced binary tree insertion and deletion

Mainly from: Data Structures and Algorithms java language to describe
Who should read: If you already have some understanding of the concept of balanced binary tree, and have some understanding of the inserted logic, this article provides an incomplete code.
Reading Time: 10 minutes

Balance factor

Definition: The height (depth) of the left subtree of a node with a right subtree is the difference between the balance factor of the node (BF, Balance Factor), balanced binary tree does not exist in the node balancing factor is greater than 1. In a balanced binary tree, the balance factor nodes can be 0, 1 or -1, corresponding to the left and right subtrees contour, the left subtree is relatively high, is relatively high in the right subtree.

AVL tree definitions

Balanced binary search tree: Acronym balanced binary tree, AVL tree is a binary search tree
that has the following properties:
1. Can be empty tree.
2. If the tree is not empty, any left subtree of a node with a right subtree are balanced binary tree, and the absolute value of the difference between the height of not more than 1. | BF | <= 1

Node represents

public class AVLNode<T> {
    int height;
    AVLNode left;
    AVLNode right;
    T val;

    public AVLNode(T val AVLNode left, AVLNode right) {
        this.left = left;
        this.right = right;
        this.val = val;
    }
    public AVLNode(int val){
       this(T val,null,null);
    }  
}

The definition of a binary search tree

Insert

priate AVLNode<T> insert(T x ,AVLNode<T> t){
    if(t == null) return new AVLNode(x , null ,null);
    
    int cmp = x.compareTo(t.val);
    if(cmp < 0)
        t.left = insert(x, t.left);
    else if(cmp>0)
        t.right = insert(x , t.right);
    else
        ;
   return balance(t);
}

private AVLNode<T> balance( AVLNode<T> t){
    if(t == null) return t;
    if(height(t.left) - height(t.right) > 1){
        if(height(t.left.left) >= height(t.left.right)){
            t = rotateWithLeftChild( t );
        }else{
            t = doubleWithLeftChild( t );
        }
    }else{
        if(height(t.right.right) >= height(t.right.left)){
            t = rotateWithRightChild( t );
        }else{
            t = doubleWithRightChild( t );
        }
    }
    t.height = Max.max(height(t.left),height(t.right)) + 1;
    return t;
}

private AVLNode<T> rotateWithLeftChild(AVLNode<T> k2){
    AVLNode<T> k1 = k2.left;
    k2.left = k1.left;
    k1.right = k2;
    k2.height = Math.max(height(k2.left) , height(k2.right) ) + 1;
    k1.height = Math.max(height(k1.left) , height(k1.right) ) + 1;
}
private AVLNode<T> doubleWithLeftChild(AVLNode<T> k3){
    k3.left = rotateWithRightChild(k3.left);
    return rotateWithLeftChild(k3);
}

Deletion

private AVLNode<T> remove(T x , ABLNode<T> t){
    if(t == null) return t;
    in cmp = x.compareTo(t.val);
    if(cmp < 0)
        t.left = remove(x , t.left);
    else if(cmp > 0){
        t.right = remove(x , t.right);
    else if(t.left != null && t.right != null)
        t.val = findMin(t.right).val;
       t.right = remove(t.val , t.right);
    } 
    else 
        t =(t.left != null)? t.left : t.right;
    return balance(t);
}

Guess you like

Origin www.cnblogs.com/0ffff/p/11113833.html