Fixed SubArrayMin to check children

This fixes SubArrayMin to check the appropriate children of the endpoint nodes, in addition to the children of their ancestors.
This commit is contained in:
Bill Jacobs
2016-06-17 21:50:16 -07:00
parent 8c98d5cc42
commit 91b5ae633a
4 changed files with 19 additions and 8 deletions

View File

@@ -360,7 +360,7 @@ public abstract class RedBlackNode<N extends RedBlackNode<N>> 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<? super N> comparator) {
@@ -829,10 +829,14 @@ public abstract class RedBlackNode<N extends RedBlackNode<N>> 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<N extends RedBlackNode<N>> 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<N extends RedBlackNode<N>> 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<? super N> comparator) {
if (comparator == null) {

View File

@@ -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) {

View File

@@ -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();