CSE3144 ยท Advanced Data Structures ยท Julโ€“Nov Semester 2026 ยท Lecture 23 of 36 ยท CO CSE3144.4

k-d Trees and Multidimensional Searching

Lecture 22 built the tree. Today we actually use it: range search that prunes whole regions of space, nearest-neighbor search that can't trust "closest in the tree" to mean "closest in space", and a deletion algorithm subtle enough that a plain BST's trick quietly breaks.

Dr. Manu ShrivastavaCourse Instructor ยท Consultation Fri 2โ€“5 PM, LHC 308F
~127 minutesSession outcome: implement multidimensional searching operations
L23 ยท 00 โ€” Agenda ~127 min

Three operations, one recurring trick: prune using the cutting dimension

Recap & a richer example

00โ€“08

Lecture 22's 6-point tree was kept small on purpose. Today's operations need real branching to show real pruning.

Range search โ€” concept

08โ€“16

Report everything inside a query rectangle, without touching every node.

Animation: range search

16โ€“34

A query rectangle that prunes an entire 3-node subtree in one comparison.

Nearest-neighbor search โ€” concept

34โ€“48

Why "the cell containing Q" is not the same question as "the closest point to Q".

Animation: nearest-neighbor search

48โ€“73

Best-so-far, a shrinking search radius, and a genuine backtrack-and-prune.

FindMin โ€” the operation deletion depends on

73โ€“83

Minimum along ONE dimension โ€” and why that changes which subtrees can be skipped.

Deletion โ€” concept

83โ€“93

Why a BST's "promote from the right subtree" trick doesn't directly survive alternating dimensions.

Why not findMax(left, d)? — the picture

93โ€“97

Why a tie on the cutting dimension makes the largest-on-the-left choice unsafe, drawn as trees.

Animation: why not findMax(left, d)?

97โ€“101

The same deletion run step by step: the shortcut loses a point, the swap keeps it.

Animation: deletion

101โ€“121

Deleting a node with no right child โ€” the genuinely tricky case, fully unpacked.

Complexity & recap

121โ€“127

Average-case vs. worst-case, honestly, and what's next.

CSE3144 โ€” Lecture 23
L23 ยท 01 โ€” Recap & a richer example ~8 min

A new, bushier tree for today's operations

Quick recap from Lecture 22
  • A k-d tree stores one point per node; each level has a fixed cutting dimension, alternating x, y, x, y, ... down the tree.
  • Every node's split line only extends across the sub-region that node is responsible for โ€” the tree literally is a recursive partition of space.
  • Lecture 22's 6-point example was deliberately small and mostly single-child, to keep the CONCEPT of construction clear.
Why we need a new example today

Today's whole point is pruning โ€” skipping subtrees without visiting them. A tree with real branches on both sides is needed to show a prune that actually saves visiting real, populated nodes, not just an empty pointer. So: 7 points, inserted in the order (50,50), (20,30), (70,80), (30,20), (10,60), (60,30), (90,70).

Today's tree and its partition (already built โ€” construction itself is Lecture 22's job)
CSE3144 โ€” Lecture 23 ยท A richer example
L23 ยท 02 โ€” Range search โ€” concept ~8 min

Report every point inside a rectangle, without touching every node

The query

Given a rectangle [x₀,x₁] × [y₀,y₁], report every stored point that falls inside it. A naive scan checks all n points: O(n). A k-d tree can often do much better by skipping entire subtrees.

Shorthand used in the trace: lo[x]=x₀, hi[x]=x₁, lo[y]=y₀, hi[y]=y₁ — whichever pair matches the current node's cutting dimension is the one that gets compared at that node.

The algorithm

At each node, visiting cost is O(1): check if the node's own point lies in the query rectangle (report if so), then decide which children to recurse into. Say this node's cutting dimension is d (its own coordinate along d is what earlier split the plane into a left half where d-values are smaller, and a right half where they're ≥) — then, comparing ONLY on d:

  • Recurse into the left subtree (smaller-d half) only if the query rectangle's lower bound on d is ≤ the node's own d-coordinate — i.e. the rectangle still overlaps that smaller-d region, so points down there could still be in range.
  • Recurse into the right subtree (larger-d half) only if the query rectangle's upper bound on d is ≥ the node's own d-coordinate — same idea, mirrored.
  • If both conditions hold, the rectangle straddles this node's split line — visit both children, no pruning happens here.
Scope note

We'll trace this on today's 7-point tree with a rectangle that prunes a real 3-node subtree in a single comparison โ€” not a toy example where nothing is actually skipped.

CSE3144 โ€” Lecture 23 ยท Range search concept
L23 ยท 03 โ€” Animation: range search ~18 min

Query rectangle [0,40] × [0,70]

Watch which subtree gets skipped entirely, and why โ€” every visit and every prune-decision is its own step.

Interactive โ€” range search on the 7-point tree
CSE3144 โ€” Lecture 23 ยท Range search
L23 ยท 04 โ€” Nearest-neighbor search โ€” concept ~14 min

"Closest in the tree" and "closest in space" are different questions

Why the obvious idea fails

Tempting shortcut: find the region (leaf cell) that WOULD contain query point Q if we inserted it, and return the point stored there. This fails โ€” the true nearest neighbor can sit in a completely different branch of the tree, just across a split line from Q's own cell. The animation just below runs the shortcut on a real query so you can see it fail.

The correct idea
  • Keep a running best point found so far and its distance to Q.
  • At each node, visit the subtree that WOULD contain Q first (most likely to improve the best) โ€” then the other subtree.
  • Prune the other subtree only if the distance from Q to the splitting line already exceeds the current best distance โ€” if the line itself is farther than our best answer, nothing on the far side of it can possibly be closer.
Interactive โ€” the shortcut, run on Q = (49,25)
Scope note

We'll trace a query where the root LOOKS close but isn't the answer, the true answer is found three levels down a different branch, and a real 3-node subtree gets pruned by the distance test at the very end.

CSE3144 โ€” Lecture 23 ยท Nearest-neighbor concept
L23 ยท 05 โ€” Animation: nearest-neighbor search ~25 min

Query point Q = (65,35)

Watch the search radius (dashed circle around Q) shrink each time a better point is found, and watch the final prune succeed only because the circle no longer reaches the splitting line.

Interactive โ€” nearest-neighbor search on the 7-point tree
CSE3144 โ€” Lecture 23 ยท Nearest-neighbor search
L23 ยท 06 โ€” FindMin โ€” the operation deletion depends on ~10 min

Minimum along ONE dimension โ€” a different pruning rule

The query

FindMin(dim): of all stored points, find the one with the smallest coordinate in dimension dim (e.g. smallest x, or smallest y) โ€” regardless of the other coordinate.

The rule that makes it fast
  • If the current node's cutting dimension equals dim: everything in the right subtree has a ≥ value on dim than this node โ€” it CANNOT contain the minimum. Recurse left only (and compare against this node itself).
  • If the cutting dimension is different from dim: the minimum could be on either side (this node's split doesn't order anything with respect to dim) โ€” must recurse into both subtrees.
What if two points tie for the minimum?

Return either one — it doesn't matter which. Whichever tied point is picked, every OTHER point (including the other tie) already satisfies ≥ the returned value, since a tie means EQUAL, and equal satisfies ≥. This is exactly why FindMin is the safe choice for deletion's swap fix (Section 09): the invariant it has to satisfy is non-strict (≥), so ties never break it — unlike FindMax, whose shortcut needs a STRICT <, which a tie fails.

We'll trace FindMin(x) and FindMin(y) on the SAME tree, back to back, so the contrast in pruning behaviour is visible immediately.

CSE3144 โ€” Lecture 23 ยท FindMin concept
L23 ยท 07 โ€” Animation: FindMin(x) then FindMin(y) ~10 min

Same tree, two dimensions, two different amounts of pruning

Interactive โ€” FindMin on the 7-point tree
CSE3144 โ€” Lecture 23 ยท FindMin
L23 ยท 08 โ€” Deletion โ€” concept ~10 min

Why a BST's deletion trick quietly breaks here

What a plain BST does

A BST has three deletion cases, decided by how many children the node has:

  • Leaf — just remove it.
  • One child — splice: lift the child (with its whole subtree) into the deleted node’s place. Safe because every node compares on the same single key.
  • Two children — promote findMin(right), then delete that minimum from the right subtree.
Why the k-d tree can’t reuse those cases

Splicing is illegal. A child sits one level lower, so it cuts on a different dimension than the slot it would move into. Lifted up, its whole subtree would be routed on the wrong axis. So even a node with a single child must be handled by replacing its value with a minimum from a subtree — and what matters is no longer how many children it has, but whether a right subtree exists.

BST casek-d tree caseWhat to do (d = deleted node’s cutting dimension)
LeafLeafRemove it — identical to a BST.
Two childrenHas a right subtree (left may or may not exist)Promote findMin(right, d), then recursively delete it from the right — same as a BST; the left subtree is left untouched.
One child (right)Has a right subtreeSame row as above — a BST would splice, the k-d tree promotes findMin(right, d) instead.
One child (left)No right subtree — the tricky caseCannot splice and cannot use findMax(left, d) — SAME dimension d as findMin below (next slide explains why). Promote findMin(left, d) and swap the subtrees.
The fix: swap the subtrees

Instead of promoting from the left and leaving it in place, use findMin(left, d) and promote it. Then recursively delete that point from the old left subtree, and hang whatever remains as the promoted node’s new RIGHT subtree, leaving its left slot empty. Every remaining point has a d-value ≥ the promoted minimum, so “right” is exactly where it belongs. This keeps every remaining point on the geometrically correct side, at the cost of one extra recursive delete call. This case โ€” no right subtree โ€” is genuinely the trickiest part of the whole lecture, so we will unpack every sub-step of it in the animation.

CSE3144 โ€” Lecture 23 ยท Deletion concept
L23 ยท 09 โ€” Why not findMax(left, d) when there is no right subtree? ~4 min

The largest value on the left can tie — and a tie belongs on the right

In a picture — the same deletion, drawn as trees
In words
  • How can the left subtree contain a tie at all? Two different points can share one coordinate: (40,20) and (40,70) both have x = 40. They were separated by y, at a node that cuts y, so no node ever compared their x-values with each other — the tie sits there unnoticed.
  • The rule every k-d node keeps: its left subtree is strictly smaller on the cutting dimension d, its right subtree is ≥ — a tie goes right.
  • Promoting a point makes it the new split value. findMax(left, d) promotes the LARGEST d-value — but the left subtree may hold another point with the same value.
  • That point stays on the left, yet now equals the split value, so it belongs on the right. A search for it turns right at this node and never finds it.
  • findMin(left, d) promotes the SMALLEST value, so every other left point is ≥ it: all of them belong on the right — exactly where the subtree swap puts them. Ties are harmless.
  • It is the tie rule that makes the maximum unsafe: in a tree that put ties on the LEFT, findMax(left, d) would be the safe choice.

Next slide: the same deletion as a step-by-step animation.

CSE3144 โ€” Lecture 23 ยท Why not findMax(left, d)
L23 ยท 10 โ€” Animation: why not findMax(left, d)? ~4 min

Delete (50,50) twice: the shortcut loses a point, the swap doesn’t

(50,50) has no right subtree; its left subtree holds three points, and two of them tie on the cutting dimension: (40,20) and (40,70) both have x=40. Attempt 1 uses the BST-style shortcut; attempt 2 uses findMin(left) plus the subtree swap.

Interactive โ€” delete (50,50), shortcut vs. swap
CSE3144 โ€” Lecture 23 ยท Why not findMax(left, d) — animation
L23 ยท 11 โ€” Animation: deletion ~20 min

Delete (70,80) โ€” the no-right-subtree case

Every FindMin sub-call, every promotion, and the final subtree rebuild gets its own step โ€” nothing folded together.

Interactive โ€” delete (70,80) from the 7-point tree
CSE3144 โ€” Lecture 23 ยท Deletion
L23 ยท 12 โ€” Complexity & recap ~6 min

Average-case wins, worst-case honesty

OperationAverage caseWorst case
INSERTO(log n)O(n) — e.g. inserting already-sorted points
RANGE SEARCH (2D)O(√n + k)O(n) — k = number of points reported
NEAREST-NEIGHBORO(2d + log n) in practiceO(n) — might have to search close to the whole tree
FINDMINO(log n)O(n)
DELETEO(log n) — one FindMin plus one recursive delete, same order as searchO(n)

The recurring theme: every operation's efficiency comes from the SAME source โ€” being able to rule out an entire subtree using only the current node's cutting dimension, without inspecting a single point inside it. Every worst case above happens when that pruning stops working, typically because of an adversarial insertion order (Lecture 22's comparison table, Section 08) — the same shape-quality problem BSTs have always had, one dimension later.

Lecture 24 โ€” next

Quad Trees and Oct Trees

CO CSE3144.4 โ€” full range search, neighbor-finding, and balancing for the quadtree family Lecture 22 only introduced, plus the 3D octree.

Homework โ€” bring to Lecture 24
  • On today's ORIGINAL 7-point tree (before any deletion), run range search with rectangle [40,100]×[0,100]. Show every visit and every prune, and state the final report set.
  • On the same original tree, run nearest-neighbor search for Q=(15,50). Show the traversal order, every distance comparison, and every prune decision.
  • On the tree that RESULTS from today's deletion of (70,80), delete (50,50) โ€” the root. Identify which case applies (has a right subtree, or not) and carry out every step.
CSE3144 โ€” Lecture 23

Questions?

Dr. Manu Shrivastava โ€” LHC 308F โ€” Friday 2:00โ€“5:00 PM

Next: Lecture 24 โ€” Quad Trees and Oct Trees.