Java Creating an AVL tree

Source: Internet
Author: User
Tags comparable

The AVL tree is a two-fork search tree with equilibrium condition, the time complexity of finding and deleting is logn, and the improvement of the two-fork search tree, we call the difference of the Saozi right subtree depth of the node as the Balance factor (BF), in which the absolute value of the balance factor of each node is not greater than 1.

A subtree that is closest to the insertion node and that has an absolute balance factor greater than 1 is the root of the tree, called the smallest unbalanced subtree.

To implement the AVL tree, you must ensure that the unbalanced subtree is eliminated at insertion time, that is, in some way, each time a node is inserted into a balanced BST tree, let's talk about four situations when inserting:

1: One-time insertion of left child's left subtree;

2: Insert the right child tree of the left child once;

3: Insert the left subtree of the right child;

4: One-time insertion of right child tree;

Where Condition 1 and condition 4 are about mirror symmetry, condition 2 and condition 3 are about mirrored symmetry,

As a result of the picture, I went on a few bad pictures, of which x, Y, Z and a,b,c,d for the corresponding node of the child, I directly to four kinds of situations to explain:

Scenario 1: Suppose the node k1,k1 left child K2,

Single right Rotation: The left child K2 of the K1 node is inserted into the left sub-tree, so that the K1 Bf is 2, at this time the smallest unbalanced subtree is the K1 as the root node of the subtree, so the right rotation operation, so that the root of the new subtree of the original node of the left child namely K2,K2 the right child K1, K1 's left child is the K2 right child in the atomic tree ;


Scenario 2: Assumptions node K1,k1 left child k3,k3 right child K2,

Double left Rotation: The K2 is the root node of the sub-tree for the insertion operation, when the smallest unbalanced subtree is k1 as the root node of the subtree, but the simple left rotation does not solve the problem, which is different from the situation 1, the reason is that the K1 BF is 2, and the K3 BF 1, the two symbols inconsistent, Causes a rotation that cannot be performed only once, so it is divided into two rotations:

1: The subtree with K3 as the root node is left rotated first,

2: The subtree with K1 as the root node is rotated right,

Get the result shown in the figure, at this time K2 Zuozi B is K3 right subtree,K2 right subtree c is K1 left dial hand tree, K2 left child for k3,k2 right child K1;


Scenario 3:

Suppose the node K1,k1 right child K3,k3 's left child K2,

Double right Rotation: The K2 is the root node of the sub-tree to insert operations, the smallest imbalance subtree is the K1 as the root node of the subtree, but the simple left rotation does not solve the problem, which is different from the situation 1, the reason is that the K1 BF is-2, and K3 BF 1, the two symbols inconsistent, Causes a rotation that cannot be performed only once, so it is divided into two rotations:

1: First, the subtree with K3 as the root node is rotated right,

2: The subtree with K1 as the root node is rotated to the left,

Get the result shown in the picture, at this time K2 right subtree c is K3 left dial hand tree,K2 Zuozi B is K1 right subtree, K2 left child for k1,k2 right child K3;

Scenario 1: Suppose the node k1,k1 left child K2,

Single right Rotation: The left child K2 of the K1 node is inserted into the left sub-tree, so that the K1 Bf is 2, at this time the smallest unbalanced subtree is the K1 as the root node of the subtree, so the right rotation operation, so that the root of the new subtree of the original node of the left child namely K2,K2 the right child K1, K1 's left child is K2 's right child;


Scenario 4: Suppose the node k1,k1 right child K2,

Single right Rotation: the right child of the K1 node K2 the left subtree inserted, so that the K1 BF is-2, at this time the minimum imbalance subtree is the K1 as the root node of the subtree, so that the left rotation operation, so that the root node of the new subtree is the original node of the right child that k2,k2 the left child is K1, The left child of K1 is the left child of K2 in the Atom tree;


Here is the Java code implementation:

Package Data_structure, @SuppressWarnings ("Rawtypes") public class Avl<anytype extends comparable> {private Avlnode Root; Defines the size comparison method for the Paradigm Anytype @SuppressWarnings ("unused")/*private class Anytype implements comparable<anytype>{ Private AnyType x; @Overridepublic int compareTo (AnyType o) {String s=x.tostring (); String target=o.tostring (); return S.compareto (target);}} */private class Avlnode<anytype>{anytype element; Avlnode<anytype> left; avlnode<anytype> right;int height; @SuppressWarnings ("unused") public Avlnode (AnyType data) {This (Data,null, NULL);} Public Avlnode (AnyType data,avlnode<anytype> lc,avlnode<anytype> rc) {ELEMENT=DATA;LEFT=LC;RIGHT=RC; height=0;}} private int getheight (Avlnode<anytype> t) {return t==null?-1:t.height;} @SuppressWarnings ("unchecked") private int compare (AnyType X,anytype y) {if (X.compareto (y) >0) return 1;else if ( X.compareto (y) <0) Return-1;return 0;} Single rotation left rotation private avlnode<anytype> rotatewithleftchild (Avlnode<anytype> K1) {avlnode<anytype> K2=k1.right;k1.right=k2.left;k2.left=k1;k2.height=math.max (GetHeight (K2.left), GetHeight (K2.right)) +1;k1.height=math.max (GetHeight (K1.left), GetHeight (k1.right)) +1;return K2;} Single rotation right rotation private avlnode<anytype> rotatewithrightchild (avlnode<anytype> K1) {avlnode<anytype> k2= K1.left;k1.left=k2.right;k2.right=k1;k2.height=math.max (GetHeight (K2.left), GetHeight (k2.right)) +1;k1.height= Math.max (GetHeight (K1.left), GetHeight (k1.right)) +1;return K2;} Double rotation left rotation, divided into two steps: first right to the right child as the root of the subtree, and then left to rotate K1private avlnode<anytype> doublewithleftchild (avlnode<anytype> K1 Double rotation right rotation, divided into two steps: first left to the right child root subtree, and then right rotation k1private avlnode<anytype> doublewithrightchild (avlnode<anytype> K1) {k1.left=rotatewithleftchild (k1.left);//Pay attention to k1.left do not drop, or K2 lost the return Rotatewithrightchild (K1);} Use recursion to insert, each return insert subtree node, x for the element to be inserted @suppresswarnings ("unchecked") Private Avlnode<anytype> Insert (AnyType x,avlnode<anytype> t) {System.out.print (x+ ""), if (t==null) return new Avlnode (X,null, NULL), int result=compare (x, T.element), if (result<0) {T.left=insert (x, T.left), if (GetHeight (t.left)-getheight (    T.right) ==2) if (compare (x, t.left.element) <0) t=rotatewithrightchild (t); if (compare (x, t.left.element) >0) t=doublewithrightchild (t);} if (result>0) {T.right=insert (x, T.right), if (GetHeight (t.right)-getheight (t.left) ==2) if (compare (x,    t.right.element) >0) t=rotatewithleftchild (t); if (compare (x,t.right.element) <0) t=doublewithleftchild (t);} T.height=math.max (GetHeight (T.left), GetHeight (t.right)) +1;return t;} @SuppressWarnings ("unchecked") private void Createavl (AnyType [] data) {for (AnyType a:data) Root=insert (A, root);} private void Printtree (Avlnode root) {if (root==null) return;p rinttree (root.left); System.out.print (root.element+ "");p Rinttree (root.right);} public static void Main (String []args) {Integer []data={2,6,8,4,12,45,25,14,28}; Avl<integer>Avl=new avl<integer> (); Avl.createavl (data); System.out.println (); Avl.printtree (Avl.root);}}


Java Creating an AVL tree

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.