summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-04-10 16:40:33 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-04-10 16:40:33 -0400
commit889fac2bdf993dc86f64a8893c0260fdcf848acb (patch)
tree99ed08552efa86fdc5fdf4ddb8720d10e599fafe /BJC-Utils2/src/main/java/bjc/utils/funcdata/bst
parent1656b02144446aeedebb3d1179e07ed99c01861c (diff)
Cleanup
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/bst')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java56
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java48
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java130
3 files changed, 100 insertions, 134 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java
index e85ae34..8acd477 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java
@@ -1,13 +1,13 @@
package bjc.utils.funcdata.bst;
-import bjc.utils.funcdata.FunctionalList;
-import bjc.utils.funcdata.IList;
-
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
+import bjc.utils.funcdata.FunctionalList;
+import bjc.utils.funcdata.IList;
+
/**
* A binary search tree, with some mild support for functional traversal.
*
@@ -20,7 +20,7 @@ public class BinarySearchTree<T> {
/*
* The comparator for use in ordering items
*/
- private Comparator<T> comparator;
+ private final Comparator<T> comparator;
/*
* The current count of elements in the tree
@@ -38,9 +38,8 @@ public class BinarySearchTree<T> {
* @param cmp
* The thing to use for comparing elements
*/
- public BinarySearchTree(Comparator<T> cmp) {
- if (cmp == null)
- throw new NullPointerException("Comparator must not be null");
+ public BinarySearchTree(final Comparator<T> cmp) {
+ if (cmp == null) throw new NullPointerException("Comparator must not be null");
elementCount = 0;
comparator = cmp;
@@ -52,7 +51,7 @@ public class BinarySearchTree<T> {
* @param element
* The data to add to the binary search tree.
*/
- public void addNode(T element) {
+ public void addNode(final T element) {
elementCount++;
if (root == null) {
@@ -73,7 +72,7 @@ public class BinarySearchTree<T> {
* The distance from the pivot
* @return Whether the adjusted pivot is with the list
*/
- private boolean adjustedPivotInBounds(IList<T> elements, int pivot, int pivotAdjustment) {
+ private boolean adjustedPivotInBounds(final IList<T> elements, final int pivot, final int pivotAdjustment) {
return pivot - pivotAdjustment >= 0 && pivot + pivotAdjustment < elements.getSize();
}
@@ -83,7 +82,7 @@ public class BinarySearchTree<T> {
* Takes O(N) time, but also O(N) space.
*/
public void balance() {
- IList<T> elements = new FunctionalList<>();
+ final IList<T> elements = new FunctionalList<>();
// Add each element to the list in sorted order
root.forEach(TreeLinearizationMethod.INORDER, element -> elements.add(element));
@@ -92,7 +91,7 @@ public class BinarySearchTree<T> {
root = null;
// Set up the pivot and adjustment for readding elements
- int pivot = elements.getSize() / 2;
+ final int pivot = elements.getSize() / 2;
int pivotAdjustment = 0;
// Add elements until there aren't any left
@@ -129,7 +128,7 @@ public class BinarySearchTree<T> {
* @param element
* The node to delete
*/
- public void deleteNode(T element) {
+ public void deleteNode(final T element) {
elementCount--;
root.delete(element, comparator);
@@ -151,7 +150,7 @@ public class BinarySearchTree<T> {
* The node to check the presence of for the tree.
* @return Whether or not the node is in the tree.
*/
- public boolean isInTree(T element) {
+ public boolean isInTree(final T element) {
return root.contains(element, comparator);
}
@@ -163,11 +162,10 @@ public class BinarySearchTree<T> {
* @param traversalPredicate
* The function to use until it fails
*/
- public void traverse(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) {
+ public void traverse(final TreeLinearizationMethod linearizationMethod, final Predicate<T> traversalPredicate) {
if (linearizationMethod == null)
throw new NullPointerException("Linearization method must not be null");
- else if (traversalPredicate == null)
- throw new NullPointerException("Predicate must not be nulls");
+ else if (traversalPredicate == null) throw new NullPointerException("Predicate must not be nulls");
root.forEach(linearizationMethod, traversalPredicate);
}
@@ -176,7 +174,7 @@ public class BinarySearchTree<T> {
* Remove all soft-deleted nodes from the tree.
*/
public void trim() {
- List<T> nodes = new ArrayList<>(elementCount);
+ final List<T> nodes = new ArrayList<>(elementCount);
// Add all non-soft deleted nodes to the tree in insertion order
traverse(TreeLinearizationMethod.PREORDER, node -> {
@@ -201,28 +199,22 @@ public class BinarySearchTree<T> {
final int prime = 31;
int result = 1;
result = prime * result + elementCount;
- result = prime * result + ((root == null) ? 0 : root.hashCode());
+ result = prime * result + (root == null ? 0 : root.hashCode());
return result;
}
@Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (!(obj instanceof BinarySearchTree<?>))
- return false;
+ public boolean equals(final Object obj) {
+ if (this == obj) return true;
+ if (obj == null) return false;
+ if (!(obj instanceof BinarySearchTree<?>)) return false;
- BinarySearchTree<?> other = (BinarySearchTree<?>) obj;
+ final BinarySearchTree<?> other = (BinarySearchTree<?>) obj;
- if (elementCount != other.elementCount)
- return false;
+ if (elementCount != other.elementCount) return false;
if (root == null) {
- if (other.root != null)
- return false;
- } else if (!root.equals(other.root))
- return false;
+ if (other.root != null) return false;
+ } else if (!root.equals(other.root)) return false;
return true;
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java
index fe30dad..8c4f3f0 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java
@@ -30,25 +30,24 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> {
* @param element
* The data for the leaf to hold.
*/
- public BinarySearchTreeLeaf(T element) {
+ public BinarySearchTreeLeaf(final T element) {
data = element;
}
@Override
- public void add(T element, Comparator<T> comparator) {
+ public void add(final T element, final Comparator<T> comparator) {
throw new IllegalArgumentException("Can't add to a leaf.");
}
@Override
- public <E> E collapse(Function<T, E> leafTransformer, BiFunction<E, E, E> branchCollapser) {
- if (leafTransformer == null)
- throw new NullPointerException("Transformer must not be null");
+ public <E> E collapse(final Function<T, E> leafTransformer, final BiFunction<E, E, E> branchCollapser) {
+ if (leafTransformer == null) throw new NullPointerException("Transformer must not be null");
return leafTransformer.apply(data);
}
@Override
- public boolean contains(T element, Comparator<T> comparator) {
+ public boolean contains(final T element, final Comparator<T> comparator) {
return this.data.equals(element);
}
@@ -58,16 +57,15 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> {
}
@Override
- public void delete(T element, Comparator<T> comparator) {
+ public void delete(final T element, final Comparator<T> comparator) {
if (data.equals(element)) {
isDeleted = true;
}
}
@Override
- public boolean directedWalk(DirectedWalkFunction<T> treeWalker) {
- if (treeWalker == null)
- throw new NullPointerException("Tree walker must not be null");
+ public boolean directedWalk(final DirectedWalkFunction<T> treeWalker) {
+ if (treeWalker == null) throw new NullPointerException("Tree walker must not be null");
switch (treeWalker.walk(data)) {
case SUCCESS:
@@ -82,9 +80,9 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> {
}
@Override
- public boolean forEach(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) {
- if (traversalPredicate == null)
- throw new NullPointerException("Predicate must not be null");
+ public boolean forEach(final TreeLinearizationMethod linearizationMethod,
+ final Predicate<T> traversalPredicate) {
+ if (traversalPredicate == null) throw new NullPointerException("Predicate must not be null");
return traversalPredicate.test(data);
}
@@ -98,29 +96,23 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> {
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result + ((data == null) ? 0 : data.hashCode());
+ result = prime * result + (data == null ? 0 : data.hashCode());
result = prime * result + (isDeleted ? 1231 : 1237);
return result;
}
@Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (!(obj instanceof BinarySearchTreeLeaf<?>))
- return false;
+ public boolean equals(final Object obj) {
+ if (this == obj) return true;
+ if (obj == null) return false;
+ if (!(obj instanceof BinarySearchTreeLeaf<?>)) return false;
- BinarySearchTreeLeaf<?> other = (BinarySearchTreeLeaf<?>) obj;
+ final BinarySearchTreeLeaf<?> other = (BinarySearchTreeLeaf<?>) obj;
if (data == null) {
- if (other.data != null)
- return false;
- } else if (!data.equals(other.data))
- return false;
- if (isDeleted != other.isDeleted)
- return false;
+ if (other.data != null) return false;
+ } else if (!data.equals(other.data)) return false;
+ if (isDeleted != other.isDeleted) return false;
return true;
}
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 527f221..9f45c17 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,15 +1,15 @@
package bjc.utils.funcdata.bst;
-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;
+import java.util.Comparator;
+import java.util.function.BiFunction;
+import java.util.function.Function;
+import java.util.function.Predicate;
+
/**
* A binary node in a tree.
*
@@ -39,16 +39,15 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
* @param rght
* The right child of this node.
*/
- public BinarySearchTreeNode(T element, ITreePart<T> lft, ITreePart<T> rght) {
+ public BinarySearchTreeNode(final T element, final ITreePart<T> lft, final ITreePart<T> rght) {
super(element);
this.left = lft;
this.right = rght;
}
@Override
- public void add(T element, Comparator<T> comparator) {
- if (comparator == null)
- throw new NullPointerException("Comparator must not be null");
+ public void add(final T element, final Comparator<T> comparator) {
+ if (comparator == null) throw new NullPointerException("Comparator must not be null");
switch (comparator.compare(data, element)) {
case -1:
@@ -61,8 +60,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
case 0:
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) {
@@ -77,19 +75,20 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
}
@Override
- public <E> E collapse(Function<T, E> nodeCollapser, BiFunction<E, E, E> branchCollapser) {
+ public <E> E collapse(final Function<T, E> nodeCollapser, final BiFunction<E, E, E> branchCollapser) {
if (nodeCollapser == null || branchCollapser == null)
throw new NullPointerException("Collapser must not be null");
- E collapsedNode = nodeCollapser.apply(data);
+ final E collapsedNode = nodeCollapser.apply(data);
if (left != null) {
- E collapsedLeftBranch = left.collapse(nodeCollapser, branchCollapser);
+ final E collapsedLeftBranch = left.collapse(nodeCollapser, branchCollapser);
if (right != null) {
- E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser);
+ final E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser);
- E collapsedBranches = branchCollapser.apply(collapsedLeftBranch, collapsedRightBranch);
+ final E collapsedBranches = branchCollapser.apply(collapsedLeftBranch,
+ collapsedRightBranch);
return branchCollapser.apply(collapsedNode, collapsedBranches);
}
@@ -98,7 +97,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
}
if (right != null) {
- E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser);
+ final E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser);
return branchCollapser.apply(collapsedNode, collapsedRightBranch);
}
@@ -107,9 +106,8 @@ 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");
+ public boolean contains(final T element, final Comparator<T> comparator) {
+ if (comparator == null) throw new NullPointerException("Comparator must not be null");
return directedWalk(currentElement -> {
switch (comparator.compare(element, currentElement)) {
@@ -126,9 +124,8 @@ 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");
+ public void delete(final T element, final Comparator<T> comparator) {
+ if (comparator == null) throw new NullPointerException("Comparator must not be null");
directedWalk(currentElement -> {
switch (comparator.compare(data, element)) {
@@ -146,9 +143,8 @@ 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");
+ public boolean directedWalk(final DirectedWalkFunction<T> treeWalker) {
+ if (treeWalker == null) throw new NullPointerException("Walker must not be null");
switch (treeWalker.walk(data)) {
case SUCCESS:
@@ -165,11 +161,11 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
}
@Override
- public boolean forEach(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) {
+ public boolean forEach(final TreeLinearizationMethod linearizationMethod,
+ final Predicate<T> traversalPredicate) {
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) {
case PREORDER:
@@ -184,48 +180,41 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
}
}
- private boolean inorderTraverse(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) {
- if (!traverseLeftBranch(linearizationMethod, traversalPredicate))
- return false;
+ private boolean inorderTraverse(final TreeLinearizationMethod linearizationMethod,
+ final Predicate<T> traversalPredicate) {
+ 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;
+ private boolean postorderTraverse(final TreeLinearizationMethod linearizationMethod,
+ final Predicate<T> traversalPredicate) {
+ 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;
+ private boolean preorderTraverse(final TreeLinearizationMethod linearizationMethod,
+ final Predicate<T> traversalPredicate) {
+ 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;
}
- private boolean traverseElement(Predicate<T> traversalPredicate) {
+ private boolean traverseElement(final Predicate<T> traversalPredicate) {
boolean nodeSuccesfullyTraversed;
if (isDeleted) {
@@ -237,8 +226,8 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
return nodeSuccesfullyTraversed;
}
- private boolean traverseLeftBranch(TreeLinearizationMethod linearizationMethod,
- Predicate<T> traversalPredicate) {
+ private boolean traverseLeftBranch(final TreeLinearizationMethod linearizationMethod,
+ final Predicate<T> traversalPredicate) {
boolean leftSuccesfullyTraversed;
if (left == null) {
@@ -250,8 +239,8 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
return leftSuccesfullyTraversed;
}
- private boolean traverseRightBranch(TreeLinearizationMethod linearizationMethod,
- Predicate<T> traversalPredicate) {
+ private boolean traverseRightBranch(final TreeLinearizationMethod linearizationMethod,
+ final Predicate<T> traversalPredicate) {
boolean rightSuccesfullyTraversed;
if (right == null) {
@@ -272,33 +261,26 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
- result = prime * result + ((left == null) ? 0 : left.hashCode());
- result = prime * result + ((right == null) ? 0 : right.hashCode());
+ result = prime * result + (left == null ? 0 : left.hashCode());
+ result = prime * result + (right == null ? 0 : right.hashCode());
return result;
}
@Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (!super.equals(obj))
- return false;
- if (!(obj instanceof BinarySearchTreeNode<?>))
- return false;
+ public boolean equals(final Object obj) {
+ if (this == obj) return true;
+ if (!super.equals(obj)) return false;
+ if (!(obj instanceof BinarySearchTreeNode<?>)) return false;
- BinarySearchTreeNode<?> other = (BinarySearchTreeNode<?>) obj;
+ final BinarySearchTreeNode<?> other = (BinarySearchTreeNode<?>) obj;
if (left == null) {
- if (other.left != null)
- return false;
- } else if (!left.equals(other.left))
- return false;
+ 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 (other.right != null) return false;
+ } else if (!right.equals(other.right)) return false;
return true;
}