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

Introduction to Spatial Data Structures

Every structure so far has organized keys along one line. The moment data has two or more independent coordinates โ€” a location on a map, a pixel in an image, a point in a scatter plot โ€” that one-line ordering stops working, and a genuinely different family of data structures takes over.

Dr. Manu ShrivastavaCourse Instructor ยท Consultation Fri 2โ€“5 PM, LHC 308F
~98 minutesSession outcome: explain multidimensional data representation techniques
L22 ยท 00 โ€” Agenda ~98 min

Why one dimension isn't enough, and two ways to fix it

Why 1D orderings break down

00โ€“08

A concrete 2-attribute example: sorting by one key, then the other, destroys spatial nearness.

Vocabulary: query types & two families

08โ€“16

Range query, nearest-neighbor, point location โ€” and space-partitioning vs. data-partitioning as the two design philosophies.

Technique 1: k-d trees

16โ€“24

One tree, alternating cutting dimension โ€” a direct generalization of the BST.

Animation: building a k-d tree

24โ€“44

Six points, six inserts, watching the tree AND the 2D partition grow together, step by step.

k-d trees beyond 2D

44โ€“52

Three (or more) coordinates: the cutting dimension simply cycles x, y, z, x, ... — one worked 3-D build.

Technique 2: quad trees

52โ€“60

Recursive splitting into four fixed quadrants โ€” a completely different philosophy from k-d trees.

Animation: building a quad tree

60โ€“80

Five points, full recursive construction โ€” every quadrant checked, every split shown.

k-d tree vs. quad tree, side by side

80โ€“85

Same job, two very different structural bets.

The wider family: octrees, BSP trees, R-trees

85โ€“93

A quick preview of what Lectures 24โ€“26 will build in full.

Recap & what's next

93โ€“98

Lecture 23: k-d Trees and Multidimensional Searching, in full depth.

CSE3144 โ€” Lecture 22
L22 ยท 01 โ€” Why 1D orderings break down ~8 min

Every structure so far assumed keys could be sorted along one line

BSTs, AVL trees, B-trees, hash tables, even heaps โ€” all of Units Iโ€“III assumed a single, total order on keys: given any two keys, one is simply "less than" the other. Consider four employee records, each with two attributes, (age, salary): (11, 80), (12, 10), (12, 20), (13, 75) โ€” using "age" in units of years-of-service and "salary" in thousands, purely for a clean small example.

Sort by age, then by salary โ€” the natural "1D" fix

This is exactly what a composite-key B+ tree does: sort first by age, then by salary within each age. The resulting order is:

(11,80)
(12,10)
(12,20)
(13,75)
1D order: sorted by age, ties broken by salary
The same 4 points, plotted in the actual 2D plane
age on the x-axis, salary on the y-axis
The problem, made concrete

(11,80) and (13,75) are genuinely close together in the 2D plane โ€” both high-salary points โ€” yet they sit at opposite ends of the 1D order, separated by both (12,10) and (12,20). Meanwhile (11,80) and (12,10), which the 1D order places right next to each other, are about as far apart in salary as two points in this data set can be. 1D adjacency in the sorted order tells you almost nothing about 2D nearness โ€” and that gap only gets worse as the number of attributes grows. This is precisely the problem spatial data structures are built to solve.

CSE3144 โ€” Lecture 22 ยท Why 1D fails
L22 ยท 02 โ€” Vocabulary: query types & two families ~8 min

What we actually want to ask โ€” and two philosophies for answering it

Range query

"Find every point inside this rectangle / region." E.g. every city with population between 50K and 90K located within a bounding box on the map.

Nearest-neighbor query

"Find the point closest to this query point." E.g. the nearest hospital to a given location โ€” the query most responsible for k-d trees existing at all.

Point location / intersection

"Which region contains this point?" or "which stored objects overlap this shape?" โ€” the core query behind GIS map layers and collision detection.

Family 1 โ€” Space-partitioning

Recursively divide the space itself into regions, then see which points fall into which region. The regions are decided first; where the data happens to sit is almost incidental. k-d trees, quad trees, octrees, and BSP trees are all in this family โ€” today's two full animations, plus Lecture 24's Quad/Oct trees and Lecture 25's BSP trees.

Family 2 โ€” Data-partitioning

Group the data itself into clusters, then draw the tightest bounding region around each cluster. The regions are a consequence of where the data sits, and can overlap. R-trees are the flagship example โ€” Lecture 26.

CSE3144 โ€” Lecture 22 ยท Query types & families
L22 ยท 03 โ€” Technique 1: k-d trees ~8 min

One tree, alternating cutting dimension โ€” a direct BST generalization

The core idea
  • A k-d tree (originally "k-dimensional tree", now read as "a kd-tree of dimension d") stores one point per node, exactly like a BST stores one key per node.
  • The difference: each level of the tree has a cutting dimension โ€” level 0 compares x, level 1 compares y, level 2 compares x again, and so on, cycling through all d dimensions (a 3-D example follows the animation).
  • At a node whose cutting dimension is x: a new point goes left if its x-coordinate is smaller than the node's, right otherwise โ€” precisely BST logic, just restricted to one coordinate at a time.
  • Still exactly 2 children per node, regardless of dimension count d โ€” this is what makes k-d trees attractive over a naive 2d-way tree.
What each split means geometrically

Every node doesn't just store a point โ€” it also implicitly draws a line that splits the plane (or a hyperplane in higher dimensions): a vertical line at that point's x-value if the cutting dimension is x, a horizontal line at its y-value if the cutting dimension is y. The line only extends across the region that node is responsible for, not across the whole plane. This is the representation technique: the tree IS a recursive partition of space, not just a comparison structure.

Scope note

Today we only build the tree and watch the partition emerge. Range search, nearest-neighbor search, and deletion for k-d trees โ€” all genuinely subtle, especially deletion โ€” are Lecture 23's full treatment.

CSE3144 โ€” Lecture 22 ยท k-d tree concept
L22 ยท 04 โ€” Animation: building a k-d tree ~20 min

Insert (30,40), (5,25), (10,12), (70,70), (50,30), (35,45)

Watch the tree and the 2D partition grow together, one insert at a time โ€” cutting dimension starts at x (level 0) and alternates with each level down.

Interactive โ€” build a k-d tree, 6 inserts
CSE3144 โ€” Lecture 22 ยท k-d tree construction
L22 ยท 05 โ€” k-d trees beyond 2D ~8 min

Nothing here is special to two coordinates — the cutting dimension just cycles through more axes

The rule for k dimensions
  • A point now has k coordinates — (x, y, z) for k = 3, an (R, G, B) colour, a sensor reading, a feature vector.
  • The cutting dimension at a node is its level mod k: with k = 3 the levels cut x, y, z, x, y, z, ...
  • At each node the comparison is still ONE coordinate: smaller goes left, greater-or-equal goes right. Still exactly 2 children per node.
  • Geometrically each split is now a plane (a hyperplane for k > 3) cutting the region into two half-spaces.
Which coordinate is compared at each level
Level0123456
k = 2xyxyxyx
k = 3xyzxyzx
k = 4xyzwxyz

Insert order: A(50,30,70), B(20,60,40), C(80,10,20), D(30,80,90), E(10,40,10), F(35,90,60).

Interactive — build a 3-D k-d tree, 6 inserts
Why not 100 dimensions?

Each level inspects only ONE coordinate, so a root-to-leaf path needs depth ≥ k just to use every coordinate once — roughly 2k points. The rule of thumb: k-d trees work well when n ≫ 2k. In very high dimensions (beyond roughly 10–20) almost every query ends up visiting most of the tree — the “curse of dimensionality” — and other techniques take over.

CSE3144 โ€” Lecture 22 ยท k-d trees in higher dimensions
L22 ยท 06 โ€” Technique 2: quad trees ~8 min

Recursive splitting into four FIXED quadrants โ€” a different philosophy entirely

The core idea
  • A quadtree is a rooted tree in which every internal node has exactly 4 children โ€” each child corresponds to one quadrant (NW, NE, SE, SW) of the node's square region.
  • Construction rule: start with one square containing all the points. If it contains more than 1 point, split it into 4 equal quadrants at the square's midpoint, assign each point to the quadrant it falls in, and recurse on each quadrant that still has more than 1 point.
  • Unlike a k-d tree, the split point is never a data point โ€” it is always the geometric midpoint of the current square, fixed in advance, regardless of where the data actually sits.
  • Tie-break rule: a point exactly on a split line always goes to the ≥ side — x ≥ midx → east, y ≥ midy → north — the same ≥-goes-to-the-larger-side convention used for k-d tree cuts in Section 04.
The trade-off this creates

Because splits are geometric rather than data-driven, a quadtree's shape does not depend on insertion order โ€” the same point set always produces the same partition. But it also means quadtrees can become unbalanced when points cluster tightly: a handful of nearby points can force several levels of splitting just to separate them, while other quadrants stay empty leaves after a single split. We'll see exactly this in today's example.

Scope note

Today we only build the tree via this recursive rule. Neighbor-finding, balancing, and octrees (the 3D generalization) are Lecture 24's full treatment.

CSE3144 โ€” Lecture 22 ยท Quad tree concept
L22 ยท 07 โ€” Animation: building a quad tree ~20 min

Points A(15,15), B(25,60), C(65,20), D(70,65), E(20,20)

Full recursive construction on an 80×80 square โ€” every quadrant is checked explicitly, even the ones that turn out empty, and nothing is skipped.

Interactive โ€” build a quad tree, 5 points
CSE3144 โ€” Lecture 22 ยท Quad tree construction
L22 ยท 08 โ€” k-d tree vs. quad tree, side by side ~5 min

Same job, two very different structural bets

Propertyk-d treeQuad tree
What decides the splitThe inserted data point itselfThe fixed geometric midpoint of the current region
Children per internal node24 (8 for the 3D octree, Lecture 24)
Depends on insertion order?Yes โ€” different orders of the same points can give different treesNo โ€” the same point set always gives the same partition
Empty regionsNever represented as a nodeCan appear as empty leaves (we saw two in today's example)
Vulnerable toAdversarial insertion order (e.g. sorted-order inserts)Tight point clustering (forces extra levels of splitting)

Neither weakness is fatal โ€” both have well-known fixes (balanced/median-choosing k-d tree construction; balanced quadtrees that cap the depth difference between neighbors) โ€” but notice the pattern: it's the same shape-quality problem we saw with BSTs back in Lecture 7, just re-appearing in two dimensions with two different causes.

CSE3144 โ€” Lecture 22 ยท k-d tree vs. quad tree
L22 ยท 09 โ€” The wider family: octrees, BSP trees, R-trees ~8 min

Three more structures built on the same two philosophies

Octree โ€” the 3D analog of a quad tree

Same recursive-midpoint-split rule, but every internal node has 8 children (one per octant of a cube) instead of 4. Used for 3D point clouds, volumetric graphics, and collision detection in 3D games. Full treatment: Lecture 24.

BSP tree โ€” arbitrary-orientation splitting

A genuine generalization of both k-d trees and quadtrees: splitting hyperplanes can have any orientation, not just axis-aligned. Invented in 1969 for 3D computer-graphics rendering (solving the painter's-algorithm sorting problem); still central to ray tracing and game collision detection. Full treatment: Lecture 25.

R-tree โ€” switching to data-partitioning

The flagship data-partitioning structure: groups nearby objects and stores the tightest bounding box around each group, nested recursively like a B-tree. Stays balanced under insert/delete โ€” unlike either technique built today. The standard spatial index in GIS and spatial databases. Full treatment: Lecture 26.

CSE3144 โ€” Lecture 22 ยท The wider family
L22 ยท 10 โ€” Where spatial data structures actually run ~5 min

Any time "location" is part of the data, one of today's ideas is underneath

GIS & mapping

"Every hotel within 5 miles" or "every city on the Nile" โ€” range and containment queries over millions of geographic records.

Computer graphics

Mesh generation for finite-element simulation (quadtrees), ray tracing and scene rendering (BSP trees), 3D model storage (octrees).

Spatial databases

Multidimensional range queries like "50 < age < 55 AND 80K < salary < 90K" โ€” exactly the kind of query Section 01 showed a plain sorted index cannot answer efficiently.

Nearest-neighbor / ML

"Find the 5 most similar faces" or the nearest training example to a query point โ€” k-d trees are a standard accelerator for k-NN search.

Games & robotics

Collision detection between thousands of objects, and motion planning around obstacles, both lean on space-partitioning trees to avoid all-pairs comparisons.

Image processing

Quadtree image compression: uniform-color regions collapse to a single leaf instead of storing every pixel.

CSE3144 โ€” Lecture 22 ยท Applications
L22 ยท 11 โ€” Recap & what's next ~5 min

A one-line order cannot capture multidimensional nearness

Lecture 23 โ€” next

k-d Trees and Multidimensional Searching

CO CSE3144.4 โ€” the full operation suite this lecture deferred: range search, nearest-neighbor search (with real pruning), and the surprisingly subtle deletion algorithm.

Homework โ€” bring to Lecture 23
  • Build a k-d tree by inserting (40,20), (20,60), (60,70), (10,10), (70,30) in that order, alternating x then y from the root. Draw both the tree and the resulting 2D partition.
  • Insert the SAME 5 points in a different order and check: does the resulting tree (and partition) come out identical? Explain why or why not, tying your answer to Section 08's comparison table.
  • Build a 3-D k-d tree (cutting x, y, z, x, ...) by inserting (10,20,30), (5,40,15), (30,10,50), (20,25,5) in that order. For each insert, write the level, the cutting dimension and every comparison.
  • Build a quad tree over the 6 points (10,10), (15,15), (12,70), (70,15), (65,68), (72,72) on an 80×80 square. Show every quadrant check, including any empty leaves.
CSE3144 โ€” Lecture 22

Questions?

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

Next: Lecture 23 โ€” k-d Trees and Multidimensional Searching.