Commit 08fbf7be authored by pysaumont's avatar pysaumont
Browse files

Fixes after AC review for chapter 11

parent 39c17309
......@@ -122,7 +122,7 @@ public abstract class Heap<A extends Comparable<A>> {
return new H<>(1, 1, empty(), element, empty());
}
public static <A extends Comparable<A>> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
protected static <A extends Comparable<A>> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
return first.rank() >= second.rank()
? new H<>(first.length() + second.length() + 1, second.rank() + 1, first, head, second)
: new H<>(first.length() + second.length() + 1, first.rank() + 1, second, head, first);
......
......@@ -133,7 +133,7 @@ public abstract class Heap<A extends Comparable<A>> {
return new H<>(1, 1, empty(), element, empty());
}
public static <A extends Comparable<A>> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
protected static <A extends Comparable<A>> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
return first.rank() >= second.rank()
? new H<>(first.length() + second.length() + 1, second.rank() + 1, first, head, second)
: new H<>(first.length() + second.length() + 1, first.rank() + 1, second, head, first);
......
......@@ -136,7 +136,7 @@ public abstract class Heap<A extends Comparable<A>> {
return new H<>(1, 1, empty(), element, empty());
}
public static <A extends Comparable<A>> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
protected static <A extends Comparable<A>> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
return first.rank() >= second.rank()
? new H<>(first.length() + second.length() + 1, second.rank() + 1, first, head, second)
: new H<>(first.length() + second.length() + 1, first.rank() + 1, second, head, first);
......
......@@ -201,7 +201,7 @@ public abstract class Heap<A> {
return new H<>(1, 1, element, empty, empty, Result.success(comparator));
}
public static <A> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
protected static <A> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
Result<Comparator<A>> comparator = first.comparator().orElse(second::comparator);
return first.rank() >= second.rank()
? new H<>(first.length() + second.length() + 1, second.rank() + 1, head, first, second, comparator)
......
......@@ -20,8 +20,6 @@ public abstract class Heap<A extends Comparable<A>> {
public abstract Result<A> head();
public abstract Result<Heap<A>> tail();
public abstract int length();
public abstract boolean isEmpty();
......@@ -49,11 +47,6 @@ public abstract class Heap<A extends Comparable<A>> {
return Result.failure(new NoSuchElementException("head() called on empty heap"));
}
@Override
public Result<Heap<A>> tail() {
throw new IllegalStateException("To be implemented");
}
@Override
public int length() {
return 0;
......@@ -106,11 +99,6 @@ public abstract class Heap<A extends Comparable<A>> {
return Result.success(this.head);
}
@Override
public Result<Heap<A>> tail() {
throw new IllegalStateException("To be implemented");
}
@Override
public int length() {
return this.length;
......@@ -141,7 +129,7 @@ public abstract class Heap<A extends Comparable<A>> {
return new H<>(1, 1, empty(), element, empty());
}
public static <A extends Comparable<A>> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
protected static <A extends Comparable<A>> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
return first.rank() >= second.rank()
? new H<>(first.length() + second.length() + 1, second.rank() + 1, first, head, second)
: new H<>(first.length() + second.length() + 1, first.rank() + 1, second, head, first);
......
......@@ -133,7 +133,7 @@ public abstract class Heap<A extends Comparable<A>> {
return new H<>(1, 1, empty(), element, empty());
}
public static <A extends Comparable<A>> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
protected static <A extends Comparable<A>> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
return first.rank() >= second.rank()
? new H<>(first.length() + second.length() + 1, second.rank() + 1, first, head, second)
: new H<>(first.length() + second.length() + 1, first.rank() + 1, second, head, first);
......
......@@ -135,7 +135,7 @@ public abstract class Heap<A extends Comparable<A>> {
return new H<>(1, 1, empty(), element, empty());
}
public static <A extends Comparable<A>> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
protected static <A extends Comparable<A>> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
return first.rank() >= second.rank()
? new H<>(first.length() + second.length() + 1, second.rank() + 1, first, head, second)
: new H<>(first.length() + second.length() + 1, first.rank() + 1, second, head, first);
......
......@@ -210,7 +210,7 @@ public abstract class Heap<A> {
return new H<>(1, 1, element, empty, empty, Result.success(comparator));
}
public static <A> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
protected static <A> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
Result<Comparator<A>> comparator = first.comparator().orElse(second::comparator);
return first.rank() >= second.rank()
? new H<>(first.length() + second.length() + 1, second.rank() + 1, head, first, second, comparator)
......
......@@ -206,7 +206,7 @@ public abstract class Heap<A> {
return new H<>(1, 1, element, empty, empty, Result.success(comparator));
}
public static <A> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
protected static <A> Heap<A> heap(A head, Heap<A> first, Heap<A> second) {
Result<Comparator<A>> comparator = first.comparator().orElse(second::comparator);
return first.rank() >= second.rank()
? new H<>(first.length() + second.length() + 1, second.rank() + 1, head, first, second, comparator)
......
......@@ -11,17 +11,13 @@ public abstract class Tree<A extends Comparable<A>> {
private static Tree E = new E();
private static Color R = new Red();
private static Color B = new Black();
protected abstract boolean isE();
protected abstract boolean isT();
protected abstract boolean isB();
protected abstract boolean isR();
protected abstract boolean isTB();
protected abstract boolean isTR();
public abstract boolean isEmpty();
protected abstract Tree<A> right();
protected abstract Tree<A> left();
protected abstract A value();
......@@ -47,12 +43,12 @@ public abstract class Tree<A extends Comparable<A>> {
@Override
public Tree<A> right() {
return empty();
return E;
}
@Override
public Tree<A> left() {
return empty();
return E;
}
@Override
......@@ -61,7 +57,7 @@ public abstract class Tree<A extends Comparable<A>> {
}
@Override
public boolean isR() {
protected boolean isR() {
return false;
}
......@@ -97,6 +93,7 @@ public abstract class Tree<A extends Comparable<A>> {
}
private static class T<A extends Comparable<A>> extends Tree<A> {
private final Tree<A> left;
private final Tree<A> right;
private final A value;
......@@ -183,6 +180,7 @@ public abstract class Tree<A extends Comparable<A>> {
}
private static class Red extends Color {
@Override
boolean isR() {
return true;
......@@ -200,6 +198,7 @@ public abstract class Tree<A extends Comparable<A>> {
}
private static class Black extends Color {
@Override
boolean isR() {
return false;
......@@ -216,7 +215,6 @@ public abstract class Tree<A extends Comparable<A>> {
}
}
@SuppressWarnings("unchecked")
public static <A extends Comparable<A>> Tree<A> empty() {
return E;
}
......
package com.fpinjava.advancedtrees.listing11_02;
/*
* see http://www.cs.cmu.edu/~rwh/theses/okasaki.pdf
* see http://matt.might.net/papers/germane2014deletion.pdf
* see http://matt.might.net/articles/red-black-delete/
*/
public abstract class Tree<A extends Comparable<A>> {
private static Tree E = new E();
private static Color R = new Red();
private static Color B = new Black();
private static Color BB = new DoubleBlack();
private static Color NB = new NegativeBlack();
protected abstract boolean isE();
protected abstract boolean isT();
protected abstract boolean isB();
protected abstract boolean isR();
protected abstract boolean isTB();
protected abstract boolean isTR();
public abstract boolean isEmpty();
protected abstract Tree<A> right();
protected abstract Tree<A> left();
protected abstract A value();
public abstract int size();
public abstract int height();
private static class E<A extends Comparable<A>> extends Tree<A> {
@Override
protected boolean isE() {
return true;
}
@Override
public int size() {
return 0;
}
@Override
public int height() {
return -1;
}
@Override
public Tree<A> right() {
return empty();
}
@Override
public Tree<A> left() {
return empty();
}
@Override
protected A value() {
throw new IllegalStateException("value called on Empty");
}
@Override
public boolean isR() {
return false;
}
@Override
protected boolean isT() {
return false;
}
@Override
protected boolean isB() {
return true;
}
@Override
protected boolean isTB() {
return false;
}
@Override
protected boolean isTR() {
return false;
}
@Override
public boolean isEmpty() {
return true;
}
@Override
public String toString() {
return "E";
}
}
private static class T<A extends Comparable<A>> extends Tree<A> {
private final Tree<A> left;
private final Tree<A> right;
private final A value;
private final Color color;
private final int length;
private final int height;
private T(Color color, Tree<A> left, A value, Tree<A> right) {
this.color = color;
this.left = left;
this.right = right;
this.value = value;
this.length = left.size() + 1 + right.size();
this.height = Math.max(left.height(), right.height()) + 1;
}
public boolean isR() {
return this.color.isR();
}
public boolean isB() {
return this.color.isB();
}
@Override
protected boolean isTB() {
return this.color.isB();
}
@Override
protected boolean isTR() {
return this.color.isR();
}
@Override
protected boolean isE() {
return false;
}
@Override
protected boolean isT() {
return true;
}
@Override
public int size() {
return length;
}
@Override
public int height() {
return height;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
protected Tree<A> right() {
return right;
}
@Override
protected Tree<A> left() {
return left;
}
@Override
protected A value() {
return value;
}
@Override
public String toString() {
return String.format("(T %s %s %s %s)", color, left, value, right);
}
}
private static abstract class Color {
abstract boolean isR();
abstract boolean isB();
abstract boolean isBB();
abstract boolean isNB();
abstract Color blacker();
abstract Color redder();
}
private static class Red extends Color {
@Override
boolean isR() {
return true;
}
@Override
boolean isB() {
return false;
}
@Override
boolean isBB() {
return false;
}
@Override
boolean isNB() {
return false;
}
@Override
Color blacker() {
return B;
}
@Override
Color redder() {
return NB;
}
@Override
public String toString() {
return "R";
}
}
private static class Black extends Color {
@Override
boolean isR() {
return false;
}
@Override
boolean isB() {
return true;
}
@Override
boolean isBB() {
return false;
}
@Override
boolean isNB() {
return false;
}
@Override
Color blacker() {
return BB;
}
@Override
Color redder() {
return R;
}
@Override
public String toString() {
return "B";
}
}
private static class DoubleBlack extends Color {
@Override
boolean isR() {
return false;
}
@Override
boolean isB() {
return false;
}
@Override
boolean isBB() {
return true;
}
@Override
boolean isNB() {
return false;
}
@Override
Color blacker() {
throw new IllegalStateException("Can't make DoubleBlack blacker");
}
@Override
Color redder() {
return B;
}
@Override
public String toString() {
return "BB";
}
}
private static class NegativeBlack extends Color {
@Override
boolean isR() {
return false;
}
@Override
boolean isB() {
return false;
}
@Override
boolean isBB() {
return false;
}
@Override
boolean isNB() {
return true;
}
@Override
Color blacker() {
return R;
}
@Override
Color redder() {
throw new IllegalStateException("Can't make NegativeBlack redder");
}
@Override
public String toString() {
return "NB";
}
}
@SuppressWarnings("unchecked")
public static <A extends Comparable<A>> Tree<A> empty() {
return E;
}
}
package com.fpinjava.advancedtrees.listing11_04;
package com.fpinjava.advancedtrees.listing11_03;
......@@ -16,7 +16,6 @@ public abstract class Heap<A extends Comparable<A>> {
protected abstract int rank();
public abstract Result<A> head();
public abstract Result<Heap<A>> tail();
public abstract int length();