2D Segment/Quad Tree Explanation with C
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Both 2D segment trees and quadtrees are used to answer spatial queries efficiently, but they solve the problem in different ways. A 2D segment tree is usually better when you have a fixed grid and want predictable rectangular range queries, while a quadtree is often a better fit for sparse geometric points distributed across space.
What a 2D Segment Tree Stores
A normal segment tree indexes one dimension. A 2D segment tree extends that idea so each node on one axis also stores information for the other axis.
Typical use cases include:
- sum in a sub-rectangle
- minimum or maximum in a rectangle
- point updates on a fixed 2D grid
Conceptually, you can think of it as a tree of trees. That makes queries fast, but also makes the structure heavier in memory and code complexity.
A Small 2D Segment Tree Example
The following C++ example shows the core idea for range-sum queries on a small matrix:
This is powerful, but it is not lightweight.
What a Quadtree Stores
A quadtree recursively divides 2D space into four quadrants. It is a spatial partitioning structure rather than a fixed-grid aggregation tree.
It is often used for:
- point indexing
- collision detection
- region search
- sparse spatial data
Instead of building on a fixed matrix shape, a quadtree subdivides only where needed.
A Minimal Quadtree Skeleton
This is enough to show the spatial subdivision model, even though a full implementation would also include region queries.
When to Choose Which
Choose a 2D segment tree for fixed-grid rectangular range queries. Choose a quadtree for sparse geometric data and adaptive spatial partitioning.
Common Pitfalls
The biggest mistake is using a 2D segment tree for arbitrary scattered geometry when the real problem is spatial indexing. That often leads to more memory and complexity than necessary.
Another issue is expecting a quadtree to give the same rectangular aggregation behavior as a segment tree.
Developers also underestimate implementation complexity. Both structures are more subtle in 2D than simpler counterparts.
Finally, do not choose based only on asymptotic complexity. Data distribution, update pattern, and query type matter just as much.
Summary
- A 2D segment tree is best for fixed-grid range aggregation queries.
- A quadtree is better for sparse spatial partitioning and point-region work.
- Segment trees trade memory and implementation cost for strong rectangular query support.
- Quadtrees adapt to data distribution and often fit geometric workloads more naturally.
- Pick the structure that matches the shape of the data, not just the name of the problem.
Related reading
- 3d array traversal originating from center
- 3d Fenwick tree
- 500,000 street names - what data structure and to use to implement a fast search?
- Create ArrayList from array
- How can I add new keys to a dictionary?
- How can I check if an object is an array?
- How do I check if a list is empty?
- How do I check if an array includes a value in JavaScript?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.