A priority search tree stores one point with the smallest y-coordinate in each component at its root. The remaining points split around their median x-coordinate, so recursive subtrees cover narrower horizontal spans. A query for x in [left,right] and y at most a limit prunes a subtree when its x-span misses the interval or its root minimum y exceeds the limit. Otherwise, it tests the root point and descends. The program sorts returned event records by ID after traversal. Equal y values break by event ID at construction, and duplicate IDs are rejected. This is a static reporting index for a three-sided rectangle, not a general arbitrary rectangle aggregator.
Priority search trees: report events in a three-sided region
Operational case
Five depot events include E-29 at (29,17) and E-61 at (61,23). The query x from 20 through 70 with y no greater than 30 reports those two events. A second query x from 18 through 50 and y no greater than 50 reports E-19 and E-29. Pruning solely because the root's x lies outside the query would be wrong: descendants may still occupy the requested horizontal span. In contrast, if the minimum y at a node is above the limit, every point below it is above the limit and the entire component is safe to skip.
Working Python program
class PriorityNode:
def __init__(self, point, left, right, x_min, x_max):
self.point, self.left, self.right = point, left, right
self.x_min, self.x_max = x_min, x_max
class DepotPrioritySearch:
def __init__(self, events):
points = list(events)
if len({point[0] for point in points}) != len(points):
raise ValueError("event IDs must be unique")
self.root = self._build(points)
def _build(self, points):
if not points:
return None
chosen = min(points, key=lambda point: (point[2], point[0]))
remaining = sorted((point for point in points if point[0] != chosen[0]),
key=lambda point: (point[1], point[0]))
middle = len(remaining) // 2
left = self._build(remaining[:middle])
right = self._build(remaining[middle:])
return PriorityNode(chosen, left, right,
min(point[1] for point in points), max(point[1] for point in points))
def three_sided(self, x_left, x_right, y_limit):
if x_left > x_right:
raise ValueError("reversed horizontal range")
matches = []
def visit(node):
if node is None or node.x_max < x_left or node.x_min > x_right:
return
if node.point[2] > y_limit:
return
event_id, x_position, y_position = node.point
if x_left <= x_position <= x_right:
matches.append((event_id, x_position, y_position))
visit(node.left)
visit(node.right)
visit(self.root)
return sorted(matches)
if __name__ == "__main__":
events = [("E-19", 19, 47), ("E-29", 29, 17), ("E-47", 47, 61),
("E-61", 61, 23), ("E-83", 83, 11)]
index = DepotPrioritySearch(events)
print(index.three_sided(20, 70, 30))
print(index.three_sided(18, 50, 50))Output
[('E-29', 29, 17), ('E-61', 61, 23)]
[('E-19', 19, 47), ('E-29', 29, 17)]Time, space, and tradeoff
This direct builder finds a minimum and sorts remaining x values within each balanced component, costing at most O(N log-squared N) time and O(N) stored nodes. A query can visit O(N) nodes in an unfavorable region and sorts K reported records in O(K log K); its recursion uses O(log N) stack depth for the balanced construction. No output-sensitive worst-case bound is claimed for this particular Python walk. The fixed point set must be rebuilt after a location or score update. A k-d tree cuts coordinates differently, while a two-dimensional range tree spends more space to answer four-sided counts.
Common Mistakes
- Do not prune a subtree from the stored point's x alone.
- Do not forget the inclusive y threshold at equality.
- Do not use the static x-span after moving an event.
- Do not claim a four-sided rectangle contract for this three-sided query.
Connected lessons
- Trees and Heaps
- Data Structures
- Two-dimensional range trees: count a static rectangle
- K-d trees: exact nearest depot with plane pruning
- Point-region quadtrees: subdivide crowded cells
- Projects
- Quizzes
Compare its query and update boundary with Fibonacci heaps: cut on decrease and consolidate on removal, Tournament trees: merge sorted runs through one winner path, Reduced ordered decision diagrams: share identical rule branches, then complete the structure audit and decision quiz.
