Java implementation of the Insert () of data structure--AVL tree

Source: Internet
Author: User

An AVL tree is a two-fork lookup tree with a height difference of up to 1 for each node of the Saozi right subtree; the AVL tree is one of the oldest balancing search trees

On the code:

Package Com.itany.avlshu;public class Avltree<t extends Comparable<?super t>>{private static Class Avlnode        <T> {private int height;        private T element;        Private avlnode<t> left;        Private avlnode<t> right;        Public Avlnode (T element) {this (element,null,null);            } public Avlnode (T element,avlnode<t> Left,avlnode right) {this.element=element;            This.left=left;            This.right=right;        height=0;    }} private int height (avlnode<t> node) {return (node==null) -1:node.height;    } private int Compare (t X, t Element) {return X.compareto (element); } private avlnode<t> Insert (T x,avlnode<t> t) {if (t==null) return new Avlnode<t&gt        ;(x,null,null);//created by default height=0 int compareresult=compare (x,t.element); if (compareresult<0) {T.left=insERT (X,t.left); After the insertion, I immediately compare the height difference between the left and right son of T is equal to 2 if the corresponding rotation is not the next step to update the height of this t directly//At this time the left son height is relatively large if (height (t.left)-H Eight (t.right) ==2) {//Under two rotational conditions one is single rotation and the other is a double rotation if (compare (x,t.le                ft.element) <0) t=rotatewithleftchild (t);            else T=doublerotatewithleftchild (t);            }} else if (compareresult>0) {T.right=insert (x,t.right); Done after the insertion immediately compare T's left and right son's height difference is equal to 2 if the corresponding rotation if not then the next step directly update the height value of this t//at this time, the son of high-altitude comparison if (height (t.right)- Height (t.left) ==2) {//Under two rotation conditions one is single rotation and the other is a double rotation if (compare (X,t.right.element) <                0) T=doublerotatewithrightchild (t);            else T=rotatewithrightchild (t);        }} else; T.height=math.max (height (t.left), height (t.right)) +1;//+1 is the addition of a self-return t;        } private avlnode<t> Rotatewithrightchild (avlnode<t> K1) {avlnode<t> k2=k1.right;        K1.right=k2.left;        K2.LEFT=K1;        K2.height=math.max (height (k2.left), height (k2.right)) +1;        K1.height=math.max (height (k1.left), height (k1.right)) +1;    return K2; } private avlnode<t> Doublerotatewithrightchild (avlnode<t> K3) {k3.right=rotatewithleftchild (K3.        right);    Return Rotatewithrightchild (K3); }//Double rotation is obtained by the two-time single rotation of the private avlnode<t> Doublerotatewithleftchild (avlnode<t> K3) {K3.left=rotatew        Ithrightchild (K3.left);    Return Rotatewithleftchild (K3);        } private avlnode<t> Rotatewithleftchild (avlnode<t> K2) {avlnode<t> k1=k2.left;        K2.left=k1.right;        K1.RIGHT=K2;        K2.height=math.max (height (k2.left), height (k2.right)) +1;        K1.height=math.max (height (k1.left), height (k1.right)) +1;    return K1; }    }


Java implementation of the Insert () of data structure--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.