SlideShare a Scribd company logo
1 of 37
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
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
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

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

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

avl.ppt
avl.pptavl.ppt
avl.ppt
 
avl insertion-rotation
avl insertion-rotationavl insertion-rotation
avl insertion-rotation
 
Avl tree detailed
Avl tree detailedAvl tree detailed
Avl tree detailed
 
Lecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfLecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdf
 
Lecture3
Lecture3Lecture3
Lecture3
 
Avl tree
Avl treeAvl tree
Avl tree
 
Chapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptChapter 12 - Heaps.ppt
Chapter 12 - Heaps.ppt
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
AVL-TREE.ppt
AVL-TREE.pptAVL-TREE.ppt
AVL-TREE.ppt
 
Study about AVL Tree & Operations
Study about AVL Tree & OperationsStudy about AVL Tree & Operations
Study about AVL Tree & Operations
 
Ie
IeIe
Ie
 
Time complexity of avl tree
Time complexity of avl treeTime complexity of avl tree
Time complexity of avl tree
 
Avltrees 121106175039-phpapp01
Avltrees 121106175039-phpapp01Avltrees 121106175039-phpapp01
Avltrees 121106175039-phpapp01
 
Avl trees
Avl treesAvl trees
Avl trees
 
Review session2
Review session2Review session2
Review session2
 
9 chapter4 trees_avl
9 chapter4 trees_avl9 chapter4 trees_avl
9 chapter4 trees_avl
 
AVL_Trees using DSA concepts and how to do
AVL_Trees using DSA concepts and how to doAVL_Trees using DSA concepts and how to do
AVL_Trees using DSA concepts and how to do
 
4. avl
4. avl4. avl
4. avl
 
4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt4.10.AVLTrees[1].ppt
4.10.AVLTrees[1].ppt
 

Recently uploaded

(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
(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
 
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
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
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
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 

Recently uploaded (20)

(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
(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...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
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...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
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...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 

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
  • 20.
  • 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
  • 28.
  • 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: