Interval Trees Store intervals of the form [li,ri], li <= ri.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Interval Trees Store intervals of the form [li,ri], li <= ri.
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part I Prof. Dr. Th. Ottmann Summer Semester 2006.
2/14/13CMPS 3120 Computational Geometry1 CMPS 3120: Computational Geometry Spring 2013 Planar Subdivisions and Point Location Carola Wenk Based on: Computational.
Tirgul 5 AVL trees.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
Chapter 10 Search Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
1 Trees 3: The Binary Search Tree Section Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation
Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity.  insert  remove min (or max)  initialize Can.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
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.
Segment Trees Basic data structure in computational geometry. Computational geometry.  Computations with geometric objects.  Points in 1-, 2-, 3-, d-space.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Priority Search Trees Keys are pairs (x,y). Basic (search, insert, delete) and rectangle operations. Two varieties.  Based on a balanced binary search.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
1 Sorting an almost sorted input Suppose we know that the input is “almost” sorted Let I be the number of “inversions” in the input: The number of pairs.
Dynamic Dictionaries Primary Operations:  get(key) => search  put(key, element) => insert  remove(key) => delete Additional operations:  ascend()
Foundation of Computing Systems Lecture 4 Trees: Part I.
UNC Chapel Hill M. C. Lin Geometric Data Structures Reading: Chapter 10 of the Textbook Driving Applications –Windowing Queries Related Application –Query.
Leftist Trees Linked binary tree.
Part-D1 Binary Search Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
Data Structures – LECTURE Balanced 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.
Multiway Search Trees Data may not fit into main memory
Binary Search Trees A binary search tree is a binary tree
CMPS 3130/6130 Computational Geometry Spring 2017
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Multidimensional Range Search
AVL Trees binary tree for every node x, define its balance factor
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Binary Search Trees.
Binary Search Tree Chapter 10.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Dynamic Dictionaries Primary Operations: Additional operations:
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Interval Heaps Complete binary tree.
Orthogonal Range Searching and Kd-Trees
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Data Structures: Segment Trees, Fenwick Trees
Chapter 6 Transform and Conquer.
Priority Search Trees Keys are pairs (x,y).
Initializing A Max Heap
Multi-Way Search Trees
AVL Trees: AVL Trees: Balanced binary search tree
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.
Search Sorted Array: Binary Search Linked List: Linear Search
Segment Trees Basic data structure in computational geometry.
Dynamic Dictionaries Primary Operations: Additional operations:
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
Introduction to Algorithms
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL-Trees.
AVL-Trees (Part 1).
Binary SearchTrees [CLRS] – Chap 12.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
การวิเคราะห์และออกแบบขั้นตอนวิธี
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
A Heap Is Efficiently Represented As An Array
Chapter 7 : Sorting 교수 : 이상환 강의실 : 113,118호, 324호 연구실 : 과학관 204호
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Interval Trees Store intervals of the form [li,ri], li <= ri. An interval is stored in exactly 1 node. O(n) nodes. 3 versions. Differing capability. Note, when l = r, interval intersection becomes point enclosure. Interval heap stores points; not intervals. In a segment tree: l < r; an interval is stored in O(log range) nodes; number of nodes is linear in range not in number of intervals.

Version 1 Store intervals of the form [li,ri], li <= ri. At least 1 interval per node. Static interval set. Report all intervals that intersect/overlap a given interval [l,r]. Note, when l = r, interval intersection becomes point enclosure.

Definition—Version 1 A binary tree. Each node v has a point v.pt and two lists v.left and v.right. u.pt < v.pt for nodes u in left subtree of v. u.pt > v.pt for nodes u in right subtree of v. So, it is a binary search tree on pt.

Definition—Version 1 Intervals with ri < v.pt are stored in the left subtree of v. Intervals with li > v.pt are stored in the right subtree of v. Intervals with li <= v.pt <= ri are stored in v. v.left has these intervals sorted by li. v.right has these intervals sorted by ri. Note sorted doesn’t imply an array organization necessarily. Could be a red-black tree, implicitly sorted.

Example e a c b f d v.pt = 4 L = {a, e} R = {d} v.left = {c, f, b} 1 e 3 a 2 4 c 6 b f 5 7 d 4 L R v v.pt = 4 L = {a, e} R = {d} v.left = {c, f, b} v.right = {c,b,f}

Properties Each interval is stored in exactly one node. Each node stores between 1 and n intervals. Number of nodes is O(n). Sum of sizes of left and right lists is O(n). Tree height depends on how you choose the points v.pt.

Selection of v.pt v is the median of the end points of the intervals stored in the subtree rooted at v. 1 e 3 a 2 4 c 6 b f 5 7 d End points = {1, 2, 3, 4, 5, 6, 7} Use 4 as v.pt.

Selection of v.pt With median selection, tree height is O(log n). Median selection is possible only for static interval set. So, no inserts/deletes. Could relax to support insert/delete.

Find All Overlapping Intervals Query interval is [l,r]. v.pt e [l,r] All intervals in v overlap. Search L and R for additional overlapping intervals. 4 L R v l r

Find All Overlapping Intervals v.pt < l Intervals in v with ri >= l overlap. No interval in L overlaps. Search R for additional overlapping intervals. 4 L R v l r Search v.right for overlapping intervals. If v.right is a sorted array, start at right end of array and pick off the overlapping intervals. Time is 1 + number of overlapping intervals in v.right.

Find All Overlapping Intervals v.pt > r Intervals in v with li <= r overlap. No interval in R overlaps. Search L for additional overlapping intervals. 4 L R v l r Search v.left for overlapping intervals. If v.left is a sorted array, start at left end of array and pick off the overlapping intervals. Time is 1 + number of overlapping intervals in v.left.

Find All Overlapping Intervals Complexity O(log n + |output|) nodes encountered. All intervals in v overlap. Intervals in v with ri >= l overlap. Intervals in v with li <= r overlap. O(log n + |output|) when v.left and v.right are sorted arrays. 4 L R v l r Look at subtree of visited nodes. If there is no degree 2 node, total number of nodes in subtree is O(log n). Otherwise, mark nodes on leftmost and rightmost paths from root as boundary nodes. Number of boundary nodes is O(log n). Other nodes are interior nodes. Let P be degree 2 node closest to root. All interior nodes are descendents of P. The interval stored in an interior node overlaps the query interval as the query interval includes P.pt.

Version 2 Store intervals of the form [li,ri], li <= ri. Empty nodes permitted. Inserts and deletes. Answer queries of the form: which intervals include the point d. Note, when l = r, interval intersection becomes point enclosure.

Inserts & Deletes Difficult in version 1 because v.pt is median. Select v.pt (almost) arbitrarily and use a red-black tree. Each node stores between 0 and n intervals. At most 2n nodes permissible. Tree height is O(log n). Not completely arbitrary as the intervals stored in the node must contain the point.

Need For Empty Nodes Deletion from a degree 2 node. 20 10 6 2 8 15 40 30 25 35 7 18 Suppose that deletion of an interval from root causes it to become empty. Delete 20  replace with 18  intervals stored in 10 and 15 (I.e., in all nodes on path from degree 2 node that has become empty to replacement node) may need to be moved to new location (root) of 18. This relocation may cause 10 and 15 to become empty, so we repeat the process! Hence, update is expensive. Note that deletion from a degree 0 or 1 node doesn’t suffer from this problem.

Why Upto 2n Nodes Permissible When number of nodes > 2n, at least 1 degree 0 or degree 1 node must be empty. Empty degree 0 and 1 nodes are easily removed. So, no need to keep them around. 2n suffices to avoid having to handle empty degree 2 nodes. Proof, by contradiction. |tree| > 2n >= n+1 empty nodes (ne). Assume all are degree 2 nodes. nn = nonempty nodes, n2 = degree 2 nodes So, n2 >= ne and n >= nn (each nn node has at least 1 interval). So, n2 + n >= ne + nn = |tree|. So, n2 >= |tree| - n. But, n0 = n2 + 1 So, |tree| = n0+n1+n2 = n2 + 1 + n1 + n2 > 2n2 >= 2|tree| - 2n > |tree|. When a delete is done and a node becomes empty, it may be necessary to remove up to 2 degree 0/1 nodes to ensure #nodes <= 2*#intervals.

LL Rotation Intervals change only for A and B. B’L BR AR After insertion. B A After rotation. BR AR B’L Intervals change only for A and B. Those intervals of A that include B.pt need to be moved into B.

Remaining Rotations All insert/delete rotations require relocating intervals from O(1) nodes. O(1) rotations per insert/delete. Complexity of insert/delete is O(f(n) + log n), where f(n) is time needed to relocate O(n) intervals from one node to another. F(n) = log n when the lists are themselves stored as red-black trees and the intervals are either disjoint or nest; n when arrays are used

All intervals that contain d d = v.pt All intervals in v. Done! 4 L R v

All intervals that contain d d < v.pt Intervals in v with left end point <= d. No interval in R contains d. Search L for additional overlapping intervals. d > v.pt Similar O(log n + |output|) 4 L R v

Version 3 Store intervals of the form [li,ri], li <= ri. Exactly 1 interval per node. Inserts and deletes. Report just 1 overlapping interval. Note, when l = r, interval intersection becomes point enclosure.

Version 3—Structure Red-black tree. Each node has exactly one interval v.int and one point v.max. v.max = max (right) end point of intervals in subtree rooted at v. Binary search tree on intervals. So, need an ordering relation for intervals. Subtree rooted at v includes node v.

Interval Ordering Ordered by left end points. Tie breaker for equal left end points. i and j are two intervals. i < j iff li < lj or (li = lj and ri > rj) i j i j i j Each node has an interval and a max right end-point. Binary search tree on left end points of intervals. Insert, delete, search (for an overlapping interval) O(log n) time.

Example 1 e 3 a 2 4 c 6 b f 5 7 d f,7 e,4 a,3 c,4 d,7 b,6

Version 3—Search Search for an interval that has an overlap with Q = [l,r] If v.interval and Q overlap, done. Otherwise, if v.leftChild.max >= l search v.leftChild. Otherwise search v.rightChild. f,7 e,4 a,3 c,4 d,7 b,6 Prove correct by showing (a) if you go into left subtree, then either there is an overlapping interval in left or right has no overlapping interval and (b) if you go into the right subtree then there is no overlapping interval in the left subtree. (b) is easy as leftChild.max < l  no overlap in left subtree. For (a), suppose there is no overlapping interval in the left subtree. So, the interval L in the left sub tree that ends at leftChild.max begins after r. All intervals in the right subtree begin at or after the left end point of L and so do not overlap Q. Search time is O(log n).

Version 3—Search v.leftChild.max >= l l r max Suppose no interval in left subtree overlaps [l,r]. All intervals in right subtree begin at or after left end point of blue interval, which is an interval in the left subtree that ends at max. max

Version 3—LL Rotation Max values changes only for A and B. A B B’L BR AR After insertion. B A After rotation. BR AR B’L Note new max for B may not equal old max for A as an interval has been inserted/deleted. Max values changes only for A and B. A.max = max{A.interval.right, BR.max, AR.max}. B.max = max{B.interval.right, B’L.max, A.max}.

Remaining Rotations All insert/delete rotations require computing max for O(1) nodes. O(1) rotations per insert/delete. Complexity of insert/delete is O(log n).