summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java
diff options
context:
space:
mode:
Diffstat (limited to 'base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java')
-rw-r--r--base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java92
1 files changed, 47 insertions, 45 deletions
diff --git a/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java b/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java
index 3124474..eb7b6b5 100644
--- a/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java
+++ b/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java
@@ -16,7 +16,7 @@ import java.util.function.Predicate;
* @author ben
*
* @param <T>
- * The data type stored in the tree.
+ * The data type stored in the tree.
*/
public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
/* The left child of this node */
@@ -29,13 +29,13 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
* Create a new node with the specified data and children.
*
* @param element
- * The data to store in this node.
+ * The data to store in this node.
*
* @param lft
- * The left child of this node.
+ * The left child of this node.
*
* @param rght
- * The right child of this node.
+ * The right child of this node.
*/
public BinarySearchTreeNode(final T element, final ITreePart<T> lft, final ITreePart<T> rght) {
super(element);
@@ -45,23 +45,24 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
@Override
public void add(final T element, final Comparator<T> comparator) {
- if (comparator == null) throw new NullPointerException("Comparator must not be null");
+ if(comparator == null) throw new NullPointerException("Comparator must not be null");
- switch (comparator.compare(data, element)) {
+ switch(comparator.compare(data, element)) {
case -1:
- if (left == null) {
+ if(left == null) {
left = new BinarySearchTreeNode<>(element, null, null);
} else {
left.add(element, comparator);
}
break;
case 0:
- if (isDeleted) {
+ if(isDeleted) {
isDeleted = false;
- } else throw new IllegalArgumentException("Can't add duplicate values");
+ } else
+ throw new IllegalArgumentException("Can't add duplicate values");
break;
case 1:
- if (right == null) {
+ if(right == null) {
right = new BinarySearchTreeNode<>(element, null, null);
} else {
right.add(element, comparator);
@@ -74,15 +75,15 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
@Override
public <E> E collapse(final Function<T, E> nodeCollapser, final BiFunction<E, E, E> branchCollapser) {
- if (nodeCollapser == null || branchCollapser == null)
+ if(nodeCollapser == null || branchCollapser == null)
throw new NullPointerException("Collapser must not be null");
final E collapsedNode = nodeCollapser.apply(data);
- if (left != null) {
+ if(left != null) {
final E collapsedLeftBranch = left.collapse(nodeCollapser, branchCollapser);
- if (right != null) {
+ if(right != null) {
final E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser);
final E collapsedBranches = branchCollapser.apply(collapsedLeftBranch,
@@ -94,7 +95,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
return branchCollapser.apply(collapsedNode, collapsedLeftBranch);
}
- if (right != null) {
+ if(right != null) {
final E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser);
return branchCollapser.apply(collapsedNode, collapsedRightBranch);
@@ -105,10 +106,10 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
@Override
public boolean contains(final T element, final Comparator<T> comparator) {
- if (comparator == null) throw new NullPointerException("Comparator must not be null");
+ if(comparator == null) throw new NullPointerException("Comparator must not be null");
return directedWalk(currentElement -> {
- switch (comparator.compare(element, currentElement)) {
+ switch(comparator.compare(element, currentElement)) {
case -1:
return LEFT;
case 0:
@@ -123,10 +124,10 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
@Override
public void delete(final T element, final Comparator<T> comparator) {
- if (comparator == null) throw new NullPointerException("Comparator must not be null");
+ if(comparator == null) throw new NullPointerException("Comparator must not be null");
directedWalk(currentElement -> {
- switch (comparator.compare(data, element)) {
+ switch(comparator.compare(data, element)) {
case -1:
return left == null ? FAILURE : LEFT;
case 0:
@@ -142,9 +143,9 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
@Override
public boolean directedWalk(final DirectedWalkFunction<T> treeWalker) {
- if (treeWalker == null) throw new NullPointerException("Walker must not be null");
+ if(treeWalker == null) throw new NullPointerException("Walker must not be null");
- switch (treeWalker.walk(data)) {
+ switch(treeWalker.walk(data)) {
case SUCCESS:
return true;
case LEFT:
@@ -160,13 +161,13 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
@Override
public boolean forEach(final TreeLinearizationMethod linearizationMethod,
final Predicate<T> traversalPredicate) {
- if (linearizationMethod == null) {
+ if(linearizationMethod == null) {
throw new NullPointerException("Linearization method must not be null");
- } else if (traversalPredicate == null) {
+ } else if(traversalPredicate == null) {
throw new NullPointerException("Predicate must not be null");
}
- switch (linearizationMethod) {
+ switch(linearizationMethod) {
case PREORDER:
return preorderTraverse(linearizationMethod, traversalPredicate);
case INORDER:
@@ -174,7 +175,8 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
case POSTORDER:
return postorderTraverse(linearizationMethod, traversalPredicate);
default:
- String msg = String.format("Passed an incorrect TreeLinearizationMethod %s. WAT", linearizationMethod);
+ String msg = String.format("Passed an incorrect TreeLinearizationMethod %s. WAT",
+ linearizationMethod);
throw new IllegalArgumentException(msg);
}
@@ -183,11 +185,11 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
/* Do an in-order traversal. */
private boolean inorderTraverse(final TreeLinearizationMethod linearizationMethod,
final Predicate<T> traversalPredicate) {
- if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false;
+ if(!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false;
- if (!traverseElement(traversalPredicate)) return false;
+ if(!traverseElement(traversalPredicate)) return false;
- if (!traverseRightBranch(linearizationMethod, traversalPredicate)) return false;
+ if(!traverseRightBranch(linearizationMethod, traversalPredicate)) return false;
return true;
}
@@ -195,11 +197,11 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
/* Do a post-order traversal. */
private boolean postorderTraverse(final TreeLinearizationMethod linearizationMethod,
final Predicate<T> traversalPredicate) {
- if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false;
+ if(!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false;
- if (!traverseRightBranch(linearizationMethod, traversalPredicate)) return false;
+ if(!traverseRightBranch(linearizationMethod, traversalPredicate)) return false;
- if (!traverseElement(traversalPredicate)) return false;
+ if(!traverseElement(traversalPredicate)) return false;
return true;
@@ -208,11 +210,11 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
/* Do a pre-order traversal. */
private boolean preorderTraverse(final TreeLinearizationMethod linearizationMethod,
final Predicate<T> traversalPredicate) {
- if (!traverseElement(traversalPredicate)) return false;
+ if(!traverseElement(traversalPredicate)) return false;
- if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false;
+ if(!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false;
- if (!traverseRightBranch(linearizationMethod, traversalPredicate)) return false;
+ if(!traverseRightBranch(linearizationMethod, traversalPredicate)) return false;
return true;
}
@@ -221,7 +223,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
private boolean traverseElement(final Predicate<T> traversalPredicate) {
boolean nodeSuccesfullyTraversed;
- if (isDeleted) {
+ if(isDeleted) {
nodeSuccesfullyTraversed = true;
} else {
nodeSuccesfullyTraversed = traversalPredicate.test(data);
@@ -235,7 +237,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
final Predicate<T> traversalPredicate) {
boolean leftSuccesfullyTraversed;
- if (left == null) {
+ if(left == null) {
leftSuccesfullyTraversed = true;
} else {
leftSuccesfullyTraversed = left.forEach(linearizationMethod, traversalPredicate);
@@ -249,7 +251,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
final Predicate<T> traversalPredicate) {
boolean rightSuccesfullyTraversed;
- if (right == null) {
+ if(right == null) {
rightSuccesfullyTraversed = true;
} else {
rightSuccesfullyTraversed = right.forEach(linearizationMethod, traversalPredicate);
@@ -274,19 +276,19 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
@Override
public boolean equals(final Object obj) {
- if (this == obj) return true;
- if (!super.equals(obj)) return false;
- if (!(obj instanceof BinarySearchTreeNode<?>)) return false;
+ if(this == obj) return true;
+ if(!super.equals(obj)) return false;
+ if(!(obj instanceof BinarySearchTreeNode<?>)) return false;
final BinarySearchTreeNode<?> other = (BinarySearchTreeNode<?>) obj;
- if (left == null) {
- if (other.left != null) return false;
- } else if (!left.equals(other.left)) return false;
+ if(left == null) {
+ if(other.left != null) return false;
+ } else if(!left.equals(other.left)) return false;
- if (right == null) {
- if (other.right != null) return false;
- } else if (!right.equals(other.right)) return false;
+ if(right == null) {
+ if(other.right != null) return false;
+ } else if(!right.equals(other.right)) return false;
return true;
}