SlideShare a Scribd company logo
1 of 36
Download to read offline
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
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
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
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
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

Similar to CS-102 AVLSv2.pdf (20)

Logic microoperations
Logic microoperationsLogic microoperations
Logic microoperations
 
Avl excercise
Avl excerciseAvl excercise
Avl excercise
 
Avl trees
Avl treesAvl trees
Avl trees
 
Avl tree ppt
Avl tree pptAvl tree ppt
Avl tree ppt
 
Avl tree
Avl treeAvl tree
Avl tree
 
3-avl-tree.ppt
3-avl-tree.ppt3-avl-tree.ppt
3-avl-tree.ppt
 
Avl trees 2
Avl trees 2Avl trees 2
Avl trees 2
 
Sequential Circuit
Sequential CircuitSequential Circuit
Sequential Circuit
 
Digital Electronics R-S, J-K flip flop etc.pptx
Digital Electronics R-S, J-K  flip flop etc.pptxDigital Electronics R-S, J-K  flip flop etc.pptx
Digital Electronics R-S, J-K flip flop etc.pptx
 
Chebyshev filter
Chebyshev filterChebyshev filter
Chebyshev filter
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDYAVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
 
ch10_EffiBinSearchTrees.ppt
ch10_EffiBinSearchTrees.pptch10_EffiBinSearchTrees.ppt
ch10_EffiBinSearchTrees.ppt
 
Logic Gates (1).ppt
Logic Gates (1).pptLogic Gates (1).ppt
Logic Gates (1).ppt
 
Sinusoidal oscillators
Sinusoidal oscillatorsSinusoidal oscillators
Sinusoidal oscillators
 
Avl tree
Avl treeAvl tree
Avl tree
 
Simplex part 2 of 4
Simplex part 2 of 4Simplex part 2 of 4
Simplex part 2 of 4
 
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
 
Week4
Week4Week4
Week4
 
Lec27
Lec27Lec27
Lec27
 

More from ssuser034ce1

CS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdfCS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdfssuser034ce1
 
CS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdfCS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdfssuser034ce1
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphsssuser034ce1
 
CS-102 DS-class04a Lectures DS Class.pdf
CS-102 DS-class04a Lectures DS Class.pdfCS-102 DS-class04a Lectures DS Class.pdf
CS-102 DS-class04a Lectures DS Class.pdfssuser034ce1
 
CS-102 DS-class03 Class DS Lectures .pdf
CS-102 DS-class03 Class DS Lectures .pdfCS-102 DS-class03 Class DS Lectures .pdf
CS-102 DS-class03 Class DS Lectures .pdfssuser034ce1
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfssuser034ce1
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfssuser034ce1
 
CS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdfCS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdfssuser034ce1
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfssuser034ce1
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfssuser034ce1
 
SURA presentation_.pptx
SURA presentation_.pptxSURA presentation_.pptx
SURA presentation_.pptxssuser034ce1
 

More from ssuser034ce1 (12)

CS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdfCS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdf
 
CS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdfCS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdf
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
 
CS-102 DS-class04a Lectures DS Class.pdf
CS-102 DS-class04a Lectures DS Class.pdfCS-102 DS-class04a Lectures DS Class.pdf
CS-102 DS-class04a Lectures DS Class.pdf
 
CS-102 DS-class03 Class DS Lectures .pdf
CS-102 DS-class03 Class DS Lectures .pdfCS-102 DS-class03 Class DS Lectures .pdf
CS-102 DS-class03 Class DS Lectures .pdf
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdf
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
 
CS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdfCS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdf
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
 
CS-102 AVLS.pdf
CS-102 AVLS.pdfCS-102 AVLS.pdf
CS-102 AVLS.pdf
 
SURA presentation_.pptx
SURA presentation_.pptxSURA presentation_.pptx
SURA presentation_.pptx
 

Recently uploaded

Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 

Recently uploaded (20)

DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 

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
  • 21.
  • 22.
  • 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
  • 30.
  • 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
  • 32.
  • 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
  • 34.
  • 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).