Fixed RedBlackNode.concatenate on two one-node trees

This fixes RedBlackNode.concatenate to work on two one-node trees.  The check for determining which tree had the greater red-black height was incorrect in that case.
This commit is contained in:
Bill Jacobs
2016-07-22 17:03:36 -07:00
parent 5e23bd9c81
commit 9534c4ae06
3 changed files with 12 additions and 5 deletions

Binary file not shown.

View File

@@ -772,9 +772,10 @@ public abstract class RedBlackNode<N extends RedBlackNode<N>> implements Compara
N parent;
if (firstBlackHeight <= lastBlackHeight) {
parent = null;
while (lastBlackHeight > firstBlackHeight) {
int blackHeight = lastBlackHeight;
while (blackHeight > firstBlackHeight) {
if (!lastChild.isRed) {
lastBlackHeight--;
blackHeight--;
}
parent = lastChild;
lastChild = lastChild.left;
@@ -785,9 +786,10 @@ public abstract class RedBlackNode<N extends RedBlackNode<N>> implements Compara
}
} else {
parent = null;
while (firstBlackHeight > lastBlackHeight) {
int blackHeight = firstBlackHeight;
while (blackHeight > lastBlackHeight) {
if (!firstChild.isRed) {
firstBlackHeight--;
blackHeight--;
}
parent = firstChild;
firstChild = firstChild.right;
@@ -802,7 +804,7 @@ public abstract class RedBlackNode<N extends RedBlackNode<N>> implements Compara
pivot.isRed = true;
pivot.parent = parent;
if (parent != null) {
if (parent.left == lastChild) {
if (firstBlackHeight < lastBlackHeight) {
parent.left = pivot;
} else {
parent.right = pivot;

View File

@@ -72,6 +72,11 @@ public class TreeListTest {
assertEquals(6, list.get(2).intValue());
assertEquals(null, list.get(5));
list = new TreeList<Integer>();
list.add(7);
list.addAll(Collections.singleton(37));
assertEquals(Arrays.asList(7, 37), list);
list = new TreeList<Integer>();
for (int i = 0; i < 200; i++) {
list.add(i + 300);