Example #1
0
  /** Test of nearest method, of class CoverTree. */
  @Test
  public void testCoverTree() {
    long start = System.currentTimeMillis();
    for (int i = 0; i < testx.length; i++) {
      coverTree.nearest(testx[i]);
    }
    double time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("NN: %.2fs\n", time);

    start = System.currentTimeMillis();
    for (int i = 0; i < testx.length; i++) {
      coverTree.knn(testx[i], 10);
    }
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("10-NN: %.2fs\n", time);

    start = System.currentTimeMillis();
    List<Neighbor<double[], double[]>> n = new ArrayList<Neighbor<double[], double[]>>();
    for (int i = 0; i < testx.length; i++) {
      coverTree.range(testx[i], 8.0, n);
      n.clear();
    }
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("Range: %.2fs\n", time);
  }
Example #2
0
  // Usage example
  public static void main(String[] args) throws Exception {
    PrintWriter pw = new PrintWriter(System.out);
    int t = nextInt();
    for (int i = 0; i < t; i++) {
      CoverTree tree = new CoverTree();

      int n = nextInt();
      CoverTree.Node[] nodes = new CoverTree.Node[n];
      for (int j = 0; j < n; j++) {
        nodes[j] = new CoverTree.Node(nextInt(), nextInt());
        tree.insert(nodes[j]);
      }
      for (int j = 0; j < n; j++) {
        CoverTree.Node p = tree.findNearest(nodes[j]);
        pw.println(dist2(p, nodes[j]));
      }
    }
    pw.close();
  }