Download presentation
Presentation is loading. Please wait.
1
Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part II Prof. Dr. Th. Ottmann Summer Semester 2006
2
2 Examples for Augmenting DS Dynamic order statistics: Augmenting binary search trees by size information D-dimensional range trees: Recursive construction of (static) d-dim range trees Min-augmented dynamic range trees: Augmenting 1-dim range trees by min- information Interval trees Priority search trees
3
3 Interval Trees (CLR-Version) Problem: Given a set R of intervals that changes under insertions and deletions, construct a data structure to store R that can be updated in O(log n) time and that can find for any given query interval i an interval in R that overlaps i, and returns nil if there is no such interval, in O(log n) time. Idea: Store the set of intervals in an appropriately augmented balanced binary search tree and design an algorithm for the new operation. Interval-search(T, i): Report an interval stored in T that overlaps the query interval i, if such an interval exists, and Nil otherwise.
4
4 Observation Let i = [low(i), high(i)] and i‘ = [low(i‘), high(i‘)] be two intervals. Then i and i‘ overlap if and only if low(i) ≤ high(i‘) and low(i‘) ≤ high(i) Any two intervals satisfy the interval trichotomy: a)i and i‘ overlap b)high(i) ≤ low(i‘) c)high(i‘) ≤ low(i)
5
5 Interval Trichotomy The cases when intervals I 1 and I 2 overlap: The cases when intervals I 1 and I 2 do not overlap: x1x1 y1y1 x2x2 y2y2 x1x1 y1y1 x2x2 y2y2 x1x1 y1y1 x2x2 y2y2 x1x1 y1y1 x2x2 y2y2 x2x2 y2y2 x1x1 y1y1 x1x1 y1y1 x2x2 y2y2
6
6 Interval tree (CLR Version) [0,3] [6,10] [8,9] [15,23] [16,21] [17,19] [25,30] [27,33] [26,26] [5,8] [29,30] 3 9 21 30 26 30 833 1033 [19,20] 20 Each node v stores an interval int(v) and the maximum upper endponit of all intervals stored in the subtree rooted at v; The interval tree is a search tree on the lower endpoints of intervals. Max(v) is the maximum value of all right endpoints in the subtree rooted at v.
7
7 Maintaining max-information Max (x) = max(high(int(x), max (left(x)), max (right(x)) Max-information can be maintained during updates and rebalancing operations (rotations).
8
8 Finding an interval in T that overlaps interval i [0,3] [6,10] [8,9] [15,23] [16,21] [17,19] [25,30] [27,33] [26,26] [5,8] [29,30] 3 9 21 30 26 30 833 1033 [19,20] 20 Interval-search(T, i) x root(T) while x ≠ Nil and i does not overlap int(x) do if left(x) ≠ Nil and max(left(x))≥ low(i) then x left(x) else x right(x) return x Interval-search can be carried out in time O(height T). Observation: if int(x) does not overlap i, the search always proceeds in a safe direction!
9
9 Interval Trees: Point-set Variant Problem: Given a set R of intervals that changes under insertions and deletions, construct a data structure to store R that can be updated in O(log n) time and that can find for any given query interval i an interval in R that overlaps i, and returns nil if there is no such interval, in O(log n) time. Solution: Map intervals to points and store points in appropriately augmented tree. i l = low[i]r = high[i] (l, r)
10
10 Max-augmented Range Tree (2, 5)(3, 4)(4, 5) (8, 13)(10, 15) (11, 12) (14, 17) (21, 28)(15, 22)(17, 18) 24 3 11 10 8 14 17 1521 28 15 5 Store intervals as points in sorted x-order in a leaf search tree. Store max y-coordinates at internal nodes. Leaf-search tree on x-coordinates of points Max-tournament tree on y-coordinates of points 2822
11
11 Interval Search Interval-Search(T, i) /* Find an interval in tree T that overlaps i */ 1 P = root[T] 2while p is not a leaf do 3 if max-y[left[p]] low[i] 4then p = left[p] 5else p = right[p] /* Now p is a leaf storing interval i’ */ 6If (i and i’ overlap) then return “found” else return “not found”
12
12 Correctness Proof Case 1: We go right, low[i] > max-y[left[p]] i Intervals in left subtree of p None of them can overlap with i.
13
13 Correctness Proof Case 2: We go left, low[i] ≤ max-y[left[p]] If T[left[p]] does not contain an interval i‘ that overlaps i, then T[right[p]] cannot contain such an interval as well! i i‘ max-y[left[p]] (Here we utilize the fact that the intervals are sorted according to their x-coordinates!)
14
14 Interval Tree-Summary A interval tree for a set of n intervals [l 1, r 1 ], …, [l n, r n ] on the line is a daynamic max- augmented range tree for the set of points P = {(l 1, r 1 ), …, (l n, r n )}. Interval trees can be used to carry out the following operations: Interval-Insert(T, i) inserts the interval i into the tree T Interval-Delete(T, i) removes the interval i from the tree T Interval-Search(T, i) returns a pointer to a node storing an interval i‘ that overlaps i, or NIL if no such interval is stored in T.
15
15 Examples for Augmenting DS Dynamic order statistics: Augmenting binary search trees by size information D-dimensional range trees: Recursive construction of (static) d-dim range trees Min-augmented dynamic range trees: Augmenting 1-dim range trees by min- information Interval trees Priority search trees
16
16 3-Sided Range Queries Goal: Report all k points in the query range in O(log n + k) time.
17
17 3-Sided Range Queries Goal: Report all k points in the query range in O(log n + k) time. Age Salary
18
18 Priority Search Trees 234 58 11 14 21151733 24 3 11 8 5 14 17 1521 Two data structures in one: ● Search tree on points’ x-coordinates ● Heap on points’ y-coordinates { (2, 12), (3, 4) (4, 11), (5, 3), (8, 5), (11, 21), (14, 7), (15, 2), (17, 30), (21, 8), (33, 33) }
19
19 Priority Search Trees 24 58 11211517 24 3 11 8 5 14 17 1521 (33, 33) (17, 30) (21, 8) (15, 2) (11, 21) (2, 12)(4, 11) (5, 3) Two data structures in one: ● Search tree on points’ x-coordinates ● Heap on points’ y-coordinates 33 3 14 (8, 5) (14, 7) (3, 4)
20
20 Query procedure: Inspect all nodes on the two bounding paths and report the points that match the query. For every tree between the two bounding paths, apply the following strategy: Inspect the root. If this reports a point, recursively visit the children of the root. 3-Sided Range Queries on a Priority Search Tree O(log n) time to query red paths O(log n + k) time to query blue subtrees
21
21 Observations: We never report a point that is not in the query range. Points in the yellow subtrees cannot match the query. Points in the blue subtrees that are not reported cannot match the query. Correctness of the Query Procedure
22
22 Insertion into a Priority Search Tree Insertion procedure: 1. Insert new leaf based on point’s x-coordinate. 2. Insert point down the tree, based on its y-coordinate. 24 58 11211517 24 3 11 8 5 14 17 1521 (33, 33) (17, 30) (21, 8) (15, 2) (11, 21) (2, 12)(4, 11) (5, 3) 33 3 14 (8, 5) (14, 7) (3, 4)
23
23 Deletion from a Priority Search Tree Deletion procedure: 1. Search for the point and delete it. 2. Fill the gap by pulling-up points according to their y-values 24 58 11211517 24 3 11 8 5 14 17 1521 (33, 33) (17, 30) (21, 8) (15, 2) (11, 21) (2, 12)(4, 11) (5, 3) 33 3 14 (8, 5) (14, 7) (3, 4)
24
24 Priority Search Tress: Observations Insertion and deletion of point in a priority search tree T of n nodes can be carried out in time O(height(T)). Priority search trees support north-grounded range reporting, if the heap-structure is a max-heap, and they support south-grounded range reporting, if the heap-structure is a min-heap. Maintaining the height of the leaf search tree underlying a priority search tree such that the height is always of order O(log n) for a priority search tree storing n nodes requires rebalancing! In order to obtain O(log n) algorithms for insertion and deletion of points one must use a rebalancing scheme with constant restructuring cost per update! A PST storing n points requires space O(N).
25
25 Rotations in a Priority Search Tree p1p1 p2p2 p1p1 ? Push p 2 to the appropriate child of y. Store p 1 at y. Propagate the point with maximal y-coordinate from the appropriate child of x. x y y x
26
26 Priority Search Trees — Summary Theorem: There exists a data structure to represent a dynamically changing set S of points in two dimensions with the following properties: The data structure can be updated in O(log n) time after every insertion or deletion into or from S. The data structure allows us to answer 3-sided range queries in O(log n + k) time. The data structure occupies O(n) space.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.