SlideShare a Scribd company logo
1 of 18
Download to read offline
1 
AVL Trees 
… 
AVL Trees
2 
Insertion 
… 
Inserting a node, v, into an AVL tree changes the heights of some of the nodes in T. 
… 
The only nodes whose heights can increase are the ancestors of node v. 
… 
If insertion causes T to become unbalanced,then some ancestor of v would have a height- imbalance. 
… 
We travel up the tree from v until we find the first node x such that its grandparent z is unbalanced. 
… 
Let y be the parent of node x.
3 
Insertion (2) 
To rebalance the subtree rooted at z, we must perform a rotation. 
44 
17 
32 
78 
50 
48 
62 
88 
54 
z 
x 
y 
44 
17 
32 
62 
50 
48 
54 
78 
88 
z 
x 
y
4 
Rotations 
… 
Rotation is a way of locally reorganizing a BST. 
… 
Let u,v be two nodes such that u=parent(v) 
… 
Keys(T1) < key(v) < keys(T2) < key (u) < keys(T3) 
u 
v 
T1 
T2 
T3
5 
Insertion 
… 
Insertion happens in subtree T1. 
… 
ht(T1) increases from h to h+1. 
… 
Since x remains balanced ht(T2) is h or h+1 or h+2. 
… 
If ht(T2)=h+2 then x is originally unbalanced 
… 
If ht(T2)=h+1 then ht(x) does not increase. 
… 
Hence ht(T2)=h. 
… 
So ht(x) increases from h+1 to h+2. 
z 
x 
y 
T2 
T1 
T3 
T4 
h to h+1 
h 
h+1 to h+2
6 
Insertion(2) 
… 
Since y remains balanced, ht(T3) is h+1 or h+2 or h+3. 
… 
If ht(T3)=h+3 then y is originally unbalanced. 
… 
If ht(T3)=h+2 then ht(y) does not increase. 
… 
So ht(T3)=h+1. 
… 
So ht(y) inc. from h+2 to h+3. 
… 
Since z was balanced ht(T4) is h+1 or h+2 or h+3. 
… 
z is now unbalanced and so ht(T4)=h+1. 
z 
x 
y 
T2 
T1 
T3 
T4 
h to h+1 
h 
h+1 to h+2 
h+1 
h+2 to h+3 
h+1 
h+3
7 
Single rotation 
The height of the subtree remains the same after rotation. Hence no further rotations required 
z 
x 
y 
T2 
T1 
T3 
T4 
h to h+1 
h 
h+1 to h+2 
h+1 
h+2 to h+3 
h+1 
z 
x 
y 
T2 
T1 
T3 
T4 
h+1 
h 
h+2 
h+1 
h+3 
h+1 
h+3 
h+2 
rotation(y,z)
8 
Double rotation 
Final tree has same height as original tree. Hence we need not go further up the tree. 
z 
x 
y 
T3 
T2 
T1 
T4 
h to h+1 
h 
h+1 to h+2 
h+1 
h+2 to h+3 
h+1 
h+3 
z 
x 
y 
T3 
T2 
T1 
T4 
h+1 
h 
h+2 
h+1 
h+3 
h+1 
h+4 
z 
x 
y 
T3 
T2 
T1 
T4 
h+1 
h 
h+2 
h+1 
h+3 
h+1 
h+2 
rotation(x,y) 
rotation(x,z)
9 
Restructuring 
… 
The four ways to rotate nodes in an AVL tree, graphically represented 
-Single Rotations:
10 
Restructuring (contd.) 
… 
double rotations:
11 
Deletion 
… 
When deleting a node in a BST, we either delete a leaf or a node with only one child. 
… 
In an AVL tree if a node has only one child then that child is a leaf. 
… 
Hence in an AVL tree we either delete a leaf or the parent of a leaf. 
… 
Hence deletion can be assumed to be at a leaf.
12 
Deletion(2) 
… 
Let w be the node deleted. 
… 
Let zbe the first unbalancednode encountered while travelling up the tree from w. Also, let y be the child of z with larger height, and let x be the child of y with larger height. 
… 
We perform rotations to restore balance at the subtree rooted at z. 
… 
As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached
13 
Deletion(3) 
… 
Suppose deletion happens in subtree T4and its ht. reduces from h to h-1. 
… 
Since z was balanced but is now unbalanced, ht(y) = h+1. 
… 
x has larger ht. than T3and so ht(x)=h. 
… 
Since y is balanced ht(T3)= h or h-1 
z 
x 
y 
T2 
T1 
T3 
T4 
h-1 or h-2 
h-1 or h-2 
h 
h or h-1 
h+1 
h to h-1 
h+2
14 
Deletion(4) 
… 
Since ht(x)=h, and x is balanced ht(T1), ht(T2) is h-1 or h-2. 
… 
However, both T1and T2cannot have ht. h-2 
z 
x 
y 
T2 
T1 
T3 
T4 
h-1 or h-2 
h-1 or h-2 
h 
h or h-1 
h+1 
h to h-1 
h+2
15 
Single rotation (deletion) 
After rotation height of subtree might be 1 less than original height. In that case we continue up the tree 
z 
x 
y 
T2 
T1 
T3 
T4 
h-1 or h-2 
h-1 or h-2 
h 
h or h-1 
h+1 
h to h-1 
h+2 
z 
x 
y 
T2 
T1 
T3 
T4 
h-1 or h-2 
h-1 or h-2 
h 
h or h-1 
h+1 or h+2 
h-1 
h or h+1 
rotation(y,z)
16 
Deletion: another case 
… 
As before we can claim that ht(y)=h+1 and ht(x)=h. 
… 
Since y is balanced ht(T1) is h or h-1. 
… 
If ht(T1) is h then we would have picked x as the root of T1. 
… 
So ht(T1)=h-1 
z 
x 
y 
T3 
T2 
T1 
T4 
h-1 or h-2 
h-1 or h-2 
h 
h-1 
h+1 
h to h-1 
h+2
17 
Double rotation 
Final tree has height less than original tree. Hence we need to continue up the tree 
z 
x 
y 
T3 
T2 
T1 
T4 
h-1 or h-2 
h-1 or h-2 
h 
h-1 
h+1 
h to h-1 
h+2 
z 
x 
y 
T3 
T2 
T1 
T4 
h-1 or h-2 
h-1 or h-2 
h 
h-1 
h+1 
h-1 
h+2 
z 
x 
y 
T3 
T2 
T1 
T4 
h 
h+1 
h-1 
h 
rotation(x,y) 
rotation(x,z) 
h-1 
h-1 or h-2 
h-1 or h-2
18 
Running time of insertion & deletion 
… 
Insertion 
… 
We perform rotation only once but might have to go O(log n) levels to find the unbalanced node. 
… 
So time for insertion is O(log n) 
… 
Deletion 
… 
We need O(log n) time to delete a node. 
… 
Rebalancing also requires O(log n) time. 
… 
More than one rotation may have to be performed.

More Related Content

What's hot

What's hot (6)

Fourier series basic results
Fourier series basic resultsFourier series basic results
Fourier series basic results
 
PaperNo3-YousefiHabibi-IMF
PaperNo3-YousefiHabibi-IMFPaperNo3-YousefiHabibi-IMF
PaperNo3-YousefiHabibi-IMF
 
Maths 3 ppt
Maths 3 pptMaths 3 ppt
Maths 3 ppt
 
Fourier series
Fourier seriesFourier series
Fourier series
 
Sol76
Sol76Sol76
Sol76
 
Signal & System
Signal & SystemSignal & System
Signal & System
 

Similar to Lec12

linear transformation and rank nullity theorem
linear transformation and rank nullity theorem linear transformation and rank nullity theorem
linear transformation and rank nullity theorem Manthan Chavda
 
Linear transforamtion and it,s applications.(VCLA)
Linear transforamtion and it,s applications.(VCLA)Linear transforamtion and it,s applications.(VCLA)
Linear transforamtion and it,s applications.(VCLA)DeepRaval7
 
Varian, microeconomic analysis, solution book
Varian, microeconomic analysis, solution bookVarian, microeconomic analysis, solution book
Varian, microeconomic analysis, solution bookJosé Antonio PAYANO YALE
 
CHAIN RULE AND IMPLICIT FUNCTION
CHAIN RULE AND IMPLICIT FUNCTIONCHAIN RULE AND IMPLICIT FUNCTION
CHAIN RULE AND IMPLICIT FUNCTIONNikhil Pandit
 
20 the chain rule
20 the chain rule20 the chain rule
20 the chain rulemath267
 
Lesson 32: The Fundamental Theorem Of Calculus
Lesson 32: The Fundamental Theorem Of CalculusLesson 32: The Fundamental Theorem Of Calculus
Lesson 32: The Fundamental Theorem Of CalculusMatthew Leingang
 
Lesson 8: Curves, Arc Length, Acceleration
Lesson 8: Curves, Arc Length, AccelerationLesson 8: Curves, Arc Length, Acceleration
Lesson 8: Curves, Arc Length, AccelerationMatthew Leingang
 
Linear Transformations_part1.pdf
Linear Transformations_part1.pdfLinear Transformations_part1.pdf
Linear Transformations_part1.pdfHirunManujaya
 
Fixed point and common fixed point theorems in vector metric spaces
Fixed point and common fixed point theorems in vector metric spacesFixed point and common fixed point theorems in vector metric spaces
Fixed point and common fixed point theorems in vector metric spacesAlexander Decker
 
Negative correlation properties for graphs
Negative correlation properties for graphsNegative correlation properties for graphs
Negative correlation properties for graphsAlejandro Erickson
 
1D graphs, kinematics, and calculus
1D graphs, kinematics, and calculus1D graphs, kinematics, and calculus
1D graphs, kinematics, and calculuscpphysics
 
Double_Integral.pdf
Double_Integral.pdfDouble_Integral.pdf
Double_Integral.pdfd00a7ece
 

Similar to Lec12 (20)

Lec12
Lec12Lec12
Lec12
 
Trees
TreesTrees
Trees
 
L15-clean.pptx
L15-clean.pptxL15-clean.pptx
L15-clean.pptx
 
linear transformation and rank nullity theorem
linear transformation and rank nullity theorem linear transformation and rank nullity theorem
linear transformation and rank nullity theorem
 
Linear transforamtion and it,s applications.(VCLA)
Linear transforamtion and it,s applications.(VCLA)Linear transforamtion and it,s applications.(VCLA)
Linear transforamtion and it,s applications.(VCLA)
 
Lecture3
Lecture3Lecture3
Lecture3
 
Varian, microeconomic analysis, solution book
Varian, microeconomic analysis, solution bookVarian, microeconomic analysis, solution book
Varian, microeconomic analysis, solution book
 
The wave equation
The wave equationThe wave equation
The wave equation
 
CHAIN RULE AND IMPLICIT FUNCTION
CHAIN RULE AND IMPLICIT FUNCTIONCHAIN RULE AND IMPLICIT FUNCTION
CHAIN RULE AND IMPLICIT FUNCTION
 
20 the chain rule
20 the chain rule20 the chain rule
20 the chain rule
 
Line integrals
Line integralsLine integrals
Line integrals
 
Lesson 32: The Fundamental Theorem Of Calculus
Lesson 32: The Fundamental Theorem Of CalculusLesson 32: The Fundamental Theorem Of Calculus
Lesson 32: The Fundamental Theorem Of Calculus
 
Lesson 8: Curves, Arc Length, Acceleration
Lesson 8: Curves, Arc Length, AccelerationLesson 8: Curves, Arc Length, Acceleration
Lesson 8: Curves, Arc Length, Acceleration
 
Linear Transformations_part1.pdf
Linear Transformations_part1.pdfLinear Transformations_part1.pdf
Linear Transformations_part1.pdf
 
Fixed point and common fixed point theorems in vector metric spaces
Fixed point and common fixed point theorems in vector metric spacesFixed point and common fixed point theorems in vector metric spaces
Fixed point and common fixed point theorems in vector metric spaces
 
Negative correlation properties for graphs
Negative correlation properties for graphsNegative correlation properties for graphs
Negative correlation properties for graphs
 
1D graphs, kinematics, and calculus
1D graphs, kinematics, and calculus1D graphs, kinematics, and calculus
1D graphs, kinematics, and calculus
 
Double_Integral.pdf
Double_Integral.pdfDouble_Integral.pdf
Double_Integral.pdf
 
2d transformation
2d transformation2d transformation
2d transformation
 
Sol75
Sol75Sol75
Sol75
 

More from Nikhil Chilwant (20)

The joyless economy
The joyless economyThe joyless economy
The joyless economy
 
I 7
I 7I 7
I 7
 
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
 
Lec39
Lec39Lec39
Lec39
 
Lec38
Lec38Lec38
Lec38
 
Lec37
Lec37Lec37
Lec37
 
Lec36
Lec36Lec36
Lec36
 
Lec35
Lec35Lec35
Lec35
 
Lec34
Lec34Lec34
Lec34
 
Lec33
Lec33Lec33
Lec33
 
Lec32
Lec32Lec32
Lec32
 
Lec30
Lec30Lec30
Lec30
 
Lec29
Lec29Lec29
Lec29
 
Lec28
Lec28Lec28
Lec28
 
Lec27
Lec27Lec27
Lec27
 
Lec26
Lec26Lec26
Lec26
 
Lec25
Lec25Lec25
Lec25
 
Lec24
Lec24Lec24
Lec24
 
Lec23
Lec23Lec23
Lec23
 
Lec21
Lec21Lec21
Lec21
 

Lec12

  • 1. 1 AVL Trees … AVL Trees
  • 2. 2 Insertion … Inserting a node, v, into an AVL tree changes the heights of some of the nodes in T. … The only nodes whose heights can increase are the ancestors of node v. … If insertion causes T to become unbalanced,then some ancestor of v would have a height- imbalance. … We travel up the tree from v until we find the first node x such that its grandparent z is unbalanced. … Let y be the parent of node x.
  • 3. 3 Insertion (2) To rebalance the subtree rooted at z, we must perform a rotation. 44 17 32 78 50 48 62 88 54 z x y 44 17 32 62 50 48 54 78 88 z x y
  • 4. 4 Rotations … Rotation is a way of locally reorganizing a BST. … Let u,v be two nodes such that u=parent(v) … Keys(T1) < key(v) < keys(T2) < key (u) < keys(T3) u v T1 T2 T3
  • 5. 5 Insertion … Insertion happens in subtree T1. … ht(T1) increases from h to h+1. … Since x remains balanced ht(T2) is h or h+1 or h+2. … If ht(T2)=h+2 then x is originally unbalanced … If ht(T2)=h+1 then ht(x) does not increase. … Hence ht(T2)=h. … So ht(x) increases from h+1 to h+2. z x y T2 T1 T3 T4 h to h+1 h h+1 to h+2
  • 6. 6 Insertion(2) … Since y remains balanced, ht(T3) is h+1 or h+2 or h+3. … If ht(T3)=h+3 then y is originally unbalanced. … If ht(T3)=h+2 then ht(y) does not increase. … So ht(T3)=h+1. … So ht(y) inc. from h+2 to h+3. … Since z was balanced ht(T4) is h+1 or h+2 or h+3. … z is now unbalanced and so ht(T4)=h+1. z x y T2 T1 T3 T4 h to h+1 h h+1 to h+2 h+1 h+2 to h+3 h+1 h+3
  • 7. 7 Single rotation The height of the subtree remains the same after rotation. Hence no further rotations required z x y T2 T1 T3 T4 h to h+1 h h+1 to h+2 h+1 h+2 to h+3 h+1 z x y T2 T1 T3 T4 h+1 h h+2 h+1 h+3 h+1 h+3 h+2 rotation(y,z)
  • 8. 8 Double rotation Final tree has same height as original tree. Hence we need not go further up the tree. z x y T3 T2 T1 T4 h to h+1 h h+1 to h+2 h+1 h+2 to h+3 h+1 h+3 z x y T3 T2 T1 T4 h+1 h h+2 h+1 h+3 h+1 h+4 z x y T3 T2 T1 T4 h+1 h h+2 h+1 h+3 h+1 h+2 rotation(x,y) rotation(x,z)
  • 9. 9 Restructuring … The four ways to rotate nodes in an AVL tree, graphically represented -Single Rotations:
  • 10. 10 Restructuring (contd.) … double rotations:
  • 11. 11 Deletion … When deleting a node in a BST, we either delete a leaf or a node with only one child. … In an AVL tree if a node has only one child then that child is a leaf. … Hence in an AVL tree we either delete a leaf or the parent of a leaf. … Hence deletion can be assumed to be at a leaf.
  • 12. 12 Deletion(2) … Let w be the node deleted. … Let zbe the first unbalancednode encountered while travelling up the tree from w. Also, let y be the child of z with larger height, and let x be the child of y with larger height. … We perform rotations to restore balance at the subtree rooted at z. … As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached
  • 13. 13 Deletion(3) … Suppose deletion happens in subtree T4and its ht. reduces from h to h-1. … Since z was balanced but is now unbalanced, ht(y) = h+1. … x has larger ht. than T3and so ht(x)=h. … Since y is balanced ht(T3)= h or h-1 z x y T2 T1 T3 T4 h-1 or h-2 h-1 or h-2 h h or h-1 h+1 h to h-1 h+2
  • 14. 14 Deletion(4) … Since ht(x)=h, and x is balanced ht(T1), ht(T2) is h-1 or h-2. … However, both T1and T2cannot have ht. h-2 z x y T2 T1 T3 T4 h-1 or h-2 h-1 or h-2 h h or h-1 h+1 h to h-1 h+2
  • 15. 15 Single rotation (deletion) After rotation height of subtree might be 1 less than original height. In that case we continue up the tree z x y T2 T1 T3 T4 h-1 or h-2 h-1 or h-2 h h or h-1 h+1 h to h-1 h+2 z x y T2 T1 T3 T4 h-1 or h-2 h-1 or h-2 h h or h-1 h+1 or h+2 h-1 h or h+1 rotation(y,z)
  • 16. 16 Deletion: another case … As before we can claim that ht(y)=h+1 and ht(x)=h. … Since y is balanced ht(T1) is h or h-1. … If ht(T1) is h then we would have picked x as the root of T1. … So ht(T1)=h-1 z x y T3 T2 T1 T4 h-1 or h-2 h-1 or h-2 h h-1 h+1 h to h-1 h+2
  • 17. 17 Double rotation Final tree has height less than original tree. Hence we need to continue up the tree z x y T3 T2 T1 T4 h-1 or h-2 h-1 or h-2 h h-1 h+1 h to h-1 h+2 z x y T3 T2 T1 T4 h-1 or h-2 h-1 or h-2 h h-1 h+1 h-1 h+2 z x y T3 T2 T1 T4 h h+1 h-1 h rotation(x,y) rotation(x,z) h-1 h-1 or h-2 h-1 or h-2
  • 18. 18 Running time of insertion & deletion … Insertion … We perform rotation only once but might have to go O(log n) levels to find the unbalanced node. … So time for insertion is O(log n) … Deletion … We need O(log n) time to delete a node. … Rebalancing also requires O(log n) time. … More than one rotation may have to be performed.