Quadtrees in Data Structure

Quadtrees are trees implemented to efficiently store data of points on a two-dimensional space. In this tree, each node has maximum four children.

We can build a quadtree from a two-dimensional area implementing the following steps

  • The current two dimensional space is divided into four boxes.
  • If a box consists of one or more points in it, build a child object, storing in it the two dimensional space of the box.
  • If a box does not contain any points, do not build a child for it.
  • Perform recursion for each of the children.

Quadtrees are implemented in image compression, where each node consists of the average colour of each of its children.

The deeper we visit in the tree, the more the detail of the image.

Quadtrees are also implemented in searching for nodes in a two-dimensional area. For instance, if we wanted to compute the closest point to given coordinates, we can do it implementing quadtrees.

Insert Function

The insert functions is implemented to insert a node into an existing Quad Tree. This function first verifies whether the given node is within the boundaries of the current quad. If it is not, then we immediately cancel the insertion. If it is within the boundaries, we choose the appropriate child to contain this node based on its location. This function is O(Log N) where N indicates the size of distance.

Search Function

The search function is implemented to locate a node in the given quad. It can also be edited to return the closest node to the given point. This function is applied by taking the given point, comparing with the boundaries of the child quads and recursing. This function is O(Log N) where N indicates the size of distance.

Some common uses of quadtrees

  • Image Representation of image
  • Image Processing of image
  • Mesh generation of mesh
  • Performs efficient collision detection in two dimensions
  • Performs to find solution of multidimensional fields (computational fluid dynamics, electromagnetism)
  • Estimation of state
  • Quadtrees are also implemented in the area of fractal image analysis

Updated on: 08-Jan-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started