Ordered Dictionary & Binary Search Tree

Similar documents
Weight-balanced Binary Search Trees

Weight-balanced Binary Search Trees

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17

CS 240 Data Structures and Data Management. Module 4: Dictionaries

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

Data Structures and Algorithms " Search Trees!!

AVL Trees. Manolis Koubarakis. Data Structures and Programming Techniques

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM

CS 310: AVL Trees. Chris Kauffman. Week 13-1

CS Data Structures and Algorithm Analysis

16. Binary Search Trees

16. Binary Search Trees

Search Trees. EECS 2011 Prof. J. Elder Last Updated: 24 March 2015

AVL Trees. Properties Insertion. October 17, 2017 Cinda Heeren / Geoffrey Tien 1

Advanced Implementations of Tables: Balanced Search Trees and Hashing

Binary Search Trees. Motivation

Fundamental Algorithms

Splay Trees. CMSC 420: Lecture 8

Graphs and Trees Binary Search Trees AVL-Trees (a,b)-trees Splay-Trees. Search Trees. Tobias Lieber. April 14, 2008

searching algorithms

Dictionary: an abstract data type

Binary Search Trees. Lecture 29 Section Robb T. Koether. Hampden-Sydney College. Fri, Apr 8, 2016

past balancing schemes require maintenance of balance info at all times, are aggresive use work of searches to pay for work of rebalancing

Dictionary: an abstract data type

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

Mon Tue Wed Thurs Fri

Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures

8: Splay Trees. Move-to-Front Heuristic. Splaying. Splay(12) CSE326 Spring April 16, Search for Pebbles: Move found item to front of list

Assignment 5: Solutions

Average Case Analysis of QuickSort and Insertion Tree Height using Incompressibility

The null-pointers in a binary search tree are replaced by pointers to special null-vertices, that do not carry any object-data

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

Fundamental Algorithms

AVL trees. AVL trees

Each internal node v with d(v) children stores d 1 keys. k i 1 < key in i-th sub-tree k i, where we use k 0 = and k d =.

Lecture VI AMORTIZATION

INF2220: algorithms and data structures Series 1

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

7 Dictionary. EADS c Ernst Mayr, Harald Räcke 109

6.854 Advanced Algorithms

Inge Li Gørtz. Thank you to Kevin Wayne for inspiration to slides

Lower Bounds for Dynamic Connectivity (2004; Pǎtraşcu, Demaine)

Lecture 5: Splay Trees

Problem. Maintain the above set so as to support certain

Chapter 10 Search Structures

Lecture 18 April 26, 2012

Data Structures and and Algorithm Xiaoqing Zheng

A Simple Implementation Technique for Priority Search Queues

Alpha-Beta Pruning: Algorithm and Analysis

7.3 AVL-Trees. Definition 15. Lemma 16. AVL-trees are binary search trees that fulfill the following balance condition.

Data Structure. Mohsen Arab. January 13, Yazd University. Mohsen Arab (Yazd University ) Data Structure January 13, / 86

Review Of Topics. Review: Induction

Algorithm for exact evaluation of bivariate two-sample Kolmogorov-Smirnov statistics in O(nlogn) time.

Part III. Data Structures. Abstract Data Type. Dynamic Set Operations. Dynamic Set Operations

What is SSA? each assignment to a variable is given a unique name all of the uses reached by that assignment are renamed

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

7 Dictionary. 7.1 Binary Search Trees. Binary Search Trees: Searching. 7.1 Binary Search Trees

Lecture 5: Fusion Trees (ctd.) Johannes Fischer

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

7 Dictionary. Ernst Mayr, Harald Räcke 126

CS 151. Red Black Trees & Structural Induction. Thursday, November 1, 12

Outline. Computer Science 331. Cost of Binary Search Tree Operations. Bounds on Height: Worst- and Average-Case

Alpha-Beta Pruning: Algorithm and Analysis

Design and Analysis of Algorithms

Problem 5. Use mathematical induction to show that when n is an exact power of two, the solution of the recurrence

CS60007 Algorithm Design and Analysis 2018 Assignment 1

Data selection. Lower complexity bound for sorting

Alpha-Beta Pruning: Algorithm and Analysis

Functional Data Structures

25. Minimum Spanning Trees

25. Minimum Spanning Trees

7 Dictionary. Harald Räcke 125

Nearest Neighbor Search with Keywords

Fixed-Universe Successor : Van Emde Boas. Lecture 18

Binary Search Trees. Dictionary Operations: Additional operations: find(key) insert(key, value) erase(key)

Binary Decision Diagrams. Graphs. Boolean Functions

Part IA Algorithms Notes

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

The Complexity of Constructing Evolutionary Trees Using Experiments

Disjoint Sets : ADT. Disjoint-Set : Find-Set

Chapter 5 Arrays and Strings 5.1 Arrays as abstract data types 5.2 Contiguous representations of arrays 5.3 Sparse arrays 5.4 Representations of

Biased Quantiles. Flip Korn Graham Cormode S. Muthukrishnan

Heaps and Priority Queues

Wolf-Tilo Balke Silviu Homoceanu Institut für Informationssysteme Technische Universität Braunschweig

Lecture 15 April 9, 2007

Algorithms Theory. 08 Fibonacci Heaps

Data Structures for Disjoint Sets

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

MIDTERM I CMPS Winter 2013 Warmuth

Skylines. Yufei Tao. ITEE University of Queensland. INFS4205/7205, Uni of Queensland

Priority queues implemented via heaps

Non-context-Free Languages. CS215, Lecture 5 c

Multimedia Databases 1/29/ Indexes for Multimedia Data Indexes for Multimedia Data Indexes for Multimedia Data

Computing Static Single Assignment (SSA) Form

Another way of saying this is that amortized analysis guarantees the average case performance of each operation in the worst case.

Transcription:

Ordered Dictionary & Binary Search Tree Finite map from keys to values, assuming keys are comparable (<, =, >). insert(k, v) lookup(k) aka find: the associated value if any delete(k) (Ordered set: No values; just keys, call them elements). We will use a special kind of binary search trees called AVL trees. It prevents pathological trees, ensures O(lg n) worst-case time. (Binary tree height in Ω(lg n), so actually Θ(lg n) time.) 1 / 24

AVL Tree: Definition AVL trees are one way to ensure Θ(lg n) tree height. (Georgy Adelson-Velsky and Evgenii Landis) is a binary search tree at every node: subtree heights differ by at most 1. 44 17 78 32 50 88 48 62 (Keys shown, values omitted.) 2 / 24

Binary Search Tree: lookup public class Node <K extends Comparable <K>, V> { public K key; public V value; public Node <K> left, right; } public class BST <K extends Comparable <K>, V> { private Node <K> root; public V lookup(k x) { Node <K> n = root; while (n!= null) { int r = n. key. compareto(x); if (r == 0) { return n. value; } else { n = r > 0? n. left : n. right; } } return null; } 3 / 24

AVL Tree: insert insert(k, v) begins like lookup, expects to not find k (if found, change value to v), but now it knows where to put the new node. Add the new node there. The AVL property may break. Now fix it: There are rotations we do along the path from insertion point to root, restoring the AVL property. And want to manipulate only that path. Why? Because time budget is O(lg n). 4 / 24

Rebalancing: Overview To check and fix a node v: if height(v.left) + 1 < height(v.right) (the right is taller by 2) re-balance at v (two further subcases) else if height(v.left) > height(v.right) + 1 (the left is taller by 2) re-balance at v (two further subcases) else nothing to fix for v Do this for each node on the path from new node back to root. When processing v, descendents are already fixed. 5 / 24

Rebalance (Right Side) Subcase 1 of 2 Single-Rotation Counterclockwise If height(v.left) + 1 < height(v.right): Let x = v.right (Why does it exist?) If height(x.left) height(x.right): h + 3 v h + {2, 3} x R h h + 2 x h + {1, 2} v T h + 1 S h + {0, 1} T h + 1 R h S h + {0, 1} Why can we assume x is balanced? Answer on last slide. Exercise: Why is the outcome a binary search tree? 6 / 24

Rebalance (Right Side) Subcase 1 of 2 Example 20 20 10 50 10 70 15 40 70 15 50 80 60 80 40 60 75 75 7 / 24

Rebalance (Right Side) Subcase 2 of 2 Why Single-Rotation No Workie If height(v.left) + 1 < height(v.right): Let x = v.right If height(x.left) > height(x.right): h + 3 v h + 3 x R h h + 2 x h + 2 v T h S h + 1 T h R h S h + 1 Result still unbalanced. Solution on next slide. 8 / 24

Rebalance (Right Side) Subcase 2 of 2 Double-Rotation Clockwise Then Counterclockwise If height(v.left) + 1 < height(v.right): Let x = v.right If height(x.left) > height(x.right): Let w = x.left: h + 3 v h + 2 w R h h + 2 x h + 1 v h + 1 x h + 1 w T h R h S 1 S 2 h {0, 1} h {0, 1} T h S 1 h {0, 1} S 2 h {0, 1} 9 / 24

Rebalance (Right Side) Subcase 2 of 2 Example 20 20 10 50 10 60 15 40 70 15 50 70 60 80 40 55 80 55 10 / 24

Rebalance (Left Side) Subcase 1 of 2 Single-Rotation Clockwise If height(v.left) > height(v.right) + 1: Let x = v.left If height(x.left) height(x.right): v x x R T v T S S R 11 / 24

Rebalance (Left Side) Subcase 2 of 2 Double-Rotation Counterclockwise Then Clockwise If height(v.left) > height(v.right) + 1: Let x = v.left If height(x.left) < height(x.right): Let w = x.right: v w x R x v T w T S 2 S 1 R S 2 S 1 12 / 24

Rebalancing: Summary For each node v on the path from new node back to root: if height(v.left) + 1 < height(v.right) let x = v.right if height(x.left) height(x.right) single-rotation ccw else double-rotation cw then ccw else if height(v.left) > height(v.right) + 1 let x = v.left if height(x.left) height(x.right) single-rotation cw else double-rotation ccw then cw else no rotation 13 / 24

Height Comparison And Update Two alternatives: Cache height or cache difference. Cache height (more bits): Each node has field h for known height of self. Query: height(v) = (v = null? 0 : v.h) Update: Set children s before parent s, so simply: v.h := 1 + max(height(v.left), height(v.right)) Cache difference (a.k.a. balance factor, fewer bits): Each node has field BF for known difference of children. Update: See Hadzilacos s notes. Either way, update at: New node and ancestors (later ancestors of deleted node), nodes affected by rotations. 14 / 24

Only Path of Ancestors Needs Fixing Why is it enough to just fix the path from new node back to root? Say a node v is visited when finding where to add new node. Say it is decided to be v s right subtree. v s left subtree won t change, doesn t even need to check. (v itself needs checking later because right subtree will change, but it is on the path.) Similar story for updating heights. Similar story for deleting a node in later slides. 15 / 24

Summary of AVL Tree Insertion Later we will see why tree height is in O(lg n). With that in mind, AVL tree insertion: 1. find which node to become parent of new node [Θ(lg n) time] 2. put new node there [Θ(1) time] 3. from that parent to root (bottom-up): check and fix balance, update height [Θ(lg n) nodes, Θ(1) time per node] Total Θ(lg n) time. 16 / 24

Delete: Easy Case 44 17 78 32 50 88 48 62 Delete 32, or 48, or 62, or 88. If the node has no children, just unlink from parent. (Then update heights of ancestors, rebalance... ) 17 / 24

Delete: Slightly Harder Case 44 17 78 32 50 88 48 62 Delete 17. (Note that 32 is a good replacement.) If the node has at most one child, just link parent to that child. (Then update heights of ancestors, rebalance... ) This generalizes the easy case. 18 / 24

Delete: Slightly Harder Case, Generally Prune w. T 0 may be empty. p and ancestors need height updates and rebalancing. p 44 p 44 w 17 T 1 T 0 T 1 T 0 p 44 p 44 w 17 T 1 T 0 T 1 T 0 There are two more mirror images. 19 / 24

Delete: Hard Case Delete 5. Call the node w. Both children non-empty. w w 5 10 S 0 80 S 0 80 50 S 1 50 S 1 x 10 S 2 T S 2 T Find successor: Go right once, go left all the way, call it x. Replace w.key by x.key. x s parent adopts x s right child T. Rebalancing and height updates start from: x s old parent. 20 / 24

Delete: Hard Case, Degenerate Delete 5. Call the node w. Both children non-empty. w 5 w 80 x S 0 80 S 0 T T Go right. Go left all the way can t! That s already x. x s parent is w. Still, replace w.key by x.key. w also adopts x s right child T. 21 / 24

Summary of AVL Tree Deletion Next we will see why tree height is O(lg n). 1. find which node has the key, call it w [Θ(lg n) time] 2. if at most one child, w.parent adopts that child [Θ(1) time] 3. else: 3.1 go to successor x [Θ(lg n) time] 3.2 w.key := x.key [Θ(1) time] 3.3 x.parent adopts x.right [Θ(1) time] 4. from adopter to root (bottom-up): check and fix balance, update heights [Θ(lg n) time] Total Θ(lg n) time. 22 / 24

AVL Tree Height If there are n nodes, what is the maximum possible height? If the height is h, what is the minimum possible number of nodes? minsize(0) = 0 minsize(1) = 1 minsize(h + 2) = 1 + minsize(h + 1) + minsize(h) Can prove by induction: minsize(h) = fib(h + 2) 1 Golden ratio: φ = ( 5 + 1)/2 = 1.618... minsize(h) = φh+2 (1 φ) h+2 5 1 23 / 24

AVL Tree Height n minsize(h) = φh+2 (1 φ)h+2 1 5 5 φ h+2 5 2 < n h < > φh+2 5 1 1 lg(n + 2) lg φ + 5 lg φ + 2 = 1.44 lg(n + 2) + constant O(lg n) 24 / 24