summaryrefslogtreecommitdiffstats
path: root/Algorithms
diff options
context:
space:
mode:
Diffstat (limited to 'Algorithms')
-rw-r--r--Algorithms/Part-I/5-KdTrees/KdTree.java13
1 files changed, 10 insertions, 3 deletions
diff --git a/Algorithms/Part-I/5-KdTrees/KdTree.java b/Algorithms/Part-I/5-KdTrees/KdTree.java
index 73d9a90..bcf4609 100644
--- a/Algorithms/Part-I/5-KdTrees/KdTree.java
+++ b/Algorithms/Part-I/5-KdTrees/KdTree.java
@@ -100,7 +100,7 @@ public class KdTree
// does the tree contain the point p?
public boolean contains(Point2D p)
{
- if (root == null) return false;
+ if ((p == null) || (root == null)) return false;
return get(root, p, true);
}
@@ -113,7 +113,11 @@ public class KdTree
cmp = Point2D.Y_ORDER.compare(p, n.p);
if (cmp < 0)
+ {
+ if (n.left == null) return false;
return get(n.left, p, !vsplit);
+ }
+ if (n.right == null) return false;
return get(n.right, p, !vsplit);
}
@@ -163,6 +167,8 @@ public class KdTree
{
Stack<Point2D> stack = new Stack<Point2D>();
+ if ((rect == null) || (root == null)) return stack;
+
range(root, rect, stack, true);
return stack;
@@ -191,10 +197,11 @@ public class KdTree
// a nearest neighbor in the tree to p; null if tree is empty
public Point2D nearest(Point2D p)
{
- if (p == null) return null;
- if (root == null) return null;
+ if ((p == null) || (root == null)) return null;
+
NearestChampion ncp = new NearestChampion(null, Double.MAX_VALUE);
nearest(root, p, ncp, true);
+
return ncp.p;
}