AVL Tree Java implementation, including deletion

Source: Internet
Author: User

The article comes from this. After a variety of queries, the AVL tree implementation after a long time has finally been completed. Below is a dynamic demonstration of the animation aVL, the source does not remember. Before reading the code, you must understand the algorithm description. There are also several rotation methods (it is important that the balance between insertion and deletion depends on these steps ), for details, refer to Yan Weimin's "data structure and algorithm", which is described in C language. Besides deletion, the other statements are quite detailed. The reference here is wiki.

We recommend that you take a look at this structure to understand the basic operations on BST (insert and delete ). In this way, it won't be difficult to understand.

Package COM. mars. datastructure;/**** http://dongxicheng.org/structure/avl/ * http://www.asiteof.me/2010/06/avl/ * @ author hoot */public class avltree4 {Private Static class node {node left; node right; int height; int data ;} private int heigth (node T) {If (t = NULL) {return 0;} return T. height;} // ll Private node rightrotate (node A) {Node B =. left;. left = B. right; B. right = A;. heweigh T = max (heigth (. left), heigth (. right); B. height = max (heigth (B. left), heigth (B. right); return B;} // RR private node leftrotate (node A) {Node B =. right;. right = B. left; B. left = A;. height = max (heigth (. left), heigth (. right); B. height = max (heigth (B. left), heigth (B. right); return B;} // LR private node leftrightrotate (node A) {. left = leftrotate (. left); Return rightrotate ();}// RL private node rightleftrotate (node A) {. right = rightrotate (. right); Return leftrotate (a);} // insert public node insert (INT data, node T) {If (t = NULL) {T = newnode (data );} else if (Data <t. data) {T. left = insert (data, T. left); If (heigth (T. left)-heigth (T. right) = 2) {If (Data <t. left. data) {T = rightrotate (t);} else {T = leftrightrotate (t) ;}} else {T. right = insert (data, T. right); If (H Eigth (T. right)-heigth (T. left) = 2) {If (data> T. right. data) {T = leftrotate (t);} else {T = rightleftrotate (t) ;}} T. height = max (heigth (T. left), heigth (T. right); Return t;} // Delete public node Delete (INT data, node T) {If (t = NULL) {return NULL;} If (T. data = data) {If (T. right = NULL) {T = T. left;} else {node head = T. right; while (head. left! = NULL) {head = head. left;} t. data = head. data; // remember to delete the node in the BST tree. right = Delete (T. data, T. right); T. height = max (heigth (T. left), heigth (T. right) + 1;} return t;} else if (T. DATA> data) {T. left = Delete (data, T. left);} else {T. right = Delete (data, T. right);} t. height = max (heigth (T. left), heigth (T. right); // The above only deletes the node, but needs to maintain a balance, so you need to rotate it to balance it to meet the AVL nature. // Check if (T. Left! = NULL) {T. Left = rotate (T. Left);} If (T. Right! = NULL) {T. right = rotate (T. right);} return rotate (t);} private node newnode (INT data) {node A = new node ();. left =. right = NULL;. height = 0;. data = data; return a;} private int max (INT heigth, int heig22.) {return heigth> heig22? Heigth: heig2;} public void travel (node root) {node = root; If (node = NULL) {return;} travel (node. left); system. out. print (node. data + ""); travel (node. right);} private node rotate (node T) {/* AVL adjustment for a single node */If (heigth (T. left)-heigth (T. right) = 2) {If (heigth (T. left. left)> = heigth (T. left. right) {T = rightrotate (t);} else {T = leftrightrotate (t) ;}} if (heigth (T. right)-heigth (T. left) = 2) {If (heigth (T. right. right)> = heigth (T. right. left) {T = leftrotate (t);} else {T = rightleftrotate (t) ;}return t;} public static void main (string [] ARGs) {int [] A = {0, 1, 4, 3, 8, 9, 2, 5, 7, 6,-10}; node root = NULL; avltree4 AVL = new avltree4 (); For (int B: a) {// system. out. println ("for:" + root); root = AVL. insert (B, root);} system. out. println (Root); AVL. travel (Root); AVL. delete (9, root); system. out. println (); AVL. travel (Root );}}

In view of the method and name of rotation, the rotation method and corresponding name described in the algorithm are given here.

Ll

Rr

LR

 

RL



(The illustration is from here)

References: http://dongxicheng.org/structure/avl/

Http://www.asiteof.me/2010/06/avl/

Yan Weimin, data structure and Algorithm

In fact, Yan Weimin's data structure and algorithms are very good, and many common algorithms and structures are described, A large part of the knowledge used in the interview questions of major companies can be found to solve the problem. Therefore, it is recommended that you have a requirement in school or in this respect. If you think that the introduction to algorithms is too thick, you can first read Yan's old book and then read about algorithms, such as Abacus programming and algorithm books.

 

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.