SlideShare a Scribd company logo
AVL Trees
• binary tree
• for every node x, define its balance factor
balance factor of x = height of left subtree of x
– height of right subtree of x
• balance factor of every node x is – 1, 0, or 1
• log2 (n+1) <= height <= 1.44 log2 (n+2)
Example AVL Search Tree
0 0
0 0
1
0
-1 0
1
0
-1
1
-1
10
7
8
3
1 5
30
40
20
25
35
45
60
put(9)
0 0
0 0
1
0
-1 0
1
0
-1
1
-1
9
0
-1
0
10
7
8
3
1 5
30
40
20
25
35
45
60
put(29)
0 0
0 0
1
0
0
1
0
-1
1
-1
10
7
8
3
1 5
30
40
20
25
35
45
60
29
0
-1
-1
-2
RR imbalance => new node is in
right subtree of right subtree of
white node (node with bf = –2)
0 0
0 0
1
0
0
1
0
-1
1
-1
10
7
8
3
1 5
30
40
25
35
45
60
0
RR rotation.
20
0
29
put(29)
Insert/Put
• Following insert/put, retrace path towards root
and adjust balance factors as needed.
• Stop when you reach a node whose balance
factor becomes 0, 2, or –2, or when you reach
the root.
• The new tree is not an AVL tree only if you
reach a node whose balance factor is either 2 or
–2.
• In this case, we say the tree has become
unbalanced.
A-Node
• Let A be the nearest ancestor of the newly
inserted node whose balance factor becomes
+2 or –2 following the insert.
• Balance factor of nodes between new node
and A is 0 before insertion.
Imbalance Types
• RR … newly inserted node is in the right
subtree of the right subtree of A.
• LL … left subtree of left subtree of A.
• RL… left subtree of right subtree of A.
• LR… right subtree of left subtree of A.
LL rotation
LL Rotation
• Subtree height is unchanged.
• No further adjustments to be done.
Before insertion.
1
0
A
B
BL BR
AR
h h
h
A
B
B’L BR
AR
After insertion.
h+1 h
h
B
A
After rotation.
BR
h
AR
h
B’L
h+1
0
0
1
2
Example
RR rotation
In an AVL search
After Inserting Node D

RR rotation
Example
LR Rotation (case 1)
• Subtree height is unchanged.
• No further adjustments to be done.
Before insertion.
1
0
A
B
A
B
After insertion.
C
C
A
After rotation.
B
0
-1
2
0 0
0
LR Rotation (case 2)
• Subtree height is unchanged.
• No further adjustments to be done.
C
A
CR
h-1
AR
h
1
0
A
B
BL
CR
AR
h
h-1
h
0
CL
h-1
C
A
B
BL
CR
AR
h
h-1
h
C’L
h
C
B
BL
h
C’L
h
1
-1
2
0 -1
0
LR Rotation (case 3)
• Subtree height is unchanged.
• No further adjustments to be done.
1
0
A
B
BL
CR
AR
h
h-1
h
0
CL
h-1
C
A
B
BL
C’R
AR
h
h
h
CL
h-1
C
-1
-1
2
C
A
C’R
h
AR
h
B
BL
h
CL
h-1
1 0
0
Example
RL Rotation
CS-102 AVLSv2.pdf
CS-102 AVLSv2.pdf
Single & Double Rotations
• Single
 LL and RR
• Double
 LR and RL
 LR is RR followed by LL
 RL is LL followed by RR
LR Is RR + LL
A
B
BL
C’R
AR
h
h
h
CL
h-1
C
-1
-1
2
After insertion.
A
C
CL
C’R
AR
h
h
BL
h
B
2
After RR rotation.
h-1
C
A
C’R
h
AR
h
B
BL
h
CL
h-1
1 0
0
After LL rotation.
Remove An Element
0 0
0 0
1
0
-1 0
1
0
-1
1
-1
10
7
8
3
1 5
30
40
20
25
35
45
60
Remove 8.
Remove An Element
0 0
0
2
0
-1 0
1
0
-1
1
-1
10
7
3
1 5
30
40
20
25
35
45
60
• Let q be parent of deleted node.
• Retrace path from q towards root.
q
New Balance Factor Of q
• Deletion from left subtree of q => bf--.
• Deletion from right subtree of q => bf++.
• New balance factor = 1 or –1 => no change in height of
subtree rooted at q.
• New balance factor = 0 => height of subtree rooted at q
has decreased by 1.
• New balance factor = 2 or –2 => tree is unbalanced at q.
q
Imbalance Classification
• Let A be the nearest ancestor of the deleted
node whose balance factor has become 2 or –2
following a deletion.
• Deletion from left subtree of A => type L.
• Deletion from right subtree of A => type R.
• Type R => new bf(A) = 2.
• So, old bf(A) = 1.
• So, A has a left child B.
 bf(B) = 0 => R0.
 bf(B) = 1 => R1.
 bf(B) = –1 => R-1.
R0 Rotation
• Subtree height is unchanged.
• No further adjustments to be done.
• Similar to LL rotation.
Before deletion.
1
0
A
B
BL BR
AR
h h
h
B
A
After rotation.
BR
h
A’R
h-1
BL
h
1
-1
A
B
BL BR
A’R
After deletion.
h h
h-1
0
2
CS-102 AVLSv2.pdf
R1 Rotation
• Subtree height is reduced by 1.
• Must continue on path to root.
• Similar to LL and R0 rotations.
Before deletion.
1
1
A
B
BL BR
AR
h h-1
h
B
A
After rotation.
BR
h-1
A’R
h-1
BL
h
0
0
A
B
BL BR
A’R
After deletion.
h h-1
h-1
1
2
CS-102 AVLSv2.pdf
R-1 Rotation
• New balance factor of A and B depends on b.
• Subtree height is reduced by 1.
• Must continue on path to root.
• Similar to LR.
1
-1
A
B
BL
CR
AR
h-1
h
b
CL
C
A
B
BL
CR
A’R
h-1
h-1
CL
C
b
-1
2
C
A
CR A’R
h-1
B
BL
h-1
CL
0
CS-102 AVLSv2.pdf
Number of Rebalancing Rotations
• At most 1 for an insert.
• O(log n) for a delete.
Rotation Frequency
• Insert random numbers.
 No rotation … 53.4% (approx).
 LL/RR … 23.3% (approx).
 LR/RL … 23.2% (approx).

More Related Content

PPT
lec41.ppt
SharmilaBP1
 
PPTX
Avl trees
priyapavi96
 
PPTX
9.bst(contd.) avl tree
Chandan Singh
 
PDF
AVL.pdf.................................
snamya20
 
PPTX
Lecture 10 data structures and algorithms
Aakash deep Singhal
 
PPTX
Avl trees final
PRAKASH RANJAN SINGH
 
PDF
DS_Mod4_2.pdf
SankarTerli
 
PPT
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 
lec41.ppt
SharmilaBP1
 
Avl trees
priyapavi96
 
9.bst(contd.) avl tree
Chandan Singh
 
AVL.pdf.................................
snamya20
 
Lecture 10 data structures and algorithms
Aakash deep Singhal
 
Avl trees final
PRAKASH RANJAN SINGH
 
DS_Mod4_2.pdf
SankarTerli
 
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 

Similar to CS-102 AVLSv2.pdf (20)

PPTX
4. avl
Rajandeep Gill
 
PDF
Logic microoperations
Nitesh Singh
 
PDF
Avl excercise
Shipra Swati
 
PPT
Presentation1 data structure for CSE.ppt
umakalaimani1
 
PDF
Part 3 section B Kinamatics V20. And rotation matrix pdf
ssuser2e9a1d1
 
PDF
Avl trees
Xad Kuain
 
PPTX
Avl tree ppt
Surkhab Shelly
 
PPT
Avl tree
Van Pham
 
PDF
MCS222-DDCA-Lec.7-111111111111111111.pdf
RashaOrban2
 
PPTX
AVLDeletion in advanced data structures in jvava
shivanibasika06
 
PPT
3-avl-tree.ppt
meenamadhuvandhi2
 
PPT
Avl trees 2
Shankar Bishnoi
 
PPT
M.E - Computer Science and Engineering-Data structure avl-tree
poonkodiraja2806
 
PDF
Sequential Circuit
Heman Pathak
 
PPTX
Digital Electronics R-S, J-K flip flop etc.pptx
ProfVilasShamraoPati
 
PPTX
Chebyshev filter
MOHAMMAD AKRAM
 
PPT
DSA Leactrure # 2! Explaining abstract AVL trees
amazonventuresinfo
 
PPTX
AVL Tree in Data Structure
Meghaj Mallick
 
PPT
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
 
PPT
ch10_EffiBinSearchTrees ch10_EffiBinSearchTrees
tahirnaquash2
 
Logic microoperations
Nitesh Singh
 
Avl excercise
Shipra Swati
 
Presentation1 data structure for CSE.ppt
umakalaimani1
 
Part 3 section B Kinamatics V20. And rotation matrix pdf
ssuser2e9a1d1
 
Avl trees
Xad Kuain
 
Avl tree ppt
Surkhab Shelly
 
Avl tree
Van Pham
 
MCS222-DDCA-Lec.7-111111111111111111.pdf
RashaOrban2
 
AVLDeletion in advanced data structures in jvava
shivanibasika06
 
3-avl-tree.ppt
meenamadhuvandhi2
 
Avl trees 2
Shankar Bishnoi
 
M.E - Computer Science and Engineering-Data structure avl-tree
poonkodiraja2806
 
Sequential Circuit
Heman Pathak
 
Digital Electronics R-S, J-K flip flop etc.pptx
ProfVilasShamraoPati
 
Chebyshev filter
MOHAMMAD AKRAM
 
DSA Leactrure # 2! Explaining abstract AVL trees
amazonventuresinfo
 
AVL Tree in Data Structure
Meghaj Mallick
 
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
 
ch10_EffiBinSearchTrees ch10_EffiBinSearchTrees
tahirnaquash2
 
Ad

More from ssuser034ce1 (20)

PDF
CSN221_Lec_27 Computer Architecture and Microprocessor
ssuser034ce1
 
PDF
CSN221_Lec_26 Computer Architecture and Microprocessor
ssuser034ce1
 
PDF
CSN221_Lec_25 Computer Architecture and Microprocessor
ssuser034ce1
 
PDF
CSN221_Lec_36 Computer Architecture and Microprocessor
ssuser034ce1
 
PDF
CSN221_Lec_35 Computer Architecture and Microprocessor
ssuser034ce1
 
PDF
CSN221_Lec_34 Computer Architecture and Microprocessor
ssuser034ce1
 
PDF
CSN221_Lec_22.pdf Computer Architecture and Microprocessor
ssuser034ce1
 
PDF
CSN221_Lec_17.pdf Multi Cycle Datapath Design
ssuser034ce1
 
PDF
CSN221_Lec_16.pdf MIPS ISA and Datapath design
ssuser034ce1
 
PDF
CSN221_Lec_15.pdf MIPS ISA and Datapath design
ssuser034ce1
 
PDF
Computer Architecture CSN221_Lec_37_SpecialTopics.pdf
ssuser034ce1
 
PDF
CSN221_Lec_5.pdf Computer Organization, CPU Structure and Functions
ssuser034ce1
 
PDF
CSN221_Lec_4.pdf Computer Organization & Architecture
ssuser034ce1
 
PDF
CS-102 Data Structures huffman coding.pdf
ssuser034ce1
 
PDF
CS-102 Data Structures HashFunction CS102.pdf
ssuser034ce1
 
PDF
CS-102 Data Structure lectures on Graphs
ssuser034ce1
 
PDF
CS-102 DS-class04a Lectures DS Class.pdf
ssuser034ce1
 
PDF
CS-102 DS-class03 Class DS Lectures .pdf
ssuser034ce1
 
PDF
CS-102 DS-class_01_02 Lectures Data .pdf
ssuser034ce1
 
PDF
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
ssuser034ce1
 
CSN221_Lec_27 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_26 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_25 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_36 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_35 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_34 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_22.pdf Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_17.pdf Multi Cycle Datapath Design
ssuser034ce1
 
CSN221_Lec_16.pdf MIPS ISA and Datapath design
ssuser034ce1
 
CSN221_Lec_15.pdf MIPS ISA and Datapath design
ssuser034ce1
 
Computer Architecture CSN221_Lec_37_SpecialTopics.pdf
ssuser034ce1
 
CSN221_Lec_5.pdf Computer Organization, CPU Structure and Functions
ssuser034ce1
 
CSN221_Lec_4.pdf Computer Organization & Architecture
ssuser034ce1
 
CS-102 Data Structures huffman coding.pdf
ssuser034ce1
 
CS-102 Data Structures HashFunction CS102.pdf
ssuser034ce1
 
CS-102 Data Structure lectures on Graphs
ssuser034ce1
 
CS-102 DS-class04a Lectures DS Class.pdf
ssuser034ce1
 
CS-102 DS-class03 Class DS Lectures .pdf
ssuser034ce1
 
CS-102 DS-class_01_02 Lectures Data .pdf
ssuser034ce1
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
ssuser034ce1
 
Ad

Recently uploaded (20)

PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PDF
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PDF
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PPTX
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
PPT
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
All chapters of Strength of materials.ppt
girmabiniyam1234
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
Zero Carbon Building Performance standard
BassemOsman1
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
Inventory management chapter in automation and robotics.
atisht0104
 
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
All chapters of Strength of materials.ppt
girmabiniyam1234
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 

CS-102 AVLSv2.pdf

  • 1. AVL Trees • binary tree • for every node x, define its balance factor balance factor of x = height of left subtree of x – height of right subtree of x • balance factor of every node x is – 1, 0, or 1 • log2 (n+1) <= height <= 1.44 log2 (n+2)
  • 2. Example AVL Search Tree 0 0 0 0 1 0 -1 0 1 0 -1 1 -1 10 7 8 3 1 5 30 40 20 25 35 45 60
  • 3. put(9) 0 0 0 0 1 0 -1 0 1 0 -1 1 -1 9 0 -1 0 10 7 8 3 1 5 30 40 20 25 35 45 60
  • 4. put(29) 0 0 0 0 1 0 0 1 0 -1 1 -1 10 7 8 3 1 5 30 40 20 25 35 45 60 29 0 -1 -1 -2 RR imbalance => new node is in right subtree of right subtree of white node (node with bf = –2)
  • 5. 0 0 0 0 1 0 0 1 0 -1 1 -1 10 7 8 3 1 5 30 40 25 35 45 60 0 RR rotation. 20 0 29 put(29)
  • 6. Insert/Put • Following insert/put, retrace path towards root and adjust balance factors as needed. • Stop when you reach a node whose balance factor becomes 0, 2, or –2, or when you reach the root. • The new tree is not an AVL tree only if you reach a node whose balance factor is either 2 or –2. • In this case, we say the tree has become unbalanced.
  • 7. A-Node • Let A be the nearest ancestor of the newly inserted node whose balance factor becomes +2 or –2 following the insert. • Balance factor of nodes between new node and A is 0 before insertion.
  • 8. Imbalance Types • RR … newly inserted node is in the right subtree of the right subtree of A. • LL … left subtree of left subtree of A. • RL… left subtree of right subtree of A. • LR… right subtree of left subtree of A.
  • 10. LL Rotation • Subtree height is unchanged. • No further adjustments to be done. Before insertion. 1 0 A B BL BR AR h h h A B B’L BR AR After insertion. h+1 h h B A After rotation. BR h AR h B’L h+1 0 0 1 2
  • 13. In an AVL search After Inserting Node D 
  • 16. LR Rotation (case 1) • Subtree height is unchanged. • No further adjustments to be done. Before insertion. 1 0 A B A B After insertion. C C A After rotation. B 0 -1 2 0 0 0
  • 17. LR Rotation (case 2) • Subtree height is unchanged. • No further adjustments to be done. C A CR h-1 AR h 1 0 A B BL CR AR h h-1 h 0 CL h-1 C A B BL CR AR h h-1 h C’L h C B BL h C’L h 1 -1 2 0 -1 0
  • 18. LR Rotation (case 3) • Subtree height is unchanged. • No further adjustments to be done. 1 0 A B BL CR AR h h-1 h 0 CL h-1 C A B BL C’R AR h h h CL h-1 C -1 -1 2 C A C’R h AR h B BL h CL h-1 1 0 0
  • 23. Single & Double Rotations • Single  LL and RR • Double  LR and RL  LR is RR followed by LL  RL is LL followed by RR
  • 24. LR Is RR + LL A B BL C’R AR h h h CL h-1 C -1 -1 2 After insertion. A C CL C’R AR h h BL h B 2 After RR rotation. h-1 C A C’R h AR h B BL h CL h-1 1 0 0 After LL rotation.
  • 25. Remove An Element 0 0 0 0 1 0 -1 0 1 0 -1 1 -1 10 7 8 3 1 5 30 40 20 25 35 45 60 Remove 8.
  • 26. Remove An Element 0 0 0 2 0 -1 0 1 0 -1 1 -1 10 7 3 1 5 30 40 20 25 35 45 60 • Let q be parent of deleted node. • Retrace path from q towards root. q
  • 27. New Balance Factor Of q • Deletion from left subtree of q => bf--. • Deletion from right subtree of q => bf++. • New balance factor = 1 or –1 => no change in height of subtree rooted at q. • New balance factor = 0 => height of subtree rooted at q has decreased by 1. • New balance factor = 2 or –2 => tree is unbalanced at q. q
  • 28. Imbalance Classification • Let A be the nearest ancestor of the deleted node whose balance factor has become 2 or –2 following a deletion. • Deletion from left subtree of A => type L. • Deletion from right subtree of A => type R. • Type R => new bf(A) = 2. • So, old bf(A) = 1. • So, A has a left child B.  bf(B) = 0 => R0.  bf(B) = 1 => R1.  bf(B) = –1 => R-1.
  • 29. R0 Rotation • Subtree height is unchanged. • No further adjustments to be done. • Similar to LL rotation. Before deletion. 1 0 A B BL BR AR h h h B A After rotation. BR h A’R h-1 BL h 1 -1 A B BL BR A’R After deletion. h h h-1 0 2
  • 31. R1 Rotation • Subtree height is reduced by 1. • Must continue on path to root. • Similar to LL and R0 rotations. Before deletion. 1 1 A B BL BR AR h h-1 h B A After rotation. BR h-1 A’R h-1 BL h 0 0 A B BL BR A’R After deletion. h h-1 h-1 1 2
  • 33. R-1 Rotation • New balance factor of A and B depends on b. • Subtree height is reduced by 1. • Must continue on path to root. • Similar to LR. 1 -1 A B BL CR AR h-1 h b CL C A B BL CR A’R h-1 h-1 CL C b -1 2 C A CR A’R h-1 B BL h-1 CL 0
  • 35. Number of Rebalancing Rotations • At most 1 for an insert. • O(log n) for a delete.
  • 36. Rotation Frequency • Insert random numbers.  No rotation … 53.4% (approx).  LL/RR … 23.3% (approx).  LR/RL … 23.2% (approx).