AVL Trees CSE 373 Data Structures.

Slides:



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

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
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
CS202 - Fundamental Structures of Computer Science II
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
CSE 326: Data Structures AVL Trees
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
AVL Trees ITCS6114 Algorithms and Data Structures.
1 CSE 326: Data Structures Trees Lecture 7: Wednesday, Jan 23, 2003.
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 Joe Meehean.  BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height  We can balance an unbalanced.
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.
Binary trees -2 Chapter Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
Data Structures AVL Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
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:
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Balancing Binary Search Trees
UNIT III TREES.
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Balanced Binary Search Trees
Chapter 29 AVL Trees.
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Tree Mohammad Asad Abbasi Lecture 12
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Tree 27th Mar 2007.
Trees (Chapter 4) Binary Search Trees - Review Definition
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
Monday, April 16, 2018 Announcements… For Today…
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
AVL Trees CENG 213 Data Structures.
Friday, April 13, 2018 Announcements… For Today…
Instructor: Lilian de Greef Quarter: Summer 2017
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Balanced BSTs "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust CLRS, pages 333, 337.
CSE 332: Data Abstractions AVL Trees
Lecture No.20 Data Structures Dr. Sohail Aslam
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL 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
Lecture 10: BST and AVL Trees
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CSE 373 Data Structures Lecture 8
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Self-Balancing Search Trees
Presentation transcript:

AVL Trees CSE 373 Data Structures

Binary Search Tree - Best Time All BST operations are O(d), where d is tree depth minimum d is for a binary tree with N nodes What is the best case tree? What is the worst case tree? So, best case running time of BST operations is O(log N) 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Binary Search Tree - Worst Time Worst case running time is O(N) What happens when you Insert elements in ascending order? Insert: 2, 4, 6, 8, 10, 12 into an empty BST Problem: Lack of “balance”: compare depths of left and right subtree Unbalanced degenerate tree 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Balanced and unbalanced BST 1 4 2 2 5 3 1 3 4 4 Is this “balanced”? 5 2 6 6 1 3 5 7 7 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Approaches to balancing trees Don't balance May end up with some nodes very deep Strict balance The tree must always be balanced perfectly Pretty good balance Only allow a little out of balance Adjust on access Self-adjusting 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Balancing Binary Search Trees Many algorithms exist for keeping binary search trees balanced Adelson-Velskii and Landis (AVL) trees (height-balanced trees) Splay trees and other self-adjusting trees B-trees and other multiway search trees 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Perfect Balance Want a complete tree after every operation tree is full except possibly in the lower right This is expensive For example, insert 2 in the tree on the left and then rebuild as a complete tree 6 5 Insert 2 & complete tree 4 9 2 8 1 5 8 1 4 6 9 1/15/2019 CSE 373 - AU 05 -- AVL Trees

AVL - Good but not Perfect Balance AVL trees are height-balanced binary search trees Balance factor of a node height(left subtree) - height(right subtree) An AVL tree has balance factor calculated at every node For every node, heights of left and right subtree can differ by no more than 1 Store current heights in each node 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Height of an AVL Tree N(h) = minimum number of nodes in an AVL tree of height h. Basis N(0) = 1, N(1) = 2 Induction N(h) = N(h-1) + N(h-2) + 1 Solution (Fibonacci) N(h) > h (  1.62) h h-2 h-1 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Height of an AVL Tree N(h) > h (  1.62) Suppose we have n nodes in an AVL tree of height h. n > N(h) (because N(h) was the minimum) n > h hence log n > h (relatively well balanced tree!!) h < 1.44 log2n (i.e., O(height) takes O(logn)) 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Node Heights 6 6 4 9 4 9 1 5 1 5 8 Tree A (AVL) Tree B (AVL) height=3 balance factor=2-1=1 3 6 6 2 1 2 2 4 9 4 9 1 1 1 1 1 1 5 1 5 8 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Worst-case AVL Trees 4 nodes: h = 3 vs 3 7 nodes: h = 4 vs 3 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Node Heights after Insert 7 Tree A (AVL) Tree B (not AVL) balance factor 2-0 = 2 3 4 6 6 2 2 2 3 4 9 4 9 1 1 1 1 1 2 1 5 7 1 5 8 1 7 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Insert and Rotation in AVL Trees Insert operation may cause balance factor to become 2 or –2 for some node only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (the difference hleft-hright) is 2 or –2, adjust tree by rotation around the node 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Single Rotation in an AVL Tree 3 3 6 6 2 3 2 2 4 9 4 8 1 1 1 2 1 1 1 1 5 8 1 5 7 9 1 7 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Insertions in AVL Trees Let the node that needs rebalancing be . There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . 2. Insertion into right subtree of right child of . Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . 4. Insertion into left subtree of right child of . The rebalancing is performed through four separate rotation algorithms. 1/15/2019 CSE 373 - AU 05 -- AVL Trees

AVL Insertion: Outside Case f Consider a valid AVL subtree b h G h h A D 1/15/2019 CSE 373 - AU 05 -- AVL Trees

AVL Insertion: Outside Case f Inserting into A destroys the AVL property at node f b h G h+1 h D A 1/15/2019 CSE 373 - AU 05 -- AVL Trees

AVL Insertion: Outside Case f Do a “right rotation” b h G h+1 h D A 1/15/2019 CSE 373 - AU 05 -- AVL Trees

f b G D A Single right rotation Do a “right rotation” h h+1 h 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Outside Case Completed b “Right rotation” done! (“Left rotation” is mirror symmetric) f h+1 h h A G D AVL property has been restored! 1/15/2019 CSE 373 - AU 05 -- AVL Trees

AVL Insertion: Inside Case f Consider a valid AVL subtree b h G h h A D 1/15/2019 CSE 373 - AU 05 -- AVL Trees

AVL Insertion: Inside Case f Inserting into D destroys the AVL property at node f Does “right rotation” restore balance? b h G h h+1 A D 1/15/2019 CSE 373 - AU 05 -- AVL Trees

AVL Insertion: Inside Case b “Right rotation” does not restore balance… now b is out of balance f h A h h+1 G D 1/15/2019 CSE 373 - AU 05 -- AVL Trees

AVL Insertion: Inside Case f Consider the structure of subtree Y… b h G h h+1 A D 1/15/2019 CSE 373 - AU 05 -- AVL Trees

AVL Insertion: Inside Case f Y = node i and subtrees V and W b h G d h h+1 A h or h-1 C E 1/15/2019 CSE 373 - AU 05 -- AVL Trees

AVL Insertion: Inside Case f We will do a left-right “double rotation” . . . b G d A C E 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Double rotation : first rotation left rotation complete d G b E C A 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Double rotation : second rotation f Now do a right rotation d G b E C A 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Double rotation : second rotation right rotation complete Balance has been restored d b f h h h or h-1 C A E G 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Example of Insertions in an AVL Tree 3 20 Insert 5, 40 1 2 10 30 1 1 25 35 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Example of Insertions in an AVL Tree 3 4 20 20 2 2 2 3 10 30 10 30 1 1 1 2 1 1 5 25 35 5 25 35 1 40 Now Insert 45 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Single rotation (outside case) 5 4 20 20 2 4 2 3 10 30 10 30 1 3 1 1 1 2 5 25 35 5 25 40 1 1 35 45 40 2 Imbalance 1 45 Now Insert 34 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Double rotation (inside case) 5 4 20 20 4 4 2 3 10 30 10 35 1 3 3 1 2 2 5 Imbalance 25 40 5 30 40 1 25 34 2 1 45 35 45 1 Insertion of 34 1 34 1/15/2019 CSE 373 - AU 05 -- AVL Trees

AVL Tree Deletion Similar but more complex than insertion Rotations and double rotations needed to rebalance Imbalance may propagate upward so that many rotations may be needed. 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Pros and Cons of AVL Trees Arguments for AVL trees: Search is O(log N) since AVL trees are always balanced. Insertion and deletions are also O(logn) The height balancing adds no more than a constant factor to the speed of insertion. Arguments against using AVL trees: Difficult to program & debug; more space for balance factor. Asymptotically faster but rebalancing costs time. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). May be OK to have O(N) for a single operation if total run time for many consecutive operations is fast (e.g. Splay trees). 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Deletion 6 100 4 5 44 144 2 3 4 3 17 62 117 162 1 1 2 2 3 2 2 32 50 78 111 132 150 178 1 1 2 1 1 1 1 72 101 128 132 147 172 181 1 129 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Deletion 6 100 4 5 44 144 1 3 4 3 17 62 117 162 1 1 2 2 3 2 2 32 50 78 111 132 150 178 1 1 2 1 1 1 1 72 101 128 132 147 172 181 1 129 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Deletion 6 100 5 44 144 4 3 17 62 117 162 2 3 2 2 50 78 111 132 150 178 1 2 1 1 1 1 72 101 128 132 147 172 181 1 129 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Deletion 6 100 5 62 144 4 3 44 117 162 78 2 3 2 2 17 50 72 111 132 150 178 1 2 1 1 1 1 101 128 132 147 172 181 1 129 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Deletion 6 100 3 5 62 144 2 4 3 2 44 117 162 78 1 1 1 2 3 2 2 17 50 72 111 132 150 178 1 2 1 1 1 1 101 128 132 147 172 181 1 129 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Deletion 6 100 3 5 62 144 2 4 3 2 44 117 162 78 1 1 1 2 3 2 2 17 50 72 111 132 150 178 1 2 1 1 1 1 101 128 132 147 172 181 1 129 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Deletion 100 62 144 44 117 162 78 17 50 72 111 132 150 178 101 128 132 147 172 181 129 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Deletion 100 62 117 44 144 78 111 17 50 72 101 132 162 128 132 150 178 129 147 172 181 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Deletion 117 100 144 62 111 132 162 44 78 101 128 132 150 178 17 50 72 129 147 172 181 1/15/2019 CSE 373 - AU 05 -- AVL Trees

Deletion 5 117 4 4 100 144 3 2 3 3 62 111 132 162 2 2 1 2 1 2 2 44 78 101 128 132 150 178 1 1 1 1 1 1 1 17 50 72 129 147 172 181 1/15/2019 CSE 373 - AU 05 -- AVL Trees