Using a QuadTree to get all points within a bounding circle
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
In computational geometry, efficiently querying spatial data is crucial for applications such as computer graphics, geographic information systems (GIS), and collision detection in games. One popular data structure for this task is the QuadTree, a two-dimensional tree structure that partitions space into quadrants. This article explores the use of QuadTrees for efficiently retrieving all points within a bounding circle.
What is a QuadTree?
A QuadTree is a tree data structure in which each internal node has four children. This structure is particularly useful for partitioning a 2D space by recursively subdividing it into four quadrants or regions. The root node represents the entire space, and the children subdivide the space into four quadrants, continuing recursively until the space is divided to a defined level of granularity.
QuadTree Structure
- Leaf Nodes: These contain the actual data points (e.g., coordinates in a 2D plane).
- Internal Nodes: These serve as spatial dividers that encapsulate four sub-regions.
Advantages
- Efficient Range Queries: Ideal for range queries, nearest neighbor searches, and collision detection.
- Space Partitioning: Divides space efficiently, reducing the number of points that need to be checked.
Searching for Points Within a Bounding Circle
Problem Definition
Given a set of points in 2D space and a circle defined by a center and radius , retrieve all points within the circle using a QuadTree.
Steps to Solve
- QuadTree Construction: Insert all points into the QuadTree. Nodes are created and subdivided until they meet a predefined capacity limit or reach a minimum spatial resolution.
- Range Search within Circle:
- Overlap Check: Begin at the root node and traverse the tree. For each node, check if the bounding box of the node overlaps with the bounding circle.
- Leaf Node Check: If the node is a leaf, iterate through the points to determine if they lie within the circle.
- Pruning: If a node's bounding box does not intersect with the circle, prune that subtree, as it cannot contain points within the circle.
Pseudocode Example
- Bounding Box Overlapping: Use the circle bounding box overlap to decide if further exploration of a node is necessary.
- Point Inside Circle: Use the equation to determine if a point is inside the circle.
- Loose QuadTree: Allows overlapping contents across multiple nodes to provide better flexibility and fewer subdivisions, useful for dynamic datasets.
- Adaptive QuadTree: Adjusts the level of subdivisions based on regional point density, optimizing search in irregular distributions.
- Dynamic Updates: Support for real-time inserts and deletes with rebalancing.
- Integration with Other Spatial Structures: Combine with R-trees or KD-trees for extended functionalities.
Related reading
- using a tf.Tensor as a Python bool is not allowed in Graph execution. Use Eager execution or decorate this function with tf.function
- Using BFS for topological sort
- Using BFS for Weighted Graphs
- Using Dijkstra's algorithm to find a path that can carry the most weight
- Using circular permutations to reduce Traveling Salesman complexity
- Using machine learning ANN to classify odd numbers
- Using kubectl set image to update image of initContainer
- Using LINQ to remove elements from a ListT

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.