using System;
struct Point
{
public int x;
public int y;
public Point(int _x, int _y)
{
x = _x;
y = _y;
}
}
struct Node
{
public Point pos;
public int data;
public Node(Point _pos, int _data)
{
pos = _pos;
data = _data;
}
}
class Quad
{
private Point topLeft;
private Point botRight;
private Node n;
private Quad topLeftTree;
private Quad topRightTree;
private Quad botLeftTree;
private Quad botRightTree;
public Quad()
{
topLeft = new Point(0, 0);
botRight = new Point(0, 0);
n = new Node();
topLeftTree = null;
topRightTree = null;
botLeftTree = null;
botRightTree = null;
}
public Quad(Point topL, Point botR)
{
n = new Node();
topLeftTree = null;
topRightTree = null;
botLeftTree = null;
botRightTree = null;
topLeft = topL;
botRight = botR;
}
public void Insert(Node node)
{
if (node.Equals(default(Node)))
return;
if (!InBoundary(node.pos))
return;
if (Math.Abs(topLeft.x - botRight.x) <= 1
&& Math.Abs(topLeft.y - botRight.y) <= 1)
{
if (n.Equals(default(Node)))
n = node;
return;
}
if ((topLeft.x + botRight.x) / 2 >= node.pos.x)
{
if ((topLeft.y + botRight.y) / 2 >= node.pos.y)
{
if (topLeftTree == null)
topLeftTree = new Quad(
new Point(topLeft.x, topLeft.y),
new Point((topLeft.x + botRight.x) / 2,
(topLeft.y + botRight.y) / 2));
topLeftTree.Insert(node);
}
else
{
if (botLeftTree == null)
botLeftTree = new Quad(
new Point(topLeft.x,
(topLeft.y + botRight.y) / 2),
new Point((topLeft.x + botRight.x) / 2,
botRight.y));
botLeftTree.Insert(node);
}
}
else
{
if ((topLeft.y + botRight.y) / 2 >= node.pos.y)
{
if (topRightTree == null)
topRightTree = new Quad(
new Point((topLeft.x + botRight.x) / 2,
topLeft.y),
new Point(botRight.x,
(topLeft.y + botRight.y) / 2));
topRightTree.Insert(node);
}
else
{
if (botRightTree == null)
botRightTree = new Quad(
new Point((topLeft.x + botRight.x) / 2,
(topLeft.y + botRight.y) / 2),
new Point(botRight.x, botRight.y));
botRightTree.Insert(node);
}
}
}
public Node Search(Point p)
{
if (!InBoundary(p))
return default(Node);
if (!n.Equals(default(Node)))
return n;
if ((topLeft.x + botRight.x) / 2 >= p.x)
{
if ((topLeft.y + botRight.y) / 2 >= p.y)
{
if (topLeftTree == null)
return default(Node);
return topLeftTree.Search(p);
}
else
{
if (botLeftTree == null)
return default(Node);
return botLeftTree.Search(p);
}
}
else
{
if ((topLeft.y + botRight.y) / 2 >= p.y)
{
if (topRightTree == null)
return default(Node);
return topRightTree.Search(p);
}
else
{
if (botRightTree == null)
return default(Node);
return botRightTree.Search(p);
}
}
}
private bool InBoundary(Point p)
{
return (p.x >= topLeft.x && p.x <= botRight.x
&& p.y >= topLeft.y && p.y <= botRight.y);
}
}
class Program
{
static void Main()
{
Quad center = new Quad(new Point(0, 0), new Point(8, 8));
Node a = new Node(new Point(1, 1), 1);
Node b = new Node(new Point(2, 5), 2);
Node c = new Node(new Point(7, 6), 3);
center.Insert(a);
center.Insert(b);
center.Insert(c);
Console.WriteLine("Node a: " + center.Search(new Point(1, 1)).data);
Console.WriteLine("Node b: " + center.Search(new Point(2, 5)).data);
Console.WriteLine("Node c: " + center.Search(new Point(7, 6)).data);
Console.WriteLine("Non-existing node: " + center.Search(new Point(5, 5)).data);
}
}