|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
QuadTree: Spatial Data Structure for 2D Points. More...
#include <iostream>#include <iomanip>#include <string>#include <vector>#include <random>#include <chrono>#include <quadtree.H>#include <tclap/CmdLine.h>Go to the source code of this file.
Functions | |
| void | demo_basic_operations () |
| Demonstrate basic QuadTree operations. | |
| void | demo_tree_structure () |
| Demonstrate tree structure. | |
| void | demo_geographic_points () |
| Practical example: Geographic Points. | |
| void | demo_collision_detection () |
| Practical example: Game Collision Detection. | |
| void | demo_performance (int n) |
| Performance demonstration. | |
| void | demo_traversal () |
| Demonstrate node traversal. | |
| int | main (int argc, char *argv[]) |
QuadTree: Spatial Data Structure for 2D Points.
This example demonstrates the QuadTree, a hierarchical spatial data structure that recursively divides 2D space into four quadrants. QuadTrees are essential for efficient spatial queries and are widely used in computer graphics, GIS, game development, and computational geometry.
A QuadTree is a tree data structure where:
Each internal node divides space into four quadrants:
Example: Dividing a 100×100 space:
| Operation | Average | Worst Case | Notes |
|---|---|---|---|
| Insert | O(log n) | O(depth) | Depends on distribution |
| Search (point) | O(log n) | O(depth) | Point lookup |
| Range query | O(log n + k) | O(n) | k = points in range |
| Leaf neighbors (QuadNode) | O(log n) | O(depth) | Neighbor navigation between leaf regions |
| Remove | O(log n) | O(depth) | May require merging |
Note: Worst case occurs when points are poorly distributed (e.g., all on a line), causing deep trees.
✅ Efficient queries: O(log n) average case ✅ Adaptive: Automatically adjusts to data distribution ✅ Memory efficient: Only subdivides where needed ✅ Simple concept: Easy to understand and implement
❌ Worst case: Can degrade to O(n) with bad distribution ❌ 2D only: Designed for 2D space (use octree for 3D) ❌ Rebalancing: May need periodic rebalancing
| Structure | Dimensions | Best For | Complexity |
|---|---|---|---|
| QuadTree | 2D | Uniform/adaptive | O(log n) avg |
| Octree | 3D | 3D spatial data | O(log n) avg |
| KD-Tree | k-D | High-dimensional | O(log n) avg |
| R-Tree | k-D | Rectangles | O(log n) avg |
| Grid | 2D | Uniform distribution | O(1) |
Definition in file quadtree_example.C.
| void demo_basic_operations | ( | ) |
Demonstrate basic QuadTree operations.
Definition at line 196 of file quadtree_example.C.
References QuadTree::contains(), Aleph::divide_and_conquer_partition_dp(), QuadTree::insert(), QuadTree::remove(), and QuadTree::search().
Referenced by main().
| void demo_collision_detection | ( | ) |
Practical example: Game Collision Detection.
Definition at line 345 of file quadtree_example.C.
References QuadTree::contains(), Aleph::divide_and_conquer_partition_dp(), and QuadTree::insert().
Referenced by main().
| void demo_geographic_points | ( | ) |
Practical example: Geographic Points.
Definition at line 294 of file quadtree_example.C.
References QuadTree::contains(), Aleph::divide_and_conquer_partition_dp(), QuadTree::insert(), QuadTree::search(), and y.
Referenced by main().
| void demo_performance | ( | int | n | ) |
Performance demonstration.
Definition at line 398 of file quadtree_example.C.
References QuadTree::contains(), Aleph::divide_and_conquer_partition_dp(), QuadTree::insert(), and log2().
| void demo_traversal | ( | ) |
Demonstrate node traversal.
Definition at line 452 of file quadtree_example.C.
References Aleph::divide_and_conquer_partition_dp(), QuadTree::for_each(), QuadNode::get_points_set(), QuadTree::insert(), QuadNode::is_leaf(), and LEVEL.
Referenced by main().
| void demo_tree_structure | ( | ) |
Demonstrate tree structure.
Definition at line 251 of file quadtree_example.C.
References Aleph::divide_and_conquer_partition_dp(), and QuadTree::insert().
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 499 of file quadtree_example.C.
References Aleph::and, cmd, demo_basic_operations(), demo_collision_detection(), demo_geographic_points(), demo_performance(), demo_traversal(), demo_tree_structure(), and Aleph::divide_and_conquer_partition_dp().