Skip to content

Commit 8c28f16

Browse files
author
Justin Wetherell
committed
Cleaned up the new KdTree pull request
1 parent 8aa7e11 commit 8c28f16

File tree

3 files changed

+46
-80
lines changed

3 files changed

+46
-80
lines changed

.settings/org.eclipse.jdt.ui.prefs

-54
This file was deleted.

src/com/jwetherell/algorithms/data_structures/KdTree.java

+41-22
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
package com.jwetherell.algorithms.data_structures;
22

3-
import java.util.*;
3+
import static java.lang.Math.cos;
4+
import static java.lang.Math.sin;
5+
6+
import java.util.ArrayDeque;
7+
import java.util.ArrayList;
8+
import java.util.Collection;
9+
import java.util.Collections;
10+
import java.util.Comparator;
11+
import java.util.Deque;
12+
import java.util.HashSet;
13+
import java.util.Iterator;
414
import java.util.List;
15+
import java.util.Set;
16+
import java.util.TreeSet;
517

6-
import static java.lang.Math.*;
7-
8-
/** A k-d tree (short for k-dimensional tree) is a space-partitioning data
18+
/**
19+
* A k-d tree (short for k-dimensional tree) is a space-partitioning data
920
* structure for organizing points in a k-dimensional space. k-d trees are a
1021
* useful data structure for several applications, such as searches involving a
1122
* multidimensional search key (e.g. range searches and nearest neighbor
@@ -316,7 +327,8 @@ private static final List<XYZPoint> getTree(KdNode root) {
316327
return list;
317328
}
318329

319-
/** Searches the K nearest neighbor.
330+
/**
331+
* Searches the K nearest neighbor.
320332
*
321333
* @param K
322334
* Number of neighbors to retrieve. Can return more than K, if
@@ -442,15 +454,18 @@ private static final <T extends KdTree.XYZPoint> void searchNode(T value, KdNode
442454
}
443455
}
444456

445-
/** Adds, in a specified queue, a given node and its related nodes (lesser, greater).
446-
* @param node Node to check. May be null.
447-
* @param results Queue containing all found entries. Must not be null.
457+
/**
458+
* Adds, in a specified queue, a given node and its related nodes (lesser, greater).
459+
*
460+
* @param node
461+
* Node to check. May be null.
462+
*
463+
* @param results
464+
* Queue containing all found entries. Must not be null.
448465
*/
449466
@SuppressWarnings("unchecked")
450-
private static <T extends XYZPoint> void search(final KdNode node, final Deque<T> results)
451-
{
452-
if (node != null)
453-
{
467+
private static <T extends XYZPoint> void search(final KdNode node, final Deque<T> results) {
468+
if (node != null) {
454469
results.add((T) node.id);
455470
search(node.greater, results);
456471
search(node.lesser, results);
@@ -488,22 +503,26 @@ else if (d2.compareTo(d1) < 0)
488503
}
489504
}
490505

491-
/** Searches all entries from the first to the last entry.
492-
* @return Iterator allowing to iterate through a collection containing all found entries.
506+
/**
507+
* Searches all entries from the first to the last entry.
508+
*
509+
* @return Iterator
510+
* allowing to iterate through a collection containing all found entries.
493511
*/
494-
public Iterator<T> iterator()
495-
{
496-
final Deque<T> results = new LinkedList<T>();
512+
public Iterator<T> iterator() {
513+
final Deque<T> results = new ArrayDeque<T>();
497514
search(root, results);
498515
return results.iterator();
499516
}
500517

501-
/** Searches all entries from the last to the first entry.
502-
* @return Iterator allowing to iterate through a collection containing all found entries.
518+
/**
519+
* Searches all entries from the last to the first entry.
520+
*
521+
* @return Iterator
522+
* allowing to iterate through a collection containing all found entries.
503523
*/
504-
public Iterator<T> reverse_iterator()
505-
{
506-
final Deque<T> results = new LinkedList<T>();
524+
public Iterator<T> reverse_iterator() {
525+
final Deque<T> results = new ArrayDeque<T>();
507526
search(root, results);
508527
return results.descendingIterator();
509528
}

src/com/jwetherell/algorithms/data_structures/test/KdTreeTests.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package com.jwetherell.algorithms.data_structures.test;
22

3+
import static org.junit.Assert.assertTrue;
4+
35
import java.util.ArrayList;
46
import java.util.Collection;
7+
import java.util.List;
58

69
import org.junit.Test;
710

8-
import static org.junit.Assert.assertTrue;
9-
1011
import com.jwetherell.algorithms.data_structures.KdTree;
1112
import com.jwetherell.algorithms.data_structures.KdTree.XYZPoint;
1213

1314
public class KdTreeTests {
1415

1516
@Test
1617
public void testKdTree() {
17-
java.util.List<XYZPoint> points = new ArrayList<XYZPoint>();
18+
List<XYZPoint> points = new ArrayList<XYZPoint>();
1819
XYZPoint p1 = new XYZPoint(2, 3);
1920
points.add(p1);
2021
XYZPoint p2 = new XYZPoint(5, 4);
@@ -50,7 +51,7 @@ public void testKdTree() {
5051

5152
@Test
5253
public void testKdTree_as_iterable() {
53-
java.util.List<XYZPoint> points = new ArrayList<XYZPoint>();
54+
List<XYZPoint> points = new ArrayList<XYZPoint>();
5455
XYZPoint p1 = new XYZPoint(2, 3);
5556
points.add(p1);
5657
XYZPoint p2 = new XYZPoint(5, 4);

0 commit comments

Comments
 (0)