CSE3144 · Advanced Data Structures · Jul–Nov Semester 2026 · Lecture 29 of 36 · CO CSE3144.5

Priority Search Trees

A heap answers "what's on top" fast. A BST answers "what's in this range" fast. Neither alone answers "what's in this X-range AND above this Y-threshold" efficiently — until you build one structure that is, level by level, both at once.

Dr. Manu ShrivastavaCourse Instructor · Consultation Fri 2–5 PM, LHC 308F
~116 minutesSession outcome: implement combined searching and priority operations
L29 · 00 — Agenda ~116 min

A heap and a search tree, welded together, level by level

Why priority search trees?

00–08

A 3-sided range query neither a plain BST nor a plain heap handles efficiently on its own.

Animation: a placement shortlist

08–20

“Born 2003–2004 and CGPA ≥ 7.5”: watch a BST fail, a heap fail, and a priority search tree succeed.

Definition & construction — concept

20–28

Max-Y becomes the root; a median X-value splits the rest in half; recurse.

Animation: building a priority search tree

28–58

13 points, every single recursive call shown, including the explicit empty ones.

Querying — concept

58–66

The one-directional prune: fail the Y-test once, and the ENTIRE subtree is safely skipped.

Animation: a 3-sided range query

66–88

Every node visited, reported, or pruned — with a genuine 4-node prune along the way.

Complexity analysis

88–98

O(n) space, O(log n) height, O(k + log n) query time — verified on today's own numbers.

Applications & recap

98–116

Where this 3-sided query shows up, and what's next.

CSE3144 — Lecture 29
L29 · 01 — Why priority search trees? ~8 min

A range query where one side is unbounded

Two simple cases you already know how to solve
  • All points between two vertical lines (a bounded X-range, Y unconstrained): a balanced BST, ordered on X, is the right tool — exactly Lectures 7–10.
  • All points above a horizontal line (a bounded Y-lower-bound, X unconstrained): a heap, ordered on Y, is the right tool — exactly Lectures 16–21.
The case neither alone handles well

What if you need BOTH at once: "every point with x′ ≤ x ≤ x″, AND y ≥ y′"? A plain BST ordered on X can't quickly rule out points with a bad Y. A plain heap ordered on Y can't quickly rule out points with a bad X. A priority search tree is a single structure engineered to answer exactly this 3-sided query — bounded on X on both sides, bounded on Y on only one side — in O(k + log n) time, where k is the number of points reported. The next slide shows all three on a placement shortlist.

CSE3144 — Lecture 29 · Why priority search trees
L29 · 02 — Animation: a placement shortlist ~12 min

Born 2003–2004 and CGPA ≥ 7.5: a BST fails, a heap fails, a PST works

Interactive — one query, three structures, the same 15 students
CSE3144 — Lecture 29 · Placement shortlist
L29 · 03 — Definition & construction — concept ~8 min

Max-Y becomes the root; a median X splits the rest

Definition

A priority search tree is a hybrid of a heap and a balanced binary search tree: every node stores one point, and the tree simultaneously satisfies a HEAP-order property on Y (root has the largest Y in its subtree) and a SEARCH-tree-like split on X (an associated value X(node) partitions the remaining points into a lower-X half and an upper-X half).

Construction, recursively, on a point set S
  1. If S is empty, return NULL.
  2. The point P in S with the GREATEST Y-coordinate becomes the root R.
  3. If S−P is empty, R is a leaf — stop.
  4. Sort the remaining m points of S−P by X: the first ⌊m/2⌋ form the lower-X half, the rest the upper-X half.
    • X(R) = the midpoint of the two middle X-values. In the next slide: X(F) = (7 + 9)/2 = 8, and X(N) = (2 + 4)/2 = 3 — not the median point’s own X of 4.
    • Why a midpoint: the query (slide 05) compares with strict <, so a split value equal to some point’s X could make the query skip that point.
    • One point left: it cannot be halved, so X(R) is R’s own X and the point goes to whichever side its X falls on (J ends up right of I, M left of E).
    • Sorting once at the start is enough: each recursive call receives its points already in X-order.
  5. Recursively build a priority search tree on the lower-X half → R's left child.
  6. Recursively build a priority search tree on the upper-X half → R's right child.
CSE3144 — Lecture 29 · Definition & construction concept
L29 · 04 — Animation: building a priority search tree ~30 min

13 points: F, N, A, I, H, G, J, K, B, C, D, E, M

F(−1,9), N(1,8), A(15,7), I(−2,5), H(7,6), G(6,4), J(2,3), K(4,0), B(16,2), C(12,1), D(14,−1), E(10,−2), M(9,−3). Every single recursive call is its own step, including the two that return an explicit empty subtree.

Interactive — construction, 13 points
CSE3144 — Lecture 29 · Construction
L29 · 05 — Querying — concept ~8 min

One failed Y-test prunes an entire subtree, instantly

The query: x′ ≤ x ≤ x″, y ≥ y′
  1. If the tree is NULL, return — nothing found here.
  2. Let R be the root, with X-coordinate x, Y-coordinate y, and split value X(R).
  3. If y < y′: return immediately. Every node in this subtree has Y ≤ this node's Y (the heap property), so NONE of them can satisfy y ≥ y′ either.
  4. If x′ ≤ x ≤ x″: report R.
  5. If x′ < X(R): the left subtree's X-range could overlap the query — recurse left.
  6. If X(R) < x″: the right subtree's X-range could overlap the query — recurse right.
The single most important idea today

Step 3 is what makes this structure fast for an UNBOUNDED-above query: because of the heap property, a single failed comparison at the root of a subtree proves that every descendant fails too — no matter how large that subtree is. This is exactly the same "one test rules out a whole region" idea from every space-partitioning tree this semester, applied to Y instead of a spatial split.

CSE3144 — Lecture 29 · Querying concept
L29 · 06 — Animation: a 3-sided range query ~22 min

x′=0, x″=11, y′=4.5, on the tree we just built

Watch for the moment an entire 3-node subtree gets pruned by a single Y comparison.

Interactive — range query on the 13-point tree
CSE3144 — Lecture 29 · Range query
L29 · 07 — Complexity analysis ~10 min

O(n) space, O(log n) height, O(k + log n) query time

Why each bound holds
  • Space O(n): exactly one tree node per input point, no duplication.
  • Height O(log n): every recursive call splits the remaining points roughly in half by X, exactly like a balanced BST.
  • Query time O(k + log n): visited nodes fall into 3 categories — (1) reported nodes, exactly k of them; (2) visited-but-not-reported nodes with good X but bad Y — each such node's PARENT must have been visited too, so there are at most 2× as many of these as all other visited nodes combined; (3) visited-but-not-reported nodes with bad X — at most 2 per tree level (one on each side of the range), so O(log n) of these in total.
Verified on today's own query

Reported (k): N, H — 2 nodes. Visited, bad Y, good X: J, K, G, E — 4 nodes (each ≤ 2× the "good" nodes, consistent with category 2's bound). Visited, bad X: F, I, A — 3 nodes, and the tree has 4 levels (⌈log₂13⌉+1), consistent with category 3's O(log n) bound. Total visited: 9 out of 13 — B, C, D, M were never touched at all.

CSE3144 — Lecture 29 · Complexity analysis
L29 · 08 — Applications & recap ~18 min

Wherever a range query has a naturally unbounded side

Interval intersection & scheduling

Conflict detection between time intervals, and CAD systems checking whether shapes' projected intervals overlap. Store each interval [a, b] as the point (a, b): the intervals overlapping a query [l, r] are exactly the points with a ≤ r and b ≥ l — an X-range plus a lower bound on Y, the shape this lecture answers.

Computational geometry

Priority search trees are a standard building block anywhere a query is bounded on one axis both ways but only lower- (or upper-) bounded on the other — a recurring shape in geometric range-searching problems.

Genome & annotation overlap queries

"Which annotated regions overlap this genomic interval, with at least this much confidence score" is the same 3-sided shape: bounded genomic position range, lower-bounded confidence.

Lecture 30 — next

Persistent Data Structures

CO CSE3144.5 — structures that remember every past version of themselves, not just the current one.

Homework — bring to Lecture 30
  • On today's exact tree, run the query x′=5, x″=17, y′=1. Show every node visited, reported, or pruned, and state the final reported set.
  • Build a priority search tree from scratch on these 7 points: P(2,10), Q(8,9), R(1,3), S(5,7), T(9,2), U(3,1), V(6,5). Show every recursive call.
  • In 3–4 sentences: explain why the height-O(log n) guarantee depends specifically on choosing X(R) as a MEDIAN split rather than, say, always splitting after the first remaining point.
CSE3144 — Lecture 29

Questions?

Dr. Manu Shrivastava — LHC 308F — Friday 2:00–5:00 PM

Next: Lecture 30 — Persistent Data Structures.