|
1 | 1 | package com.jwetherell.algorithms.data_structures;
|
2 | 2 |
|
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; |
4 | 14 | import java.util.List;
|
| 15 | +import java.util.Set; |
| 16 | +import java.util.TreeSet; |
5 | 17 |
|
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 |
9 | 20 | * structure for organizing points in a k-dimensional space. k-d trees are a
|
10 | 21 | * useful data structure for several applications, such as searches involving a
|
11 | 22 | * multidimensional search key (e.g. range searches and nearest neighbor
|
@@ -316,7 +327,8 @@ private static final List<XYZPoint> getTree(KdNode root) {
|
316 | 327 | return list;
|
317 | 328 | }
|
318 | 329 |
|
319 |
| - /** Searches the K nearest neighbor. |
| 330 | + /** |
| 331 | + * Searches the K nearest neighbor. |
320 | 332 | *
|
321 | 333 | * @param K
|
322 | 334 | * 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
|
442 | 454 | }
|
443 | 455 | }
|
444 | 456 |
|
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. |
448 | 465 | */
|
449 | 466 | @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) { |
454 | 469 | results.add((T) node.id);
|
455 | 470 | search(node.greater, results);
|
456 | 471 | search(node.lesser, results);
|
@@ -488,22 +503,26 @@ else if (d2.compareTo(d1) < 0)
|
488 | 503 | }
|
489 | 504 | }
|
490 | 505 |
|
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. |
493 | 511 | */
|
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>(); |
497 | 514 | search(root, results);
|
498 | 515 | return results.iterator();
|
499 | 516 | }
|
500 | 517 |
|
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. |
503 | 523 | */
|
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>(); |
507 | 526 | search(root, results);
|
508 | 527 | return results.descendingIterator();
|
509 | 528 | }
|
|
0 commit comments