diff options
Diffstat (limited to 'base/src/main/java/bjc/utils/funcdata/bst')
6 files changed, 124 insertions, 108 deletions
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 8acd477..f9dc4a2 100644 --- a/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java +++ b/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java @@ -14,29 +14,23 @@ import bjc.utils.funcdata.IList; * @author ben * * @param <T> - * The data type stored in the node. + * The data type stored in the node. */ public class BinarySearchTree<T> { - /* - * The comparator for use in ordering items - */ + /* The comparator for use in ordering items */ private final Comparator<T> comparator; - /* - * The current count of elements in the tree - */ + /* The current count of elements in the tree */ private int elementCount; - /* - * The root element of the tree - */ + /* The root element of the tree */ private ITreePart<T> root; /** * 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<T> cmp) { if (cmp == null) throw new NullPointerException("Comparator must not be null"); @@ -49,7 +43,7 @@ public class BinarySearchTree<T> { * 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++; @@ -62,18 +56,22 @@ public class BinarySearchTree<T> { } /** - * Check if an adjusted pivot falls with the bounds of a list + * 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 - * @return Whether the adjusted pivot is with the list + * The distance from the pivot. + * + * @return + * Whether the adjusted pivot is with the list. */ private boolean adjustedPivotInBounds(final IList<T> elements, final int pivot, final int pivotAdjustment) { - return pivot - pivotAdjustment >= 0 && pivot + pivotAdjustment < elements.getSize(); + return ((pivot - pivotAdjustment) >= 0) && ((pivot + pivotAdjustment) < elements.getSize()); } /** @@ -84,34 +82,35 @@ public class BinarySearchTree<T> { public void balance() { final IList<T> elements = new FunctionalList<>(); - // Add each element to the list in sorted order + /* Add each element to the list in sorted order. */ root.forEach(TreeLinearizationMethod.INORDER, element -> elements.add(element)); - // Clear the tree + /* Clear the tree. */ root = null; - // Set up the pivot and adjustment for readding elements + /* Set up the pivot and adjustment for readding elements. */ final int pivot = elements.getSize() / 2; int pivotAdjustment = 0; - // Add elements until there aren't any left + /* Add elements until there aren't any left. */ while (adjustedPivotInBounds(elements, pivot, pivotAdjustment)) { if (root == null) { - // Create a new root element + /* Create a new root element. */ root = new BinarySearchTreeNode<>(elements.getByIndex(pivot), null, null); } else { - // Add the left and right elements in a balanced - // manner + /* + * Add the left and right elements in a balanced + * manner. + */ root.add(elements.getByIndex(pivot + pivotAdjustment), comparator); - root.add(elements.getByIndex(pivot - pivotAdjustment), comparator); } - // Increase the distance from the pivot + /* Increase the distance from the pivot. */ pivotAdjustment++; } - // Add any trailing unbalanced elements + /* Add any trailing unbalanced elements. */ if (pivot - pivotAdjustment >= 0) { root.add(elements.getByIndex(pivot - pivotAdjustment), comparator); } else if (pivot + pivotAdjustment < elements.getSize()) { @@ -126,7 +125,7 @@ public class BinarySearchTree<T> { * 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--; @@ -137,17 +136,19 @@ public class BinarySearchTree<T> { /** * Get the root of the tree. * - * @return The root of the tree. + * @return + * The root of the tree. */ public ITreePart<T> getRoot() { return root; } /** - * Check if a node is in the tree + * 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. */ public boolean isInTree(final T element) { @@ -155,37 +156,41 @@ public class BinarySearchTree<T> { } /** - * Traverse the tree in a specified way until the function fails + * 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<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 nulls"); + } else if (traversalPredicate == null) { + throw new NullPointerException("Predicate must not be nulls"); + } root.forEach(linearizationMethod, traversalPredicate); } - /** - * Remove all soft-deleted nodes from the tree. - */ + /** Remove all soft-deleted nodes from the tree. */ public void trim() { final List<T> nodes = new ArrayList<>(elementCount); - // Add all non-soft deleted nodes to the tree in insertion order + /* + * Add all non-soft deleted nodes to the tree in insertion + * order. + */ traverse(TreeLinearizationMethod.PREORDER, node -> { nodes.add(node); return true; }); - // Clear the tree + /* Clear the tree. */ root = null; - // Add the nodes to the tree in the order they were inserted + /* Add the nodes to the tree in the order they were inserted. */ nodes.forEach(node -> addNode(node)); } 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 8c4f3f0..46f597e 100644 --- a/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java +++ b/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java @@ -11,24 +11,20 @@ import java.util.function.Predicate; * @author ben * * @param <T> - * The data stored in the tree. + * The data stored in the tree. */ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { - /** - * The data held in this tree leaf - */ + /** The data held in this tree leaf */ protected T data; - /** - * Whether this node is soft-deleted or not - */ + /** Whether this node is soft-deleted or not */ protected boolean isDeleted; /** * 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; @@ -70,7 +66,7 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { switch (treeWalker.walk(data)) { case SUCCESS: return true; - // We don't have any children to care about + /* We don't have any children to care about. */ case FAILURE: case LEFT: case RIGHT: 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 9f45c17..3124474 100644 --- a/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java +++ b/base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java @@ -16,28 +16,26 @@ 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 - */ + /* The left child of this node */ private ITreePart<T> left; - /* - * The right child of this node - */ + /* The right child of this node */ private ITreePart<T> right; /** * 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); @@ -154,7 +152,6 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { case RIGHT: return right.directedWalk(treeWalker); case FAILURE: - return false; default: return false; } @@ -163,9 +160,11 @@ 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) throw new NullPointerException("Predicate must not be null"); + } else if (traversalPredicate == null) { + throw new NullPointerException("Predicate must not be null"); + } switch (linearizationMethod) { case PREORDER: @@ -175,11 +174,13 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { case POSTORDER: return postorderTraverse(linearizationMethod, traversalPredicate); default: - throw new IllegalArgumentException( - "Passed an incorrect TreeLinearizationMethod " + linearizationMethod + ". WAT"); + String msg = String.format("Passed an incorrect TreeLinearizationMethod %s. WAT", linearizationMethod); + + throw new IllegalArgumentException(msg); } } + /* Do an in-order traversal. */ private boolean inorderTraverse(final TreeLinearizationMethod linearizationMethod, final Predicate<T> traversalPredicate) { if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false; @@ -191,6 +192,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { return true; } + /* Do a post-order traversal. */ private boolean postorderTraverse(final TreeLinearizationMethod linearizationMethod, final Predicate<T> traversalPredicate) { if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false; @@ -203,6 +205,7 @@ 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; @@ -214,6 +217,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { return true; } + /* Traverse an element. */ private boolean traverseElement(final Predicate<T> traversalPredicate) { boolean nodeSuccesfullyTraversed; @@ -226,6 +230,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { return nodeSuccesfullyTraversed; } + /* Traverse the left branch of a tree. */ private boolean traverseLeftBranch(final TreeLinearizationMethod linearizationMethod, final Predicate<T> traversalPredicate) { boolean leftSuccesfullyTraversed; @@ -239,6 +244,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { return leftSuccesfullyTraversed; } + /* Traverse the right branch of a tree. */ private boolean traverseRightBranch(final TreeLinearizationMethod linearizationMethod, final Predicate<T> traversalPredicate) { boolean rightSuccesfullyTraversed; 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 e11524a..fdf86d7 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 <T> - * The type of element stored in the walked tree + * The type of element stored in the walked tree */ @FunctionalInterface public interface DirectedWalkFunction<T> { @@ -14,12 +14,9 @@ public interface DirectedWalkFunction<T> { * Represents the results used to direct a walk in a binary tree. * * @author ben - * */ public enum DirectedWalkResult { - /** - * Specifies that the function has failed. - */ + /** Specifies that the function has failed. */ FAILURE, /** * Specifies that the function wants to move left in the tree @@ -31,10 +28,7 @@ public interface DirectedWalkFunction<T> { * next. */ RIGHT, - /** - * Specifies that the function has succesfully completed - * - */ + /** Specifies that the function has succesfully completed */ SUCCESS } @@ -42,8 +36,10 @@ public interface DirectedWalkFunction<T> { * Perform a directed walk on a node of a tree. * * @param element - * The data stored in the node currently being visited - * @return The way the function wants the walk to go next. + * The data stored in the node currently being visited. + * + * @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 3aa8880..a2ce71f 100644 --- a/base/src/main/java/bjc/utils/funcdata/bst/ITreePart.java +++ b/base/src/main/java/bjc/utils/funcdata/bst/ITreePart.java @@ -11,53 +11,61 @@ import java.util.function.Predicate; * @author ben * * @param <T> - * The data contained in this part of the tree. + * The data contained in this part of the tree. */ public interface ITreePart<T> { /** * 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<T> comparator); /** - * Collapses this tree part into a single value. Does not change the - * underlying tree. + * Collapses this tree part into a single value. + * + * Does not change the underlying tree. * * @param <E> - * 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. - * @return A single value from collapsing the tree. + * The function to use to collapse data in mapped form into a + * single value. + * + * @return + * A single value from collapsing the tree. */ public <E> E collapse(Function<T, E> nodeCollapser, BiFunction<E, E, E> branchCollapser); /** - * Check if this tre part or below it contains the specified data item + * 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 - * @return Whether or not the given item is contained in this tree part - * or its children. + * 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. */ public boolean contains(T element, Comparator<T> 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(); @@ -65,9 +73,10 @@ public interface ITreePart<T> { * 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<T> comparator); @@ -75,22 +84,27 @@ public interface ITreePart<T> { * Execute a directed walk through the tree. * * @param walker - * The function to use to direct the walk through the - * tree. - * @return Whether the directed walk finished successfully. + * The function to use to direct the walk through the + * tree. + * + * @return + * Whether the directed walk finished successfully. */ public boolean directedWalk(DirectedWalkFunction<T> walker); /** * Execute a provided function for each element of tree it succesfully - * completes for + * 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 - * @return Whether the traversal finished succesfully + * The predicate to apply to each element, where it returning false + * terminates traversal early. + * + * @return + * Whether the traversal finished succesfully. */ public boolean forEach(TreeLinearizationMethod linearizationMethod, Predicate<T> predicate); } diff --git a/base/src/main/java/bjc/utils/funcdata/bst/TreeLinearizationMethod.java b/base/src/main/java/bjc/utils/funcdata/bst/TreeLinearizationMethod.java index 0c83867..80afaf2 100644 --- a/base/src/main/java/bjc/utils/funcdata/bst/TreeLinearizationMethod.java +++ b/base/src/main/java/bjc/utils/funcdata/bst/TreeLinearizationMethod.java @@ -4,7 +4,6 @@ package bjc.utils.funcdata.bst; * Represents the ways to linearize a tree for traversal. * * @author ben - * */ public enum TreeLinearizationMethod { /** @@ -22,4 +21,4 @@ public enum TreeLinearizationMethod { * then the right part. */ PREORDER -}
\ No newline at end of file +} |
