Self-Balancing Search Trees

Slides:



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

Lecture 9 : Balanced Search Trees Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
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.
AVL Tree Rotations Daniel Box. Binary Search Trees A binary search tree is a tree created so that all of the items in the left subtree of a node are less.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
CSE 326: Data Structures AVL Trees
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
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.
AVL Trees ITCS6114 Algorithms and Data Structures.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
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.
SELF-BALANCING SEARCH TREES Chapter 9. Self-Balancing Search Trees  The performance of a binary search tree is proportional to the height of the tree.
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.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
CSE332: Data Abstractions Lecture 7: AVL Trees
COMP261 Lecture 23 B Trees.
AA Trees.
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.
Red Black Trees Colored Nodes Definition Binary search tree.
Multiway Search Trees Data may not fit into main memory
Search Trees.
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
CSIT 402 Data Structures II
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
Chapter 29 AVL Trees.
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
CSE373: Data Structures & Algorithms
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
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.
Lecture 25 Splay Tree Chapter 10 of textbook
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…
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Self-Balancing Search Trees
Wednesday, April 18, 2018 Announcements… For Today…
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.
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
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
CS223 Advanced 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.
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
Self-Balancing Search Trees
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
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
326 Lecture 9 Henry Kautz Winter Quarter 2002
Red Black Trees Colored Nodes Definition Binary search tree.
Self-Balancing Search Trees
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
CS 261 – Data Structures AVL Trees.
Presentation transcript:

Self-Balancing Search Trees Chapter 11 Self-Balancing Search Trees Chapter 11

Which STL Container to Use?

Self-Balancing Search Trees Chapter 11 Self-Balancing Search Trees 11.1, pgs. 624-628

4 Critically Unbalanced Trees Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child

AVL Tree Example Build an AVL tree from the words: Self-Balancing Search Trees Build an AVL tree from the words: "The quick brown fox jumps over the lazy dog"

AVL Tree Example The Insert The Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The Insert The

AVL Tree Example The +1 quick Insert quick Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The +1 Insert quick quick

AVL Tree Example The +2 -1 quick brown Insert brown Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The +2 -1 Insert brown quick brown The overall tree is right-heavy (Right-Left) 1. Rotate right around the child (quick)

AVL Tree Example The brown +2 +1 quick Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child The brown +2 +1 quick Rotate right around the child (quick) Rotate left around the parent (The)

AVL Tree Example brown quick The Insert fox Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The Insert fox

AVL Tree Example +1 brown The quick fox Insert fox Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +1 brown Insert fox The quick fox

AVL Tree Example brown quick The +1 fox Insert jumps Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The +1 Insert jumps fox

The tree is now left-heavy about quick (Left-Right case) AVL Tree Example Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +2 -2 +1 brown Insert jumps The quick fox The tree is now left-heavy about quick (Left-Right case) jumps

AVL Tree Example brown quick The +2 -2 fox +1 jumps Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The +2 -2 fox +1 Rotate left around the child (fox) jumps

AVL Tree Example brown quick The +2 -2 jumps -1 fox Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown quick The +2 -2 jumps -1 Rotate left around the child fox Rotate right around the parent

AVL Tree Example brown jumps The +1 fox quick Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown jumps The +1 fox quick

We now have a Right-Right imbalance AVL Tree Example Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +2 +1 -1 brown Insert over The jumps fox quick We now have a Right-Right imbalance over

1. Rotate left around the parent AVL Tree Example Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child brown jumps The +2 +1 fox quick -1 1. Rotate left around the parent over

AVL Tree Example jumps quick brown -1 The fox over Insert the Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps quick brown -1 The fox over Insert the

AVL Tree Example jumps brown quick The fox over the Insert the Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps Insert the brown quick The fox over the

AVL Tree Example jumps quick brown The fox over the Insert lazy Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps quick brown The fox over the Insert lazy

AVL Tree Example +1 -1 jumps brown quick The fox over the lazy Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child +1 -1 jumps Insert lazy brown quick The fox over the lazy

AVL Tree Example jumps quick brown +1 -1 The fox over the lazy Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child jumps quick brown +1 -1 The fox over the lazy Insert dog

AVL Tree Example -1 +1 jumps brown quick The fox over the dog lazy Self-Balancing Search Trees Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child -1 +1 jumps Insert dog brown quick The fox over the dog lazy

Insert the following into an AVL tree: 14, 17, 11, 7, 53, 4, 13, 12, 8 Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child

Insert the following into an AVL tree: 14, 17, 11, 7, 53, 4, 13, 12, 8 Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent Left-Right (parent balance -2, left child balance +1) Rotate left around child Right-Right (parent balance +2, right child balance +1) Rotate left around parent Right-Left (parent balance +2, right child balance -1) Rotate right around child

Implementing an AVL Tree Self-Balancing Search Trees

Performance of the AVL Tree Self-Balancing Search Trees Since each subtree is kept close to balanced, the AVL has expected O(log n). Each subtree is allowed to be out of balance ±1 so the tree may contain some holes. In the worst case (which is rare) an AVL tree can be 1.44 times the height of a full binary tree that contains the same number of items. Ignoring constants, this still yields O(log n) performance. Empirical tests show that on average log2.n + 0.25 comparisons are required to insert the nth item into an AVL tree – close to insertion into a corresponding complete binary search tree.

Pros and Cons of AVL Trees Self-Balancing Search Trees

Pros and Cons of AVL Trees Self-Balancing Search Trees Argument for AVL trees: Search is O(log n) since AVL trees are always balanced. Insertion and deletions are also O(log n). 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).