using System;
public class Node
{
public int[] Point { get; set; }
public Node Left { get; set; }
public Node Right { get; set; }
public Node(int[] point)
{
Point = point;
Left = null;
Right = null;
}
}
class Program
{
const int k = 2;
class Node
{
public int[] point = new int[k];
public Node left, right;
};
static Node NewNode(int[] arr)
{
Node temp = new Node();
for (int i = 0; i < k; i++)
temp.point[i] = arr[i];
temp.left = temp.right = null;
return temp;
}
static Node InsertRec(Node root, int[] point, uint depth)
{
if (root == null)
return NewNode(point);
uint cd = depth % k;
if (point[cd] < root.point[cd])
root.left = InsertRec(root.left, point, depth + 1);
else
root.right = InsertRec(root.right, point, depth + 1);
return root;
}
static Node Insert(Node root, int[] point)
{
return InsertRec(root, point, 0);
}
static int Min(int x, int y, int z)
{
return Math.Min(x, Math.Min(y, z));
}
static int FindMinRec(Node root, int d, uint depth)
{
if (root == null)
return int.MaxValue;
uint cd = depth % k;
if (cd == d)
{
if (root.left == null)
return root.point[d];
return Math.Min(root.point[d], FindMinRec(root.left, d, depth + 1));
}
return Min(root.point[d],
FindMinRec(root.left, d, depth + 1),
FindMinRec(root.right, d, depth + 1));
}
static int FindMin(Node root, int d)
{
return FindMinRec(root, d, 0);
}
static void Main(string[] args)
{
Node root = null;
int[][] points = new int[][] { new int[] { 30, 40 }, new int[] { 5, 25 },
new int[] { 70, 70 }, new int[] { 10, 12 }, new int[] { 50, 30 }, new int[] { 35, 45 } };
int n = points.Length;
for (int i = 0; i < n; i++)
root = Insert(root, points[i]);
Console.WriteLine("Minimum of 0'th dimension is " + FindMin(root, 0));
Console.WriteLine("Minimum of 1'th dimension is " + FindMin(root, 1));
Console.ReadLine();
}
}