Quadtrees are trees used to efficiently store data of points on a two-dimensional space. In this tree, each node has at most four children.
We can construct a quadtree from a two-dimensional area using the following steps:
- Divide the current two dimensional space into four boxes.
- If a box contains one or more points in it, create a child object, storing in it the two dimensional space of the box
- If a box does not contain any points, do not create a child for it
- Recurse for each of the children.
Quadtrees are used in image compression, where each node contains the average colour of each of its children. The deeper you traverse in the tree, the more the detail of the image.
Quadtrees are also used in searching for nodes in a two-dimensional area. For instance, if you wanted to find the closest point to given coordinates, you can do it using quadtrees.
The insert functions is used to insert a node into an existing Quad Tree. This function first checks whether the given node is within the boundaries of the current quad. If it is not, then we immediately cease the insertion. If it is within the boundaries, we select the appropriate child to contain this node based on its location.
This function is O(Log N) where N is the size of distance.
The search function is used to locate a node in the given quad. It can also be modified to return the closest node to the given point. This function is implemented by taking the given point, comparing with the boundaries of the child quads and recursing.
This function is O(Log N) where N is size of distance.
The program given below demonstrates storage of nodes in a quadtree.
Node a: 1 Node b: 2 Node c: 3 Non-existing node: 0
Implement a Quad Tree which returns 4 closest nodes to a given point.
This article is contributed by Aditya Kamath. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Overview of Data Structures | Set 3 (Graph, Trie, Segment Tree and Suffix Tree)
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Convert a given Binary tree to a tree that holds Logical OR property
- Convert a given Binary tree to a tree that holds Logical AND property
- Sub-tree with minimum color difference in a 2-coloured tree
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Cartesian tree from inorder traversal | Segment Tree
- Two Dimensional Binary Indexed Tree or Fenwick Tree
- Tournament Tree (Winner Tree) and Binary Heap
- Difference between General tree and Binary tree
- Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2