Question: Problem . (2d-tree Implementation) Write a mutable data type KdTreePointST that uses a 2d-tree to implement the above symbol table API. A 2d-tree is a
Problem. (2d-tree Implementation) Write a mutable data type KdTreePointST that uses a 2d-tree to implement the above symbol table API. A 2d-tree is a generalization of a BST to two-dimensional keys. The idea is to build a BST with points in the nodes, using the x- and y-coordinates of the points as keys in strictly alternating sequence, starting with the x-coordinates.
java KdTreePointST 0.661633 0.287141 0.65 0.68 0.28 0.29 5 < data/input10K.txt st.empty()? false st.size() = 10000 First 5 values: 0 2 1 4 3 62 st.contains((0.661633 , 0.287141))? true st.range([0.65 , 0.68] x [0.28 , 0.29]): (0.671793, 0.288608) (0.663908, 0.285337) (0.661633, 0.287141) st.nearest((0.661633, 0.287141)) = (0.663908, 0.285337) st.nearest((0.661633, 0.287141), 5): (0.668229, 0.276482) (0.65471, 0.276885) (0.671793, 0.288608) (0.658329, 0.290039) (0.663908, 0.285337)
import edu.princeton.cs.algs4.MaxPQ; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.Queue; import edu.princeton.cs.algs4.RectHV; import edu.princeton.cs.algs4.StdIn; import edu.princeton.cs.algs4.StdOut;
public class KdTreePointST
// Construct a node given the point, the associated value, and the // axis-aligned rectangle corresponding to the node. Node(Point2D p, Value val, RectHV rect) { this.p = p; this.val = val; this.rect = rect; } }
// Construct an empty symbol table of points. public KdTreePointST() { ... }
// Is the symbol table empty? public boolean isEmpty() { ... }
// Number of points in the symbol table. public int size() { ... }
// Associate the value val with point p. public void put(Point2D p, Value val) { ... }
// Helper for put(Point2D p, Value val). private Node put(Node x, Point2D p, Value val, RectHV rect, boolean lr) { ... }
// Value associated with point p. public Value get(Point2D p) { ... }
// Helper for get(Point2D p). private Value get(Node x, Point2D p, boolean lr) { ... }
// Does the symbol table contain the point p? public boolean contains(Point2D p) { ... }
// All points in the symbol table, in level order. public Iterable
// All points in the symbol table that are inside the rectangle rect. public Iterable
// Helper for public range(RectHV rect). private void range(Node x, RectHV rect, Queue
// A nearest neighbor to point p; null if the symbol table is empty. public Point2D nearest(Point2D p) { ... } // Helper for public nearest(Point2D p). private Point2D nearest(Node x, Point2D p, Point2D nearest, double nearestDistance, boolean lr) { ... }
// k points that are closest to point p. public Iterable
// Helper for public nearest(Point2D p, int k). private void nearest(Node x, Point2D p, int k, MaxPQ
// Test client. [DO NOT EDIT] public static void main(String[] args) { KdTreePointST
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
