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

BSP Trees (Binary Space Partitioning Trees)

k-d trees and quadtrees always split along the axes. BSP trees drop that restriction entirely — a splitting line can run in any direction, following the actual edges of the scene. Today: the algorithm that makes that possible, including the one thing axis-aligned trees never had to deal with — a polygon that gets cut in half.

Dr. Manu ShrivastavaCourse Instructor · Consultation Fri 2–5 PM, LHC 308F
~139 minutesSession outcome: explain partitioning techniques used in graphics and GIS
L25 · 00 — Agenda ~139 min

One restriction removed, one new problem to solve

Why BSP trees?

00–08

The painter's algorithm's two failure modes, and how a 1969 idea from computer graphics fixed both.

Seeing it in 3D

08–22

A tiny game level rendered twice: painter’s algorithm vs BSP order, with every wrong pixel marked.

Definition & the key generalization

22–30

Axis-aligned BSP is just a k-d tree in disguise. Real BSP trees follow the scene's own edges.

Construction algorithm — concept

30–38

Pick a polygon, classify everything else against its plane, split what crosses it, recurse.

Animation: building a BSP tree

38–78

5 segments, arbitrary orientation, 2 genuine splits — every classification and every split shown.

Point classification & render order — concept

78–86

The same traversal that locates a point also gives you front-to-back polygon order for free.

Animation: classifying a query point

86–94

Walking the tree we just built, one comparison at a time.

Animation: painting order from the tree

94–109

The eye walks the same tree: far side first. The camera moves, the tree is never rebuilt.

Complexity & limitations

109–119

What building and querying actually cost, honestly.

Applications & recap

119–139

Ray tracing, collision detection, GIS — and what's next.

CSE3144 — Lecture 25
L25 · 01 — Why BSP trees? ~8 min

A 1969 fix for a rendering problem that's still with us

The painter's algorithm

To draw a 3D scene made of polygons on a 2D screen, the simplest approach is: sort all polygons back-to-front from the viewer, then paint them in that order, letting nearer polygons overwrite farther ones — like a painter working back to front.

Where it breaks
  • Sorting cost. Re-sorting every polygon by distance, for every new viewpoint, is expensive to do naively every frame.
  • Overlapping / intersecting polygons. If two polygons overlap in a way that no single back-to-front order can resolve (e.g. they interpenetrate), the painter's algorithm produces visibly wrong output — no ordering fixes it.
What a BSP tree buys you

Build the tree ONCE, using the scene's own polygons as splitting planes. After that, for ANY viewpoint, a single tree traversal produces a correct back-to-front (or front-to-back) order in time linear in the number of polygons — no re-sorting. And because the construction process explicitly SPLITS any polygon that crosses a partition, the "interpenetrating polygons" problem is resolved once, up front, at build time. The next slide shows both problems, and the fix, in 3D.

CSE3144 — Lecture 25 · Why BSP trees
L25 · 02 — Seeing it in 3D ~14 min

Paint far-to-near, and watch it go wrong

A tiny game level — walls standing on a floor — seen by one camera and drawn by two renderers side by side. Red marks are pixels where the wrong wall ended up on top, found by tracing a ray through each pixel. Step through the tour, then move the camera yourself.

Interactive 3D — painter’s algorithm vs BSP order
CSE3144 — Lecture 25 · Seeing it in 3D
L25 · 03 — Definition & the key generalization ~8 min

Axis-aligned BSP is a k-d tree wearing a different name

The definition

A BSP tree recursively subdivides space into two convex pieces using hyperplanes, exactly like a k-d tree or quadtree — except the hyperplane at each node can have any orientation, not just axis-aligned. Each node stores one splitting plane (or the polygon that defines it); its two children are everything in front of that plane and everything behind it.

Two flavours of the same idea
  • Axis-aligned BSP: every splitting line is horizontal or vertical — this is precisely a k-d tree (Lecture 23) in 2D, or a k-d tree of walls in 3D. Simple, but wastes splits on scenes whose real edges run diagonally.
  • Polygon-aligned (general) BSP: each splitting line is the actual edge of one of the scene's own polygons, at whatever angle that edge happens to run. This is the version that actually earns the name "Binary Space Partitioning Tree" in the graphics literature, and the one we build today.

The tree-construction LOGIC is identical either way — classify, split what crosses, recurse. Only the geometry of the splitting line changes. That's the entire generalization.

CSE3144 — Lecture 25 · Definition
L25 · 04 — Construction algorithm — concept ~8 min

Pick one, classify the rest, split what crosses, recurse

The algorithm
  1. Select one polygon (or segment) P from the current list. Make a tree node N for P.
  2. For every OTHER item in the list: if it's wholly in front of P's plane, put it in the front-list; if wholly behind, the back-list.
  3. If an item is INTERSECTED by P's plane: split it into two pieces along the intersection, and put each piece in its correct list.
  4. If an item lies exactly in P's own plane, attach it to node N itself.
  5. Recurse: build N's front-child from the front-list, and N's back-child from the back-list.
The genuinely new idea

Every tree we've built this semester stored whole, unmodified data items. BSP construction can cut an input in half — and the two halves of the SAME original segment can end up in completely different parts of the final tree. That's the one mechanic today's animation is built to make unmistakable.

CSE3144 — Lecture 25 · Construction concept
L25 · 05 — Animation: building a BSP tree ~40 min

5 segments, arbitrary orientation, 2 genuine splits

S1(0,60)–(60,0), S2(70,50)–(90,20), S3(10,40)–(50,50), S4(70,20)–(90,50), S5(10,10)–(30,5) on a 100×100 plane. Teal and purple shading shows the front and back half of whichever splitter is being processed; every classification and both cuts get their own step.

Interactive — BSP construction, 5 segments
CSE3144 — Lecture 25 · Building a BSP tree
L25 · 06 — Point classification & render order — concept ~8 min

The same traversal answers two different questions

Point classification

Given a query point Q, walk the tree from the root: at each node, evaluate Q against that node's plane, go front or back accordingly, and stop at a leaf — that leaf's region is where Q lives. O(depth) per query, no re-scanning every polygon.

Front-to-back / back-to-front rendering order

Replace "Q" with the VIEWER's position. At each node: if the viewer is in front of the plane, the polygons behind it are farther away — recurse into whichever side you want drawn first. This is exactly how a BSP tree gives correct painter's-algorithm ordering for ANY viewpoint, without re-sorting anything — animated in slide 08.

CSE3144 — Lecture 25 · Point classification concept
L25 · 07 — Animation: classifying a query point ~8 min

Q = (75,15) — which leaf region does it belong to?

Interactive — locate Q in the BSP tree we just built
CSE3144 — Lecture 25 · Query point classification
L25 · 08 — Animation: painting order from the tree ~15 min

The camera moves, the tree stays — only the walk changes

Slide 07 walked the tree with a query point Q and went down one branch. Here the “query point” is the eye, and the walk visits both branches of every node — far side first, then the node, then the eye’s side. The result is exactly the far-to-near order the painter’s algorithm needs.

Interactive 3D — back-to-front walk of the slide-05 tree
CSE3144 — Lecture 25 · Painting order from the tree
L25 · 09 — Complexity & limitations ~10 min

Cheap to query, expensive to build, and not a complete answer alone

OperationCost
Point classification / render-order traversalO(depth) per query
ConstructionDepends heavily on split count — each split can create 2 new items to recurse on, so worst case is well above O(n log n) if splits cascade
Tree sizeCan exceed n (the original polygon count), since splitting grows the total item count
Genuine limitations
  • Construction is genuinely time-consuming — choosing which polygon to split on at each step matters a lot, and a bad choice can cascade into far more splits than necessary.
  • A BSP tree alone does not solve visible-surface determination — it gives you correct DRAWING ORDER, but still relies on the drawing step itself (e.g. z-buffering or explicit clipping) to resolve what's actually visible pixel by pixel.
Why it's still worth it

The construction cost is paid ONCE, offline, for a static scene. Every query afterward — from any viewpoint, forever — is O(depth). For a game level or a GIS layer that's built once and queried millions of times, that trade is usually an easy win.

CSE3144 — Lecture 25 · Complexity & limitations
L25 · 10 — Applications & recap ~20 min

Wherever "what's in front of what" needs answering fast, repeatedly

Ray tracing

Following a ray through a scene means repeatedly asking "which side of this surface am I on" — exactly the BSP classification test, applied along the ray instead of just at one point.

Collision detection (games & robotics)

A static level's geometry is BSP-built once; every frame, moving objects are classified against it in O(depth) instead of tested against every wall.

GIS & complex spatial scenes

Arbitrary-orientation region boundaries (coastlines, property lines, contour bands) are exactly the case axis-aligned k-d trees and quadtrees handle wastefully — BSP trees follow the boundary's own angle.

Lecture 26 — next

R-Trees and Spatial Indexing

CO CSE3144.4 — switching from space-partitioning to data-partitioning: bounding boxes grouped bottom-up, for GIS and database spatial indexes.

Homework — bring to Lecture 26
  • Build a BSP tree from these 4 segments on a 100×100 plane, using the first-listed segment at each level as the splitter: T1(20,20)–(80,80), T2(10,70)–(40,10), T3(60,10)–(90,60), T4(30,90)–(70,30). Show every classification and every split.
  • On today's final tree, classify a SECOND query point, Q2=(15,55). Show every comparison.
  • In 3–4 sentences: why can a BSP tree end up with MORE stored polygon pieces than the scene originally had, and why doesn't that make the structure useless?
CSE3144 — Lecture 25

Questions?

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

Next: Lecture 26 — R-Trees and Spatial Indexing.