AVL Tree----Java

Source: Internet
Author: User
Tags comparable

AVL Tree----Java

AVL Tree is a highly balanced two-fork search tree

1. Single Rotation ll rotation


Understanding Memory:1. The left child of the left child of the unbalanced node is inserted caused by the imbalance, so called 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. The right child of the unbalanced node has an imbalance caused by child insertion, so 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. Dual rotation LR

Understanding Memory:1. The left child of the unbalanced node has an imbalance caused by the child, so called LR

2. The K1 RR is required first, 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. Unbalanced node right child's left child causes imbalance, so called RL

2. Need first to K3 LL, on the K1 RR

Private avltreenode<t> rightleftrotation (avltreenode<t> K1) {    k1.right = leftleftrotation (k1.right);    return rightrightrotation (k1);}
Example of 5.AVL

Traversal, lookup, and so on are not listed in the same way as a two-fork lookup tree, mostly insertions and deletions

public class Avltree<t extends comparable<t>> {private avltreenode<t> mroot;                root node//AVL tree nodes (inner Class) class Avltreenode<t extends comparable<t>> {T key;         Keyword (key value) int height;    Height avltreenode<t> left;    Left child avltreenode<t> right; Right child public Avltreenode (T key, avltreenode<t> left, avltreenode<t>) {This.key = key            ;            This.left = left;            This.right = right;        this.height = 0;    }}//Constructor public Avltree () {mroot = null; }/* Gets the height of the tree */private int height (avltreenode<t> tree) {if (tree! = null) return        Tree.height;    return 0;    } public int height () {return height (mroot);    }/* Compare the size of two values */private int max (int a, int b) {return a>b? a:b; }/* * Pre-order Traversal "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);            }/* * Middle sequence traversal "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);            }/* post-traverse "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 in "AVL tree X" with key value */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 avltreenode<t> Search (T key) {return search (mroot, key); }/* * (non-recursive implementation) find the node in "AVL tree X" with key value */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;    } return x;    } public avltreenode<t> Iterativesearch (T key) {return Iterativesearch (Mroot, key);     }/* Finds minimum nodes: Returns the minimum node of the AVL tree that is the root node of the tree.        */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;     }/* Finds maximum nodes: Returns the largest node of the AVL tree that is the root node of the tree.        */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: Left-to-left corresponding case (left single rotation). * * Return value: The root node after rotation */private avltreenode<t> leftleftrotation (avltreenode<t> K2) {avltreenode& Lt        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-to-right case (right single rotation). * * Return value: The root node after rotation */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;     }/* LR: The corresponding case (left double rotation). * * Return value: The root node after rotation */private avltreenode<t> leftrightrotation (avltreenode<t> k3) {k3.left = R        Ightrightrotation (K3.left);    Return leftleftrotation (K3);     }/* RL: Right-left corresponding case (right double rotation). * * Return value: The root node after rotation */private avltreenode<t> rightleftrotation (avltreenode<t> k1) {k1.right =        Leftleftrotation (K1.right);    return rightrightrotation (K1); }/* * Insert the node into the AVL tree and return to the root node * parameter description: * Root nodes of the tree AVL tree * Key value of the node to insert * return value: * Root section Dot */private avltreenode<t> Insert (avltreenode<t> tree, T key) {if (tree = = null) {//new node tree = new Avltreenode<t> (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) {//should insert key into "tree's left subtree" Case tree.left = insert (Tree.left, key);                When the node is inserted, the AVL tree is adjusted accordingly if it loses its balance.                        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) {//should insert key into "Tree's right subtree" case tree.right = insert (tree.right, key);                When the node is inserted, the AVL tree is adjusted accordingly if it loses its balance. 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 ("Add failed: Do not allow the same node to 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 the node to be deleted * Return value: * Root nodes */ Private avltreenode<t> Remove (avltreenode<t> tree, avltreenode<t> z) {///root is empty or no node to delete, return directly        Null.        if (Tree==null | | z==null) return NULL;        int cmp = Z.key.compareto (Tree.key);            if (CMP < 0) {//the node to be deleted is in the left subtree of tree Tree.left = Remove (Tree.left, z);            When the node is deleted, the AVL tree is adjusted accordingly if it loses its balance.             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 in the right subtree of tree Tree.right = Remove (Tree.right, z);            When the node is deleted, the AVL tree is adjusted accordingly if it loses its balance.                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 is the node that corresponds to the deletion. The left and right children of tree are not empty if ((tree.left!=null) && (tree.right!=null)) {if (tree.left) ;                    Height (tree.right)) {//If the tree's Supi right subtree is high;//Then (01) Find the largest node in the tree's left subtree (02) Assign the value of the maximum node to TreE.                    (03) Delete the maximum node.                    This is analogous to a "tree" with the "largest node of the tree's left subtree", and the advantage of this approach is that the AVL tree is still balanced after deleting the maximum node in the tree's left subtree.                    avltreenode<t> max = maximum (tree.left);                    Tree.key = Max.key;                Tree.left = Remove (Tree.left, max);                    } else {//If the tree's left subtree is not taller than the right subtree (i.e. they are equal, or the right subtree is 1 higher than the left subtree)//Then (01) Find the smallest node in the right subtree of the tree                    (02) Assign the value of the minimum node to the tree.                    (03) Delete the minimum node.                    This is analogous to a "tree" with the "smallest node in the right subtree of a tree", and the advantage of this approach is that the AVL tree is still balanced after you delete the smallest node in the tree's right subtree.                    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 lookup tree" * * Key--node key value * Direction--0, indicating that the node is the root node;     *-1, which indicates that the node is the left child of its parent junction;     * 1, which 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 (direct            ion==0)//tree is the root node System.out.printf ("%2d is root\n", Tree.key, key); else//tree is a branch node System.out.printf ("%2d is%2d ' s%6s 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); }}

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


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.