This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Tutorial 2 Augmenting data structures Problem 14.3-4 (317): Given an interval tree, list all intervals that overlap a query interval in time O (min( n,k log n )) , where k is the number of answers. Solution: If we don’t mind changing the tree, then find and remove overlapping intervals one by one. Thereafter insert them again. This takes O (( k + 1) log n ) time (it takes O (log n ) with no overlapping intervals). Stop when k = n/ log n , since the problem is trivially solved in O ( n ) time by examining all intervals. If we don’t want to alter the tree structure, then modify INTERVAL-SEARCH in Lecture 3 to look for an overlapping interval that has the minimum low endpoint: MIN-INTERVAL-OVERLAP( T,i ) x ← root [ T ] while x negationslash = NIL do if left [ x ] negationslash = NIL and max [ left [ x ]] ≥ low [ i ] then x ← left [ x ] else if i overlaps int [ x ] then return x x ← right [ x ] return NIL We use this algorithm repeatedly, but will not actually delete overlapping intervals. Note that a node’s presence is felt by MIN-INTERVAL-OVERLAP only if the high endpoint of that interval affects the max of its parents (and ancestors). For this purpose, we maintain two stacks A and B of nodes. As we proceed from the root to an overlapping interval with minimum endpoint, we push the nodes (i.e. intervals) along the path onto A . After finding the overlapping interval with minimum endpoint, we trace back the route to the root. While doing so, we pop the nodes from A and push them onto B , modifying their max so that on reading the max of the ancestors of the minimum overlapping interval, we do not perceive the presence of this interval. Thus, after doing so, if we run MIN-INTERVAL-OVERLAP to find the next overlapping interval reading the nodes from stack B if they occur in it, then the algorithm computes as if the first overlapping interval were absent in the node and hence finds the next overlapping interval. We continue this process until all overlapping intervals are found (i.e. until MIN-INTERVAL- OVERLAP returns NIL)....
View Full Document