From df94066e3af02ff02d5ab4d033a3d603f743234c Mon Sep 17 00:00:00 2001 From: bjculkin Date: Mon, 12 Feb 2018 22:45:04 -0500 Subject: Formatting pass --- .../bjc/utils/funcdata/bst/BinarySearchTree.java | 56 +++++++------ .../utils/funcdata/bst/BinarySearchTreeLeaf.java | 28 +++---- .../utils/funcdata/bst/BinarySearchTreeNode.java | 92 +++++++++++----------- .../utils/funcdata/bst/DirectedWalkFunction.java | 7 +- .../java/bjc/utils/funcdata/bst/ITreePart.java | 50 ++++++------ 5 files changed, 113 insertions(+), 120 deletions(-) (limited to 'base/src/main/java/bjc/utils/funcdata/bst') diff --git a/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java b/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java index f9dc4a2..6631834 100644 --- a/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java +++ b/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java @@ -14,7 +14,7 @@ import bjc.utils.funcdata.IList; * @author ben * * @param - * The data type stored in the node. + * The data type stored in the node. */ public class BinarySearchTree { /* The comparator for use in ordering items */ @@ -30,10 +30,10 @@ public class BinarySearchTree { * Create a new tree using the specified way to compare elements. * * @param cmp - * The thing to use for comparing elements + * The thing to use for comparing elements */ public BinarySearchTree(final Comparator cmp) { - if (cmp == null) throw new NullPointerException("Comparator must not be null"); + if(cmp == null) throw new NullPointerException("Comparator must not be null"); elementCount = 0; comparator = cmp; @@ -43,12 +43,12 @@ public class BinarySearchTree { * Add a node to the binary search tree. * * @param element - * The data to add to the binary search tree. + * The data to add to the binary search tree. */ public void addNode(final T element) { elementCount++; - if (root == null) { + if(root == null) { root = new BinarySearchTreeNode<>(element, null, null); } else { root.add(element, comparator); @@ -59,16 +59,15 @@ public class BinarySearchTree { * Check if an adjusted pivot falls with the bounds of a list. * * @param elements - * The list to get bounds from. + * The list to get bounds from. * * @param pivot - * The pivot. + * The pivot. * * @param pivotAdjustment - * The distance from the pivot. + * The distance from the pivot. * - * @return - * Whether the adjusted pivot is with the list. + * @return Whether the adjusted pivot is with the list. */ private boolean adjustedPivotInBounds(final IList elements, final int pivot, final int pivotAdjustment) { return ((pivot - pivotAdjustment) >= 0) && ((pivot + pivotAdjustment) < elements.getSize()); @@ -93,8 +92,8 @@ public class BinarySearchTree { int pivotAdjustment = 0; /* Add elements until there aren't any left. */ - while (adjustedPivotInBounds(elements, pivot, pivotAdjustment)) { - if (root == null) { + while(adjustedPivotInBounds(elements, pivot, pivotAdjustment)) { + if(root == null) { /* Create a new root element. */ root = new BinarySearchTreeNode<>(elements.getByIndex(pivot), null, null); } else { @@ -111,9 +110,9 @@ public class BinarySearchTree { } /* Add any trailing unbalanced elements. */ - if (pivot - pivotAdjustment >= 0) { + if(pivot - pivotAdjustment >= 0) { root.add(elements.getByIndex(pivot - pivotAdjustment), comparator); - } else if (pivot + pivotAdjustment < elements.getSize()) { + } else if(pivot + pivotAdjustment < elements.getSize()) { root.add(elements.getByIndex(pivot + pivotAdjustment), comparator); } } @@ -125,7 +124,7 @@ public class BinarySearchTree { * invoked, and are not included in traversals/finds. * * @param element - * The node to delete + * The node to delete */ public void deleteNode(final T element) { elementCount--; @@ -136,8 +135,7 @@ public class BinarySearchTree { /** * Get the root of the tree. * - * @return - * The root of the tree. + * @return The root of the tree. */ public ITreePart getRoot() { return root; @@ -147,7 +145,7 @@ public class BinarySearchTree { * Check if a node is in the tree. * * @param element - * The node to check the presence of for the tree.. + * The node to check the presence of for the tree.. * * @return Whether or not the node is in the tree. */ @@ -159,15 +157,15 @@ public class BinarySearchTree { * Traverse the tree in a specified way until the function fails. * * @param linearizationMethod - * The way to linearize the tree for traversal. + * The way to linearize the tree for traversal. * * @param traversalPredicate - * The function to use until it fails. + * The function to use until it fails. */ public void traverse(final TreeLinearizationMethod linearizationMethod, final Predicate 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 nulls"); } @@ -210,16 +208,16 @@ public class BinarySearchTree { @Override public boolean equals(final Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (!(obj instanceof BinarySearchTree)) return false; + if(this == obj) return true; + if(obj == null) return false; + if(!(obj instanceof BinarySearchTree)) return false; final BinarySearchTree other = (BinarySearchTree) obj; - if (elementCount != other.elementCount) return false; - if (root == null) { - if (other.root != null) return false; - } else if (!root.equals(other.root)) 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; return true; } diff --git a/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java b/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java index 46f597e..762288f 100644 --- a/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java +++ b/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java @@ -11,7 +11,7 @@ import java.util.function.Predicate; * @author ben * * @param - * The data stored in the tree. + * The data stored in the tree. */ public class BinarySearchTreeLeaf implements ITreePart { /** The data held in this tree leaf */ @@ -24,7 +24,7 @@ public class BinarySearchTreeLeaf implements ITreePart { * Create a new leaf holding the specified data. * * @param element - * The data for the leaf to hold. + * The data for the leaf to hold. */ public BinarySearchTreeLeaf(final T element) { data = element; @@ -37,7 +37,7 @@ public class BinarySearchTreeLeaf implements ITreePart { @Override public E collapse(final Function leafTransformer, final BiFunction branchCollapser) { - if (leafTransformer == null) throw new NullPointerException("Transformer must not be null"); + if(leafTransformer == null) throw new NullPointerException("Transformer must not be null"); return leafTransformer.apply(data); } @@ -54,16 +54,16 @@ public class BinarySearchTreeLeaf implements ITreePart { @Override public void delete(final T element, final Comparator comparator) { - if (data.equals(element)) { + if(data.equals(element)) { isDeleted = true; } } @Override public boolean directedWalk(final DirectedWalkFunction treeWalker) { - if (treeWalker == null) throw new NullPointerException("Tree walker must not be null"); + if(treeWalker == null) throw new NullPointerException("Tree walker must not be null"); - switch (treeWalker.walk(data)) { + switch(treeWalker.walk(data)) { case SUCCESS: return true; /* We don't have any children to care about. */ @@ -78,7 +78,7 @@ public class BinarySearchTreeLeaf implements ITreePart { @Override public boolean forEach(final TreeLinearizationMethod linearizationMethod, final Predicate traversalPredicate) { - if (traversalPredicate == null) throw new NullPointerException("Predicate must not be null"); + if(traversalPredicate == null) throw new NullPointerException("Predicate must not be null"); return traversalPredicate.test(data); } @@ -99,16 +99,16 @@ public class BinarySearchTreeLeaf implements ITreePart { @Override public boolean equals(final Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (!(obj instanceof BinarySearchTreeLeaf)) return false; + if(this == obj) return true; + if(obj == null) return false; + if(!(obj instanceof BinarySearchTreeLeaf)) return false; 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(data == null) { + 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/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 - * The data type stored in the tree. + * The data type stored in the tree. */ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { /* The left child of this node */ @@ -29,13 +29,13 @@ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { * 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 lft, final ITreePart rght) { super(element); @@ -45,23 +45,24 @@ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { @Override public void add(final T element, final Comparator 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 extends BinarySearchTreeLeaf { @Override public E collapse(final Function nodeCollapser, final BiFunction 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 extends BinarySearchTreeLeaf { 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 extends BinarySearchTreeLeaf { @Override public boolean contains(final T element, final Comparator 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 extends BinarySearchTreeLeaf { @Override public void delete(final T element, final Comparator 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 extends BinarySearchTreeLeaf { @Override public boolean directedWalk(final DirectedWalkFunction 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 extends BinarySearchTreeLeaf { @Override public boolean forEach(final TreeLinearizationMethod linearizationMethod, final Predicate 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 extends BinarySearchTreeLeaf { 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 extends BinarySearchTreeLeaf { /* Do an in-order traversal. */ private boolean inorderTraverse(final TreeLinearizationMethod linearizationMethod, final Predicate 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 extends BinarySearchTreeLeaf { /* Do a post-order traversal. */ private boolean postorderTraverse(final TreeLinearizationMethod linearizationMethod, final Predicate 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 extends BinarySearchTreeLeaf { /* Do a pre-order traversal. */ private boolean preorderTraverse(final TreeLinearizationMethod linearizationMethod, final Predicate 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 extends BinarySearchTreeLeaf { private boolean traverseElement(final Predicate traversalPredicate) { boolean nodeSuccesfullyTraversed; - if (isDeleted) { + if(isDeleted) { nodeSuccesfullyTraversed = true; } else { nodeSuccesfullyTraversed = traversalPredicate.test(data); @@ -235,7 +237,7 @@ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { final Predicate traversalPredicate) { boolean leftSuccesfullyTraversed; - if (left == null) { + if(left == null) { leftSuccesfullyTraversed = true; } else { leftSuccesfullyTraversed = left.forEach(linearizationMethod, traversalPredicate); @@ -249,7 +251,7 @@ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { final Predicate traversalPredicate) { boolean rightSuccesfullyTraversed; - if (right == null) { + if(right == null) { rightSuccesfullyTraversed = true; } else { rightSuccesfullyTraversed = right.forEach(linearizationMethod, traversalPredicate); @@ -274,19 +276,19 @@ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { @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; } diff --git a/base/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java b/base/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java index fdf86d7..e341320 100644 --- a/base/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java +++ b/base/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java @@ -6,7 +6,7 @@ package bjc.utils.funcdata.bst; * @author ben * * @param - * The type of element stored in the walked tree + * The type of element stored in the walked tree */ @FunctionalInterface public interface DirectedWalkFunction { @@ -36,10 +36,9 @@ public interface DirectedWalkFunction { * Perform a directed walk on a node of a tree. * * @param element - * The data stored in the node currently being visited. + * The data stored in the node currently being visited. * - * @return - * The way the function wants the walk to go next. + * @return The way the function wants the walk to go next. */ public DirectedWalkResult walk(T element); } diff --git a/base/src/main/java/bjc/utils/funcdata/bst/ITreePart.java b/base/src/main/java/bjc/utils/funcdata/bst/ITreePart.java index a2ce71f..f9b3d4a 100644 --- a/base/src/main/java/bjc/utils/funcdata/bst/ITreePart.java +++ b/base/src/main/java/bjc/utils/funcdata/bst/ITreePart.java @@ -11,18 +11,18 @@ import java.util.function.Predicate; * @author ben * * @param - * The data contained in this part of the tree. + * The data contained in this part of the tree. */ public interface ITreePart { /** * Add a element below this tree part somewhere. * * @param element - * The element to add below this tree part + * The element to add below this tree part * * @param comparator - * The thing to use for comparing values to find where to - * insert the tree part. + * The thing to use for comparing values to find where to insert + * the tree part. */ public void add(T element, Comparator comparator); @@ -32,17 +32,16 @@ public interface ITreePart { * Does not change the underlying tree. * * @param - * The type of the final collapsed value + * The type of the final collapsed value * * @param nodeCollapser - * The function to use to transform data into mapped form. + * The function to use to transform data into mapped form. * * @param branchCollapser - * The function to use to collapse data in mapped form into a - * single value. + * The function to use to collapse data in mapped form into a + * single value. * - * @return - * A single value from collapsing the tree. + * @return A single value from collapsing the tree. */ public E collapse(Function nodeCollapser, BiFunction branchCollapser); @@ -50,22 +49,20 @@ public interface ITreePart { * Check if this tre part or below it contains the specified data item. * * @param element - * The data item to look for. + * The data item to look for. * * @param comparator - * The comparator to use to search for the data item. + * The comparator to use to search for the data item. * - * @return - * Whether or not the given item is contained in this tree part or - * its children. + * @return Whether or not the given item is contained in this tree part + * or its children. */ public boolean contains(T element, Comparator comparator); /** * Get the data associated with this tree part. * - * @return - * The data associated with this tree part. + * @return The data associated with this tree part. */ public T data(); @@ -73,10 +70,10 @@ public interface ITreePart { * Remove the given node from this tree part and any of its children. * * @param element - * The data item to remove. + * The data item to remove. * * @param comparator - * The comparator to use to search for the data item. + * The comparator to use to search for the data item. */ public void delete(T element, Comparator comparator); @@ -84,11 +81,9 @@ public interface ITreePart { * Execute a directed walk through the tree. * * @param walker - * The function to use to direct the walk through the - * tree. + * The function to use to direct the walk through the tree. * - * @return - * Whether the directed walk finished successfully. + * @return Whether the directed walk finished successfully. */ public boolean directedWalk(DirectedWalkFunction walker); @@ -97,14 +92,13 @@ public interface ITreePart { * completes for. * * @param linearizationMethod - * The way to linearize the tree for executing. + * The way to linearize the tree for executing. * * @param predicate - * The predicate to apply to each element, where it returning false - * terminates traversal early. + * The predicate to apply to each element, where it returning + * false terminates traversal early. * - * @return - * Whether the traversal finished succesfully. + * @return Whether the traversal finished succesfully. */ public boolean forEach(TreeLinearizationMethod linearizationMethod, Predicate predicate); } -- cgit v1.2.3