public Object getId() {
return id;
}
public DistributedImmutableBST<E> remove(E value) {
DirectoryManager locator = HyFlow.getLocator();
int comparison = this.data.compareTo(value);
if (comparison < 0) {
if (this.rightID == null) {
return this;
}
final DistributedImmutableBST<E> right = (DistributedImmutableBST<E>) locator.open(this.rightID, "r");
final DistributedImmutableBST<E> newRight = right.remove(value);
if (right.equals(newRight)) {
return this;
}
return new DistributedImmutableBST<E>(this.data, this.leftID, (newRight == null) ? null : (String) newRight.getId());
}
else if (comparison > 0) {
if (this.leftID == null) {
return this;
}
final DistributedImmutableBST<E> left = (DistributedImmutableBST<E>) locator.open(this.leftID, "r");
final DistributedImmutableBST<E> newLeft = left.remove(value);
if (left.equals(newLeft)) {
return this;
}
return new DistributedImmutableBST<E>(this.data, (newLeft == null) ? null : (String) newLeft.getId(), this.rightID);
}
else {
locator.delete(this);
if (this.leftID == null && this.rightID == null) { //0 Children
return null;
}
if (this.leftID == null || this.rightID == null) { //1 Child
return (DistributedImmutableBST<E>)((this.leftID != null) ? locator.open(this.leftID, "r") : locator.open(this.rightID, "r"));
}
else { //2 Children
//Find Right Child's left most node
Stack<DistributedImmutableBST<E>> stack = new Stack<DistributedImmutableBST<E>>();
DistributedImmutableBST<E> iter = (DistributedImmutableBST<E>) locator.open(this.rightID, "r");
while (iter.leftID != null) {
stack.push(iter);
iter = (DistributedImmutableBST<E>) locator.open(iter.leftID, "r");
}
E newValue = iter.data;
iter = (DistributedImmutableBST<E>) locator.open(iter.rightID, "r"); //Don't lose left most child's right nodes
while (!stack.empty()) {
DistributedImmutableBST<E> node = stack.pop();
iter = new DistributedImmutableBST<E>(node.data, (String) iter.getId(), node.rightID);
locator.delete(node);
}
//Remove node by replacing it's value
return new DistributedImmutableBST<E>(newValue, this.leftID, (String) iter.getId());
}
}