SlideShare a Scribd company logo
AVL TREES
AVL TREES
(Adelson-Velskii and Landis 1962)
 AVL tree is a self-balancing Binary Search Tree (BST) where the
difference between heights of left and right subtrees cannot be
more than one for all nodes.
 Key idea: if insertion or deletion get the tree out of balance then fix
it immediately
 All operations insert, delete,… can be done on an AVL tree with N
nodes in O(log N) time (average and worst case!)
Example:
An example of an AVL tree where the
heights are shown next to the nodes:
88
44
17 78
32 50
48 62
1
3
0
0
1
2
0
0
Insertion in an AVL Tree
 Insertion is as in a binary search tree (always done by expanding
an external node)
Insertion in an AVL Tree
 Insertion is as in a binary search tree (always done by expanding
an external node)
Example:
44
17 78
32 50 88
48 62
Insertion in an AVL Tree
 Insertion is as in a binary search tree (always done by expanding
an external node)
Example:
Insert node 54
44
17 78
32 50 88
48 62
Insertion in an AVL Tree
 Insertion is as in a binary search tree (always done by expanding
an external node)
Example:
44
17 78
32 50 88
48 62
54
Insert node 54
44
17 78
32 50 88
48 62
Insertion in an AVL Tree
 Insertion is as in a binary search tree (always done by expanding
an external node)
Example:
Insert node 54
2
44
17 78
32 50 88
48 62
54
44
17 78
32 50 88
48 62
Insertion in an AVL Tree
 Insertion is as in a binary search tree (always done by expanding
an external node)
Example:
Insert node 54
2
1
44
17 78
32 50 88
48 62
54
44
17 78
32 50 88
48 62
Insertion in an AVL Tree
 Insertion is as in a binary search tree (always done by expanding
an external node)
Example:
Insert node 54
2
1 0
44
17 78
32 50 88
48 62
54
44
17 78
32 50 88
48 62
Insertion in an AVL Tree
 Insertion is as in a binary search tree (always done by expanding
an external node)
Example:
Insert node 54
2
1 0
44
17 78
32 50 88
48 62
54
44
17 78
32 50 88
48 62
Unbalanced!!
How does the AVL tree work?
 First, insert the new key as a new leaf just as in
ordinary binary search tree
 Then trace the path from the new leaf towards the
root. For each node x encountered, check if heights
of left(x) and right(x) differ by at most 1.
 If yes, proceed to parent(x). If not, restructure by
doing either a single rotation or a double rotation
[next slide].
 For insertion, once we perform a rotation at a node x,
we won’t need to perform any rotation at any
ancestor of x.
Rotations
 Two types of rotations
 Single rotations
 two nodes are “rotated”
 Double rotations
 three nodes are “rotated”
Single Rotation (Right)
Rotate x with left child y
(pay attention to the resulting sub-trees positions)
Single Rotation (Left)
Rotate x with right child y
(pay attention to the resulting sub-trees positions)
Single Rotation - Example
Tree is an AVL tree by definition.
h
h+1
Example
h
h+2
Node 02 added
Tree violates the AVL definition!
Perform rotation.
Tree has this form.
h
h
h+1
A
B
C
x
y
Example
Example – After Rotation
Tree has this form.
A
B C
x
y
Design data Analysis Avl Trees.pptx by piyush sir
Single Rotation
 Sometimes a single rotation fails to solve the problem
k2
k1
X
Y
Z
k1
X
Y
Z
k2
h+2
h
h
h+2
 In such cases, we need to use a double-rotation
Double Rotations
Double Rotations
Double Rotation - Example
Tree is an AVL tree by definition.
h
h+1
Delete node 94
Example
AVL tree is violated.
h
h+2
Tree has this form.
B1 B2
C
A
x
y
z
Example
A B1 B2 C
x
y
z
Tree has this form
After Double Rotation
Design data Analysis Avl Trees.pptx by piyush sir
Insertion
 The time complexity to perform a rotation is O(1)
 The time complexity to find a node that violates the
AVL property is dependent on the height of the tree,
which is log(N)
Deletion
 Perform normal BST deletion
 Perform exactly the same checking as for insertion to
restore the tree property
Summary AVL Trees
 Maintains a Balanced Tree
 Modifies the insertion and deletion routine
 Performs single or double rotations to restore structure
 Guarantees that the height of the tree is O(logn)
 The guarantee directly implies that functions find(), min(), and
max() will be performed in O(logn)
Summary AVL Trees
 Requires a little more work for insertion and
deletion
 But, since trees are mostly used for searching
 More work for insert and delete is worth the performance
gain for searching
Self-adjusting Structures
Consider the following AVL Tree
44
17 78
32 50 88
48 62
Self-adjusting Structures
Consider the following AVL Tree
44
17 78
32 50 88
48 62
Suppose we want to search for the following sequence of
elements: 48, 48, 48, 48, 50, 50, 50, 50, 50.
Self-adjusting Structures
Consider the following AVL Tree
44
17 78
32 50 88
48 62
Suppose we want to search for the following sequence of
elements: 48, 48, 48, 48, 50, 50, 50, 50, 50.
In this case,
is this a good structure?
Self-adjusting Structures
Splay Trees (Tarjan and Sleator 1985)
 Binary search tree.
 Every accessed node is brought to the root
 Adapt to the access probability distribution
Practice time!
A)
50
10
80
30
60
10
30
42
80
50
60
10
30
50
80
42
60
10
30
50
80
42
60
42
10
30
80
60
50
B) C) D)
Which of the
following is the
updated AVL
tree after
inserting 42?
Starting with
this AVL tree:

More Related Content

PDF
data structure AVL TREES chapter slides for learning about AVL trees
PDF
Avl tree detailed
PPTX
AVL Tree.pptx
PDF
Avl trees
PPT
Presentation1 data structure for CSE.ppt
PPT
DSA Leactrure # 2! Explaining abstract AVL trees
data structure AVL TREES chapter slides for learning about AVL trees
Avl tree detailed
AVL Tree.pptx
Avl trees
Presentation1 data structure for CSE.ppt
DSA Leactrure # 2! Explaining abstract AVL trees

Similar to Design data Analysis Avl Trees.pptx by piyush sir (20)

PPTX
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
PPTX
Presentation_30219_Content_Document_20250107125144AM.pptx
PPTX
Avl trees final
PDF
Lecture 10 - AVL Trees.pdf
PPTX
Avl trees
PPTX
power point presentation on the avl trees
PPT
avl.ppt
PPT
avl.ppt
PPT
AVL TREE java dalam penerapannya dan aplikasinya
PPTX
ADSPPT.pptx advanced data structures presentation
PPT
AVL-TREE.ppt
PPTX
Adelson velskii Landis rotations based on
PPT
AVL_Trees.ppt
PPTX
Data structures trees and graphs - AVL tree.pptx
PPTX
From_Binary_Trees_to_AVL_Trees_Presentation.pptx
PPT
Avl tree
PPT
Avltrees
PPTX
AVL tree PPT.pptx
PPT
AVL Tree.ppt
PPTX
Lecture3
AVL Trees.pptx DATA STRUCTURES AND ALGORITHMS
Presentation_30219_Content_Document_20250107125144AM.pptx
Avl trees final
Lecture 10 - AVL Trees.pdf
Avl trees
power point presentation on the avl trees
avl.ppt
avl.ppt
AVL TREE java dalam penerapannya dan aplikasinya
ADSPPT.pptx advanced data structures presentation
AVL-TREE.ppt
Adelson velskii Landis rotations based on
AVL_Trees.ppt
Data structures trees and graphs - AVL tree.pptx
From_Binary_Trees_to_AVL_Trees_Presentation.pptx
Avl tree
Avltrees
AVL tree PPT.pptx
AVL Tree.ppt
Lecture3
Ad

Recently uploaded (20)

PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
OOP with Java - Java Introduction (Basics)
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Sustainable Sites - Green Building Construction
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Foundation to blockchain - A guide to Blockchain Tech
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
OOP with Java - Java Introduction (Basics)
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Mechanical Engineering MATERIALS Selection
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
Sustainable Sites - Green Building Construction
CYBER-CRIMES AND SECURITY A guide to understanding
UNIT 4 Total Quality Management .pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Ad

Design data Analysis Avl Trees.pptx by piyush sir

  • 2. AVL TREES (Adelson-Velskii and Landis 1962)  AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes.  Key idea: if insertion or deletion get the tree out of balance then fix it immediately  All operations insert, delete,… can be done on an AVL tree with N nodes in O(log N) time (average and worst case!)
  • 3. Example: An example of an AVL tree where the heights are shown next to the nodes: 88 44 17 78 32 50 48 62 1 3 0 0 1 2 0 0
  • 4. Insertion in an AVL Tree  Insertion is as in a binary search tree (always done by expanding an external node)
  • 5. Insertion in an AVL Tree  Insertion is as in a binary search tree (always done by expanding an external node) Example: 44 17 78 32 50 88 48 62
  • 6. Insertion in an AVL Tree  Insertion is as in a binary search tree (always done by expanding an external node) Example: Insert node 54 44 17 78 32 50 88 48 62
  • 7. Insertion in an AVL Tree  Insertion is as in a binary search tree (always done by expanding an external node) Example: 44 17 78 32 50 88 48 62 54 Insert node 54 44 17 78 32 50 88 48 62
  • 8. Insertion in an AVL Tree  Insertion is as in a binary search tree (always done by expanding an external node) Example: Insert node 54 2 44 17 78 32 50 88 48 62 54 44 17 78 32 50 88 48 62
  • 9. Insertion in an AVL Tree  Insertion is as in a binary search tree (always done by expanding an external node) Example: Insert node 54 2 1 44 17 78 32 50 88 48 62 54 44 17 78 32 50 88 48 62
  • 10. Insertion in an AVL Tree  Insertion is as in a binary search tree (always done by expanding an external node) Example: Insert node 54 2 1 0 44 17 78 32 50 88 48 62 54 44 17 78 32 50 88 48 62
  • 11. Insertion in an AVL Tree  Insertion is as in a binary search tree (always done by expanding an external node) Example: Insert node 54 2 1 0 44 17 78 32 50 88 48 62 54 44 17 78 32 50 88 48 62 Unbalanced!!
  • 12. How does the AVL tree work?  First, insert the new key as a new leaf just as in ordinary binary search tree  Then trace the path from the new leaf towards the root. For each node x encountered, check if heights of left(x) and right(x) differ by at most 1.  If yes, proceed to parent(x). If not, restructure by doing either a single rotation or a double rotation [next slide].  For insertion, once we perform a rotation at a node x, we won’t need to perform any rotation at any ancestor of x.
  • 13. Rotations  Two types of rotations  Single rotations  two nodes are “rotated”  Double rotations  three nodes are “rotated”
  • 14. Single Rotation (Right) Rotate x with left child y (pay attention to the resulting sub-trees positions)
  • 15. Single Rotation (Left) Rotate x with right child y (pay attention to the resulting sub-trees positions)
  • 16. Single Rotation - Example Tree is an AVL tree by definition. h h+1
  • 17. Example h h+2 Node 02 added Tree violates the AVL definition! Perform rotation.
  • 18. Tree has this form. h h h+1 A B C x y Example
  • 19. Example – After Rotation Tree has this form. A B C x y
  • 21. Single Rotation  Sometimes a single rotation fails to solve the problem k2 k1 X Y Z k1 X Y Z k2 h+2 h h h+2  In such cases, we need to use a double-rotation
  • 24. Double Rotation - Example Tree is an AVL tree by definition. h h+1 Delete node 94
  • 25. Example AVL tree is violated. h h+2
  • 26. Tree has this form. B1 B2 C A x y z Example
  • 27. A B1 B2 C x y z Tree has this form After Double Rotation
  • 29. Insertion  The time complexity to perform a rotation is O(1)  The time complexity to find a node that violates the AVL property is dependent on the height of the tree, which is log(N)
  • 30. Deletion  Perform normal BST deletion  Perform exactly the same checking as for insertion to restore the tree property
  • 31. Summary AVL Trees  Maintains a Balanced Tree  Modifies the insertion and deletion routine  Performs single or double rotations to restore structure  Guarantees that the height of the tree is O(logn)  The guarantee directly implies that functions find(), min(), and max() will be performed in O(logn)
  • 32. Summary AVL Trees  Requires a little more work for insertion and deletion  But, since trees are mostly used for searching  More work for insert and delete is worth the performance gain for searching
  • 33. Self-adjusting Structures Consider the following AVL Tree 44 17 78 32 50 88 48 62
  • 34. Self-adjusting Structures Consider the following AVL Tree 44 17 78 32 50 88 48 62 Suppose we want to search for the following sequence of elements: 48, 48, 48, 48, 50, 50, 50, 50, 50.
  • 35. Self-adjusting Structures Consider the following AVL Tree 44 17 78 32 50 88 48 62 Suppose we want to search for the following sequence of elements: 48, 48, 48, 48, 50, 50, 50, 50, 50. In this case, is this a good structure?
  • 36. Self-adjusting Structures Splay Trees (Tarjan and Sleator 1985)  Binary search tree.  Every accessed node is brought to the root  Adapt to the access probability distribution
  • 37. Practice time! A) 50 10 80 30 60 10 30 42 80 50 60 10 30 50 80 42 60 10 30 50 80 42 60 42 10 30 80 60 50 B) C) D) Which of the following is the updated AVL tree after inserting 42? Starting with this AVL tree: