AVL balance Tree (detailed)-java version

Source: Internet
Author: User

Balanced binary trees can be unbalanced when inserting operations, and the AVL tree is a self-balancing two-fork tree.

It re-balances the two-fork tree by rotating an unbalanced node, and the Find, insert, and delete operations are all O (log n) in the average and worst case scenario

There are four scenarios in which the AVL tree rotates, noting that all rotations revolve around the first node that makes the binary tree unbalanced.

RBT VS AVL:

The speed at which you actually insert the AVL tree and the red-black tree depends on the data you insert. If your data is well distributed, it is preferable to use an AVL tree (e.g. randomly generated series numbers),
But if you want to deal with clutter, the red-black tree is faster, because the red-black tree reduces the unwanted action on the already processed data rebalancing. On the other hand, if an unusual insert series is more common (for example, inserting a key series), the AVL tree is faster, Because its strict balance rules will reduce the height of the tree.

The AVL tree makes more adjustments than the red and black trees when inserting and deleting operations.
RBT adjustment to balance up to 2 rotations, which is the advantage, the height will not exceed 2LG (n), so the search efficiency is the same
The old Linux Kernel Task Scheduler is the use of AVL, from a certain version began to change RBT.

1. LL type

Balanced binary tree A node in the left child's left subtree is inserted into a new node so that the node is no longer balanced. At this time only need to rotate the tree to the right once,, the original a left child B into the parent node, a to its right child, and the right sub-tree of the original B into a left subtree, note that after the rotation of BRH is a Zuozi (pictured in a in the brh between the line)


2. RR type

A new node is inserted into the right child tree of a node in a balanced binary tree, making the node no longer balanced. At this point only need to rotate the tree to the left once,, the original a right child B into a parent node, a to its left child, and the original B's left subtree BLH will become a right subtree.

3. LR type

Balanced binary tree a node in the left child's right subtree is inserted into a new node so that the node is no longer balanced. At this point, two rotations are required, and only one rotation is not able to balance the two-fork tree again. , the binary tree is still not balanced on the a node after the B node rotates to the left after the RR type, and then it needs to rotate to the right again.

4. RL Type

Balanced binary tree a node in the right child's left subtree is inserted into a new node so that the node is no longer balanced. Similarly, it is required to rotate two times and the direction of rotation is exactly the same as the LR type.

Import Java.util.arrays;import Java.util.collection;import Java.util.scanner;public class Main {static Node proot = null static int TOT = 0;/** * Middle sequence traversal print AVL tree * @param node */static void Print (node node) {if (Null = = node) return;print (Node.lson ); System.out.print (Node.data + "(" +node.freq + ")");p rint (Node.rson);} /** * The height of the tree * @param node * @return */static int Gethigh (node node) {if (Null = = node) return-1;return Node.high;}  /** * AVL Lookup * @param node * @param data * @return */static node Find (node node, int data) {if (Null = = node) return null;if (Node.data < data) return find (Node.lson, data), else if (Node.data > Data) return find (Node.rson, data); Elsereturn No De;} /** * AVL Insert * @param node * @param data * @return */static node Insert (node node, int data) {if (Null = = node) {node = new Node (data); tot++;} else if (Node.data > Data) {Node.lson = insert (Node.lson, data), if (Gethigh (Node.lson)-Gethigh (Node.rson) = = 2) {if ( Data < Node.lson.data) node = LL (node); elsenode = DL (node);}} else if (Data > Node.data) {node.rson = insert (Node.rson, data); if (Gethigh (Node.rson)-Gethigh (Node.lson) = = 2) {if (Data > Node.rson.data) node = RR (node); elsenode = DR (node);}} Elsenode.freq++;node.high = Math.max (Gethigh (Node.lson), Gethigh (Node.rson)) + 1;return node;} /** * AVL Single left rotation * @param node * @return */static node LL (node node) {Node T = Node.lson;node.lson = T.rson;t.rson = Node;nod E.high = Math.max (Gethigh (Node.rson), Gethigh (Node.lson)) + 1;t.high = Math.max (Gethigh (T.rson), T.high) + 1;return t;} /** * AVL Single right rotation * @param node * @return */static node RR (node node) {Node T = Node.rson;node.rson = T.lson;t.lson = Node;nod E.high = Math.max (Gethigh (Node.rson), Gethigh (Node.lson)) + 1;t.high = Math.max (Gethigh (T.rson), T.high) + 1;return t;} /** * AVL double left rotation, i.e. first right in left * @param node * @return */static node DL (node node) {Node.lson = RR (Node.lson); return LL (node);} /** * AVL double right rotation, i.e. left and right * @param node * @return */static node DR (node node) {Node.rson = LL (Node.rson); return RR (node);} /** * AVL Delete * @param node * @param data * @return */static node Delete (node node, int data) {if (Null = = node); else if (data &l T Node.data) {Node.lson = delete (Node.lson, data), if (2 = = Gethigh (Node.rson)-Gethigh (Node.lson)) {if (Gethigh. Lson) > Gethigh (Node.rson.rson)) node = DR (node); elsenode = RR (node);} By deleting a point on the left, the height on the right is likely to be larger than the other if (Data > Node.data) {node.rson = delete (Node.rson, data); if (2 = = Gethigh (Node.lson) -Gethigh (Node.rson)) {if (Gethigh (Node.lson.rson) > Gethigh (Node.lson.lson)) node = DL (node); elsenode = LL (node);} Delete a point on the right and the height on the left may be larger} else{if (null = = Node.lson && NULL = = Node.rson) {node = null;//just started missing}else if (null! = N Ode.lson && null! = Node.rson) {Node now = Node.rson;while (null! = Now.lson) now = now.lson;//Find the leftmost tree to delete the right subtree of the point Node.dat A = Now.data;node.freq = Now.freq;node.rson = Delete (Node.rson,node.data); if (2 = = Gethigh (Node.lson)-Gethigh (Node.rson ) {if (Gethigh (Node.lson.rson) > Gethigh (Node.lson.lson)) node = DL (node); eLsenode = LL (node);}} Else{if (Null = = Node.lson) node = node.rson;else node = Node.lson;}} if (null = = node) return Null;node.high = Math.max (Gethigh (Node.lson), Gethigh (Node.rson)) + 1;return node;} /** * Update operation, first delete and insert * @param node * @param data * @param newdata * @return */static Node Update (node node, int data, int new Data) {node = delete (node,data); return insert (Node,newdata);} /** * post-traversal Delete tree * @param node */static void DeleteTree (node node) {if (Null = = node) return;deletetree (Node.lson);d Eletetree (n Ode.rson); Node.free ();} public static void Main (string[] args) {System.out.println ("AVL Tree"); int [] num = new int [20]; Node root = null;for (int i = 0; i <; i++) {num[i] = (int) (Math.random () * +); root = insert (root, Num[i]);} print (root); System.out.printf ("\ n"); root = Update (root,num[3], (int) (Math.random () *)); root = Update (root,num[5], (int) ( Math.random () * +); root = Update (root,num[9], (int) (Math.random () *));p rint (root); System.out.printf ("\ n"); root = delete (root,num[1]); root = delete (Root,num[2]), root = delete (Root,num[7]);p rint (root); System.out.printf ("\ n");}} Class Node {node Lson, rson;int high;int data;int freq;public node () {super (); Lson = Rson = Null;freq = high = 0;} public Node (int data) {//TODO auto-generated constructor Stubthis (); this.data = data;} public void Free () {This.lson = This.rson = null;}}


AVL balance Tree (detailed)-java version

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.