diff --git a/RedBlackNode.jar b/RedBlackNode.jar index 7e987c9cc..f44200aa7 100644 Binary files a/RedBlackNode.jar and b/RedBlackNode.jar differ diff --git a/src/com/github/btrekkie/red_black_node/RedBlackNode.java b/src/com/github/btrekkie/red_black_node/RedBlackNode.java index 70bcb229a..91685acba 100644 --- a/src/com/github/btrekkie/red_black_node/RedBlackNode.java +++ b/src/com/github/btrekkie/red_black_node/RedBlackNode.java @@ -360,7 +360,7 @@ public abstract class RedBlackNode> implements Compara * @param allowDuplicates Whether to insert newNode if there is an equal node in the tree. To check whether we * inserted newNode, check whether newNode.parent is null and the return value differs from newNode. * @param comparator A comparator indicating where to put the node. If this is null, we use the nodes' natural - * order, as in N.compare. + * order, as in N.compareTo. * @return The root of the resulting tree. */ public N insert(N newNode, boolean allowDuplicates, Comparator comparator) { @@ -829,10 +829,14 @@ public abstract class RedBlackNode> implements Compara * inserting all of the nodes in "last". */ public N concatenate(N last) { + if (parent != null) { + throw new IllegalArgumentException("The node is not the root of a tree"); + } + if (last.parent != null) { + throw new IllegalArgumentException("The node is not the root of a tree"); + } if (isLeaf()) { return last; - } else if (parent != null) { - throw new IllegalArgumentException("This is not the root of a tree"); } else if (last.isLeaf()) { @SuppressWarnings("unchecked") N nThis = (N)this; @@ -1112,6 +1116,7 @@ public abstract class RedBlackNode> implements Compara * The base class's implementation takes O(log N) time. If a RedBlackNode subclass stores a value used to order the * nodes, then it could override compareTo to compare the nodes' values, which would take O(1) time. */ + @Override public int compareTo(N other) { if (isLeaf() || other.isLeaf()) { throw new IllegalArgumentException("One of the nodes is a leaf node"); @@ -1325,7 +1330,7 @@ public abstract class RedBlackNode> implements Compara * useful for debugging. RedBlackNode subclasses may want to override assertSubtreeIsValid() to call * assertOrderIsValid. * @param comparator A comparator indicating how the nodes should be ordered. If this is null, we use the nodes' - * natural order, as in N.compare. + * natural order, as in N.compareTo. */ public void assertOrderIsValid(Comparator comparator) { if (comparator == null) { diff --git a/src/com/github/btrekkie/sub_array_min/SubArrayMin.java b/src/com/github/btrekkie/sub_array_min/SubArrayMin.java index 874db6575..63634c9f8 100644 --- a/src/com/github/btrekkie/sub_array_min/SubArrayMin.java +++ b/src/com/github/btrekkie/sub_array_min/SubArrayMin.java @@ -57,6 +57,9 @@ public class SubArrayMin { int min = Math.min(lca.value, Math.min(start.value, end.value)); if (start != lca) { + if (start.right.min < min) { + min = start.right.min; + } for (SubArrayMinNode node = start; node.parent != lca; node = node.parent) { if (node.parent.left == node) { if (node.parent.value < min) { @@ -69,6 +72,9 @@ public class SubArrayMin { } } if (end != lca) { + if (end.left.min < min) { + min = end.left.min; + } for (SubArrayMinNode node = end; node.parent != lca; node = node.parent) { if (node.parent.right == node) { if (node.parent.value < min) { diff --git a/src/com/github/btrekkie/sub_array_min/test/SubArrayMinTest.java b/src/com/github/btrekkie/sub_array_min/test/SubArrayMinTest.java index d69657f7a..a29719ddc 100644 --- a/src/com/github/btrekkie/sub_array_min/test/SubArrayMinTest.java +++ b/src/com/github/btrekkie/sub_array_min/test/SubArrayMinTest.java @@ -13,16 +13,16 @@ public class SubArrayMinTest { SubArrayMin sam = new SubArrayMin(); sam.add(12); sam.add(42); - sam.add(16); sam.add(-3); - sam.add(8); + sam.add(16); sam.add(5); + sam.add(8); sam.add(4); assertEquals(-3, sam.min(0, 7)); - assertEquals(12, sam.min(0, 3)); + assertEquals(12, sam.min(0, 2)); assertEquals(-3, sam.min(2, 4)); assertEquals(12, sam.min(0, 1)); - assertEquals(5, sam.min(4, 6)); + assertEquals(5, sam.min(3, 6)); assertEquals(4, sam.min(4, 7)); sam = new SubArrayMin();