Orthogonal Range Queries
(1D-Version) Given an array of points and an interval, find the points (not just the number of points) that are contained the interval.
Last updated
(1D-Version) Given an array of points and an interval, find the points (not just the number of points) that are contained the interval.
Last updated
Example: Find the names of everyone aged between 22 and 27 (important in databases)
Firstly, you should be able to see how this is different from interval searching although in both the problems, there are points and intervals.
Moreover, this can be extended to d-dimensions (e.g. in the 2-D case, we would ask “find the points that lie within a given rectangle”, thus giving the name “orthogonal” range queries) but we only discuss the 1-D case here.
Use a Binary Search Tree in which all the nodes are sorted by the property we are going to query by. (Decide the underlying data structure)
Store all the points in the leaves of the tree. (Internal nodes only store copies of these) (Invariant: The tree would still have the BST property)
Each internal node v
stores the max of any leaf in its left subtree (this is quite a common strategy to help you determine whether to go left or go right). (Augment the data structure to help you perform your operations)
Find the split node (the highest node that falls between the interval bounds)
Perform leftTraverasal
Perform rightTraversal
Example of a left and right traversal:
leftTraversal
rightTraversal
is identical (in running time) to leftTraversal
Finding split node:
Doing all leaf traversal takes where k is the number of leaves in the subtree.
Then, leftTraversal
is recursively called (at most times)
So, query time complexity is where is the number of points found.
(Preprocessing) Building the entire tree takes time. - Run QuickSelect
to find the median, then run QuickSelect
on the two halves and so on.. at each level, it takes time to run the QuickSelect
.
Insertion, deletion takes time.
Total space complexity (Number of nodes in a tree number of leaves in the tree) (easy to prove since and at every level of the tree, the maximum number of possible nodes double).