Quad Tree:

What is a Quad Tree?

A Quadtree is a tree structure that helps organize 2D space by dividing it into four equal parts over and over again. Each division creates four quadrants: NW, NE, SW, SE. This continues until each section contains at most k threshold points, when the amount of points in a quadrant passes the threshold the quadrant splits into 4 smaller quadrants. -Each node in a Quadtree either: 1.Has 4 children (if it holds multiple points), or 2.Is a leaf (if it holds one or no point). 3.The result is a grid-like structure that zooms in on crowded areas—great for visualizing and querying spatial data. -Where It’s Used 1.Game development (e.g. collision detection) 2.Geographic maps (e.g. storing coordinates) 3.Image compression 4.Efficient spatial queries (range search, nearest neighbors) -Strengths 1.Fast for range and region-based queries 2.Adapts to spatial distribution of data 3.Simple to implement and visualize 4.Works well in 2D environments -Weaknesses 1.Can become deep and unbalanced with clustered data 2.Not ideal for higher-dimensional data (use KD-Trees for 3D+) 3.Requires fixed bounding region size at the start

Quad Tree Operations

Bulk Loading:

  1. Initialize: Start with all points in root node
  2. Recursive Split:
    • Even depths: Split space vertically (x-coordinate)
    • Odd depths: Split horizontally (y-coordinate)
    • Choose median point for balanced splits
  3. Terminate: When nodes contain ≤ threshold points

Nearest Neighbor Search:

  1. Traverse tree comparing target point with splitting planes
  2. Maintain priority queue of candidates
  3. Prune subtrees when bounding box is too far
  4. Backtrack to verify no closer points exist

Range Queries:

  1. Check if current node's region intersects query area
  2. Recurse into both subtrees if needed
  3. Collect points within range at leaf nodes

Complexities

Operation Average Case Worst Case
Bulk LoadingO((d + 1) · n)O((d + 1)·n)
InsertionO(log n)O(n)
DeletionO(log n)O(n)
Range QueryO(logn + m)O(n)
Nearest NeighborO(log n)O(n)
k-NN QueryO(k log n)O(kn)

Bulk Loading:

Building a Quadtree involves recursively subdividing the 2D space into four quadrants until each region contains a manageable number of points. Since each point is processed once and subdivisions depend on spatial depth (d), the complexity is O((d + 1)·n), where d is the tree depth.

Insertion/Deletion:

Inserting or removing a point requires navigating from the root to the appropriate quadrant based on coordinates. Since the tree depth is logarithmic in balanced cases, both operations take O(log n) on average, assuming uniform data distribution.

k-NN Search:

Like NN, k-NN search uses spatial pruning and a priority queue to keep track of the best candidates. It evaluates up to k promising paths in a log n depth tree, giving an average complexity of O(k log n).

Range Query:

Range searches recursively visit quadrants overlapping the query area. Since irrelevant subtrees are skipped and tree height is log n, the complexity becomes O(log n + m), where m is the number of results returned.

Node Capacity [Minimum = 2] =
Deletes all points in the tree.
Generates a new tree with random points.
Generates a new tree with random points.