|
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 | |
| ostream & | operator<< (ostream &os, const Point &p) |
| Print a point. | |
| 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 204 of file quadtree_example.C.
References QuadTree::contains(), QuadTree::insert(), Aleph::maps(), QuadTree::remove(), and QuadTree::search().
Referenced by main().
| void demo_collision_detection | ( | ) |
Practical example: Game Collision Detection.
Definition at line 353 of file quadtree_example.C.
References QuadTree::contains(), QuadTree::insert(), Aleph::maps(), and Aleph::HTList::size().
Referenced by main().
| void demo_geographic_points | ( | ) |
Practical example: Geographic Points.
Definition at line 302 of file quadtree_example.C.
References QuadTree::contains(), QuadTree::insert(), Aleph::maps(), QuadTree::search(), Aleph::HTList::size(), and y.
Referenced by main().
| void demo_performance | ( | int | n | ) |
Performance demonstration.
Definition at line 406 of file quadtree_example.C.
References QuadTree::contains(), QuadTree::insert(), log2(), and Aleph::maps().
| void demo_traversal | ( | ) |
Demonstrate node traversal.
Definition at line 460 of file quadtree_example.C.
References QuadTree::for_each(), QuadNode::get_points_set(), QuadTree::insert(), QuadNode::is_leaf(), LEVEL, Aleph::maps(), and Aleph::HTList::size().
Referenced by main().
| void demo_tree_structure | ( | ) |
Demonstrate tree structure.
Definition at line 259 of file quadtree_example.C.
References QuadTree::insert(), and Aleph::maps().
Referenced by main().
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 507 of file quadtree_example.C.
References demo_basic_operations(), demo_collision_detection(), demo_geographic_points(), demo_performance(), demo_traversal(), demo_tree_structure(), and Aleph::maps().
| ostream & operator<< | ( | ostream & | os, |
| const Point & | p | ||
| ) |
Print a point.
Definition at line 196 of file quadtree_example.C.
References Point::get_x(), Point::get_y(), and Aleph::maps().