Java Program to Perform Insertion in a 2 Dimensional K-D Tree

«
»
This is a Java Program to implement 2D KD Tree and insert the input set and print the various traversals. In computer science, a k-d tree (short for k-dimensional tree) is a space-partitioning data structure for organizing points in a k-dimensional space. k-d trees are a useful data structure for several applications, such as searches involving a multidimensional search key (e.g. range searches and nearest neighbor searches). k-d trees are a special case of binary space partitioning trees.

Here is the source code of the Java Program to Perform Insertion in a 2 Dimension K-D Tree. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to insert an element in a 2D KD Tree
  2. import java.io.IOException;
  3. import java.util.Scanner;
  4.  
  5. class KD2DNode
  6. {
  7.     int axis;
  8.     double[] x;
  9.     int id;
  10.     boolean checked;
  11.     boolean orientation;
  12.  
  13.     KD2DNode Parent;
  14.     KD2DNode Left;
  15.     KD2DNode Right;
  16.  
  17.     public KD2DNode(double[] x0, int axis0)
  18.     {
  19.         x = new double[2];
  20.         axis = axis0;
  21.         for (int k = 0; k < 2; k++)
  22.             x[k] = x0[k];
  23.  
  24.         Left = Right = Parent = null;
  25.         checked = false;
  26.         id = 0;
  27.     }
  28.  
  29.     public KD2DNode FindParent(double[] x0)
  30.     {
  31.         KD2DNode parent = null;
  32.         KD2DNode next = this;
  33.         int split;
  34.         while (next != null)
  35.         {
  36.             split = next.axis;
  37.             parent = next;
  38.             if (x0[split] > next.x[split])
  39.                 next = next.Right;
  40.             else
  41.                 next = next.Left;
  42.         }
  43.         return parent;
  44.     }
  45.  
  46.     public KD2DNode Insert(double[] p)
  47.     {
  48.         x = new double[2];
  49.         KD2DNode parent = FindParent(p);
  50.         if (equal(p, parent.x, 2) == true)
  51.             return null;
  52.  
  53.         KD2DNode newNode = new KD2DNode(p,
  54.                 parent.axis + 1 < 2 ? parent.axis + 1 : 0);
  55.         newNode.Parent = parent;
  56.  
  57.         if (p[parent.axis] > parent.x[parent.axis])
  58.         {
  59.             parent.Right = newNode;
  60.             newNode.orientation = true; //
  61.         } else
  62.         {
  63.             parent.Left = newNode;
  64.             newNode.orientation = false; //
  65.         }
  66.  
  67.         return newNode;
  68.     }
  69.  
  70.     boolean equal(double[] x1, double[] x2, int dim)
  71.     {
  72.         for (int k = 0; k < dim; k++)
  73.         {
  74.             if (x1[k] != x2[k])
  75.                 return false;
  76.         }
  77.  
  78.         return true;
  79.     }
  80.  
  81.     double distance2(double[] x1, double[] x2, int dim)
  82.     {
  83.         double S = 0;
  84.         for (int k = 0; k < dim; k++)
  85.             S += (x1[k] - x2[k]) * (x1[k] - x2[k]);
  86.         return S;
  87.     }
  88. }
  89.  
  90. class KD2DTree
  91. {
  92.     KD2DNode Root;
  93.  
  94.     int TimeStart, TimeFinish;
  95.     int CounterFreq;
  96.  
  97.     double d_min;
  98.     KD2DNode nearest_neighbour;
  99.  
  100.     int KD_id;
  101.  
  102.     int nList;
  103.  
  104.     KD2DNode CheckedNodes[];
  105.     int checked_nodes;
  106.     KD2DNode List[];
  107.  
  108.     double x_min[], x_max[];
  109.     boolean max_boundary[], min_boundary[];
  110.     int n_boundary;
  111.  
  112.     public KD2DTree(int i)
  113.     {
  114.         Root = null;
  115.         KD_id = 1;
  116.         nList = 0;
  117.         List = new KD2DNode[i];
  118.         CheckedNodes = new KD2DNode[i];
  119.         max_boundary = new boolean[2];
  120.         min_boundary = new boolean[2];
  121.         x_min = new double[2];
  122.         x_max = new double[2];
  123.     }
  124.  
  125.     public boolean add(double[] x)
  126.     {
  127.         if (nList >= 2000000 - 1)
  128.             return false; // can't add more points
  129.  
  130.         if (Root == null)
  131.         {
  132.             Root = new KD2DNode(x, 0);
  133.             Root.id = KD_id++;
  134.             List[nList++] = Root;
  135.         } else
  136.         {
  137.             KD2DNode pNode;
  138.             if ((pNode = Root.Insert(x)) != null)
  139.             {
  140.                 pNode.id = KD_id++;
  141.                 List[nList++] = pNode;
  142.             }
  143.         }
  144.  
  145.         return true;
  146.     }
  147.  
  148.     public KD2DNode find_nearest(double[] x)
  149.     {
  150.         if (Root == null)
  151.             return null;
  152.  
  153.         checked_nodes = 0;
  154.         KD2DNode parent = Root.FindParent(x);
  155.         nearest_neighbour = parent;
  156.         d_min = Root.distance2(x, parent.x, 2);
  157.         ;
  158.  
  159.         if (parent.equal(x, parent.x, 2) == true)
  160.             return nearest_neighbour;
  161.  
  162.         search_parent(parent, x);
  163.         uncheck();
  164.  
  165.         return nearest_neighbour;
  166.     }
  167.  
  168.     public void check_subtree(KD2DNode node, double[] x)
  169.     {
  170.         if ((node == null) || node.checked)
  171.             return;
  172.  
  173.         CheckedNodes[checked_nodes++] = node;
  174.         node.checked = true;
  175.         set_bounding_cube(node, x);
  176.  
  177.         int dim = node.axis;
  178.         double d = node.x[dim] - x[dim];
  179.  
  180.         if (d * d > d_min)
  181.         {
  182.             if (node.x[dim] > x[dim])
  183.                 check_subtree(node.Left, x);
  184.             else
  185.                 check_subtree(node.Right, x);
  186.         } else
  187.         {
  188.             check_subtree(node.Left, x);
  189.             check_subtree(node.Right, x);
  190.         }
  191.     }
  192.  
  193.     public void set_bounding_cube(KD2DNode node, double[] x)
  194.     {
  195.         if (node == null)
  196.             return;
  197.         int d = 0;
  198.         double dx;
  199.         for (int k = 0; k < 2; k++)
  200.         {
  201.             dx = node.x[k] - x[k];
  202.             if (dx > 0)
  203.             {
  204.                 dx *= dx;
  205.                 if (!max_boundary[k])
  206.                 {
  207.                     if (dx > x_max[k])
  208.                         x_max[k] = dx;
  209.                     if (x_max[k] > d_min)
  210.                     {
  211.                         max_boundary[k] = true;
  212.                         n_boundary++;
  213.                     }
  214.                 }
  215.             } else
  216.             {
  217.                 dx *= dx;
  218.                 if (!min_boundary[k])
  219.                 {
  220.                     if (dx > x_min[k])
  221.                         x_min[k] = dx;
  222.                     if (x_min[k] > d_min)
  223.                     {
  224.                         min_boundary[k] = true;
  225.                         n_boundary++;
  226.                     }
  227.                 }
  228.             }
  229.             d += dx;
  230.             if (d > d_min)
  231.                 return;
  232.  
  233.         }
  234.  
  235.         if (d < d_min)
  236.         {
  237.             d_min = d;
  238.             nearest_neighbour = node;
  239.         }
  240.     }
  241.  
  242.     public KD2DNode search_parent(KD2DNode parent, double[] x)
  243.     {
  244.         for (int k = 0; k < 2; k++)
  245.         {
  246.             x_min[k] = x_max[k] = 0;
  247.             max_boundary[k] = min_boundary[k] = false; //
  248.         }
  249.         n_boundary = 0;
  250.  
  251.         KD2DNode search_root = parent;
  252.         while (parent != null && (n_boundary != 2 * 2))
  253.         {
  254.             check_subtree(parent, x);
  255.             search_root = parent;
  256.             parent = parent.Parent;
  257.         }
  258.  
  259.         return search_root;
  260.     }
  261.  
  262.     public void uncheck()
  263.     {
  264.         for (int n = 0; n < checked_nodes; n++)
  265.             CheckedNodes[n].checked = false;
  266.     }
  267.  
  268.     public void inorder()
  269.     {
  270.         inorder(Root);
  271.     }
  272.  
  273.     private void inorder(KD2DNode root)
  274.     {
  275.         if (root != null)
  276.         {
  277.             inorder(root.Left);
  278.             System.out.print("(" + root.x[0] + ", " + root.x[1] + ")  ");
  279.             inorder(root.Right);
  280.         }
  281.     }
  282.  
  283.     public void preorder()
  284.     {
  285.         preorder(Root);
  286.     }
  287.  
  288.     private void preorder(KD2DNode root)
  289.     {
  290.         if (root != null)
  291.         {
  292.             System.out.print("(" + root.x[0] + ", " + root.x[1] + ")  ");
  293.             inorder(root.Left);
  294.             inorder(root.Right);
  295.         }
  296.     }
  297.  
  298.     public void postorder()
  299.     {
  300.         postorder(Root);
  301.     }
  302.  
  303.     private void postorder(KD2DNode root)
  304.     {
  305.         if (root != null)
  306.         {
  307.             inorder(root.Left);
  308.             inorder(root.Right);
  309.             System.out.print("(" + root.x[0] + ", " + root.x[1] + ")  ");
  310.         }
  311.     }
  312. }
  313.  
  314. public class KDTree_TwoD_Data
  315. {
  316.     public static void main(String args[]) throws IOException
  317.     {
  318.         int numpoints = 5;
  319.         Scanner sc = new Scanner(System.in);
  320.         KD2DTree kdt = new KD2DTree(numpoints);
  321.         double x[] = new double[2];
  322.         System.out.println("Enter the first 5 data set : <x> <y>");
  323.         for (int i = 0; i < numpoints; i++)
  324.         {
  325.             x[0] = sc.nextDouble();
  326.             x[1] = sc.nextDouble();
  327.             kdt.add(x);
  328.         }
  329.  
  330.         System.out.println("Inorder of 2D Kd tree: ");
  331.         kdt.inorder();
  332.  
  333.         System.out.println("\nPreorder of 2D Kd tree: ");
  334.         kdt.preorder();
  335.  
  336.         System.out.println("\nPostorder of 2D Kd tree: ");
  337.         kdt.postorder();
  338.         sc.close();
  339.     }
  340. }

Output:

advertisement
$ javac KD2D_Insertion.java
$ java KD2D_Insertion
 
Enter the first 10 data set : <x> <y>
0 0
2 3
3 4
4 5
5 6
Inorder of 2D Kd tree: 
(0.0, 0.0)  (2.0, 3.0)  (3.0, 4.0)  (4.0, 5.0)  (5.0, 6.0)  
Preorder of 2D Kd tree: 
(0.0, 0.0)  (2.0, 3.0)  (3.0, 4.0)  (4.0, 5.0)  (5.0, 6.0)  
Postorder of 2D Kd tree: 
(2.0, 3.0)  (3.0, 4.0)  (4.0, 5.0)  (5.0, 6.0)  (0.0, 0.0)

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.