Balanced binary tree (Java)

Package com.rao.linkList; 

/ ** 
 * @author Srao 
 * @className AvlTree 
 * @date 2019/12/3 21:23 
 * @package com.rao.linkList 
 * @Description balanced binary tree 
 * / 

/ ** 
 * Avl tree node definition 
 * / 
class AvlNode {
     int Data; 
    AvlNode lchild; // left child 
    AvlNode rchild; // right child 
    int height; // current height of the node is located 
} 

/ ** 
 * define a balanced binary tree 
 * / 
public  class AvlTree { 

    / ** 
     * 
     * @param  avlNode
     * @Return Returns the height of the current node resides
      * / 
    public  int height (AvlNode avlNode) {
         IF (avlNode == null ) {
             return -1 ; 
        } the else {
             return avlNode.height; 
        } 
    } 

    / ** 
     * Zuozuo type, for handed operation 
     * @param K2: non-balanced binary tree root to the point of rotation 
     * @return 
     * / 
    public AvlNode R_Rotate (AvlNode K2) { 
        AvlNode K1; 

        // rotate 
        K1 = k2.lchild; 
        k2.lchild= K1.rchild; 
        k1.rchild = K2; 

        // recalculate height, a height of only the node 0
         // highly original root 
        k2.height = Math.max (height (k2.lchild) , height (k2 .rchild)) +. 1 ;
         // new root heights 
        k1.height Math.max = (height (k1.rchild), height (k1.rchild)) +. 1 ; 

        // return a new root 
        return K1; 
    } 

    / ** 
     * Right Right type, the operation for L 
     * @param K2 
     * @return 
     * / 
    public AvlNode L_Rotate (AvlNode K2) { 
        AvlNode K1; 

        // rotate 
        K1 = k2.rchild;
        k2.rchild= K1.lchild; 
        k1.lchild = K2; 

        // recalculate height
         // original root height 
        k2.height Math.max = (height (k2.lchild), height (k2.rchild)) +. 1 ;
         // height of the new root 
        k1.height Math.max = (height (k1.rchild), height (k1.rchild)) +. 1 ; 

        // return a new root 
        return K1; 
    } 

    / ** 
     * around type to be left, and then right- 
     * @param K3 unbalanced tree root node 
     * @return 
     * / 
    public AvlNode L_R_Rotate (AvlNode K3) {
         // first conducted its left child left 
        k3.lchild =L_Rotate (k3.lchild); 

        // then the whole right-hand 
        return R_Rotate (K3); 
    } 

    / ** 
     * type right and left, for the first right-handed, then L 
     * @param K3 
     * @return 
     * / 
    public AvlNode R_L_Rotate (AvlNode K3) {
         // to be right-handed while the right child 
        k3.rchild = R_Rotate (k3.rchild); 
        
        // then the entire left 
        return L_Rotate (K3); 
    } 

    / ** 
     * number is inserted into a balanced binary tree 
     * @param data: the number to be inserted 
     * @param T: the root node of the binary tree 
     * @return returns binary Tree
      * / 
    publicINSERT AvlNode ( int Data, AvlNode T) {
         // If the root node of the binary tree has no, then the number into a new binary 
        IF (T == null ) { 
            T = new new AvlNode (); 
            t.data = Data; 
            t.lchild = null ; 
            t.rchild = null ; 
        } the else  IF (Data < t.data) {
             // left children recursively insert 
            t.lchild = iNSERT (Data, t.lchild); 
            
            // be adjusted, if left the child's height than the right child's height and large 2 
            IF (height (t.lchild) - == 2 height (t.rchild)) {
                 // Zuozuo type, dextrose 
                IF (Data < t.lchild.data) { 
                    T = R_Rotate (T); 
                } the else { // around type, first left, then right-handed 
                    T = L_R_Rotate (T); 
                } 
            } 
        } the else  IF (t.data < Data) {
             // right recursive inserted 
            t.rchild = iNSERT (Data, t.rchild); 
            
            // be adjusted, if right child's height than the left child of 2 large 
            IF ( height (t.rchild) - height (t.lchild) == 2 ) {
                 // right right type L 
                if(Data> t.rchild.data) { 
                    T = L_Rotate (T); 
                } the else {
                     // the right and left type, the first right-handed, left-handed and then 
                    T = R_L_Rotate (T); 
                } 
            } 
        } 
        
        // height recalculation of the tree 
        t. = Math.max height (height (t.lchild), height (t.rchild)) +. 1 ;
         return T; 
    } 

}

Just remember that every time the rotation is rotated from the root, not too difficult to understand

Guess you like

Origin www.cnblogs.com/rao11/p/11980069.html