CSE3144 · Advanced Data Structures · Jul–Nov Semester 2026 · Lecture 27 of 36 · CO CSE3144.4 · Self-study

Applications of Spatial Data Structures

Five structures, one shared job: answer “what is near what” fast. This self-study lecture steps back from the mechanics of Lectures 22–26 and shows where each structure is used — in graphics, in GIS and in machine learning — with an interactive experiment for each, a self-check quiz and worked practice problems.

Dr. Manu ShrivastavaCourse Instructor · Consultation Fri 2–5 PM, LHC 308F
~150 minutes of studySession outcome: analyze applications in graphics, GIS, and machine learning
L27 · 00 — How to study this lecture ~5 min

A self-study lecture: read, experiment, then test yourself

By the end you should be able to
  • name, for a given application, the spatial structure that fits it and justify the choice;
  • explain quadtree image compression, broad-phase collision detection and filter-and-refine queries with numbers;
  • run a k-nearest-neighbour classification on a k-d tree by hand, including evictions and pruning;
  • explain why exact k-d tree search stops working in high dimensions.
How to work through it
  • Prerequisites: k-d trees and nearest-neighbour search (L23), quadtrees (L22, L24), BSP trees (L25), R-trees (L26). Slide 01 recaps all of them.
  • Animations: press Next (or the right-arrow key) and read the explanation under the picture before moving on. The last step of most animations is a free-play mode — use it.
  • Before each Next, predict what will happen. Checking a prediction teaches far more than watching.
  • Finish with the self-check quiz (slide 13) and the practice problems (slide 14). Answers are hidden until you open them.

01 Recap: the five structures

~10 min

02–04 Graphics: overview, image compression, collision detection

~35 min

05–06 GIS: overview, filter-and-refine radius query

~20 min

07–10 Machine learning: overview, refresher, k-NN on a k-d tree, the curse of dimensionality

~50 min

11–12 Decision guide and case studies

~12 min

13–15 Self-check quiz, practice problems, recap

~20 min
CSE3144 — Lecture 27
L27 · 01 — Recap: the five structures, standalone ~10 min

Everything you need, in one table

StructurePartitionsBranchingCore ideaTypical cost
k-d tree (L22, L23)Space, at data points2Alternate the cutting dimension (x, y, x, y, …) by depth; each node’s point splits only its own region.Range query O(√n + answers) in a balanced 2D tree; nearest neighbour usually O(log n), O(n) in the worst case
Quadtree / octree (L22, L24)Space, at fixed midpoints4 (2D) / 8 (3D)Split a square (cube) into four (eight) equal parts whenever it holds more than one point — the split position never depends on the data.O(depth) insert and search; depth grows where points cluster
BSP tree (L25)Space, by planes at any angle2Generalises the k-d tree: each splitting plane follows a polygon of the scene; polygons crossing a plane are cut in two.O(n) to produce a back-to-front order for any viewpoint; O(depth) to locate a point
R-tree (L26)The data (groups of objects; boxes may overlap)up to M per node (3 or 4 in our examples; 50–200 on disk)Group nearby objects and bound each group with its minimum bounding rectangle; balanced like a B-tree through least-enlargement insertion and node splitting.O(logM n) per path followed — overlapping boxes can force several paths

The one fact that organises all five: the k-d tree, the quadtree/octree and the BSP tree partition space — their regions never overlap. The R-tree partitions the data — its regions can overlap. Most applications below are really a story about which of the two philosophies fits the job.

CSE3144 — Lecture 27 · Standalone recap
L27 · 02 — Graphics applications ~8 min

Rendering, collision and compression all reduce to “what is near what”

Drawing order and ray tracing (BSP and k-d trees)

A BSP tree built once from a static scene gives a correct back-to-front drawing order for any viewpoint with one O(n) walk and no sorting (Lecture 25) — this is how Doom (1993) and Quake drew their levels. Ray tracing asks a related question: which surface does this ray hit first? Walking a BSP or k-d tree front-to-back along the ray finds it without testing every polygon. Today’s ray tracers, including the ray-tracing hardware in GPUs, mostly use a bounding volume hierarchy — a tree of nested bounding boxes, a close cousin of the R-tree.

Collision detection (quadtrees and octrees) — slide 04

Testing every pair of n objects costs n(n−1)/2 tests per frame. A quadtree (2D) or octree (3D) broad phase tests only objects that share a cell, so far-apart objects are never compared. The exact narrow phase test then runs on the few remaining pairs.

Mesh generation (balanced quadtrees)

Simulations (heat flow, stress analysis) need triangle meshes that are fine near object boundaries and coarse elsewhere. A quadtree split only where the geometry demands it, then balanced so neighbouring cells differ by at most one level (Lecture 24), produces exactly that — and the balance rule is what keeps the resulting triangles well shaped.

Image and volume compression (quadtrees and octrees) — slide 03

A block of one colour becomes a single quadtree leaf instead of one entry per pixel; a uniform region of a 3D scan becomes a single octree leaf. The saving depends on how much of the image is uniform — the next slide measures it.

CSE3144 — Lecture 27 · Graphics applications
L27 · 03 — Experiment: quadtree image compression ~15 min

One leaf per uniform block, four children per mixed block

Interactive — build the region quadtree level by level, then paint your own image
CSE3144 — Lecture 27 · Quadtree image compression
L27 · 04 — Experiment: collision detection with a quadtree ~12 min

Test only the pairs that share a cell

Interactive — all pairs vs a quadtree broad phase
CSE3144 — Lecture 27 · Collision detection
L27 · 05 — GIS applications ~8 min

R-trees are the default spatial index because real GIS objects have size and overlap

Why R-trees specifically win here

Real-world GIS objects — buildings, parcels, roads, rivers — have length and area, are added and removed constantly (a city approves new buildings every day), and their bounding boxes overlap (a road crosses many parcels). Lecture 26’s R-tree was designed for exactly this: it stores each object’s bounding box once, stays balanced under inserts and deletes like a B-tree, and tolerates overlapping boxes.

The query types it answers
  • Region queries: “every parcel that overlaps this proposed highway” — R-tree SEARCH with the highway’s box, then an exact geometry test.
  • Proximity queries: “every restaurant within 2 km of this customer” — search with the circle’s bounding box, then an exact distance test (slide 06).
  • Point-in-region queries: “which ward or pin-code area contains this address?” — the same overlap test with a zero-area query box, then a point-in-polygon test.
  • Nearest-object queries: “the nearest hospital” — a best-first search over the boxes, the R-tree version of Lecture 23’s nearest-neighbour search.
Where space-partitioning trees still show up in GIS

Layers that rarely change and are naturally grid-like — terrain elevation, satellite imagery, map tiles — are organised with quadtrees: the familiar “zoom level” scheme of web maps is a quadtree, each tile splitting into four tiles at the next level. Moving points such as delivery riders or cabs are often bucketed into a fixed hierarchical grid, because a grid cell is cheap to recompute every few seconds.

CSE3144 — Lecture 27 · GIS applications
L27 · 06 — Experiment: a radius query, filter then refine ~12 min

The index finds candidates with boxes; exact geometry checks only those

Interactive — “restaurants within r of the customer” on an R-tree
CSE3144 — Lecture 27 · Filter and refine
L27 · 07 — Machine learning applications — concept ~8 min

k-d trees: the classic accelerator for “who are my neighbours?”

Where nearest-neighbour search shows up in ML
  • k-nearest-neighbours (k-NN) classification: label a new point by a majority vote among its k closest labelled training points (slide 09).
  • Similarity search: “find the 5 most similar faces / songs / product images”, each item described by a vector of features.
  • Clustering and anomaly detection: many algorithms repeatedly ask for the neighbours of every point.
  • Vector databases and embedding search: retrieval systems store text or images as vectors with hundreds of dimensions and need fast nearest-neighbour lookup.
The honest caveat: the curse of dimensionality

k-d trees work well in low dimensions — a rule of thumb is that n should be much larger than 2d. As d grows, Lecture 23’s pruning test (skip a subtree when its splitting plane is farther than the current best distance) succeeds less and less often, and the search slides towards a linear scan. Slide 10 measures this. That is why embedding search with hundreds of dimensions uses approximate nearest-neighbour methods, which accept an occasionally slightly-wrong answer in exchange for speed.

CSE3144 — Lecture 27 · ML applications concept
L27 · 08 — Quick refresher: how a k-d tree prunes ~6 min

Everything the next animation assumes, restated in full

Structure

One point per node. The root compares x, its children compare y, the next level x again, and so on. A new point goes left if it is smaller than the node’s value in that level’s coordinate, right otherwise. Each node therefore owns a rectangle of the plane, and its point splits that rectangle in two.

Nearest-neighbour search rule

Keep the best answer found so far. At each node: compute the node’s distance to Q and update the best; search first the side of the splitting line that contains Q; when you come back, search the other side only if the distance from Q to the splitting line is smaller than the current best distance. If the line is farther, every point beyond it is farther too, so the whole side is skipped.

The one change for k neighbours

Keep a list of the best k points found so far, sorted by distance. Until the list holds k points, every visited point is added. After that, a new point enters only if it is closer than the last point of the list (the worst of the best), which it then evicts. The pruning test compares the line’s distance with that worst-of-the-best distance, so with a bigger k the circle is larger and fewer subtrees can be skipped.

CSE3144 — Lecture 27 · k-d tree refresher
L27 · 09 — Animation: k-NN classification via a k-d tree ~25 min

k = 3, query Q = (65,35), on Lecture 23’s 7-point tree

Class A: (50,50), (20,30), (30,20), (10,60). Class B: (70,80), (60,30), (90,70). Lecture 23 found the single nearest neighbour of Q: (60,30), class B. What does k = 3 say?

Interactive — k-nearest-neighbour classification, then free play
CSE3144 — Lecture 27 · k-NN classification
L27 · 10 — Experiment: the curse of dimensionality ~12 min

Why the k-d tree that pruned so well in 2D stops pruning in 30D

Interactive — measured in your browser: 2000 random points, 25 queries per dimension
CSE3144 — Lecture 27 · Curse of dimensionality
L27 · 11 — Decision guide: which structure, when ~6 min

One scenario-based table, all five structures

ScenarioStructureWhyWhy not the obvious alternative
Nearest-neighbour search / k-NN classification over labelled feature vectors (few dimensions)k-d treePoint data, exact answers, cuts adapt to where the data is.Quadtree: fixed midpoints waste levels on clustered data, and branching 2d explodes as d grows.
Embedding / vector search with hundreds of dimensionsApproximate NN methods (beyond this course)Exact pruning collapses in high dimensions (slide 10).k-d tree: visits almost every node, slower than a plain scan.
Collision detection between moving objects in a gameQuadtree / octree (or a uniform grid)Cheap to rebuild every frame; empty space is skipped.R-tree: insertion with splits is costlier to maintain for objects that move every frame.
Drawing a static 3D scene in correct order from any viewpointBSP treeBuilt once; one walk per frame gives back-to-front order.Sorting by distance: wrong for overlapping or crossing polygons (Lecture 25).
Finite-element mesh generationBalanced quadtreeFine cells only near boundaries; the ≤1 level rule keeps triangles well shaped.Uniform grid: fine everywhere means far too many triangles.
City- or country-scale GIS database with frequent inserts and deletesR-treeObjects with size stored once; balanced under updates.Quadtree / k-d tree: objects crossing cuts are duplicated; shape depends on insertion order.
Image, map-tile or 3D-scan storageQuadtree / octreeUniform regions collapse to single leaves; levels match zoom levels.Raw pixel arrays: no saving for uniform regions, no levels of detail.
CSE3144 — Lecture 27 · Decision guide
L27 · 12 — Case studies ~6 min

Real systems, running on ideas built across five lectures

Spatial databases

PostGIS (the spatial extension of PostgreSQL), Oracle Spatial and SQLite’s R*Tree module index geometry columns with R-tree-family structures — Lecture 26’s structure in production, queried with filter-and-refine.

Web maps

Map tiles are addressed by zoom level and tile position: each tile splits into four at the next zoom level — a quadtree over the whole Earth. Google’s S2 library and Uber’s H3 index locations with hierarchical cells of the same spirit.

Game engines and 3D graphics

Classic engines (Doom, Quake) stored levels as BSP trees. Modern engines and GPU ray tracing mostly use bounding volume hierarchies; octrees and grids remain common for collision broad phases and for voxel worlds.

ML libraries

General-purpose libraries such as scikit-learn offer k-d trees and ball trees for exact nearest-neighbour search and pick between them and a plain scan depending on the data — slides 09 and 10 in production form.

Medical imaging

CT and MRI volumes are 3D grids of voxels; octrees store large uniform regions compactly and let viewers load coarse levels first, refining where the user zooms in.

Robotics and self-driving

3D occupancy maps built from laser scanners are commonly stored as octrees (free, occupied or unknown cells), and k-d trees speed up matching one laser scan to the next.

CSE3144 — Lecture 27 · Case studies
L27 · 13 — Self-check quiz ~10 min

Answer first, then open the answer

Q1. A 16 × 16 image is a perfect checkerboard. How many nodes does its region quadtree have, and is that a compression?
Every block larger than one pixel contains both colours, so the tree is complete down to single pixels: 256 leaves plus 1 + 4 + 16 + 64 = 85 internal nodes = 341 nodes, more than the 256 raw pixel values. No compression — a region quadtree only saves space when the image has uniform areas.
Q2. 100 balls move in a game. How many pair tests does the all-pairs method need per frame? Why can a quadtree broad phase never miss a collision?
100 × 99 / 2 = 4950 tests. A ball is listed in every leaf its bounding square overlaps, so two touching balls always share at least one leaf, and every pair sharing a leaf is tested exactly.
Q3. In a filter-and-refine radius query, the index is searched with the circle’s bounding box. Can the filter step miss a true answer? Can it return a wrong one?
It cannot miss an answer: the box contains the whole circle. It can return false candidates — points in the corners of the box, outside the circle — which the refine step removes with an exact distance test. The corners are 1 − π/4 ≈ 21% of the box.
Q4. During a k = 3 search the best list holds distances 7.07, 21.21, 43.01. The search returns to a node whose splitting line is 15 from Q. Prune the far side or search it?
Search it. The pruning test compares the line’s distance with the worst of the best, 43.01. Since 15 < 43.01, a point on the far side could still be closer than 43.01. (This is exactly the root test in slide 09.)
Q5. Why does the same query point visit fewer nodes with k = 1 than with k = 3?
With k = 1 the pruning radius is the single best distance (7.07 for Q = (65,35)); with k = 3 it is the 3rd-best distance (38.08 at the end, 43.01 when the root test happens). A larger radius crosses more splitting lines, so fewer subtrees can be skipped: 4 nodes visited for k = 1, all 7 for k = 3.
Q6. A k-d tree on 1000 points in 20 dimensions answers nearest-neighbour queries. Would you expect it to beat a linear scan? Why?
No. 220 ≈ 106 is far larger than n = 1000, so the tree is very shallow in every coordinate and the best distance stays large compared with the distance to each splitting plane; almost no subtree is pruned (slide 10), and the tree adds overhead to what is effectively a scan.
Q7. A city database stores 2 million building footprints and receives thousands of new permits a month. Quadtree or R-tree — and why?
R-tree. Footprints have size, so in a quadtree any building crossing a cell boundary must be duplicated or kept high in the tree; an R-tree stores each footprint’s box once, stays balanced under inserts and deletes, and maps naturally onto disk pages.
Q8. Why is a BSP tree a good fit for drawing a static level but a poor fit for a scene where walls move?
The tree depends only on where the polygons are, so for a static level it is built once (expensively, with polygon splitting) and reused from every viewpoint. If walls move, the partition is wrong and the tree must be rebuilt — too costly every frame.
CSE3144 — Lecture 27 · Self-check quiz
L27 · 14 — Practice problems with worked solutions ~10 min

Work each problem on paper, then compare

P1. On the same 7-point tree, classify Q2 = (55,55) with k = 1 and with k = 3. Record every visit, eviction and prune.
Distances: (50,50) A 7.07 · (60,30) B 25.50 · (70,80) B 29.15 · (90,70) B 38.08 · (20,30) A 43.01 · (30,20) A 43.01 · (10,60) A 45.28.
k = 1: visit (50,50): best = 7.07. Q.x = 55 ≥ 50 → right first. (70,80): 29.15, no change; Q.y = 55 < 80 → left child (60,30): 25.50, no change. At (60,30), which cuts x = 60: Q.x = 55 < 60, so the near side is the left one, which is empty; the far (right) side holds (90,70), and the line x = 60 is only 5 from Q, less than 7.07 → search it: (90,70) 38.08, no change. Back at the root: line x = 50 is 5 < 7.07 → search the left subtree: (20,30) 43.01, no change; Q.y = 55 ≥ 30 → (10,60) 45.28, no change; back at (20,30): line y = 30 is 25 ≥ 7.07 → prune (30,20). 6 nodes visited. Nearest: (50,50) → class A.
k = 3: the first three visits fill the list: (50,50) 7.07, (60,30) 25.50, (70,80) 29.15; worst = 29.15. (90,70) 38.08 does not enter. At the root, 5 < 29.15 → search left; (20,30) and (10,60) do not enter; at (20,30) the line is 25 < 29.15 away → (30,20) must be visited too (43.01, no change). All 7 visited. Neighbours: A, B, B → class B.
Opposite to Q = (65,35): there k = 1 said B and k = 3 said A. Check both in the free-play step of slide 09.
P2. Classify Q = (65,35) with k = 5. What goes wrong, and how should a classifier handle it?
The four nearest are (60,30) B 7.07, (50,50) A 21.21, (30,20) A 38.08, (90,70) B 43.01. For 5th place, (20,30) A and (70,80) B are both at √2050 ≈ 45.28 — a tie.
If (70,80) is kept (the search met it first, and a later point enters only if strictly closer), the vote is 2 A : 3 B → B. If (20,30) is taken, it is 3 A : 2 B → A. Including both gives 3 : 3, a tied vote.
The answer therefore depends on the tie-breaking rule, which a real classifier must state: for example, keep the earlier training point, or weight each vote by 1/distance (which favours the closer B at 7.07 here). Choosing an odd k avoids ties in the vote for two classes, but not ties in distance.
P3. A drone-delivery service tracks 5000 drones (positions change every second) and 200 fixed no-fly zones (polygons). Which structure for each, and why?
No-fly zones: an R-tree (or even a BSP tree) over the polygons’ boxes — few, static, with size; each drone position becomes a point-in-region query: filter with the boxes, refine with an exact point-in-polygon test.
Drones: moving points updated every second — a quadtree or uniform grid rebuilt (or updated) each second is cheap, and drone-to-drone proximity checks become a broad phase like slide 04. A k-d tree would also work for nearest-drone queries but is costlier to keep balanced under constant movement.
CSE3144 — Lecture 27 · Practice problems
L27 · 15 — Recap & what’s next ~4 min

Same question, three domains: what is near what?

Lecture 28 — next

Bloom Filters and Probabilistic Searching

CO CSE3144.5 — a new unit: trading a small, tunable false-positive rate for dramatic space savings in membership testing. Note the connection: like the filter step of slide 06, a Bloom filter may say “maybe” to a wrong item but never “no” to a right one.

Assignment — bring to Lecture 28
  • Paint a 16 × 16 image in slide 03 that needs at most 40 quadtree nodes but contains all four colours. Sketch it and report the leaf count per level.
  • Classify Q = (40,60) on the slide-09 tree with k = 1 and k = 3, showing every visit, eviction and prune. Verify with the free-play mode.
  • In 3–4 sentences: pick ONE application from today and explain why its structure beats the other four for that specific job.
CSE3144 — Lecture 27

Questions?

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

Next: Lecture 28 — Bloom Filters and Probabilistic Searching.