Quad Tree

07 Feb, 2018
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 boxIf a box does not contain any points, do not create a child for itRecurse for each of the children.
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.
Insert FunctionThe 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 FunctionThe 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.
// C++ Implementation of Quad Tree#include <iostream>#include <cmath>using namespace std;  // Used to hold details of a pointstruct Point{    int x;    int y;    Point(int _x, int _y)    {        x = _x;        y = _y;    }    Point()    {        x = 0;        y = 0;    }};  // The objects that we want stored in the quadtreestruct Node{    Point pos;    int data;    Node(Point _pos, int _data)    {        pos = _pos;        data = _data;    }    Node()    {        data = 0;    }};  // The main quadtree classclass Quad{    // Hold details of the boundary of this node    Point topLeft;    Point botRight;      // Contains details of node    Node *n;      // Children of this tree    Quad *topLeftTree;    Quad *topRightTree;    Quad *botLeftTree;    Quad *botRightTree;  public:    Quad()    {        topLeft = Point(0, 0);        botRight = Point(0, 0);        n = NULL;        topLeftTree  = NULL;        topRightTree = NULL;        botLeftTree  = NULL;        botRightTree = NULL;    }    Quad(Point topL, Point botR)    {        n = NULL;        topLeftTree  = NULL;        topRightTree = NULL;        botLeftTree  = NULL;        botRightTree = NULL;        topLeft = topL;        botRight = botR;    }    void insert(Node*);    Node* search(Point);    bool inBoundary(Point);};  // Insert a node into the quadtreevoid Quad::insert(Node *node){    if (node == NULL)        return;      // Current quad cannot contain it    if (!inBoundary(node->pos))        return;      // We are at a quad of unit area    // We cannot subdivide this quad further    if (abs(topLeft.x - botRight.x) <= 1 &&        abs(topLeft.y - botRight.y) <= 1)    {        if (n == NULL)            n = node;        return;    }      if ((topLeft.x + botRight.x) / 2 >= node->pos.x)    {        // Indicates topLeftTree        if ((topLeft.y + botRight.y) / 2 >= node->pos.y)        {            if (topLeftTree == NULL)                topLeftTree = new Quad(                    Point(topLeft.x, topLeft.y),                    Point((topLeft.x + botRight.x) / 2,                        (topLeft.y + botRight.y) / 2));            topLeftTree->insert(node);        }          // Indicates botLeftTree        else        {            if (botLeftTree == NULL)                botLeftTree = new Quad(                    Point(topLeft.x,                        (topLeft.y + botRight.y) / 2),                    Point((topLeft.x + botRight.x) / 2,                        botRight.y));            botLeftTree->insert(node);        }    }    else    {        // Indicates topRightTree        if ((topLeft.y + botRight.y) / 2 >= node->pos.y)        {            if (topRightTree == NULL)                topRightTree = new Quad(                    Point((topLeft.x + botRight.x) / 2,                        topLeft.y),                    Point(botRight.x,                        (topLeft.y + botRight.y) / 2));            topRightTree->insert(node);        }          // Indicates botRightTree        else        {            if (botRightTree == NULL)                botRightTree = new Quad(                    Point((topLeft.x + botRight.x) / 2,                        (topLeft.y + botRight.y) / 2),                    Point(botRight.x, botRight.y));            botRightTree->insert(node);        }    }}  // Find a node in a quadtreeNode* Quad::search(Point p){    // Current quad cannot contain it    if (!inBoundary(p))        return NULL;      // We are at a quad of unit length    // We cannot subdivide this quad further    if (n != NULL)        return n;      if ((topLeft.x + botRight.x) / 2 >= p.x)    {        // Indicates topLeftTree        if ((topLeft.y + botRight.y) / 2 >= p.y)        {            if (topLeftTree == NULL)                return NULL;            return topLeftTree->search(p);        }          // Indicates botLeftTree        else        {            if (botLeftTree == NULL)                return NULL;            return botLeftTree->search(p);        }    }    else    {        // Indicates topRightTree        if ((topLeft.y + botRight.y) / 2 >= p.y)        {            if (topRightTree == NULL)                return NULL;            return topRightTree->search(p);        }          // Indicates botRightTree        else        {            if (botRightTree == NULL)                return NULL;            return botRightTree->search(p);        }    }};  // Check if current quadtree contains the pointbool Quad::inBoundary(Point p){    return (p.x >= topLeft.x &&        p.x <= botRight.x &&        p.y >= topLeft.y &&        p.y <= botRight.y);}  // Driver programint main(){    Quad center(Point(0, 0), Point(8, 8));    Node a(Point(1, 1), 1);    Node b(Point(2, 5), 2);    Node c(Point(7, 6), 3);    center.insert(&a);    center.insert(&b);    center.insert(&c);    cout << "Node a: " <<        center.search(Point(1, 1))->data << "\n";    cout << "Node b: " <<        center.search(Point(2, 5))->data << "\n";    cout << "Node c: " <<        center.search(Point(7, 6))->data << "\n";    cout << "Non-existing node: "        << center.search(Point(5, 5));    return 0;}
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.
Further References:http://jimkang.com/quadtreevis/https://en.wikipedia.org/wiki/Quadtree
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 contribute@geeksforgeeks.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.
Advanced Data Structure
Tree
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Decision Tree Introduction with example
Count of strings whose prefix match with the given string to a given length k
Binary Indexed Tree or Fenwick Tree
Ordered Set and GNU C++ PBDS
AVL Tree | Set 2 (Deletion)
Tree Traversals (Inorder, Preorder and Postorder)
Binary Tree | Set 1 (Introduction)
Level Order Binary Tree Traversal
Introduction to Data Structures
Introduction to Tree Data Structure