1.7 avl tree
TRANSCRIPT
AVL Tree
AVL Tree is a height balanced binary search tree in which the height of left subtree and height of right subtree can differ at most by 1. Figure 1 shows an example of AVL tree and Figure 2 shows a non-AVL tree.
50
40
4530
80
90
80
50
6040
90
453060
Operations on AVL Tree
• Insertion
• Deletion
• Search
The operations performed on an AVL tree is similar to that of a binary search tree. The only additional requirement is the tree balancing i.e. after insertion and deletion, we must ensure that the tree is balanced.
For tree balancing, we perform rotation operation which rotates the tree without affecting the ordering.
There are two types of rotation:
left rotation and
right rotation.
Left Rotation and Right Rotation
Left Rotation
store the right of pivot in temp
attach left of temp to right of pivot
attach pivot to left of temp
update necessary parent pointers
update root, if required
Right Rotation
store the left of pivot in temp
attach right of temp to left of pivot
attach pivot to right of temp
update necessary parent pointers
update root, if required
Right Rotation - Pseudo codevoid right_rotation(struct node *p){
if (p->left != NULL){
struct node *x;x = p->left; // store the left child pivot in xp->left = x->right; // attach x->right child to pivots's leftif (x->right!=NULL) x->right->parent = p; // update parent ptr of xx->right = p; // attach pivot to right of xif (p->parent != NULL)
if(p == p->parent->right) p->parent->right=x;else
p->parent->left = x;x->parent = p->parent;p->parent = x;if (p == root) //update root if required
root = x; }
}
Left Rotationvoid left_rotation(struct node *p){
if (p->right != NULL){
struct node *x;x = p->right; //store the right of pivot in xp->right = x->left; //attach x left to pivot's rightif (x->left!=NULL) x->left->parent = p; // update x's left parent ptrx->left = p; // attach pivot to left of xif (p->parent != NULL)
if (p==p->parent->left) p->parent->left=x;else
p->parent->right=x;x->parent = p->parent;p->parent = x;if(p == root)
root=x;}
}
void right_rotation(struct node *p){
if (p->left != NULL){
struct node *x;x = p->left; p->left = x->right; if (x->right!=NULL)
x->right->parent = p; x->right = p; if (p->parent != NULL) if(p == p->parent->right)
p->parent->right=x;else
p->parent->left = x;
x->parent = p->parent;p->parent = x;if (p == root)
root = x; }
}
void left_rotation(struct node *p){
if (p->right != NULL){
struct node *x;x = p->right; p->right = x->left;
if (x->left!=NULL) x->left->parent = p;
x->left = p; if (p->parent != NULL)if (p==p->parent->left)
p->parent->left=x;else p->parent->right=x;x->parent = p->parent;
p->parent = x;if (p == root)
root=x;}
}
root IS THE GLOBAL VARIABLE
Balance Factor and Rotation Effect
50(+1)
40(0)
75(0)
45(0)
30(0)
50(+2)
40(+1)
75(0)
45(0)
30(+1)
25(0)
40(0)
30(+1)
50(0)
50(0)
25(0)
75(0)
Recall
Definition of AVL Tree
Balance Factor
Tree Rotation
Single Rotation
left rotation
Right rotation
Double Rotation
left-right rotation
right-left rotation
Implementation of Rotation Operation
Insert Operation
Case 1: left subtree becomes left heavy
Case 2: right subtree becomes right heavy
Case 3: left subtree becomes right heavy
Case 4: right subtree becomes left heavy
Case 1: left subtree becomes left heavy
50(+1)
40(0)
75(0)
45(0)
30(0)
50(+2)
40(+1)
75(0)
45(0)
30(+1)
25(0)
40(0)
30(+1)
50(0)
50(0)
25(0)
75(0)
Case 2: right subtree becomes right heavy
50(-1)
40(0)
75(0)
80(0)
70(0)
50(-2)
40(0)
75(-1)
80(-1)
70(0)
85(0)
50(0)
40(0)
75(0)
80(-1)
70(0)
85(0)
Case 3: right subtree becomes left heavy
50(-1)
40(0)
75(0)
80(0)
70(0)
50(-2)
40(0)
75(+1)
80(+1)
70(+1)
69(0)
50(-2)
40(0)
70(-1)
75(-1)
69(0)
80(0)
50(0)
40(0)
70(0)
75(-1)
69(0)
80(0)
Case 4: left subtree becomes right heavy
50(+1)
40(0)
75(0)
45(0)
30(0)
50(+2)
40(-1)
75(0)
45(-1)
30(+1)
49(0)
50(+2)
40(-1)
75(0)
45(-1)
30(+1)
49(0)
Summary of insert operation
Perform insertion like a normal BST
After insertion update the balance factor in the insertion path
if critical node is encountered (node having bf>1 or bf<-1), perform appropriate rotation and then update the balance factors.
AVL Tree Implementation
The following slides provides some implementation details for implementing AVL trees
In AVL Tree insertion, we need to keep track of the path and direction of newly inserted node.
A stack or list may be used for achieving this. The code snippets shown in the subsequent slides uses list.
Insertion is same as BST, However you need to keep track of the path and direction
//scan through the binary treenode *prev=NULL, *curr=root; level=0;while (curr != NULL){
prev = curr;level++;if (key < curr->key){
NPTR[level]=curr; ROUTE[level]='L';curr = curr->left;
}else if (key > curr->key){
NPTR[level]=curr; ROUTE[level]='R';curr = curr->right;
}else {
printf("key Already Exists"); return;}
}
//create a node and assign the keynode *newnode = (struct node *)malloc(sizeof(struct node));newnode->key = key;newnode->bfactor=0;newnode->left = newnode->right = NULL;newnode->parent=prev;
//insert the nodeif (prev==NULL)
root = newnode;else{
if (key < prev->key)prev->left = newnode;
elseprev->right = newnode;
}
CASE 1 AND CASE 2for(int i=level-1;i>=1;i--){
if (ROUTE[i]=='L') NPTR[i]->bfactor++;else NPTR[i]->bfactor--;
if (NPTR[i]->bfactor>1 or NPTR[i]->bfactor< -1 ) //critical node{
if( ROUTE[i] == 'L' and ROUTE[i+1] == 'L' ) //case 1{
X = NPTR[i]; Y = NPTR[i+1];rightrotation( X );X->bfactor=Y->bfactor = 0;
}else if( ROUTE[i] == 'R' and ROUTE[i+1] =='R' ) //case 2, {
X = NPTR[i]; Y = NPTR[i+1];leftrotation( X );X->bfactor = Y->bfactor = 0;
}
CASE 3 AND CASE 4else if ( ROUTE[i]=='L' and ROUTE[i+1] == 'R' ) //case 3 {X = NPTR[i]; Y = NPTR[i+1]; Z = NPTR[i+2];leftrotation(Y);
rightrotation(X);if(Z->bfactor == +1) { X->bfactor=-1; Y->bfactor=0; Z->bfactor=0; }else if(Z->bfactor == -1) { X->bfactor=0; Y->bfactor=1; Z->bfactor=0; }else { X->bfactor=0; Y->bfactor=0; Z->bfactor=0; }
}else if ( ROUTE[i]=='R' and ROUTE[i+1] == 'L' ) //case 4, {
X = NPTR[i]; Y = NPTR[i+1]; Z = NPTR[i+2];rightrotation(Y);leftrotation(X);if(Z->bfactor == -1) { X->bfactor=1; Y->bfactor=0; Z->bfactor=0; }else if(Z->bfactor == 1) { Y->bfactor=-1; Z->bfactor=0; X->bfactor=0; }else { X->bfactor=0; Y->bfactor=0; Z->bfactor=0; }
}break;
ASSESSMENT
1. Insert the following elements into an empty binary search tree with balance indicators: 5,4,3,2,1. Then the balance factor of root node is
A. +4
B. -4
C. +3
D. -3
Contd..
2. Insert the following elements into an empty binary search tree with balance indicators: 5,4,3,2,1. Then the root node after performing right rotation with respect to root is
A. 5
B. 4
C. 1
D. 2
Contd..
3. The maximum balance factor a node can arrive at during insertion operation into an avl tree is
A. +2 or -2
B. -1 or +1
C. +1 only
D. -1 only
Contd..
4. A left rotation cannot be performed ifA. the right child of pivot is null
B. the left child of pivot is null
C. the pivot element is root
D. the pivot element is the left child of the root