The AVL tree (named from the author's name, Adelson-velskii and Landis), which is a balanced binary tree, satisfies the following conditions:
1) Its Saozi right subtree is the AVL tree
2) The height difference of the right sub-tree of Saozi cannot exceed 1
From condition 1 It is possible to see a recursive definition.
The height of the two son subtree of any node in the AVL tree is the maximum difference of one, so it is also called a height-balanced tree.
The steps for an AVL tree to insert a node are divided into 2 categories:
Class 1th: Lateral insertion, single rotation
Class 2nd: Inner insertion, double rotation (first rotating into the outer insert, then single rotation)
Since the height of the tree is the same as before the insertion, it is no longer necessary to look up the balance case
Code implementation: http://blog.chinaunix.net/uid-20662820-id-142440.html
structnode{Node*parent; Node*Left ; Node*Right ; intBalance//the difference of height between left and right sub-trees intkey;};intSearchnode (intKey, node* Root, node** parent)//if it is not found, the parent also points to the location where you want to insert it.{node*temp; ASSERT (Root!=NULL); Temp=Root; *parent = root->parent; while(Temp! =NULL) { if(Temp->key = =key)return 1; Else { *parent =temp; if(Temp->key >key) Temp= temp->Left ; ElseTemp= temp->Right ; } } return 0;} Node* ADJUSTAVL (node* root, node* parent, node*Child ) {Node*cur; ASSERT ((Parent! = NULL) && (Child! =NULL)); Switch(parent->balance) { Case 2: if(Child->balance = =-1)//LR type (inside insert): The parent node of the inserted node is upgraded directly to do parent{cur= child->Right ; Cur->parent = parent->parent; Child->right = cur->Left ; if(Cur->left! =NULL) cur->left->parent =Child ; Parent->left = cur->Right ; if(Cur->right! =NULL) cur->right->parent =parent; Cur->left =Child ; Child->parent =cur; Cur->right =parent; if(Parent->parent! =NULL)if(Parent->parent->left = =parent) Parent->parent->left =cur; ElseParent->parent->right =cur; ElseRoot=cur; Parent->parent =cur; if(Cur->balance = =0) {Parent->balance =0; Child->balance =0; } Else if(Cur->balance = =-1) {Parent->balance =0; Child->balance =1; } Else{Parent->balance =-1; Child->balance =0; } cur->balance =0; } Else //ll type (outside insert): parent node of inserted node upgrade do Child,child upgrade do parentChild->parent = parent->parent; Parent->left = child->Right ; if(Child->right! =NULL) Child->right->parent =parent; Child->right =parent; if(Parent->parent! =NULL)if(Parent->parent->left = =parent) Parent->parent->left =Child ; ElseParent->parent->right =Child ; ElseRoot=Child ; Parent->parent =Child ; if(Child->balance = =1)//when inserted{ Child->balance =0; Parent->balance =0; } Else //when deleted{ Child->balance =-1; Parent->balance =1; } } Break; Case-2: if(Child->balance = =1)//RL Type{cur= child->Left ; Cur->parent = parent->parent; Child->left = cur->Right ; if(Cur->right! =NULL) cur->right->parent =Child ; Parent->right = cur->Left ; if(Cur->left! =NULL) cur->left->parent =parent; Cur->left =parent; Cur->right =Child ; Child->parent =cur; if(Parent->parent! =NULL)if(Parent->parent->left = =parent) Parent->parent->left =cur; ElseParent->parent->right =cur; ElseRoot=cur; Parent->parent =cur; if(Cur->balance = =0) {Parent->balance =0; Child->balance =0; } Else if(Cur->balance = =1) {Parent->balance =0; Child->balance =-1; } Else{Parent->balance =1; Child->balance =0; } cur->balance =0; } Else //RR Type{ Child->parent = parent->parent; Parent->right = child->Left ; if(Child->left! =NULL) Child->left->parent =parent; Child->left =parent; if(Parent->parent! =NULL)if(Parent->parent->left = =parent) Parent->parent->left =Child ; ElseParent->parent->right =Child ; ElseRoot=Child ; Parent->parent =Child ; if(Child->balance = =-1)//when inserted{ Child->balance =0; Parent->balance =0; } Else //when deleted{ Child->balance =1; Parent->balance =-1; } } Break; } returnRoot;} Node* Insertnode (intKey, node*root) {Node*parent, *cur, *Child ; ASSERT (Root!=NULL); if(Searchnode (key, Root, &parent))//The node already exists. returnRoot; Else{cur= (node*)malloc(sizeof(node)); Cur->parent =parent; Cur->key =key; Cur->left =NULL; Cur->right =NULL; Cur->balance =0; if(Keykey) {parent->left =cur; Child= parent->Left ; } Else{Parent->right =cur; Child= parent->Right ; } while((parent = NULL))//Find the Kid tree you need to adjust { if(Child = = Parent->Left )if(Parent->balance = =-1) {Parent->balance =0; returnRoot; } Else if(Parent->balance = =1) {Parent->balance =2; Break; } Else{Parent->balance =1; Child=parent; Parent= parent->parent; } Else if(Parent->balance = =1)//right child, not causing imbalance{Parent->balance =0; returnRoot; } Else if(Parent->balance = =-1)//is the right child, and causes the parent's imbalance{Parent->balance =-2; Break; } Else //is the right child, and may cause an imbalance in the parent's parent{Parent->balance =-1; Child=parent; Parent= parent->parent; } } if(Parent = =NULL)returnRoot; returnAdjustavl (root, parent, child); }}
The AVL tree of Balanced binary tree