Proof of operation of AVL tree

Source: Internet
Author: User

The following is a node with a large o, and ABC represents three sets.

Only the case of the left subtree is analyzed, as is the case for symmetric, right subtrees.

Before inserting a node

O

/     \

O A

/    \

B C

After inserting the node:

O

/     \

O A

/    \

B C

/

O

At this time caused the highest node imbalance, explained b+2-a = 2, and also know B = C, consider B<c, then the highest point before inserting the node is unbalanced, consider B > C, then the highest left subtree is unbalanced, and should not consider the highest point. So you know a = B = C at this point.

Left dial hand after the single rotation of the tree:

O

/     \

B O

/          /    \

O C A

For the highest point, the left subtree depth is b+1 and the right subtree depth is a+1, which is B + 1.

Comparing the inserted tree, you can know that only the depth of the original highest node changes, so you only need to update the depth of the node.

Another situation:

After insertion:

 

O

/     \

O A

/    \

B C

/

O

If a single rotation occurs, the result is:

O

/     \

B O

/    \

C A

/

O

Obviously the situation has not been resolved.

So the first thing to do is to rotate the left subtree of the highest node in a single right, the result:

O

/     \

C A

/    \

o O

/

B

At this point it is known that the depth of the C set changes, the depth of the C is updated, and the depth of the highest point is updated, so the depth of the original highest point and the current highest point need to be updated when rotating.

The second left rotation of the original highest point, the result is

C

/     \

o O

/         /    \

B O A

There are some flaws in the correct, it should be the ABC set to expand a few layers, or in double rotation when the proof of some strange, anyway is this idea, because the drawing is too troublesome.

And finally the code:

  

#include <stdio.h>#include<stdlib.h>#include<unistd.h>typedefstruct_node{intelement; intHigh ; struct_node *Lefttree; struct_node *Righttree;} node;intGethigh (Node *t) {    if(T = =0)        return-1; returnT->High ;} Node*singlerotatewithleft (Node *t) {Node*tmp = t->Lefttree; T->lefttree = tmp->Righttree; TMP->righttree =T; TMP->high = ((Gethigh (Tmp->lefttree) > Gethigh (tmp->righttree))? Gethigh (Tmp->lefttree): Gethigh (tmp- >righttree)) +1; T->high = ((Gethigh (T->lefttree) > Gethigh (t->righttree))? Gethigh (T->lefttree): Gethigh (t-> Righttree)) +1; returntmp;} Node*singlerotatewithright (Node *6) {Node*tmp = t->Righttree; T->righttree = tmp->Lefttree; TMP->lefttree =T; TMP->high = ((Gethigh (Tmp->lefttree) > Gethigh (tmp->righttree))? Gethigh (Tmp->lefttree): Gethigh (tmp- >righttree)) +1; T->high = ((Gethigh (T->lefttree) > Gethigh (t->righttree))? Gethigh (T->lefttree): Gethigh (t-> Righttree)) +1; returntmp;} Node*doubleroratewithleft (Node *t) {T->lefttree = Singlerotatewithright (t->lefttree); returnSinglerotatewithleft (t);} Node*doubleroratewithright (Node *t) {T->righttree = Singlerotatewithleft (t->righttree); returnsinglerotatewithright (t);} Node*insert (Node *t,intElement) {    if(T = =0) {T= (node *)malloc(sizeof(node)); T->element =element; T->lefttree = T->righttree =0; }    Else if(T->element >Element) {T->lefttree = insert (t->lefttree,element); if(Gethigh (T->lefttree)-Gethigh (t->righttree) = =2)            if(Element < t->lefttree->Element) T=Singlerotatewithleft (t); ElseT=Doubleroratewithleft (t); }    Else if(T->element <Element) {T->righttree = insert (t->righttree,element); if(Gethigh (T->righttree)-Gethigh (t->lefttree) = =2)            if(Element > T->righttree->Element) T=singlerotatewithright (t); ElseT=doubleroratewithright (t); } t->high = ((Gethigh (T->lefttree) > Gethigh (t->righttree))? Gethigh (T->lefttree): Gethigh (t-> Righttree)) +1; returnT;} Node*find (Node *t,intElement) {    if(T = =0)        return 0; Else if(T->element >Element)returnFind (t->lefttree,element); Else if(T->element <Element)returnFind (t->righttree,element); Else        returnT;} Node* Findmin (Node *t) {    if(T = =0)        return 0; if(T->lefttree = =0)        returnT; Else        returnFindmin (t->lefttree);} Node*delele (Node *t,intElement) {    if(T = =0)        return 0; Else if(T->element >Element) T->lefttree = Delele (t->lefttree,element); Else if(T->element <Element) T->righttree = Delele (t->righttree,element); Else    {        if(T->lefttree && t->Righttree) {Node*tmp; TMP= Findmin (t->righttree); T->element = tmp->element; T->righttree = Delele (t->righttree,tmp->element); }        Else{node*tmp; TMP= t->lefttree?t->lefttree:t->Righttree;  Free(t); T=tmp; }    }    returnt;}voidPrinttree (Node *t) {    if(T = =0)        return; Printtree (t-lefttree); printf ("%d\t",t->element); printf ("High =%d\n",t->High ); Printtree (t-righttree);}intMain () {inta[Ten] = {1,2,3,4,5,6,7,8,9}; Node*T; inti =1; T= Insert (0,1);  for(;i<7; i++) {T=Insert (t,a[i]); //Printtree (t); //sleep (1);    }        //t = Delele (t,6);Printtree (t); printf ("\ n"); //while (1);    return 0;}

 

 

 

Proof of operation of AVL tree

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.