Quad Tree
Quad Tree
#include <iostream>
#include <cmath>
struct Point
int x;
int y;
x = _x;
y = _y;
Point()
x = 0;
y = 0;
};
struct Node
Point pos;
int data;
pos = _pos;
data = _data;
}
Node()
data = 0;
};
class Quad
Point topLeft;
Point botRight;
Node *n;
Quad *topLeftTree;
Quad *topRightTree;
Quad *botLeftTree;
Quad *botRightTree;
public:
Quad()
n = NULL;
topLeftTree = NULL;
topRightTree = NULL;
botLeftTree = NULL;
botRightTree = NULL;
n = NULL;
topLeftTree = NULL;
topRightTree = NULL;
botLeftTree = NULL;
botRightTree = NULL;
topLeft = topL;
botRight = botR;
void insert(Node*);
Node* search(Point);
bool inBoundary(Point);
};
if (node == NULL)
return;
if (!inBoundary(node->pos))
return;
if (n == NULL)
n = node;
return;
// Indicates topLeftTree
if (topLeftTree == NULL)
Point(topLeft.x, topLeft.y),
Point((topLeft.x + botRight.x) / 2,
topLeftTree->insert(node);
// Indicates botLeftTree
else
if (botLeftTree == NULL)
Point(topLeft.x,
Point((topLeft.x + botRight.x) / 2,
botRight.y));
botLeftTree->insert(node);
}
}
else
// Indicates topRightTree
if (topRightTree == NULL)
Point((topLeft.x + botRight.x) / 2,
topLeft.y),
Point(botRight.x,
topRightTree->insert(node);
// Indicates botRightTree
else
if (botRightTree == NULL)
Point((topLeft.x + botRight.x) / 2,
Point(botRight.x, botRight.y));
botRightTree->insert(node);
Node* Quad::search(Point p)
{
// Current quad cannot contain it
if (!inBoundary(p))
return NULL;
if (n != NULL)
return n;
// Indicates topLeftTree
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 (topRightTree == NULL)
return NULL;
return topRightTree->search(p);
// Indicates botRightTree
else
if (botRightTree == NULL)
return NULL;
return botRightTree->search(p);
};
bool Quad::inBoundary(Point p)
// Driver program
int main()
center.insert(&a);
center.insert(&b);
center.insert(&c);
return 0;