SlideShare a Scribd company logo
.AVL TREES
Adelson-Velskii and Landis (AVL)
trees (height-balanced trees)
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
4530
60
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
Left Rotation
50
40
4530
80
9060
80
50
6040
90
90
4530
Right Rotation
50
40
4530
80
9060
40
50
8045
30
60 90
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 code
void right_rotation(struct node *p)
{
if (p->left != NULL)
{
struct node *x;
x = p->left; // store the left child pivot in x
p->left = x->right; // attach x->right child to pivots's left
if (x->right!=NULL) x->right->parent = p; // update parent ptr of x
x->right = p; // attach pivot to right of x
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) //update root if required
root = x;
}
}
Left Rotation
void left_rotation(struct node *p)
{
if (p->right != NULL)
{
struct node *x;
x = p->right; //store the right of pivot in x
p->right = x->left; //attach x left to pivot's right
if (x->left!=NULL) x->left->parent = p; // update x's left parent ptr
x->left = p; // attach pivot to left of x
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;
}
}
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 tree
node *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 key
node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->key = key;
newnode->bfactor=0;
newnode->left = newnode->right = NULL;
newnode->parent=prev;
//insert the node
if (prev==NULL)
root = newnode;
else
{
if (key < prev->key)
prev->left = newnode;
else
prev->right = newnode;
}
CASE 1 AND CASE 2
for(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 4
else 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 if
A. 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
Contd..
5. The inorder traversal of the AVL tree is 30,
40, 45, 50 and 75. Then the balance factor of
the root node is
A. +2
B. -2
C. +1 or -1
D. -1 or -2

More Related Content

What's hot

Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
United International University
 
Tree
TreeTree
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
SeethaDinesh
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
RacksaviR
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
Zaid Shabbir
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
Adam Mukharil Bachtiar
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
BINARY SEARCH TREE
BINARY SEARCH TREE BINARY SEARCH TREE
BINARY SEARCH TREE
Usha Mahalingam
 
Introduction to tree ds
Introduction to tree dsIntroduction to tree ds
Introduction to tree ds
Viji B
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
greatqadirgee4u
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
Muhazzab Chouhadry
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
Krish_ver2
 
single linked list
single linked listsingle linked list
single linked list
Sathasivam Rangasamy
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
ghhgj jhgh
 
Trees
TreesTrees
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
B+ tree intro,uses,insertion and deletion
B+ tree intro,uses,insertion and deletionB+ tree intro,uses,insertion and deletion
B+ tree intro,uses,insertion and deletion
HAMID-50
 
FORESTS
FORESTSFORESTS
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
Vrushali Dhanokar
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREE
Siddhi Shrivas
 

What's hot (20)

Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Tree
TreeTree
Tree
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
BINARY SEARCH TREE
BINARY SEARCH TREE BINARY SEARCH TREE
BINARY SEARCH TREE
 
Introduction to tree ds
Introduction to tree dsIntroduction to tree ds
Introduction to tree ds
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
single linked list
single linked listsingle linked list
single linked list
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Trees
TreesTrees
Trees
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
B+ tree intro,uses,insertion and deletion
B+ tree intro,uses,insertion and deletionB+ tree intro,uses,insertion and deletion
B+ tree intro,uses,insertion and deletion
 
FORESTS
FORESTSFORESTS
FORESTS
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREE
 

Similar to 1.7 avl tree

Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptx
MalligaarjunanN
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
banupriyar5
 
b. (10 pts) Implement the rotate left method for AVL trees.c. (10 .pdf
b. (10 pts) Implement the rotate left method for AVL trees.c. (10 .pdfb. (10 pts) Implement the rotate left method for AVL trees.c. (10 .pdf
b. (10 pts) Implement the rotate left method for AVL trees.c. (10 .pdf
akanshanawal
 
Define a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdfDefine a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdf
eyelineoptics
 
I have a .java program that I need to modify so that it1) reads i.pdf
I have a .java program that I need to modify so that it1) reads i.pdfI have a .java program that I need to modify so that it1) reads i.pdf
I have a .java program that I need to modify so that it1) reads i.pdf
allystraders
 
Ie
IeIe
Write a program that displays an AVL tree along with its balance fac.docx
 Write a program that displays an AVL tree  along with its balance fac.docx Write a program that displays an AVL tree  along with its balance fac.docx
Write a program that displays an AVL tree along with its balance fac.docx
ajoy21
 
AVL_Trees.ppt
AVL_Trees.pptAVL_Trees.ppt
AVL_Trees.ppt
SaqibShigri
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
Trad5
 
6. binary tree
6. binary tree6. binary tree
6. binary tree
Geunhyung Kim
 
Avl tree
Avl treeAvl tree
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
Vladimir Kostyukov
 
Queue
QueueQueue
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
Afaq Mansoor Khan
 
Avl tree
Avl treeAvl tree
Avl tree
wisley lukito
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
Daniel Cukier
 
05 queues
05 queues05 queues
05 queues
Rajan Gautam
 
quicksort (1).ppt
quicksort (1).pptquicksort (1).ppt
quicksort (1).ppt
Balasubramanian699229
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
tameemyousaf
 
Avl trees
Avl treesAvl trees
Avl trees
Xad Kuain
 

Similar to 1.7 avl tree (20)

Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptx
 
Adelson velskii Landis rotations based on
Adelson velskii Landis rotations based onAdelson velskii Landis rotations based on
Adelson velskii Landis rotations based on
 
b. (10 pts) Implement the rotate left method for AVL trees.c. (10 .pdf
b. (10 pts) Implement the rotate left method for AVL trees.c. (10 .pdfb. (10 pts) Implement the rotate left method for AVL trees.c. (10 .pdf
b. (10 pts) Implement the rotate left method for AVL trees.c. (10 .pdf
 
Define a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdfDefine a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdf
 
I have a .java program that I need to modify so that it1) reads i.pdf
I have a .java program that I need to modify so that it1) reads i.pdfI have a .java program that I need to modify so that it1) reads i.pdf
I have a .java program that I need to modify so that it1) reads i.pdf
 
Ie
IeIe
Ie
 
Write a program that displays an AVL tree along with its balance fac.docx
 Write a program that displays an AVL tree  along with its balance fac.docx Write a program that displays an AVL tree  along with its balance fac.docx
Write a program that displays an AVL tree along with its balance fac.docx
 
AVL_Trees.ppt
AVL_Trees.pptAVL_Trees.ppt
AVL_Trees.ppt
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
 
6. binary tree
6. binary tree6. binary tree
6. binary tree
 
Avl tree
Avl treeAvl tree
Avl tree
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
 
Queue
QueueQueue
Queue
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
Avl tree
Avl treeAvl tree
Avl tree
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
05 queues
05 queues05 queues
05 queues
 
quicksort (1).ppt
quicksort (1).pptquicksort (1).ppt
quicksort (1).ppt
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
Avl trees
Avl treesAvl trees
Avl trees
 

More from Krish_ver2

5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back tracking
Krish_ver2
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
Krish_ver2
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
Krish_ver2
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
Krish_ver2
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
Krish_ver2
 
5.4 randamized algorithm
5.4 randamized algorithm5.4 randamized algorithm
5.4 randamized algorithm
Krish_ver2
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
Krish_ver2
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
Krish_ver2
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-i
Krish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
Krish_ver2
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
Krish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
Krish_ver2
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
Krish_ver2
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
Krish_ver2
 
5.1 greedy 03
5.1 greedy 035.1 greedy 03
5.1 greedy 03
Krish_ver2
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
Krish_ver2
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
Krish_ver2
 
4.4 hashing ext
4.4 hashing  ext4.4 hashing  ext
4.4 hashing ext
Krish_ver2
 
4.4 external hashing
4.4 external hashing4.4 external hashing
4.4 external hashing
Krish_ver2
 
4.2 bst
4.2 bst4.2 bst
4.2 bst
Krish_ver2
 

More from Krish_ver2 (20)

5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back tracking
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
 
5.5 back tracking 02
5.5 back tracking 025.5 back tracking 02
5.5 back tracking 02
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
5.4 randamized algorithm
5.4 randamized algorithm5.4 randamized algorithm
5.4 randamized algorithm
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
 
5.3 dyn algo-i
5.3 dyn algo-i5.3 dyn algo-i
5.3 dyn algo-i
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
5.1 greedy 03
5.1 greedy 035.1 greedy 03
5.1 greedy 03
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
4.4 hashing ext
4.4 hashing  ext4.4 hashing  ext
4.4 hashing ext
 
4.4 external hashing
4.4 external hashing4.4 external hashing
4.4 external hashing
 
4.2 bst
4.2 bst4.2 bst
4.2 bst
 

Recently uploaded

Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
ArianaBusciglio
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 

Recently uploaded (20)

Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 

1.7 avl tree

  • 1. .AVL TREES Adelson-Velskii and Landis (AVL) trees (height-balanced trees)
  • 2. 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 4530 60
  • 3. 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
  • 6. 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
  • 7. Right Rotation - Pseudo code void right_rotation(struct node *p) { if (p->left != NULL) { struct node *x; x = p->left; // store the left child pivot in x p->left = x->right; // attach x->right child to pivots's left if (x->right!=NULL) x->right->parent = p; // update parent ptr of x x->right = p; // attach pivot to right of x 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) //update root if required root = x; } }
  • 8. Left Rotation void left_rotation(struct node *p) { if (p->right != NULL) { struct node *x; x = p->right; //store the right of pivot in x p->right = x->left; //attach x left to pivot's right if (x->left!=NULL) x->left->parent = p; // update x's left parent ptr x->left = p; // attach pivot to left of x 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; } }
  • 9. 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
  • 10. 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)
  • 11. 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
  • 12. 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
  • 13. 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)
  • 14. 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)
  • 15. 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)
  • 16. 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)
  • 17. 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.
  • 18. 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.
  • 19. Insertion is same as BST, However you need to keep track of the path and direction //scan through the binary tree node *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; } }
  • 20. //create a node and assign the key node *newnode = (struct node *)malloc(sizeof(struct node)); newnode->key = key; newnode->bfactor=0; newnode->left = newnode->right = NULL; newnode->parent=prev; //insert the node if (prev==NULL) root = newnode; else { if (key < prev->key) prev->left = newnode; else prev->right = newnode; }
  • 21. CASE 1 AND CASE 2 for(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; }
  • 22. CASE 3 AND CASE 4 else 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;
  • 23.
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. Contd.. 4. A left rotation cannot be performed if A. 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
  • 28. Contd.. 5. The inorder traversal of the AVL tree is 30, 40, 45, 50 and 75. Then the balance factor of the root node is A. +2 B. -2 C. +1 or -1 D. -1 or -2