AVL Tree ---- java

Source: Internet
Author: User

AVL Tree ---- java

AVL Tree ---- java

AVL Tree is a highly balanced binary search tree.

1. Single rotation LL Rotation


Understanding memory:1. The imbalance caused by insertion of the left child on the unbalanced node is LL.

private AVLTreeNode
 
   leftLeftRotation(AVLTreeNode
  
    k2) {    AVLTreeNode
   
     k1;    k1 = k2.left;    k2.left = k1.right;    k1.right = k2;    k2.height = max( height(k2.left), height(k2.right)) + 1;    k1.height = max( height(k1.left), k2.height) + 1;    return k1;}
   
  
 

2. Single-rotation RR


Understanding memory:1. unbalanced insertion of the right child of an unbalanced node is called RR. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kpha + PHByZSBjbGFzcz0 = "brush: java;"> private AVLTreeNode RightRightRotation (AVLTreeNode K1) {AVLTreeNode K2; k2 = k1.right; k1.right = k2.left; k2.left = k1; k1.height = max (height (k1.left), height (k1.right) + 1; k2.height = max (height (k2.right ), k1.height) + 1; return k2 ;} 3. Double rotation LR

Understanding memory:1. The left child of an unbalanced node has children which cause imbalance, so it is called LR.

2. You need to first pair k1 RR, then the root K3 LL

private AVLTreeNode
  
    leftRightRotation(AVLTreeNode
   
     k3) {    k3.left = rightRightRotation(k3.left);    return leftLeftRotation(k3);}
   
  
4. Double rotation RL


Understanding memory:1. The left child of the right child of an unbalanced node causes imbalance, so it is called RL.

2. You need to first pair k3 LL, in pair k1 RR

private AVLTreeNode
  
    rightLeftRotation(AVLTreeNode
   
     k1) {    k1.right = leftLeftRotation(k1.right);    return rightRightRotation(k1);}
   
  
5. AVL example

Traversal and search are not listed like binary search trees, mainlyInsertAndDelete

Public class AVLTree
  
   
> {Private AVLTreeNode
   
    
MRoot; // root node // AVL Tree node (internal class) class AVLTreeNode
    
     
> {T key; // key (key value) int height; // AVLTreeNode
     
      
Left; // left child AVLTreeNode
      
       
Right; // right child public AVLTreeNode (T key, AVLTreeNode
       
         Left, AVLTreeNode
        
          Right) {this. key = key; this. left = left; this. right = right; this. height = 0 ;}/// constructor public AVLTree () {mRoot = null;}/** get the height of the tree */private int height (AVLTreeNode
         
           Tree) {if (tree! = Null) return tree. height; return 0;} public int height () {return height (mRoot);}/** compare the two values */private int max (int a, int B) {return a> B? A: B;}/** preOrder traversal of "AVL Tree" */private void preOrder (AVLTreeNode
          
            Tree) {if (tree! = Null) {System. out. print (tree. key + ""); preOrder (tree. left); preOrder (tree. right) ;}} public void preOrder () {preOrder (mRoot);}/** traverse the "AVL Tree" */private void inOrder (AVLTreeNode
           
             Tree) {if (tree! = Null) {inOrder (tree. left); System. out. print (tree. key + ""); inOrder (tree. right) ;}} public void inOrder () {inOrder (mRoot);}/** traverse "AVL Tree" */private void postOrder (AVLTreeNode
            
              Tree) {if (tree! = Null) {postOrder (tree. left); postOrder (tree. right); System. out. print (tree. key + "") ;}} public void postOrder () {postOrder (mRoot);}/** (Recursive Implementation) find the node whose key value is key in "AVL Tree x" */private AVLTreeNode
             
               Search (AVLTreeNode
              
                X, T key) {if (x = null) return x; int cmp = key. compareTo (x. key); if (cmp <0) return search (x. left, key); else if (cmp> 0) return search (x. right, key); else return x;} public AVLTreeNode
               
                 Search (T key) {return search (mRoot, key) ;}/ ** (non-Recursive Implementation) search nodes with the key in "AVL Tree x" */private AVLTreeNode
                
                  IterativeSearch (AVLTreeNode
                 
                   X, T key) {while (x! = Null) {int cmp = key. compareTo (x. key); if (cmp <0) x = x. left; else if (cmp> 0) x = x. right; else return x;} public AVLTreeNode
                  
                    IterativeSearch (T key) {return iterativeSearch (mRoot, key);}/** find the minimum node: return the minimum node of the AVL tree whose tree is the root node. */Private AVLTreeNode
                   
                     Minimum (AVLTreeNode
                    
                      Tree) {if (tree = null) return null; while (tree. left! = Null) tree = tree. left; return tree;} public T minimum () {AVLTreeNode
                     
                       P = minimum (mRoot); if (p! = Null) return p. key; return null;}/** find the maximum node: return the maximum node of the AVL tree whose tree is the root node. */Private AVLTreeNode
                      
                        Maximum (AVLTreeNode
                       
                         Tree) {if (tree = null) return null; while (tree. right! = Null) tree = tree. right; return tree;} public T maximum () {AVLTreeNode
                        
                          P = maximum (mRoot); if (p! = Null) return p. key; return null;}/** LL: returns the result of the left-hand rotation ). ** Return value: the rotated root node */private AVLTreeNode
                         
                           LeftLeftRotation (AVLTreeNode
                          
                            K2) {AVLTreeNode
                           
                             K1; k1 = k2.left; k2.left = k1.right; k1.right = k2; k2.height = max (height (k2.left), height (k2.right) + 1; k1.height = max (height (k1.left ), k2.height) + 1; return k1;}/** RR: Right-right (right single rotation ). ** Return value: the rotated root node */private AVLTreeNode
                            
                              RightRightRotation (AVLTreeNode
                             
                               K1) {AVLTreeNode
                              
                                K2; k2 = k1.right; k1.right = k2.left; k2.left = k1; k1.height = max (height (k1.left), height (k1.right) + 1; k2.height = max (height (k2.right ), k1.height) + 1; return k2;}/** LR: the condition of the left and right (left rotation ). ** Return value: the rotated root node */private AVLTreeNode
                               
                                 LeftRightRotation (AVLTreeNode
                                
                                  K3) {k3.left = rightRightRotation (k3.left); return leftLeftRotation (k3) ;}/ ** RL: right-left (right-double rotation ). ** Return value: the rotated root node */private AVLTreeNode
                                 
                                   RightLeftRotation (AVLTreeNode
                                  
                                    K1) {k1.right = leftLeftRotation (k1.right); return rightRightRotation (k1);}/** Insert the node into the AVL Tree and return the parameter description of the root node: * tree AVL tree Root Node * key value of the key inserted node * return value: * root node */private AVLTreeNode
                                   
                                     Insert (AVLTreeNode
                                    
                                      Tree, T key) {if (tree = null) {// create a node tree = new AVLTreeNode
                                     
                                       (Key, null, null); if (tree = null) {System. out. println ("ERROR: create avltree node failed! "); Return null ;}} else {int cmp = key. compareTo (tree. key); if (cmp <0) {// the tree where the key should be inserted into the "Left subtree of the tree. left = insert (tree. left, key); // After the node is inserted, if the AVL Tree is out of balance, adjust it accordingly. If (height (tree. left)-height (tree. right) = 2) {if (key. compareTo (tree. left. key) <0) tree = leftLeftRotation (tree); else tree = leftRightRotation (tree) ;}} else if (cmp> 0) {// tree where the key should be inserted into the "right subtree of the tree. right = insert (tree. right, key); // After the node is inserted, if the AVL Tree is out of balance, adjust it accordingly. If (height (tree. right)-height (tree. left) = 2) {if (key. compareTo (tree. right. key)> 0) tree = rightRightRotation (tree); else tree = rightLeftRotation (tree) ;}} else {// cmp = 0 System. out. println ("failed to add: The same node cannot be added! ") ;}} Tree. height = max (height (tree. left), height (tree. right) + 1; return tree;} public void insert (T key) {mRoot = insert (mRoot, key);}/** delete node (z ), return root node ** parameter description: * root node of the tree AVL tree * z to be deleted * return value: * root node */private AVLTreeNode
                                      
                                        Remove (AVLTreeNode
                                       
                                         Tree, AVLTreeNode
                                        
                                          Z) {// if the root is null or no node is to be deleted, null is returned directly. If (tree = null | z = null) return null; int cmp = z. key. compareTo (tree. key); if (cmp <0) {// The node to be deleted is located in the left subtree of the tree. left = remove (tree. left, z); // If the AVL Tree is out of balance after the node is deleted, adjust it accordingly. If (height (tree. right)-height (tree. left) = 2) {AVLTreeNode
                                         
                                           R = tree. right; if (height (r. left)> height (r. right) tree = rightLeftRotation (tree); else tree = rightRightRotation (tree) ;}} else if (cmp> 0) {// The node to be deleted is located in the tree's right subtree. right = remove (tree. right, z); // If the AVL Tree is out of balance after the node is deleted, adjust it accordingly. If (height (tree. left)-height (tree. right) = 2) {AVLTreeNode
                                          
                                            L = tree. left; if (height (l. right)> height (l. left) tree = leftRightRotation (tree); else tree = leftLeftRotation (tree) ;}} else {// tree indicates the node to be deleted. // Left and right children of the tree are not empty if (tree. left! = Null) & (tree. right! = Null) {if (height (tree. left)> height (tree. right) {// If the left subtree of the tree is higher than the right subtree, // (01) Find the maximum node in the left subtree of the tree // (02) assign the value of the largest node to the tree. // (03) Delete the largest node. // This is similar to using "the largest node in the left subtree of the tree" as the "tree" proxy; // the advantage of using this method is: after "the largest node in the left subtree of the tree" is deleted, the AVL tree is still balanced. AVLTreeNode
                                           
                                             Max = maximum (tree. left); tree. key = max. key; tree. left = remove (tree. left, max);} else {// If the left subtree of the tree is no higher than the right subtree (that is, they are equal, or the right subtree is 1 higher than the left subtree) // (01) find the smallest node in the right subtree of the tree // (02) and assign the value of the smallest node to the tree. // (03) Delete the smallest node. // This is similar to using the "minimum node in the right subtree of the tree" as the "tree" proxy; // the advantage of using this method is: after the "minimum node in the right subtree of the tree" is deleted, the AVL tree is still balanced. AVLTreeNode
                                            
                                              Min = maximum (tree. right); tree. key = min. key; tree. right = remove (tree. right, min) ;}} else {AVLTreeNode
                                             
                                               Tmp = tree; tree = (tree. left! = Null )? Tree. left: tree. right; tmp = null;} return tree;} public void remove (T key) {AVLTreeNode
                                              
                                                Z; if (z = search (mRoot, key ))! = Null) mRoot = remove (mRoot, z);}/** destroy AVL Tree */private void destroy (AVLTreeNode
                                               
                                                 Tree) {if (tree = null) return; if (tree. left! = Null) destroy (tree. left); if (tree. right! = Null) destroy (tree. right); tree = null;} public void destroy () {destroy (mRoot );} /** print "Binary Search Tree" ** key -- key value of the node * direction -- 0, indicating that the node is the root node; *-1, indicates that the node is the left child of its parent node; * 1 indicates that the node is the right child of its parent node. */Private void print (AVLTreeNode
                                                
                                                  Tree, T key, int direction) {if (tree! = Null) {if (direction = 0) // tree is the root node System. out. printf ("% 2d is root \ n", tree. key, key); else // tree is the branch node System. out. printf ("% 2d is % 2d's % 6 s child \ n", tree. key, key, direction = 1? "Right": "left"); print (tree. left, tree. key,-1); print (tree. right, tree. key, 1) ;}} public void print () {if (mRoot! = Null) print (mRoot, mRoot. key, 0 );}}
                                                
                                               
                                              
                                             
                                            
                                           
                                          
                                         
                                        
                                       
                                      
                                     
                                    
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  

A lot of reference: http://www.cnblogs.com/skywang12345/p/3577479.html


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.