SlideShare a Scribd company logo
1 of 25
Single Rotation to Fix Case 1(left-
left)
k2 violates
An insertion in subtree X,
AVL property violated at node k2
Solution: single rotation
Single Rotation to Fix Case 4 (right-
right)
• Case 4 is a symmetric case to case 1
• Insertion takes O(Height of AVL Tree)
time, Single rotation takes O(1) time
An insertion in subtree Z
k1 violates
Single Rotation Fails to fix Case 2&3
• Single rotation fails to fix case 2&3
• Take case 2 as an example (case 3 is a
symmetry to it )
– The problem is subtree Y is too deep
– Single rotation doesn’t make it any less deep
Single rotation result
Case 2: violation in k2 because of
insertion in subtree Y
Double Rotation to Fix Case 2 (left-right)
• Facts
– The new key is inserted in the subtree B or C
– The AVL-property is violated at k3
– k3-k1-k2 forms a zig-zag shape
• Solution
– We cannot leave k3 as the root
– The only alternative is to place k2 as the new
root
Double rotation to fix case 2
Double Rotation to fix Case 3(right-left)
• Facts
– The new key is inserted in the subtree B or C
– The AVL-property is violated at k1
– k2-k3-k2 forms a zig-zag shape
• Case 3 is a symmetric case to case 2
Double rotation to fix case 3
AVL Tree Example:
• Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree
14
17
11
7 53
4
AVL Tree Example:
• Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree
14
17
7
4 53
11
13
AVL Tree Example:
• Now insert 12 Right Left case – first rotate Right W.R.T 13 and
then Left rotate W.R.T 11
14
17
7
4 53
11
13
12
AVL Tree Example:
• Now insert 12
14
17
7
4 53
11
12
13
AVL Tree Example:
• Now the AVL tree is balanced.
14
17
7
4 53
12
13
11
AVL Tree Example:
• Now insert 8
14
17
7
4 53
12
13
11
8
AVL Tree Example:
• Now insert 8
14
17
7
4 53
11
12
8
13
AVL Tree Example:
• Now the AVL tree is balanced.
14
17
7
4
53
11
12
8 13
AVL Tree Example:
• Now remove 53
14
17
7
4
53
11
12
8 13
AVL Tree Example:
• Now remove 53, unbalanced
14
17
7
4
11
12
8 13
AVL Tree Example:
• Balanced! Remove 11
14
17
7
4
11
12
8
13
AVL Tree Example:
• Remove 11, replace it with the largest in its left branch
14
17
7
4
8
12
13
AVL Tree Example:
• Remove 8, unbalanced
14
17
4
7
12
13
AVL Tree Example:
• Remove 8, unbalanced
14
17
4
7
12
13
AVL Tree Example:
• Balanced!!
14
17
4
7
12
13
In Class Exercises
• Build an AVL tree with the following values:
15, 20, 24, 10, 13, 7, 30, 36, 25
15
15, 20, 24, 10, 13, 7, 30, 36, 25
20
24
15
20
24
10
13
15
20
24
13
10
13
20
24
15
10
13
20
24
15
10
15, 20, 24, 10, 13, 7, 30, 36, 25
7
13
20
24
15
10
7
30
36
13
20
30
15
10
7
36
24
13
20
30
15
10
7
36
24
15, 20, 24, 10, 13, 7, 30, 36, 25
25
13
20
30
15
10
7
36
24
25
13
24
36
20
10
7
25
30
15
Remove 24 and 20 from the AVL tree.
13
24
36
20
10
7
25
30
15
13
20
36
15
10
7
25
30
13
15
36
10
7
25
30
13
30
36
10
7
25
15

More Related Content

More from AntareepMajumder

FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...
AntareepMajumder
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
AntareepMajumder
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
AntareepMajumder
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...
AntareepMajumder
 
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...
AntareepMajumder
 

More from AntareepMajumder (6)

FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_25-07-2022_Mo...
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_08-08-2022_D...
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_24-08-2022_In...
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
 
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...
FALLSEM2022-23_BECE102L_TH_VL2022230104428_Reference_Material_I_04-08-2022_Mo...
 

Recently uploaded

Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
meharikiros2
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
pritamlangde
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 

Recently uploaded (20)

Query optimization and processing for advanced database systems
Query optimization and processing for advanced database systemsQuery optimization and processing for advanced database systems
Query optimization and processing for advanced database systems
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 

FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_II_19-10-2022_AVL_tree_animation_Example_Insertion_and_deletion.ppt

  • 1. Single Rotation to Fix Case 1(left- left) k2 violates An insertion in subtree X, AVL property violated at node k2 Solution: single rotation
  • 2. Single Rotation to Fix Case 4 (right- right) • Case 4 is a symmetric case to case 1 • Insertion takes O(Height of AVL Tree) time, Single rotation takes O(1) time An insertion in subtree Z k1 violates
  • 3. Single Rotation Fails to fix Case 2&3 • Single rotation fails to fix case 2&3 • Take case 2 as an example (case 3 is a symmetry to it ) – The problem is subtree Y is too deep – Single rotation doesn’t make it any less deep Single rotation result Case 2: violation in k2 because of insertion in subtree Y
  • 4. Double Rotation to Fix Case 2 (left-right) • Facts – The new key is inserted in the subtree B or C – The AVL-property is violated at k3 – k3-k1-k2 forms a zig-zag shape • Solution – We cannot leave k3 as the root – The only alternative is to place k2 as the new root Double rotation to fix case 2
  • 5. Double Rotation to fix Case 3(right-left) • Facts – The new key is inserted in the subtree B or C – The AVL-property is violated at k1 – k2-k3-k2 forms a zig-zag shape • Case 3 is a symmetric case to case 2 Double rotation to fix case 3
  • 6. AVL Tree Example: • Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree 14 17 11 7 53 4
  • 7. AVL Tree Example: • Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree 14 17 7 4 53 11 13
  • 8. AVL Tree Example: • Now insert 12 Right Left case – first rotate Right W.R.T 13 and then Left rotate W.R.T 11 14 17 7 4 53 11 13 12
  • 9. AVL Tree Example: • Now insert 12 14 17 7 4 53 11 12 13
  • 10. AVL Tree Example: • Now the AVL tree is balanced. 14 17 7 4 53 12 13 11
  • 11. AVL Tree Example: • Now insert 8 14 17 7 4 53 12 13 11 8
  • 12. AVL Tree Example: • Now insert 8 14 17 7 4 53 11 12 8 13
  • 13. AVL Tree Example: • Now the AVL tree is balanced. 14 17 7 4 53 11 12 8 13
  • 14. AVL Tree Example: • Now remove 53 14 17 7 4 53 11 12 8 13
  • 15. AVL Tree Example: • Now remove 53, unbalanced 14 17 7 4 11 12 8 13
  • 16. AVL Tree Example: • Balanced! Remove 11 14 17 7 4 11 12 8 13
  • 17. AVL Tree Example: • Remove 11, replace it with the largest in its left branch 14 17 7 4 8 12 13
  • 18. AVL Tree Example: • Remove 8, unbalanced 14 17 4 7 12 13
  • 19. AVL Tree Example: • Remove 8, unbalanced 14 17 4 7 12 13
  • 20. AVL Tree Example: • Balanced!! 14 17 4 7 12 13
  • 21. In Class Exercises • Build an AVL tree with the following values: 15, 20, 24, 10, 13, 7, 30, 36, 25
  • 22. 15 15, 20, 24, 10, 13, 7, 30, 36, 25 20 24 15 20 24 10 13 15 20 24 13 10 13 20 24 15 10
  • 23. 13 20 24 15 10 15, 20, 24, 10, 13, 7, 30, 36, 25 7 13 20 24 15 10 7 30 36 13 20 30 15 10 7 36 24
  • 24. 13 20 30 15 10 7 36 24 15, 20, 24, 10, 13, 7, 30, 36, 25 25 13 20 30 15 10 7 36 24 25 13 24 36 20 10 7 25 30 15
  • 25. Remove 24 and 20 from the AVL tree. 13 24 36 20 10 7 25 30 15 13 20 36 15 10 7 25 30 13 15 36 10 7 25 30 13 30 36 10 7 25 15