49 cout <<
"=== 2D Spatial Trees: Educational Examples ===\n\n";
55 cout <<
"--- Example 1: Inserting Points into 2D-Tree ---\n\n";
61 cout <<
"Created 2D-tree for region [0,100] x [0,100]\n\n";
64 cout <<
"Inserting points:\n";
73 cout <<
" Inserted (25, 50)\n";
76 cout <<
" Inserted (75, 25)\n";
79 cout <<
" Inserted (50, 75)\n";
82 cout <<
" Inserted (10, 10)\n";
85 cout <<
" Inserted (90, 90)\n\n";
87 cout <<
"TREE STRUCTURE (conceptual):\n";
88 cout <<
" Level 0 (split on X): root = (25, 50)\n";
89 cout <<
" Left (x < 25): (10, 10)\n";
90 cout <<
" Right (x >= 25):\n";
91 cout <<
" Level 1 (split on Y): (75, 25)\n";
92 cout <<
" Left (y < 25): none\n";
93 cout <<
" Right (y >= 25): (50, 75), (90, 90)\n\n";
95 cout <<
"KEY CONCEPT: Alternating splits create balanced partitioning\n";
96 cout <<
" of 2D space into rectangular regions\n\n";
103 cout <<
"--- Example 2: Searching for Points ---\n\n";
113 cout <<
"Tree contains: (25,50), (75,25), (50,75), (10,10)\n\n";
116 cout <<
"SEARCH QUERIES:\n";
120 cout <<
" Point (50, 75): FOUND ✓\n";
122 cout <<
" Point (50, 75): NOT FOUND\n";
126 cout <<
" Point (30, 40): FOUND\n";
128 cout <<
" Point (30, 40): NOT FOUND ✓\n";
132 cout <<
" Point (25, 50): FOUND ✓ (root node)\n";
134 cout <<
" Point (25, 50): NOT FOUND\n";
136 cout <<
"\nCOMPLEXITY:\n";
137 cout <<
" Average case: O(log n) - like binary search\n";
138 cout <<
" Worst case: O(n) - if tree is unbalanced\n";
139 cout <<
" For random data: Usually well-balanced\n\n";
146 cout <<
"--- Example 3: Range Query (Rectangle Search) ---\n\n";
150 cout <<
"SCENARIO: City POI (Points of Interest) database\n";
151 cout <<
"===============================================\n\n";
160 {
Point(20, 30),
"Restaurant A"},
161 {
Point(50, 50),
"Park B"},
162 {
Point(80, 40),
"Hotel C"},
163 {
Point(120, 150),
"Museum D"},
164 {
Point(150, 180),
"Theater E"},
165 {
Point(60, 70),
"Cafe F"},
166 {
Point(180, 20),
"Mall G"}
173 cout <<
"Inserted 7 POIs across the city\n\n";
178 cout <<
"QUERY: Find all POIs in downtown area\n";
179 cout <<
" Rectangle: (40,30) to (90,80)\n\n";
184 cout <<
"Results:\n";
185 for (
size_t i = 0; i <
sizeof(
pois)/
sizeof(
POI); ++i)
192 cout <<
" " <<
pois[i].name <<
" at (" << x <<
", " <<
y <<
")\n";
195 cout <<
"\nCOMPLEXITY: O(sqrt(n) + k) where k = number of results\n";
196 cout <<
" Much better than O(n) linear scan!\n\n";
198 cout <<
"REAL-WORLD: Map applications showing POIs in visible area\n\n";
205 cout <<
"--- Example 4: Finding Nearest Point ---\n\n";
209 cout <<
"SCENARIO: Find closest emergency service\n";
210 cout <<
"========================================\n\n";
223 cout <<
"Emergency services:\n";
224 cout <<
" Hospital: (20, 80)\n";
225 cout <<
" Fire Station: (70, 30)\n";
226 cout <<
" Police: (50, 50)\n";
227 cout <<
" Ambulance: (85, 75)\n\n";
232 cout <<
"EMERGENCY at (60, 40)\n";
233 cout <<
"Finding nearest service...\n\n";
238 auto dist =
emergency.distance_with(nearest);
240 cout <<
"NEAREST SERVICE:\n";
241 cout <<
" Location: (" << nearest.
get_x() <<
", " << nearest.
get_y() <<
")\n";
242 cout <<
" Distance: " << dist <<
" units\n";
245 if (nearest.
get_x() == 70 && nearest.
get_y() == 30)
246 cout <<
" Type: Fire Station\n";
247 else if (nearest.
get_x() == 50 && nearest.
get_y() == 50)
248 cout <<
" Type: Police\n";
250 cout <<
"\nALGORITHM:\n";
251 cout <<
" 1. Start at root\n";
252 cout <<
" 2. Recursively explore promising branches\n";
253 cout <<
" 3. Prune branches that can't contain closer point\n";
254 cout <<
" 4. Track best candidate while descending\n\n";
256 cout <<
"COMPLEXITY: O(log n) average - very efficient!\n\n";
263 cout <<
"--- Example 5: Spatial Partitioning for Games ---\n\n";
265 cout <<
"SCENARIO: 2D game with moving objects\n";
266 cout <<
"=====================================\n\n";
270 cout <<
"Game world: 1000x1000 pixels\n";
271 cout <<
"Objects: Players, enemies, projectiles, items\n\n";
281 {
Point(100, 200),
"Player", 10},
282 {
Point(150, 220),
"Enemy", 15},
283 {
Point(500, 500),
"Item", 5},
284 {
Point(800, 100),
"Enemy", 15},
285 {
Point(120, 210),
"Projectile", 2}
297 cout <<
"COLLISION CHECK:\n";
298 cout <<
" Player at (100, 200)\n";
299 cout <<
" Checking range: 50 pixels\n\n";
311 cout <<
"Nearby objects:\n";
317 cout <<
" " <<
obj.type <<
" at distance " << dist <<
"\n";
320 cout <<
"\nGAME OPTIMIZATION:\n";
321 cout <<
" Without 2D-tree: Check all N objects → O(N^2) for N entities\n";
322 cout <<
" With 2D-tree: Check only nearby → O(N log N + k*N)\n";
323 cout <<
" For 1000 objects: ~1M vs ~10K comparisons!\n\n";
330 cout <<
"--- Example 6: k-NN Classification ---\n\n";
332 cout <<
"SCENARIO: Classify new data point\n";
333 cout <<
"=================================\n\n";
337 cout <<
"Training data (2D feature space):\n";
340 cout <<
" Class A (circle): around (20, 20)\n";
347 cout <<
" Class B (square): around (80, 80)\n";
353 cout <<
"\nNew data point: (25, 25)\n";
354 cout <<
"Task: Classify as A or B using 3-NN\n\n";
358 cout <<
"ALGORITHM:\n";
359 cout <<
" 1. Find 3 nearest neighbors in tree\n";
360 cout <<
" 2. Take majority vote of their classes\n";
361 cout <<
" 3. Assign that class to new point\n\n";
363 cout <<
"EXPECTED RESULT:\n";
364 cout <<
" 3 nearest neighbors likely from Class A\n";
365 cout <<
" (New point is closer to Class A cluster)\n";
366 cout <<
" Classification: Class A\n\n";
368 cout <<
"COMPLEXITY:\n";
369 cout <<
" k-NN with 2D-tree: O(k log n)\n";
370 cout <<
" k-NN without: O(n) for each query\n";
371 cout <<
" Speedup: log n times faster!\n\n";
374 cout <<
"=== SUMMARY: 2D Spatial Trees ===\n";
375 cout <<
"\n1. WHAT IS IT?\n";
376 cout <<
" Binary search tree for 2D points\n";
377 cout <<
" Alternates splitting on X and Y\n";
378 cout <<
" Partitions space into rectangles\n";
379 cout <<
"\n2. KEY OPERATIONS:\n";
380 cout <<
" Insert: O(log n) average\n";
381 cout <<
" Search: O(log n) average\n";
382 cout <<
" Range query: O(sqrt(n) + k)\n";
383 cout <<
" Nearest neighbor: O(log n) average\n";
384 cout <<
"\n3. WHEN TO USE:\n";
385 cout <<
" ✓ 2D spatial data (maps, games)\n";
386 cout <<
" ✓ Range queries (find points in region)\n";
387 cout <<
" ✓ Nearest neighbor queries\n";
388 cout <<
" ✓ Collision detection\n";
389 cout <<
" ✓ k-NN classification\n";
390 cout <<
"\n4. ADVANTAGES:\n";
391 cout <<
" * Much faster than linear scan\n";
392 cout <<
" * Simple to implement\n";
393 cout <<
" * Low memory overhead\n";
394 cout <<
" * Good for dynamic data (insert/delete)\n";
395 cout <<
"\n5. LIMITATIONS:\n";
396 cout <<
" * Can become unbalanced (worst case O(n))\n";
397 cout <<
" * Best for random data\n";
398 cout <<
" * 2D only (use k-d tree for higher dimensions)\n";
399 cout <<
" * Not cache-friendly (pointer chasing)\n";
400 cout <<
"\n6. REAL-WORLD APPLICATIONS:\n";
401 cout <<
" * GIS: Find POIs near location\n";
402 cout <<
" * Games: Collision detection, AI\n";
403 cout <<
" * ML: k-NN classification\n";
404 cout <<
" * Graphics: Ray tracing, culling\n";
405 cout <<
" * Robotics: Path planning\n";
406 cout <<
"\n7. ALTERNATIVES:\n";
407 cout <<
" QuadTree: Better for clustered data\n";
408 cout <<
" R-Tree: Better for rectangles\n";
409 cout <<
" Grid: Simpler but less flexible\n";
410 cout <<
" KD-Tree: Generalization to k dimensions\n";
411 cout <<
"\n8. BEST PRACTICES:\n";
412 cout <<
" * Use for uniformly distributed data\n";
413 cout <<
" * Set appropriate bounding box\n";
414 cout <<
" * Consider rebalancing for skewed data\n";
415 cout <<
" * Batch insert for better balance\n";
Dynamic singly linked list with functional programming support.
T & insert(const T &item)
Insert a new item by copy.
2D k-d tree for efficient spatial point operations.
static void range(Node *root, const Rectangle &rect, DynList< Point > *q)
Recursively find points within a rectangle.
Point nearest(const Point &p) noexcept
Find the nearest point to a query point.
bool contains(const Point &p) const noexcept
Check if the tree contains a point.
Point * insert(const Point &p)
Insert a point into the tree.
Rectangular point in the plane.
const Geom_Number & get_y() const
Returns y value.
const Geom_Number & get_x() const
Returns x value.
Main namespace for Aleph-w library functions.
DynList< T > maps(const C &c, Op op)
Classic map operation.
2D point and geometric utilities.
2D k-d tree implementation for spatial point indexing.