2IL50 Data Structures Spring 2015 Lecture 8: Augmenting Data Structures.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree.
Binary Search Trees Comp 550.
CS 473Lecture X1 CS473-Algorithms I Lecture X Augmenting Data Structures.
David Luebke 1 5/22/2015 CS 332: Algorithms Augmenting Data Structures: Interval Trees.
Augnenting data structures Augment an existing data structure to apply to a new problem. Red-black tree as an example.
14. Augmenting Data Structures Hsu, Lih-Hsing. Computer Theory Lab. Chapter 13P Dynamic order statistics We shall also see the rank of an element―its.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 12.
Red-Black Trees CIS 606 Spring Red-black trees A variation of binary search trees. Balanced: height is O(lg n), where n is the number of nodes.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
Comp 122, Spring 2004 Red-Black Trees. redblack - 2 Comp 122, Spring 2004 Red-black trees: Overview  Red-black trees are a variation of binary search.
Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part II Prof. Dr. Th. Ottmann Summer Semester 2006.
Introduction to Analysis of Algorithms CAS CS 330 Lecture 16 Shang-Hua Teng Thanks to Charles E. Leiserson and Silvio Micali of MIT for these slides.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Lecture X Augmenting Data Structures
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
Interval Trees CS302 Data Structures Modified from Dr Monica Nicolescu.
Red-Black Trees CS302 Data Structures Dr. George Bebis.
Red-Black Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Red-Black Trees Comp 550.
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
Lecture 10 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
2IL50 Data Structures Fall 2015 Lecture 9: Range Searching.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 11 Prof. Erik Demaine.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in They came up with the AVL tree. In.
Data StructuresData Structures Red Black Trees. Red-black trees: Overview Red-black trees are a variation of binary search trees to ensure that the tree.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
Sept Red-Black Trees What is a red-black tree? -node color: red or black - nil[T] and black height Subtree rotation Node insertion Node deletion.
2IL05 Data Structures Spring 2010 Lecture 9: Augmenting Data Structures.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Binary Search Trees What is a binary search tree?
Analysis of Algorithms CS 477/677
Balanced Search Trees Modified from authors’ slides.
Introduction to Algorithms
Red-Black Trees.
Lecture 7 Algorithm Analysis
Red-Black Trees Motivations
Augmenting Data Structures
Red-Black Trees.
CMSC 341 (Data Structures)
Lecture 9 Algorithm Analysis
Chapter 14: Augmenting Data Structures
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Lecture 7 Algorithm Analysis
CS 583 Analysis of Algorithms
Introduction to Algorithms
Lecture 7 Algorithm Analysis
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Interval Trees CS302 Data Structures Modified from Dr Monica Nicolescu.
Red-Black Trees CS302 Data Structures
Presentation transcript:

2IL50 Data Structures Spring 2015 Lecture 8: Augmenting Data Structures

Binary Search Trees

5 Binary search trees  root of T denoted by root[T]  internal nodes have four fields: key (and possible other satellite data) left: points to left child right: points to right child p: points to parent. p[root[T]] = NIL Binary tree T root[T] = NIL

Binary search trees  a binary tree is a leaf or a root node x with a binary tree as its left and/or right child Binary-search-tree property if y is in the left subtree of x, then key[y] ≤ key[x] if y is in the right subtree of x, then key[y] ≥ key[x] k ≤ k≥ k Balanced binary search trees Search, Minimum, Maximum, Predecessor, Successor, Insert, and Delete can be executed in time Θ(log n).

Red-black Trees

Red-black tree binary search tree where each node has a color attribute which is either red or black Red-black properties 1.Every node is either red or black. 2.The root is black 3.Every leaf (nil[T]) is black. 4.If a node is red, then both its children are black. (Hence no two reds in a row on a simple path from the root to a leaf) 5.For each node, all paths from the node to descendant leaves contain the same number of black nodes Red-black trees Lemma A red-black tree with n nodes has height ≤ 2 log(n+1).

15 Red-black trees: Insertion 1.Do a regular binary search tree insertion 2.Fix the red-black properties Step1 find the leaf where the node should be inserted replace the leaf by a red node that contains the key to be inserted

15 Red-black trees: Insertion 1.Do a regular binary search tree insertion 2.Fix the red-black properties Red-black properties 1.Every node is either red or black. 2.The root is black 3.Every leaf (nil[T]) is black. 4.If a node is red, then both its children are black. 5.For each node, all paths from the node to descendant leaves contain the same number of black nodes The new node is red ➨ Property 2 or 4 can be violated. Remove the violation by rotations and recoloring.

Rotation y x x y right rotation around y left rotation around x

while z ≠ root[T] and color[p[z]] = red do if p[z] == left[p[p[z]]] then else “symmetric case” y = right[p[p[z]]] case i: y is red color p[z] and y black, color p[p[z]] red z = p[p[z]] continue up the tree Step 2: Fixing the red-black properties Invariant: z and p[z] are both red and this is the only red-black violation (or z is a red root ➨ just recolor and terminate) hence p[z] ≠ root[T] z p[z] p[p[z]] y z

right rotation around b + recolor left rotation around a Step 2: Fixing the red-black properties Invariant: z and p[z] are both red and this is the only red-black violation (or z is a red root ➨ just recolor and terminate) case ii and iii: y is black z a b y a z b y b z y a case ii: z == right[p[z]]case iii: z == left[p[z]] ✔

Red-black trees: Insertion 1.Do a regular binary search tree insertion 2.Fix the red-black properties case ii (symmetric version) left rotation around 12 + recolor right rotation around case iii (symmetric version) ✔

Red-black trees: Insertion 1.Do a regular binary search tree insertion 2.Fix the red-black properties move up the tree as long as case i occurs and recolor accordingly as soon as case ii or iii occurs ➨ at most two rotations and two recolorings ➨ ✔ if you reach the root or the parent of z is black ➨ ✔ Running time? O(height of the tree) = O(log n)

Red-black trees: Deletion 1.Do a regular binary search tree deletion 2.Fix the red-black properties Slightly more complicated case distinction than insertion see book for details can be done by recoloring as before and at most three rotations ➨ Search, insert, and delete can be executed with a red-black tree in O(log n) time.

Augmenting Data Structures

Data structures  Data structures are used in many applications directly: the user repeatedly queries the data structure indirectly: to make algorithms run faster  In most cases a standard data structure is sufficient (possibly provided by a software library)  But sometimes one needs additional operations that aren’t supported by any standard data structure ➨ need to design new data structure?  Not always: often augmenting an existing structure is sufficient

Example S set of elements, each with an unique key. Operations Search(S, k): return a pointer to an element x in S with key[x] = k, or NIL if such an element does not exist. OS-select(S, i): return a pointer to an element x in S with the i th smallest key (the key with rank i) Solution: sorted array A the key with rank i is stored in A[i]

Example S set of elements, each with an unique key. Operations Search(S, k): return a pointer to an element x in S with key[x] = k, or NIL if such an element does not exist. OS-select(S, i): return a pointer to an element x in S with the i th smallest key (the key with rank i) Insert(S, x): inserts element x into S, that is, S ← S ⋃ {x} Delete(S, x): remove element x from S Solution?

OS-select(S, 3): report key with rank 3 Idea 1: store the rank of each node in the node Idea: Use red-black trees NIL NIL 10 Is the key with rank 3 in the left subtree, in the right subtree, or in the root? 2 2 │ 1 ➨

Problem: Idea 1: Store the rank in each node NIL NIL │ 6 10 │ 2 2 │ 1 12 │ 3 17 │ 4 18 │ 5 Insertion can change the rank of every node! ➨ worst case O(n)

Problem: Idea 2: store the size of the subtree in each node Idea 1: Store the rank in each node NIL NIL │ 6 10 │ 2 2 │ 1 12 │ 3 17 │ 4 18 │ 5 Insertion can change the rank of every node! ➨ worst case O(n)

Idea 2: Store the size of the subtree NIL NIL │ 1 10 │ 6 2 │ 1 12 │ 2 17 │ 1 18 │ 4

 store in each node x: left [x], right [x] parent [x] key [x] color [x] size [x] = number of keys in subtree rooted at x (size [NIL] = 0) Idea 2: Store the size of the subtree NIL NIL │ 1 10 │ 6 2 │ 1 12 │ 2 17 │ 1 18 │ 4 order-statistic tree

OS-Select(x, i): return pointer to node containing the i th smallest key of the subtree rooted at x OS-Select(x, i) 1. r = size[left[x]] if i == r 3. then return x 4. else if i < r 5. then return OS-Select(left[x], i) 6. else return OS-Select(right[x], i) Running time? Order-statistic trees: OS-Select OS-Select(x, 17) x size = 16 r = 17 size = 15size = 24 r = 25r = 16 i-r O(log n)

Order-statistic trees: OS-Rank OS-Rank(T, x): return the rank of x in the linear order determined by an inorder walk of T = 1 + number of keys smaller than x x

Order-statistic trees: OS-Rank OS-Rank(T, x): return the rank of x in the linear order determined by an inorder walk of T = 1 + number of keys smaller than x OS-Rank(T, x) 1. r = size[left[x]] y = x 3. while y ≠ root [T] 4. do if y == right[p[y]] 5. then r = r + size [left[p[y]] y = p[y] 7. return r x Running time? O(log n)

OS-Rank: Correctness OS-Rank(T, x) 1. r = size[left[x]] y = x 3. while y ≠ root [T] 4. do if y == right[p[y]] 5. then r = r + size [left[p[y]] y = p[y] 7. return r Invariant At the start of each iteration of the while loop, r = rank of key[x] in T y Initialization r = rank of key[x] in T x (y = x) = number of keys smaller than key[x] in T x +1 = size[left[x]] + 1 (binary-search-tree property) subtree with root y

OS-Rank: Correctness OS-Rank(T, x) 1. r = size[left[x]] y = x 3. while y ≠ root [T] 4. do if y == right[p[y]] 5. then r = r + size [left[p[y]] y = p[y] 7. return r Invariant At the start of each iteration of the while loop, r = rank of key[x] in T y Termination loop terminates when y = root[T] ➨ subtree rooted at y is entire tree ➨ r = rank of key[x] in entire tree

OS-Rank: Correctness OS-Rank(T, x) 1. r = size[left[x]] y = x 3. while y ≠ root [T] 4. do if y == right[p[y]] 5. then r = r + size [left[p[y]] y = p[y] 7. return r Invariant At the start of each iteration of the while loop, r = rank of key[x] in T y Maintenance case i: y = right[p[y]] ➨ all keys T left[p[y]] and key[p[y]] smaller than key[x] ➨ rank key[x] in T p[y] = rank key[x] in T y + size [left[p[y]] + 1

OS-Rank: Correctness OS-Rank(T, x) 1. r = size[left[x]] y = x 3. while y ≠ root [T] 4. do if y == right[p[y]] 5. then r = r + size [left[p[y]] y = p[y] 7. return r Invariant At the start of each iteration of the while loop, r = rank of key[x] in T y Maintenance case ii: y = left[p[y]] ➨ all keys T right[p[y]] and key[p[y]] larger than key[x] ➨ rank key[x] in T p[y] = rank key[x] in T y

Order-statistic trees: Insertion and deletion Insertion and deletion as in a regular red-black tree, but we have to update size[x] field

15 Red-black trees: Insertion 1.Do a regular binary search tree insertion 2.Fix the red-black properties Step1 find the leaf where the node should be inserted replace the leaf by a red node that contains the key to be inserted size of the new node = 1 increment size of each node on the search path size[x] = 1 +1

15 Red-black trees: Insertion 1.Do a regular binary search tree insertion 2.Fix the red-black properties Red-black properties 1.Every node is either red or black. 2.The root is black 3.Every leaf (nil[T]) is black. 4.If a node is red, then both its children are black. 5.For each node, all paths from the node to descendant leaves contain the same number of black nodes The new node is red ➨ Property 2 or 4 can be violated. Remove the violation by rotations and recoloring.

Rotation y x x y right rotation around y left rotation around x  A rotation affects only size[x] and size[y]  We can determine the new values based on the size of children: size[x] = size[left[x]] + size[right[x]] + 1 and the same for y …

Order-statistic trees The operations Insert, Delete, Search, OS-Select, and OS-Rank can be executed with an order-statistic tree in O(log n) time.

Augmenting data structures Methodology for augmenting a data structure 1.Choose an underlying data structure. 2.Determine additional information to maintain. 3.Verify that we can maintain additional information for existing data structure operations. 4.Develop new operations.  You don’t need to do these steps in strict order!  Red-black trees are very well suited to augmentation … OS tree 1.R-B tree 2.size[x] 3.maintain size during insert and delete 4.OS-Select and OS-Rank

Augmenting red-black trees Theorem Augment a R-B tree with field f, where f[x] depends only on information in x, left[x], and right[x] (including f[left[x]] and f[right[x]]). Then can maintain values of f in all nodes during insert and delete without affecting O(log n) performance. When we alter information in x, changes propagate only upward on the search path for x …

Augmenting red-black trees Theorem Augment a R-B tree with field f, where f[x] depends only on information in x, left[x], and right[x] (including f[left[x]] and f[right[x]]). Then can maintain values of f in all nodes during insert and delete without affecting O(log n) performance. Proof (insert) Step 1 Do a regular binary search tree insertion x go up from inserted node and update f additional time: O(log n)

Augmenting red-black trees Theorem Augment a R-B tree with field f, where f[x] depends only on information in x, left[x], and right[x] (including f[left[x]] and f[right[x]]). Then can maintain values of f in all nodes during insert and delete without affecting O(log n) performance. Proof (insert) Step 2 Fix the red-black properties by rotations and recoloring update f for x, y, and their ancestors additional time per rotation: O(log n) x xy y

Example: Interval trees

Interval trees S set of closed intervals Operations Interval-Insert(T, x):adds an interval x, whose int field is assumed to contain an interval, to the interval tree T. Interval-Delete(T, x): removes the element x from the interval tree T. Interval-Search(T, j):returns pointer to a node x in T such that int[x] overlaps j, or nil[T] if no such element exists. closed: endpoints are part of the interval low[i]high[i] i j

Methodology 1.Choose an underlying data structure. 2.Determine additional information to maintain. 3.Verify that we can maintain additional information for existing data structure operations. 4.Develop new operations.

Methodology 1.Choose an underlying data structure. use red-black trees  each node x contains interval int[x]  key is left endpoint low[int[x]] inorder walk would list intervals sorted by low endpoint 2.Determine additional information to maintain. 3.Verify that we can maintain additional information for existing data structure operations. 4.Develop new operations.

Methodology 1.Choose an underlying data structure. ✔ 2.Determine additional information to maintain. 3.Verify that we can maintain additional information for existing data structure operations. 4.Develop new operations.

Additional information for Interval-Search j low[.] ≤ low[i]low[.] > low[i] i i j case 1: i ∩ j ≠ Ø ➨ report i

Additional information for Interval-Search case 1: i ∩ j ≠ Ø case 2: j lies left of i ➨ j cannot overlap any interval in the right subtree j low[.] ≤ low[i]low[.] > low[i] i i j ➨ report i

Additional information for Interval-Search case 1: i ∩ j ≠ Ø case 2: j lies left of i ➨ j cannot overlap any interval in the right subtree case 3: j lies right of i ➨ need additional information! j low[.] ≤ low[i]low[.] > low[i] i i j ➨ report i max[x] = max endpoint value in subtree rooted at x = max{high[i] where i is stored in the subtree rooted at x}

Additional information for Interval-Search case 1: i ∩ j ≠ Ø case 2: j lies left of i ➨ j cannot overlap any interval in the right subtree case 3: j lies right of i ➨ j overlaps interval in left subtree if and only if low[j] ≤ max[left[i]] j low[.] ≤ low[i]low[.] > low[i] i i j ➨ report i max[x] = max endpoint value in subtree rooted at x = max{high[i] where i is stored in the subtree rooted at x}

Methodology 1.Choose an underlying data structure. ✔ 2.Determine additional information to maintain. ✔ 3.Verify that we can maintain additional information for existing data structure operations. 4.Develop new operations.

Interval-Search Interval-Search(T, j) 1. x = root [ T ] 2. while x ≠ nil[T] and j does not overlap int[x] 3. do if left[x] ≠ NIL and max[left[x]] ≥ low[j] 4. then x = left[x] 5. else x = right[x] 6. return x Correctness Invariant If tree T contains an interval that overlaps j, then there is such an interval in the subtree rooted at x. Running time? O(log n)

Methodology 1.Choose an underlying data structure. ✔ 2.Determine additional information to maintain. ✔ 3.Verify that we can maintain additional information for existing data structure operations. 4.Develop new operations. ✔

Maintain additional information Theorem Augment a R-B tree with field f, where f[x] depends only on information in x, left[x], and right[x] (including f[left[x]] and f[right[x]]). Then can maintain values of f in all nodes during insert and delete without affecting O(log n) performance. Additional information max[x] = max endpoint value in subtree rooted at x  max[x] depends only on information in x: high[int[x]] information in left[x]: max[left[x]] information in right[x]: max[right[x]] max[x] = max { high[int[x]], max[left[x]], max[right[x]] } ➨ insert and delete still run in O(log n) time