AVL Tree ---- java, avl ---- 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<T> leftLeftRotation(AVLTreeNode<T> k2) { AVLTreeNode<T> 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.
private AVLTreeNode<T> rightRightRotation(AVLTreeNode<T> k1) { AVLTreeNode<T> 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<T> leftRightRotation(AVLTreeNode<T> 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<T> rightLeftRotation(AVLTreeNode<T> 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 <T extends Comparable <T> {private AVLTreeNode <T> mRoot; // root node // AVL Tree node (internal class) class AVLTreeNode <T extends Comparable <T> {T key; // key (key value) int height; // height AVLTreeNode <T> left; // left child AVLTreeNode <T> right; // right child public AVLTreeNode (T key, AVLTreeNode <T> left, AVLTreeNode <T> right) {this. key = key; this. left = left; this. right = right; this. height = 0 ;}// constructor public VLTree () {mRoot = null;}/** get tree height */private int height (AVLTreeNode <T> 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 <T> tree) {if (tree! = Null) {System. out. print (tree. key + ""); preOrder (tree. left); preOrder (tree. right) ;}} public void preOrder () {preOrder (mRoot);}/** traverse "AVL tree" */private void inOrder (AVLTreeNode <T> tree) {if (tree! = Null) {inOrder (tree. left); System. out. print (tree. key + ""); inOrder (tree. right) ;}} public void inOrder () {inOrder (mRoot);}/** sequential traversal of "AVL tree" */private void postOrder (AVLTreeNode <T> 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 <T> search (AVLTreeNode <T> 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 AVLTre ENode <T> search (T key) {return search (mRoot, key) ;}/ ** (non-Recursive Implementation) find the node whose key value is key in "AVL Tree x" */private AVLTreeNode <T> iterativeSearch (AVLTreeNode <T> 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 <T> iterativeSearch (T key) {return iterativeSearch (mRoot, key);}/** find the minimum node: returns the minimum AVL node of the root node. */Private AVLTreeNode <T> minimum (AVLTreeNode <T> tree) {if (tree = null) return null; while (tree. left! = Null) tree = tree. left; return tree;} public T minimum () {AVLTreeNode <T> 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 <T> maximum (AVLTreeNode <T> tree) {if (tree = null) return null; while (tree. right! = Null) tree = tree. right; return tree;} public T maximum () {AVLTreeNode <T> p = maximum (mRoot); if (p! = Null) return p. key; return null;}/** LL: returns the result of the left-hand rotation ). ** Return value: rotated root node */private AVLTreeNode <T> leftLeftRotation (AVLTreeNode <T> k2) {AVLTreeNode <T> 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: rotated root node */private AVLTreeNode <T> rightRightRotation (AVLTreeNode <T> k1) {AVLTreeNode <T> k2; k2 = k1.right; k1.right = k2.left; k2.left = k1; k1.height = max (height (k1.left), height (k1.right) + 1; k2.height = max (k2.right), k1.height) + 1; return k2 ;} /** LR: the condition of the left and right sides (left and double rotation ). ** Return value: rotated root node */private AVLTreeNode <T> leftRightRotation (AVLTreeNode <T> k3) {k3.left = rightRightRotation (k3.left); return leftLeftRotation (k3 );} /** RL: right-left (right-double rotation ). ** Return value: rotated root node */private AVLTreeNode <T> rightLeftRotation (AVLTreeNode <T> k1) {k1.right = leftLeftRotation (k1.right); return rightRightRotation (k1 );} /** Insert the node into the AVL tree and return the root node ** parameter description: * key value of the root node * key of the tree AVL tree * return value: * root node */private AVLTreeNode <T> insert (AVLTreeNode <T> tree, T key) {if (tree = null) {// create a node tree = new AVLTreeNode <T> (key, null, null); if (tree = null) {System. out. print Ln ("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 the root node ** parameter description: * The root node of the tree AVL tree ** the node to be deleted * return value: * root node */private AVLTreeNode <T> remove (AVLTreeNode <T> tree, AVLTreeNode <T> z) {// The root is empty or there are no nodes to delete, return null 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 <T> 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 <T> 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 <T> 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 <T> min = maximum (tree. right); tree. key = min. key; tree. right = remove (tree. right, min) ;}} else {AVLTreeNode <T> tmp = tree; tree = (tree. left! = Null )? Tree. left: tree. right; tmp = null;} return tree;} public void remove (T key) {AVLTreeNode <T> z; if (z = search (mRoot, key ))! = Null) mRoot = remove (mRoot, z);}/** destroy AVL tree */private void destroy (AVLTreeNode <T> 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 <T> 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
All java recursive algorithms of the AVL Tree
Www.doc88.com/p-292361550358.html
I have a JAVA base to attend the Java training of Haitong technology.
The reason for choosing huicai and Feifei education JAVA express training:
I. Reality: java training classes can teach a maximum of 25 people in each class, each with a dedicated computer and six days of study per week.
Ii. Practical: Professional and professional java training instructors with more than three years of first-line development experience. Relevant java training project exercises will be arranged at each stage of the JAVA training course, accumulated practical experience during java training and learning.
Iii. Benefits: the java training fee is quite favorable. An android training course is offered to the JAVA class. JAVA is the most cost-effective JAVA training institution.