Question

In: Computer Science

Create a kD tree with: x-nodes and y-nodes -- and maintain the following two properties: The...

Create a kD tree with:

x-nodes and y-nodes -- and maintain the following two properties:

  • The children of an x-node are y-nodes
  • The children of a y-node are x-nodes

Each type of node uses a different comparison to order points. This causes different levels of the tree to compare points differently, using the following rules:

  • For every x-node:
    • All descendants in the left subtree have a smaller x-coordinate than the point stored at the node. Visually, all descendant points are left of this point.
    • All descendants in the right subtree have a larger x-coordinate than the points stored at the node. Visually, all descendant points are right of this point.
  • For every y-node:
    • All descendants in the left subtree have a smaller y-coordinate than the point stored at the node. Visually, all descendant points are below this point.
    • All descendants in the right subtree have a larger y-coordinate than the point stored at the node. Visually, all descendant points are above this point.

Create a draw method:

  • void draw()
    • Draws the spatial tree (you may use StdDraw). Each node should show the point, and the regions bounding each point (see below). (Hint: use recursion.)

Please implement in Java.

Solutions

Expert Solution

K-D Tree also called as K-Dimensional Tree, is a binary search tree where data in each node is a K-Dimensional point in space. It is used for various applications like nearest point in k-dimensional space, efficient storage of spatial data, range search etc. A non-leaf node in K-D tree divides the space into two parts, called as half-spaces. The left child of the node represents the left half while right child represents right half. The space is divided into 2 halves irrespective of the number of dimensions. To be more accurate, every internal node represents a hyperplane that cuts the space in 2 parts. The root would have an x-aligned plane, the root’s children would both have y-aligned planes, the root’s grandchildren would all have x-aligned planes, and the root’s great-grandchildren would all have y-aligned planes and so on.

Insert(x, y): Every insert operation divides the space. The algorithm here considers space to be 2-dimensional but is applicable in all dimensions:

Search the tree for (x, y) until a leaf node is reached.
If the tree is empty, add a new node as root representing the point (x, y). Here, the space can be divided along any axis. Indicate the axis along which the space is divided and end insertion.
Insert a new node where the point (x, y) should have existed and have it store (x, y). If the parent divided the space along x-axis, have the point divide the space along y-axis, otherwise have it divide space along x-axis.
Consider following points in a 2-D plane:
(3, 6), (17, 15), (13, 15), (6, 12), (9, 1), (2, 7), (10, 19)

Insert (3, 6): Since tree is empty, make it the root node.
Insert (17, 15): Compare it with root node point. Since root node is X-aligned, the X-coordinate value will be compared to determine if it lies in the rightsubtree or in the right subtree. This point will be Y-aligned.
Insert (13, 15): X-value of this point is greater than X-value of point in root node. So, this will lie in the right subtree of (3, 6). Again Compare Y-value of this point with the Y-value of point (17, 15) (Why?). Since, they are equal, this point will lie in the right subtree of (17, 15). This point will be X-aligned.
Insert (6, 12): X-value of this point is greater than X-value of point in root node. So, this will lie in the right subtree of (3, 6). Again Compare Y-value of this point with the Y-value of point (17, 15) (Why?). Since, 12 < 15, this point will lie in the left subtree of (17, 15). This point will be X-aligned.
Insert (9, 1):Similarly, this point will lie in the right of (6, 12).
Insert (2, 7):Similarly, this point will lie in the left of (3, 6).
Insert (10, 19): Similarly, this point will lie in the left of (13, 15).
Search(x, y): This function checks if the point exists in space. Start with root node as current node.

If the current node represents the point (x, y), return true.
If current node is not a leaf node, goto step 3, otherwise return false.
Let current node be the point (X, Y). If the node divides space along x-axis, compare x with X. If x < X, set current node as left child, otherwise set current node as right child. If the node divided the space along y-axis, compare y and Y.
Goto step 1.
Point2D class is a part of JavaFX. This class defines a 2-dimensional point in space. The Point2D class represents a 2D point by its x, y coordinates. It inherits java.lang.Object class. Constructor of the class is Point2D(double x, double y): Create a point2D object with specified x and y coordinates. Most common methods of Point2D class are:

distance(double x1, double y1) - Calculates the distance between this point and point (x1, y1)
equals(java.lang.Object obj) - Returns whether this object is equal to the specified object or not
getX() - Returns the x coordinate of the point
getY() - Returns the y coordinate of the point
Here is a program

kd-trees :

Each level has a cutting dimension
Cycle through the dimensions as you walk down the tree.
Each node contains a point P = (x,y)
To find (x',y') only compare coordinate from the cutting dimension, if cutting dimension is x, then ask: is x’ < x ?
Insert Code :

insert(Point x, KDNode t, int cd)
{
if t == null
t = new KDNode(x)
else if (x == t.data)
// error! duplicate
else if (x[cd] < t.data[cd])
t.left = insert(x, t.left, (cd+1) % DIM)
else
t.right = insert(x, t.right, (cd+1) % DIM)
return t
}

Java Program for you :

class KDNode
{
KDNode left;
KDNode right;
int []data;
public KDNode()
{
left=null;
right=null;
}
public KDNode(int []x)
{
left=null;
right=null;
data = new int[2];
for (int k = 0; k < 2; k++)
data[k]=x[k];
}
}
class SpatialTree
{
KDNode root;
int cd=0;
int DIM=2;
public SpatialTree()
{
root=null;
}
public boolean isEmpty()
{
return root == null;
}
public void insert(int []x)
{
root = insert(x,root,cd);
}
private KDNode insert(int []x,KDNode t,int cd)
{
if (t == null)
t = new KDNode(x);
else if (x[cd] < t.data[cd])
t.left = insert(x, t.left, (cd+1)%DIM);
else
t.right = insert(x, t.right, (cd+1)%DIM);
return t;
}
public boolean search(int []data)
{
return search(data,root,0);
}
private boolean search(int []x,KDNode t,int cd)
{
boolean found=false;
if(t==null){
return false;
}
else
{
if(x[cd]==t.data[cd])
{
if(x[0]==t.data[0] && x[1]==t.data[1])
return true;
}
else if(x[cd]<t.data[cd])
{
found = search(x,t.left,(cd+1)%DIM);
}
else if(x[cd]>t.data[cd])
{
found = search(x,t.right,(cd+1)%DIM);
}
return found;
}
}
public void inorder()
{
inorder(root);
}
private void inorder(KDNode r)
{
if (r != null)
{
inorder(r.left);
System.out.print("("+r.data[0]+","+r.data[1] +") ");
inorder(r.right);
}
}
public void preorder()
{
preorder(root);
}
private void preorder(KDNode r)
{
if (r != null)
{
System.out.print("("+r.data[0]+","+r.data[1] +") ");
preorder(r.left);
preorder(r.right);
}
}
/ Function for postorder traversal /
public void postorder()
{
postorder(root);
}
private void postorder(KDNode r)
{
if (r != null)
{
postorder(r.left);
postorder(r.right);
System.out.print("("+r.data[0]+","+r.data[1] +") ");
}
}
}
public class SpatialTreeNode
{
public static void main(String[] args)
{
SpatialTree stn = new SpatialTree();
int x[] = new int[2];
x[0] = 30;
x[1] = 40;
stn.insert(x);
x[0] = 5;
x[1] = 25;
stn.insert(x);
x[0] = 10;
x[1] = 12;
stn.insert(x);
x[0] = 70;
x[1] = 70;
stn.insert(x);
x[0] = 50;
x[1] = 30;
stn.insert(x);
System.out.println("Input Elements");
System.out.println("(30,40) (5,25) (10,12) (70,70) (50,30)\n\n");
System.out.println("Printing KD Tree in Inorder");
stn.inorder();
System.out.println("\nPrinting KD Tree in PreOder");
stn.preorder();
System.out.println("\nPrinting KD Tree in PostOrder");
stn.postorder();
System.out.println("\nsearching...............");
x[0]=40;x[1]=40;
System.out.OUTprintln(stn.search(x));
}
}

OUTPUT


Related Solutions

the following lists the nodes in a binary tree in two different orders: Preorder : A...
the following lists the nodes in a binary tree in two different orders: Preorder : A B C D E F G H I J K L M Inorder : C E D F B A H J I K G M L Draw the binary tree
Protein X has a Kd of 0.25 micromolar; protein Y has a Kd of 0.5 micromolar,...
Protein X has a Kd of 0.25 micromolar; protein Y has a Kd of 0.5 micromolar, and protein Z has a Kd of 0.75 micromolar for ligand A. Which one of the following is true? Group of answer choices 1-Protein X has the highest affinity for ligand A. 2-Protein Z has the highest affinity for ligand A. 3-Protein X has the lowest affinity for ligand A. 4-Protein Y affinity for ligand A is higher than that of protein X.
(In Matlab) Create a base class named Point that has the properties for x and y...
(In Matlab) Create a base class named Point that has the properties for x and y coordinates. From this class derive a class named Circle having an additional property named radius. For this derived class, the x and y properties represent the center coordinates of a circle. The methods of the base class should consist of a constructor, an area function that returns 0, and a distance function that returns the distance between two points. The derived class should have...
2. Prove the following properties. (b) Prove that x + ¯ xy = x + y.
2. Prove the following properties.(b) Prove that x + ¯ xy = x + y.3. Consider the following Boolean function: F = x¯ y + xy¯ z + xyz(a) Draw a circuit diagram to obtain the output F. (b) Use the Boolean algebra theorems to simplify the output function F into the minimum number of input literals.
a tree is binary, if every node has at most two children nodes. prove that the...
a tree is binary, if every node has at most two children nodes. prove that the maximum of nodes in a binary tree of height h is 2^(h+1)-1
Draw the figure and compute for the Section Properties ("x" and "y") of the following rolled...
Draw the figure and compute for the Section Properties ("x" and "y") of the following rolled structural steel section: Channel (C300 x 38) with overall depth of 300 mm, flange width of 90 mm, web thickness of 9 mm, and flange thickness of 12 mm.
Let T = (V,E) be a tree, and letr, r′ ∈ V be any two nodes....
Let T = (V,E) be a tree, and letr, r′ ∈ V be any two nodes. Prove that the height of the rooted tree (T, r) is at most twice the height of the rooted tree (T, r′).
Give examples–a formula and an illustration–of two-dimensional vector fields F⃗(x,y) with each of the following properties....
Give examples–a formula and an illustration–of two-dimensional vector fields F⃗(x,y) with each of the following properties. You could do the illustrations by hand. a) The direction of F⃗ is constant but the magnitude is not constant. b) The magnitude |F⃗| is constant but the direction is not constant. c) All the vectors F⃗ along a horizontal line are equal, but F⃗ is not constant overall. d) F⃗ (x, y) is perpendicular to xˆi + yˆj at every point (x, y)....
Write pseudocode (C++) that will create a 50 x 50 grid of nodes and an output...
Write pseudocode (C++) that will create a 50 x 50 grid of nodes and an output function that displays the row and column of each node in the grid after it is created. -Creating the grid -Printing the grid -Deleting the grid • For each function o Determine the parameters and return type o Detail the step-by-step logic that the function will perform
Two linearly independent solutions of the following equation (1 − x) y″  +  x y′  − ...
Two linearly independent solutions of the following equation (1 − x) y″  +  x y′  −  y  =  0 are  y1(x)  =  4ex and  y2(x)  =  8x. (a) Find the Wronskian W(y1, y2) of y1 and y2. (b) Using the method of variation of parameters, find a particular solution of (1 − x) y″  +  x y′  −  y  =  2(x − 1)2 e −x
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT