VOOZH about

URL: https://www.geeksforgeeks.org/dsa/quad-tree/

⇱ Quad Tree - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Quad Tree

Last Updated : 24 Feb, 2025

Quadtrees are trees used to efficiently store data of points on a two-dimensional space. Each node of a Quad Tree has at most four children. We can construct a quadtree from a two-dimensional area using the following steps:

  1. Divide the current two-dimensional space into four boxes.
  2. If a box contains one or more points in it, create a child object, storing in it the two-dimensional space of the box.
  3. If a box does not contain any points, do not create a child for it.
  4. 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 want to find the closest point to given coordinates, you can do it using quadtrees. 

Insert Function:

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. 

Search Function:

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:


Output
Node a: 1
Node b: 2
Node c: 3
Non-existing node: 0

Exercise: Implement a Quad Tree which returns 4 closest nodes to a given point. 

Comment