summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java
diff options
context:
space:
mode:
authorEVE <EVE@EVE-PC>2017-03-14 12:07:14 -0400
committerEVE <EVE@EVE-PC>2017-03-14 12:07:14 -0400
commit504ca816530efdff06bc202e0432ebd354aec304 (patch)
tree4836932fb81d1d625470502c78c94d202c9a7420 /BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java
parent5c1163df17c46f7d3e15b6c7949c38843ec56146 (diff)
Cleanup
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java106
1 files changed, 38 insertions, 68 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java
index 46a89f2..4fe9de3 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java
@@ -1,18 +1,18 @@
package bjc.utils.funcdata.bst;
-import static bjc.utils.funcdata.bst.DirectedWalkFunction.DirectedWalkResult.FAILURE;
-import static bjc.utils.funcdata.bst.DirectedWalkFunction.DirectedWalkResult.LEFT;
-import static bjc.utils.funcdata.bst.DirectedWalkFunction.DirectedWalkResult.RIGHT;
-import static bjc.utils.funcdata.bst.DirectedWalkFunction.DirectedWalkResult.SUCCESS;
-
import java.util.Comparator;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
+import static bjc.utils.funcdata.bst.DirectedWalkFunction.DirectedWalkResult.FAILURE;
+import static bjc.utils.funcdata.bst.DirectedWalkFunction.DirectedWalkResult.LEFT;
+import static bjc.utils.funcdata.bst.DirectedWalkFunction.DirectedWalkResult.RIGHT;
+import static bjc.utils.funcdata.bst.DirectedWalkFunction.DirectedWalkResult.SUCCESS;
+
/**
* A binary node in a tree.
- *
+ *
* @author ben
*
* @param <T>
@@ -31,7 +31,7 @@ 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.
* @param lft
@@ -47,27 +47,24 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
@Override
public void add(T element, 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 {
+ } 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);
@@ -80,16 +77,15 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
@Override
public <E> E collapse(Function<T, E> nodeCollapser, BiFunction<E, E, E> branchCollapser) {
- if (nodeCollapser == null || branchCollapser == null) {
+ if(nodeCollapser == null || branchCollapser == null)
throw new NullPointerException("Collapser must not be null");
- }
E collapsedNode = nodeCollapser.apply(data);
- if (left != null) {
+ if(left != null) {
E collapsedLeftBranch = left.collapse(nodeCollapser, branchCollapser);
- if (right != null) {
+ if(right != null) {
E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser);
E collapsedBranches = branchCollapser.apply(collapsedLeftBranch, collapsedRightBranch);
@@ -100,7 +96,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
return branchCollapser.apply(collapsedNode, collapsedLeftBranch);
}
- if (right != null) {
+ if(right != null) {
E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser);
return branchCollapser.apply(collapsedNode, collapsedRightBranch);
@@ -111,12 +107,10 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
@Override
public boolean contains(T element, 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:
@@ -131,12 +125,10 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
@Override
public void delete(T element, 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:
@@ -152,11 +144,9 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
@Override
public boolean directedWalk(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:
@@ -172,13 +162,11 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
@Override
public boolean forEach(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) {
- if (linearizationMethod == null) {
+ if(linearizationMethod == null)
throw new NullPointerException("Linearization method must not be null");
- } else if (traversalPredicate == null) {
- throw new NullPointerException("Predicate must not be 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:
@@ -192,51 +180,33 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
}
private boolean inorderTraverse(TreeLinearizationMethod linearizationMethod, 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;
}
private boolean postorderTraverse(TreeLinearizationMethod linearizationMethod,
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;
}
private boolean preorderTraverse(TreeLinearizationMethod linearizationMethod, 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;
}
@@ -244,7 +214,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
private boolean traverseElement(Predicate<T> traversalPredicate) {
boolean nodeSuccesfullyTraversed;
- if (isDeleted) {
+ if(isDeleted) {
nodeSuccesfullyTraversed = true;
} else {
nodeSuccesfullyTraversed = traversalPredicate.test(data);
@@ -257,7 +227,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
Predicate<T> traversalPredicate) {
boolean leftSuccesfullyTraversed;
- if (left == null) {
+ if(left == null) {
leftSuccesfullyTraversed = true;
} else {
leftSuccesfullyTraversed = left.forEach(linearizationMethod, traversalPredicate);
@@ -270,7 +240,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
Predicate<T> traversalPredicate) {
boolean rightSuccesfullyTraversed;
- if (right == null) {
+ if(right == null) {
rightSuccesfullyTraversed = true;
} else {
rightSuccesfullyTraversed = right.forEach(linearizationMethod, traversalPredicate);