AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.

Slides:



Advertisements
Similar presentations
AVL Trees CSE 373 Data Structures Lecture 8. 12/26/03AVL Trees - Lecture 82 Readings Reading ›Section 4.4,
Advertisements

1 AVL-Trees (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
CS202 - Fundamental Structures of Computer Science II
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
CSE 326: Data Structures AVL Trees
AVL Trees ITCS6114 Algorithms and Data Structures.
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
CSE332: Data Abstractions Lecture 7: AVL Trees
AVL Trees CSE, POSTECH.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Data Structures – LECTURE Balanced trees
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Balancing Binary Search Trees
CSE373: Data Structures & Algorithms
UNIT III TREES.
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
CS 201 Data Structures and Algorithms
Chapter 29 AVL Trees.
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Height Balanced Trees CPSC 335 Dr. Marina Gavrilova Computer Science
AVL Tree 27th Mar 2007.
Trees (Chapter 4) Binary Search Trees - Review Definition
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Monday, April 16, 2018 Announcements… For Today…
AVL Tree A Balanced Binary Search Tree
TCSS 342, Winter 2006 Lecture Notes
CSE 373 AVL trees read: Weiss Ch. 4, section
AVL Trees CENG 213 Data Structures.
Chapter 6 Transform and Conquer.
Instructor: Lilian de Greef Quarter: Summer 2017
CS202 - Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Copyright © Aiman Hanna All rights reserved
Data Structures & Algorithms
CSE 373: Data Structures and Algorithms
Balanced BSTs "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust CLRS, pages 333, 337.
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
AVL Search Tree put(9)
CSE 373 Data Structures and Algorithms
CE 221 Data Structures and Algorithms
CS202 - Fundamental Structures of Computer Science II
Lecture 9: Self Balancing Trees
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
Richard Anderson Spring 2016
CSE 373 Data Structures Lecture 8
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Self-Balancing Search Trees
Presentation transcript:

AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust

Binary Search Trees All the operations on a binary search tree cost 𝑂(ℎ), where ℎ is the height of the tree. These operations are efficient when ℎ= O(log 𝑛) . However, it may happen that ℎ=𝑂(𝑛). When the tree is entirely unbalanced. The cost of any operation is linear in the number of nodes and not logarithmic. Solution: Keep the tree balance to ensure operations always cost O(log n)! CS 321 - Data Structures

Measuring Tree Balance In a tree, the balance factor of a node x is defined as the difference in the heights of its two sub-trees: 𝐵𝑎𝑙𝑎𝑛𝑐𝑒𝐹𝑎𝑐𝑡𝑜𝑟 𝑥 =𝐻𝑒𝑖𝑔ℎ𝑡 𝑅𝑖𝑔ℎ𝑡 𝑥 −𝐻𝑒𝑖𝑔ℎ𝑡(𝐿𝑒𝑓𝑡 𝑥 ) Node height is the number of edges between a node and the furthest leaf in its sub-trees. Nodes with balance factors <0 are called "left-heavy." Nodes with balance factors >0 are called "right-heavy." Nodes with balance factors =0 are called "balanced." A balanced tree is a tree in which all of its nodes are balanced. CS 321 - Data Structures

Calculating Balance Factor For instance, the balance factor of J is: 𝐵𝑎𝑙𝑎𝑛𝑐𝑒𝐹𝑎𝑐𝑡𝑜𝑟 𝐽 =𝐻𝑒𝑖𝑔ℎ𝑡 𝑅𝑖𝑔ℎ𝑡 𝐽 −𝐻𝑒𝑖𝑔ℎ𝑡 𝐿𝑒𝑓𝑡 𝐽 𝐵𝑎𝑙𝑎𝑛𝑐𝑒𝐹𝑎𝑐𝑡𝑜𝑟 𝐽 =𝐻𝑒𝑖𝑔ℎ𝑡 𝑃 −𝐻𝑒𝑖𝑔ℎ𝑡 𝐹 𝐵𝑎𝑙𝑎𝑛𝑐𝑒𝐹𝑎𝑐𝑡𝑜𝑟 𝐽 =3−2 𝐵𝑎𝑙𝑎𝑛𝑐𝑒𝐹𝑎𝑐𝑡𝑜𝑟 𝐽 =+1 CS 321 - Data Structures

Balancing Trees Don’t balance Strict balance Good balance May end up with some nodes very deep Strict balance The tree must always be balanced perfectly Expensive must ensure tree complete after every modification Good balance Allow some imbalance Adjust on access Self-adjusting CS 321 - Data Structures

Balancing Binary Search Trees Ignoring balance leads to poor performance Maintaining perfect balance is too costly Many algorithms exist for keeping good balance Height-balanced trees AVL trees (Adelson-Velskii and Landis) Self-adjusting trees Splay trees Multiway search trees B-Trees CS 321 - Data Structures

AVL-Trees An AVL tree is a self-balancing, binary search tree. named after its inventors, Adelson-Velskii and Landis. first proposed dynamically balancing trees. not perfectly balanced, but each node has a balance factor of -1, 0, or 1. maintains 𝑂 log 𝑛 search, addition and deletion time. CS 321 - Data Structures

Unbalanced AVL-Trees Insertions and deletions may change the balance factor of nodes in a binary search tree For instance, given this deletion: 6 -1 6 -2 3 8 3 +1 Delete 8 2 5 2 5 +1 CS 321 - Data Structures

Unbalanced AVL-Trees or this insertion: 6 6 3 8 3 8 Insert 4 2 5 2 5 4 -1 6 -2 3 8 3 +1 8 Insert 4 2 5 2 5 +1 4 CS 321 - Data Structures

AVL-Tree Imbalance After an insertion or a deletion, one or more nodes can become too far out of balance (i.e. have balance factors of −2 𝑜𝑟 2). These imbalances can occur in four places: outside in the left sub-tree in the right sub-tree inside in the right sub-tree of the left sub-tree in the left sub-tree of the right sub-tree CS 321 - Data Structures

AVL-Tree Imbalances Outside: -Left sub-tree -Right sub-tree Note: T1, T2, and T3 represent arbitrary subtrees, which may be empty or may contain any number of nodes. CS 321 - Data Structures

AVL-Tree Imbalances Inside: - Right sub-tree of left sub-tree - Left sub-tree of right sub-tree CS 321 - Data Structures

AVL-Tree Rotation To rebalance an AVL-Tree, perform one or more tree rotations. There are 4 types of tree rotations, depending on where the imbalance occurs in the tree: Outside: LL-Rotation – right sub-tree’s, right sub-tree imbalance RR-Rotation – left sub-tree’s, left sub-tree imbalance Inside: LR-Rotation – left sub-tree’s, right sub-tree imbalance a LL-Rotation followed by a RR-Rotation RL-Rotation – right sub-tree’s, left-sub-tree imbalance a RR-Rotation followed by a LL-Rotation CS 321 - Data Structures

Key Idea If a node is out of balance in a given direction, rotate it in the opposite direction. rotation: A swap between a parent and its left or right child, maintaining proper BST ordering. 25 8 rotate right 8 3 25 3 11 11 CS 321 - Data Structures

RR-Rotations When insert a node into the left sub-tree of a left sub-tree, it may cause a sub-tree imbalance. To rebalance, perform a RR-Rotation in the clockwise direction. 𝑅𝑅 CS 321 - Data Structures

Example: RR-Rotation Below, when A is inserted into the left sub-tree of C’s left sub-tree, C has a -2 imbalance. We perform a RR-Rotation by making C the right-subtree of its left sub-tree, B. Note: If B had a right sub-tree, that sub-tree would become C’s left sub-tree (i.e. a second right rotation). -2 -1 CS 321 - Data Structures

LL-Rotations When insert a node into the right sub-tree of a right sub-tree, it may cause a sub-tree imbalance. To rebalance the tree, perform a LL-Rotation, in the counter-clockwise direction. 𝐿𝐿 CS 321 - Data Structures

Example: LL-Rotation Below, when C is inserted into the right sub-tree of A’s right sub-tree, A has a +2 imbalance. Perform a left rotation by making A the left-subtree of its right sub-tree, B. Note: If B had a left sub-tree, that sub-tree would become A’s right sub-tree (i.e a second left rotation). +2 +1 CS 321 - Data Structures

LR-Rotations When insert a node into the right sub-tree of a left sub-tree, it may cause a sub-tree imbalance. To rebalance, perform a LR-Rotation. Inside rotations are combinations of the outside rotations. A LR-Rotation is the combination of a LL-Rotation and a RR-Rotation. 𝐿𝑅 CS 321 - Data Structures

Example: LR-Rotation -2 For example, when B is inserted into the right sub-tree of C’s left sub-tree, C has a -2 imbalance. +1 Perform a LL-Rotation on the left sub-tree of C. This makes A the left sub-tree of B. Note: If B had a left sub-tree, it would become A’s right sub-tree. CS 321 - Data Structures

Example: LR-Rotation -2 The sub-tree is still unbalanced. However, it’s now because of the left sub-tree of the left sub-tree. -1 Perform a RR-Rotation, making B the new root node of this sub-tree. C becomes the right sub-tree of its left sub-tree, B. Note: If B had a right sub-tree, it would become C’s left sub-tree. The sub-tree is now balanced. CS 321 - Data Structures

RL-Rotations When insert a node into the left sub-tree of a right sub-tree, it may cause a sub-tree imbalance. To rebalance, perform a RL-Rotation. A RL-Rotation is the combination of a RR-Rotation a LL-Rotation. 𝑅𝐿 CS 321 - Data Structures

Example: RL-Rotation +2 For example, when B is inserted into the left sub-tree of A’s right sub-tree, B has a +2 imbalance. -1 Perform a RR-Rotation on the right sub-tree of A. This makes C the right sub-tree of B. Note: If B had a right sub-tree, it would become C’s left sub-tree. CS 321 - Data Structures

Example: RL-Rotation +2 The sub-tree is still unbalanced. However, it’s now because of the right sub-tree of the right sub-tree. -1 Perform LL-Rotation, making B the new root of the sub-tree. A becomes the left sub-tree of its right sub-tree B. Note: If B had a left sub-tree, it would become A’s right sub-tree. The sub-tree is now balanced. CS 321 - Data Structures

AVL-Tree Insertion Insert the new node into the tree, like insertion into a binary search tree Fix tree balance via rotations, if necessary A rotation is performed in sub-trees with a root that has a balance factor equal to +2 or -2. If there is more than one imbalance, first rotate at the node closest to the newly inserted node. CS 321 - Data Structures

Insert 15 in the following AVL-Tree: Example: Insertion I Insert 15 in the following AVL-Tree: 10 20 5 12 4 7 8 2 -1 +1 CS 321 - Data Structures

Insertion induces left, right imbalance Example: Insertion I Insertion induces left, right imbalance 10 20 5 12 4 7 8 2 -1 -2 +1 15 CS 321 - Data Structures

Example: Insertion I Which rotation use? CS 321 - Data Structures

After apply LR-Rotation at node 20 Example: Insertion I After apply LR-Rotation at node 20 10 15 5 12 4 7 8 2 -1 +1 20 CS 321 - Data Structures

Insert 9 in the following AVL-Tree: Example: Insertion II Insert 9 in the following AVL-Tree: 10 20 5 12 4 7 8 2 -1 +1 CS 321 - Data Structures

Example: Insertion II Insertion induces left, right imbalance 10 20 5 12 4 7 8 2 -2 -1 +1 +2 We rotate at 7, closest node to 9 9 and a right, right imbalance CS 321 - Data Structures

Example: Insertion II Which rotation use? CS 321 - Data Structures

After apply LL-Rotation at node 7 Example: Insertion II 10 20 5 12 4 8 9 2 -1 7 After apply LL-Rotation at node 7 CS 321 - Data Structures

AVL-Trees Deletion Delete node as in other binary search trees Fix tree balance via rotations, if necessary A rotation is performed in sub-trees having a root that has a balance factor equal to +2 or -2. If there is more than one imbalance, first rotate at the node closest to the deleted node. CS 321 - Data Structures

Delete 7 from the following AVL-Tree: Example: Deletion I Delete 7 from the following AVL-Tree: 10 20 5 12 4 7 8 2 -1 +1 CS 321 - Data Structures

Example: Deletion I 10 20 5 12 4 8 2 No need to rotate! -1 No need to rotate! After deleting 7 CS 321 - Data Structures

Delete 10 from the following AVL-Tree: Example: Deletion II Delete 10 from the following AVL-Tree: 10 20 5 12 4 7 8 2 -1 +1 CS 321 - Data Structures

Example: Deletion II After deleting 10, promote its successor, 12 Could also promote its predecessor, 5 12 20 5 4 7 8 2 -2 -1 +1 CS 321 - Data Structures

Example: Deletion II Deletion induces left, left imbalance Choose to use RR-Rotation but could use a LR-Rotation too. 12 20 5 4 7 8 2 -2 -1 +1 and a left, right imbalance! CS 321 - Data Structures

Example: Deletion II Which rotation use? CS 321 - Data Structures

After apply RR-Rotation at node 12 Example: Deletion II After apply RR-Rotation at node 12 5 12 4 2 20 8 +1 -1 +1 7 CS 321 - Data Structures

Delete 4 from the following AVL-Tree: Example: Deletion III Delete 4 from the following AVL-Tree: 10 20 5 12 4 7 6 -1 +1 CS 321 - Data Structures

Deletion induces right, left imbalance Example: Deletion III Deletion induces right, left imbalance 10 20 5 12 7 6 -1 +2 CS 321 - Data Structures

Example: Deletion II Which rotation use? CS 321 - Data Structures

After apply RL-Rotation at node 5 Example: Deletion III 10 20 6 12 5 7 -1 After apply RL-Rotation at node 5 CS 321 - Data Structures

Height of AVL-Trees Maximum number of nodes in a full AVL-tree: 𝑛 ≤ 2 ℎ+1 −1 Then, ℎ ≥ log 2 𝑛+1 −1 = Ω( log 𝑛 ) CS 321 - Data Structures

Minimum Number of Nodes Minimum number of nodes in the AVL-tree: nh: minimum number of nodes when the height is h CS 321 - Data Structures

Minimum Number of Nodes Minimum number of nodes in the AVL-tree: Recall the Fibonacci numbers nh = Fh + 1 CS 321 - Data Structures

Height of AVL-Trees Minimum number of nodes in the AVL-tree: 𝑛 ℎ = 𝐹 ℎ +1⟹ 𝑛 ℎ > 𝐹 ℎ 𝐹 ℎ is the closest integer to 𝜙 ℎ 5 , where 𝜙=1.618 Then, 𝐹 ℎ ≈ 𝜙 ℎ 5 and 𝑛 ℎ > 𝜙 ℎ 5 , then ℎ< log 𝜙 5 + log 𝜙 𝑛 =𝑂( log 𝑛 ) Since h is Ω( log 𝑛 )and 𝑂 log 𝑛 , h is Θ( log 𝑛 ). CS 321 - Data Structures

Disadvantages of AVL-Trees Difficult to program and debug Asymptotically faster than other self-adjusting trees but rebalancing takes time. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). A 𝑂(𝑛) runtime for a single operation may be justified, if total run time for many consecutive operations is fast (e.g. Splay trees). CS 321 - Data Structures

Advantages of AVL-Trees Since AVL-Trees are always close to balanced, search is 𝑂( log 𝑛 ). Insertion and deletion are also 𝑂( log 𝑛 ). For both insertion and deletion, height re-balancing adds no more than a constant factor to the runtime. CS 321 - Data Structures

CS 321 - Data Structures

AVL Insertion: Outside Case j Consider a valid AVL subtree k h Z h h X Y CS 321 - Data Structures

AVL Insertion: Outside Case j Inserting into X destroys the AVL property at node j k h Z h+1 h Y X CS 321 - Data Structures

AVL Insertion: Outside Case j Do a “right rotation” k h Z h+1 h Y X CS 321 - Data Structures

j k Z Y X Single right rotation Do a “right rotation” CS 321 - Data Structures

Outside Case Completed k “Right rotation” done! (“Left rotation” is mirror symmetric) j h+1 h h X Z Y AVL property has been restored! CS 321 - Data Structures

AVL Insertion: Inside Case j Consider a valid AVL subtree k h Z h h X Y CS 321 - Data Structures

AVL Insertion: Inside Case j Inserting into Y destroys the AVL property at node j Does “right rotation” restore balance? k h Z h h+1 X Y CS 321 - Data Structures

AVL Insertion: Inside Case k “Right rotation” does not restore balance… now k is out of balance j h X h h+1 Z Y CS 321 - Data Structures

AVL Insertion: Inside Case j Consider the structure of subtree Y… k h Z h h+1 X Y CS 321 - Data Structures

AVL Insertion: Inside Case j Y = node i and subtrees V and W k h Z i h h+1 X h or h-1 V W CS 321 - Data Structures

AVL Insertion: Inside Case j We will do a left-right “double rotation” . . . k Z i X V W CS 321 - Data Structures

Double rotation : first rotation j left rotation complete i Z k W V X CS 321 - Data Structures

Double rotation : second rotation j Now do a right rotation i Z k W V X CS 321 - Data Structures

Double rotation : second rotation right rotation complete Balance has been restored i k j h h h or h-1 V X W Z CS 321 - Data Structures

Implementation CS 321 - Data Structures balance (1,0,-1) key left right No need to keep the height; just the difference in height, i.e. the balance factor; this has to be modified on the path of insertion even if you don’t perform rotations Once you have performed a rotation (single or double) you won’t need to go back up the tree CS 321 - Data Structures