CSE3144 · Advanced Data Structures · Jul–Nov Semester 2026 · Lecture 26 of 36 · CO CSE3144.4

R-Trees and Spatial Indexing

Every structure since Lecture 22 has partitioned SPACE first and asked where the data landed. Today we flip the approach entirely: group the DATA first, and let the bounding boxes — which are now allowed to overlap — fall out of that grouping. This is the structure GIS systems and spatial databases actually run on.

Dr. Manu ShrivastavaCourse Instructor · Consultation Fri 2–5 PM, LHC 308F
~166 minutesSession outcome: apply spatial indexing for GIS and database systems
L26 · 00 — Agenda ~166 min

Data-partitioning, at last — and the one place trees can overlap

Why R-trees?

00–08

A map app’s everyday question, and what an index for it must do.

Seeing the problem: one query, four indexes

08–22

Scan everything, a B+ tree, a quadtree, an R-tree — on one city map, with a screen you can drag.

R-tree structure & properties

22–28

Leaf and non-leaf entries, capacity, balance — and boxes that may overlap.

Animation: anatomy of an R-tree

28–36

From an object’s outline to its MBR, to leaves, to the root.

Building an R-tree — the insertion algorithm

36–48

ChooseLeaf, split, AdjustTree: the pseudocode, with one number worked out.

Animation: building the tree, one insertion at a time

48–88

8 boxes, capacity 3, 4 splits — one split climbs all the way to a new root.

The splitting heuristic

88–96

All 7 ways to split one node, and why we cannot try them all for real node sizes.

Bulk loading: building the tree in one pass

96–108

Sort-Tile-Recursive on the city map, compared with insertion.

SEARCH — concept

108–114

Overlap-test your way down; sometimes several subtrees qualify.

Animation: SEARCH, two queries

114–130

One query prunes almost everything; the other must take several paths and pays for dead space.

Deletion — concept

130–136

Search, remove, and re-insert an underfull node’s survivors.

Animation: deleting from the tree

136–150

Delete h (two nodes removed, two re-insertions, height 3 → 2), then delete a (the common case).

Complexity, variants & applications

150–160

What it costs, R+/R* trees, and where this actually runs.

Recap & what's next

160–166

Lecture 27: Applications of Spatial Data Structures.

CSE3144 — Lecture 26
L26 · 01 — Why R-trees? ~8 min

“What is on my screen?” — asked millions of times a day

The everyday version

Open a food-delivery or cab app and drag the map. Every time the screen moves, the app asks its database: which restaurants, roads and buildings overlap this rectangle? That is a two-dimensional range query, and the objects in it are not points: a road, a park or a campus has length and width. The same question appears as “all parcels touching this proposed highway”, “all parts touching the fuselage” in CAD, or “50 < age < 55 AND 80K < salary < 90K” in an ordinary table.

Recall from Lecture 22

A composite-key B+ tree on <age, sal> sorts by age, then by sal — it lays 2D space out on one line. (11,80) and (13,75), close in 2D, end up far apart in that order, while (11,80) and (12,10) are neighbours in the order but about as far apart in 2D as two points can be. A one-dimensional order cannot keep 2D neighbours together.

What the index must doB+ treeHash indexk-d / quad / BSP treeR-tree
Answer 2D (or 3D) range queriesone dimension onlyno — exact match onlyyesyes
Store objects with size, each exactly oncenonono — built for points; an object crossing a cut is duplicatedyes — stores each object’s bounding box once
Stay balanced on disk under inserts and deletesyesyesno — shape depends on the data and insertion orderyes — B-tree-style pages, all leaves at one depth

The R-tree (Guttman, 1984) was designed to tick all three boxes at once. The next slide shows the difference on one city map.

CSE3144 — Lecture 26 · Why R-trees
L26 · 02 — Seeing the problem: one query, four indexes ~14 min

Same screen, same map — four very different amounts of work

Interactive — drag the screen over the city map
CSE3144 — Lecture 26 · One query, four indexes
L26 · 03 — R-tree structure & properties ~6 min

For the first time this unit: regions that can overlap

The structure
  • Leaf entry = ⟨MBR, object id⟩ — the minimum bounding rectangle of one real object.
  • Non-leaf entry = ⟨MBR, child pointer⟩ — a box covering every box in that child’s ENTIRE subtree.
  • Each node is one disk page with room for M entries (M is the capacity, typically 50–200 on disk; 3 or 4 in our examples).
  • All leaves sit at the same depth — an R-tree is always balanced, exactly like a B-tree.
  • Every node except the root holds between m and M entries, with 1 ≤ m ≤ M/2 (in practice m is about 40% of M) — the minimum occupancy, as in a B-tree. m ≥ 1 forbids empty nodes; m ≤ M/2 guarantees that when M + 1 entries are split, both halves can still hold at least m. The root holds at least 2 entries unless it is a leaf.
The family-defining difference

k-d trees, quadtrees and BSP trees all partition space — their regions never overlap, by construction. An R-tree partitions the data: it groups nearby OBJECTS and draws the tightest box around each group. Two sibling boxes CAN overlap when their objects interleave in space. This is normal, not a bug — and it is why a search sometimes has to enter more than one subtree (slide 10). The next slide takes one tree apart, level by level.

CSE3144 — Lecture 26 · R-tree structure
L26 · 04 — Animation: anatomy of an R-tree ~8 min

Outline → MBR → leaf → node → root

Interactive — the city map’s R-tree, one level at a time (capacity 4, minimum 2 per node)
CSE3144 — Lecture 26 · Anatomy of an R-tree
L26 · 05 — Building an R-tree — the insertion algorithm ~12 min

A tree is built by inserting objects one at a time

The algorithm (Guttman’s INSERT)
INSERT(E)                      // E = ⟨MBR, object⟩
  L ← CHOOSELEAF(root, E)
  if L has fewer than M entries: put E in L
  else: split L ∪ {E} into L and LL       // SPLIT
  ADJUSTTREE(L, LL)
    // walk back up: fix every box on the path;
    // add an entry for LL to the parent, which
    // may overflow and split in turn
  if the root split: new root = {root, its sibling}
                                  // height + 1

CHOOSELEAF(N, E)
  while N is not a leaf:
    F ← entry of N whose box needs the LEAST
        enlargement to cover E  (tie: smaller area)
    N ← F.child
  return N

SPLIT(entries)                 // quadratic split
  seeds: the pair (i, j) with the largest waste
         area(cover(i, j)) − area(i) − area(j)
  repeat: take the entry with the largest |d1 − d2|
          (d = growth of each group’s box)
          and add it to the group that grows less
  if a group needs all remaining entries to
     reach m, give them all to it
“Least enlargement”, worked once

Node L2 has box [70,60]–[90,85]: 20 × 25 = 500. A new object e = [30,60]–[45,75] arrives. The smallest box covering both is [30,60]–[90,85]: 60 × 25 = 1500. So L2 would have to grow by 1500 − 500 = 1000 to take e. ChooseLeaf computes this growth for every entry of the node and follows the smallest.

Why these choices

A search can skip a subtree only when the query misses that subtree’s box. Small boxes are missed more often, so every decision — which leaf to enter, how to split a full node — is made to keep the total box area small. Growing only at the root, as a B-tree does, keeps every leaf at the same depth.

Two ways to build

Insertion (this slide and the next): works for any arrival order and keeps working as the data changes. Bulk loading (slide 08): when all data is known in advance, sort it and pack full nodes bottom-up in one pass.

CSE3144 — Lecture 26 · Building an R-tree
L26 · 06 — Animation: building the tree, one insertion at a time ~40 min

8 boxes, capacity 3, 4 splits — one split climbs to a new root

a[10,10]–[20,20], b[15,15]–[25,25], c[70,70]–[85,85], d[75,60]–[90,75], e[30,60]–[45,75], f[35,65]–[50,80], g[60,10]–[75,25], h[45,5]–[55,15], inserted in that order. Every enlargement, waste and split choice is computed and shown.

Interactive — build by insertion: 8 boxes, capacity 3, minimum 1 per node
CSE3144 — Lecture 26 · Building by insertion
L26 · 07 — The splitting heuristic ~8 min

Minimise the total area of the two halves — without trying every split

The goal

A good split leaves two small boxes that ideally do not overlap, so a future query usually needs only one of them. A bad split leaves two large, overlapping boxes, and almost every query has to read both. Below are all 7 ways to split the overflowing node {c, d, e, f} from slide 06 into two non-empty groups, sorted by total area. The best one, , is exactly what the quadratic split chose.

Why not simply try every split?

M + 1 entries can be divided into two non-empty groups in (2M+1 − 2)/2 ways: 7 for M = 3, but 250 − 1 ≈ 1.1 × 1015 for a realistic M = 50. The quadratic split (PickSeeds over all pairs, then PickNext) costs O(M2) and usually lands close to the best. A cheaper linear split picks the seeds from the extreme sides along each axis, at O(M), with somewhat worse boxes.

CSE3144 — Lecture 26 · Splitting heuristic
L26 · 08 — Bulk loading: building the tree in one pass ~12 min

When all the data is known: sort, tile, repeat

Interactive — Sort-Tile-Recursive (STR) on the 24-object city map, M = 4
CSE3144 — Lecture 26 · Bulk loading
L26 · 09 — SEARCH — concept ~6 min

Overlap-test your way down — sometimes down several paths at once

Search for objects overlapping query box Q
  1. Start at the root.
  2. If the current node is NON-LEAF: for every entry ⟨E,ptr⟩, if box E overlaps Q, search the subtree at ptr.
  3. If the current node is a LEAF: for every entry ⟨E,rid⟩, if box E overlaps Q, report the object as a candidate (its exact shape can then be checked against Q).

Two boxes overlap exactly when their x-intervals overlap and their y-intervals overlap.

The key contrast with a B-tree

A B-tree search for one key follows exactly ONE path. In an R-tree, several entries of one node can overlap Q, so a query may have to follow SEVERAL paths — and it can also PRUNE a subtree outright when its box misses Q. The animation runs two queries on the tree built in slide 06: one that prunes almost everything, and one that has to take several paths and reads a leaf for nothing.

CSE3144 — Lecture 26 · SEARCH concept
L26 · 10 — Animation: SEARCH, two queries ~16 min

Q1 = [65,55]–[95,90] and Q2 = [40,20]–[72,63]

Interactive — SEARCH on the tree from slide 06
CSE3144 — Lecture 26 · SEARCH
L26 · 11 — Deletion — concept ~6 min

Search, remove, and re-insert an underfull node's survivors

The procedure
  1. FindLeaf: locate the leaf holding the entry, with the overlap descent of SEARCH (slide 10), using the entry’s own box as the query.
  2. Remove the entry from that leaf.
  3. CondenseTree: walk back up to the root. A node left with fewer than m entries is removed from its parent and its remaining entries are set aside; every other node on the path just has its box tightened.
  4. Re-insert everything set aside with INSERT (slides 05–06), each entry at its own level: an object goes into a leaf, a whole leaf goes into a node one level above the leaves, so all leaves stay at one depth.
  5. Shorten: if the root is left with a single child, that child becomes the new root (height − 1).
Why re-insert instead of merging, as a B-tree does?

A B-tree repairs an underfull node by borrowing from, or merging with, its left or right sibling — keys have one natural order, so the right partner is obvious. R-tree entries have no left-to-right order: there is no natural sibling, and merging with whichever node is at hand could create a large, overlapping box. Re-inserting each survivor with INSERT puts it where it fits best in the tree as it is now. It reuses code that already exists and often tidies the tree; the price is that a re-insertion can itself cause a split. The next slide traces two deletions on the tree from slide 06.

CSE3144 — Lecture 26 · Deletion concept
L26 · 12 — Animation: deleting from the tree ~14 min

Delete h, then a — on the tree built in slide 06

Read here with M = 4, m = 2: every node of that tree holds exactly 2 entries, so it is a legal R-tree for these values, and every node sits at its minimum. Deleting h shows every part of the procedure; deleting a shows the common case.

Interactive — two deletions on the slide-06 tree (capacity 4, minimum 2 per node)
CSE3144 — Lecture 26 · Deletion
L26 · 13 — Complexity, variants & applications ~10 min

What it costs, its descendants, and where it runs

OperationCost
SEARCHO(logM n) nodes per path followed — but overlapping boxes can force MULTIPLE paths; no useful worst-case bound below reading every node
INSERTO(logM n) to reach the leaf, plus O(M2) per quadratic split, cascading at most once per level
DELETEa search to find the entry, plus the cost of re-inserting an underfull node’s survivors
Bulk load (STR)O(n log n) — dominated by sorting; every node full, no splits
R+ trees

Force sibling boxes never to overlap by storing an object in several leaves when needed — one path per point query, at the cost of duplicated entries (the quadtree’s trade-off, brought back on purpose).

R* trees

Refine ChooseLeaf and SPLIT to minimise overlap and perimeter as well as area, and re-insert some entries of a full node before splitting it — consistently better-shaped trees in practice, at some extra insertion cost.

GIS

The standard spatial index for maps: “everything within this bounding region,” “every parcel touching this road,” at country- or city-scale datasets.

Spatial databases

PostGIS, Oracle Spatial, and similar systems use R-tree variants as the default index type for geometry columns.

CAD & similarity search

Bounding-box indexing for mechanical parts (“find all parts touching the fuselage”) and content-based retrieval (nearest faces, nearest feature vectors) both reduce to the same overlap/nearest-box queries.

CSE3144 — Lecture 26 · Complexity, variants & applications
L26 · 14 — Recap & what's next ~6 min

Data-partitioning, balanced, and built for real inserts and deletes

Lecture 27 — next

Applications of Spatial Data Structures

CO CSE3144.4 — a discussion-based synthesis across k-d trees, quad/oct trees, BSP trees, and R-trees: which one, when, and why, across graphics, GIS, and ML.

Homework — bring to Lecture 27
  • Insert one more box, i[55,55]–[65,68], into today’s final tree (M = 3). Show every ChooseLeaf growth computation and whether a split happens.
  • Run SEARCH on that tree with query box [5,5]–[30,30]. Show every box tested, pruned or descended into.
  • Bulk-load the 8 boxes a–h with STR (M = 3). Compare the number of nodes and the total leaf-box area with slide 06’s tree.
  • In 3–4 sentences: why does allowing sibling boxes to overlap (R-tree) trade away some search-time guarantees that non-overlapping partitions (quadtree, k-d tree, BSP) have — and why is that trade still worth it for real GIS/database workloads?
CSE3144 — Lecture 26

Questions?

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

Next: Lecture 27 — Applications of Spatial Data Structures.