From d2af58b0f68ebfbba2be7e7679efec6c8c0af12f Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 9 Feb 2017 11:50:31 -0500 Subject: Update --- .../bjc/utils/funcdata/bst/BinarySearchTree.java | 74 +++++----- .../utils/funcdata/bst/BinarySearchTreeLeaf.java | 50 +------ .../utils/funcdata/bst/BinarySearchTreeNode.java | 153 ++++++++------------- .../java/bjc/utils/funcdata/bst/ITreePart.java | 10 +- 4 files changed, 100 insertions(+), 187 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/bst') 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 865f4d6..b595946 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 @@ -17,20 +17,20 @@ import bjc.utils.funcdata.IList; * The data type stored in the node. */ public class BinarySearchTree { - /** + /* * The comparator for use in ordering items */ private Comparator comparator; - /** + /* * The current count of elements in the tree */ private int elementCount; - /** + /* * The root element of the tree */ - private ITreePart rootElement; + private ITreePart root; /** * Create a new tree using the specified way to compare elements. @@ -56,10 +56,10 @@ public class BinarySearchTree { public void addNode(T element) { elementCount++; - if (rootElement == null) { - rootElement = new BinarySearchTreeNode<>(element, null, null); + if (root == null) { + root = new BinarySearchTreeNode<>(element, null, null); } else { - rootElement.add(element, comparator); + root.add(element, comparator); } } @@ -74,25 +74,23 @@ public class BinarySearchTree { * The distance from the pivot * @return Whether the adjusted pivot is with the list */ - private boolean adjustedPivotInBounds(IList elements, int pivot, - int pivotAdjustment) { - return (pivot - pivotAdjustment) >= 0 - && (pivot + pivotAdjustment) < elements.getSize(); + private boolean adjustedPivotInBounds(IList elements, int pivot, int pivotAdjustment) { + return (pivot - pivotAdjustment) >= 0 && (pivot + pivotAdjustment) < elements.getSize(); } /** - * Balance the tree, and remove soft-deleted nodes for free. Takes O(N) - * time, but also O(N) space. + * Balance the tree, and remove soft-deleted nodes for free. + * + * Takes O(N) time, but also O(N) space. */ public void balance() { IList elements = new FunctionalList<>(); // Add each element to the list in sorted order - rootElement.forEach(TreeLinearizationMethod.INORDER, - element -> elements.add(element)); + root.forEach(TreeLinearizationMethod.INORDER, element -> elements.add(element)); // Clear the tree - rootElement = null; + root = null; // Set up the pivot and adjustment for readding elements int pivot = elements.getSize() / 2; @@ -100,19 +98,14 @@ public class BinarySearchTree { // Add elements until there aren't any left while (adjustedPivotInBounds(elements, pivot, pivotAdjustment)) { - if (rootElement == null) { + if (root == null) { // Create a new root element - rootElement = new BinarySearchTreeNode<>( - elements.getByIndex(pivot), null, null); + root = new BinarySearchTreeNode<>(elements.getByIndex(pivot), null, null); } else { // Add the left and right elements in a balanced manner - rootElement.add( - elements.getByIndex(pivot + pivotAdjustment), - comparator); + root.add(elements.getByIndex(pivot + pivotAdjustment), comparator); - rootElement.add( - elements.getByIndex(pivot - pivotAdjustment), - comparator); + root.add(elements.getByIndex(pivot - pivotAdjustment), comparator); } // Increase the distance from the pivot @@ -121,18 +114,17 @@ public class BinarySearchTree { // Add any trailing unbalanced elements if ((pivot - pivotAdjustment) >= 0) { - rootElement.add(elements.getByIndex(pivot - pivotAdjustment), - comparator); + root.add(elements.getByIndex(pivot - pivotAdjustment), comparator); } else if ((pivot + pivotAdjustment) < elements.getSize()) { - rootElement.add(elements.getByIndex(pivot + pivotAdjustment), - comparator); + root.add(elements.getByIndex(pivot + pivotAdjustment), comparator); } } /** - * Soft-delete a node from the tree. Soft-deleted nodes stay in the - * tree until trim()/balance() is invoked, and are not included in - * traversals/finds. + * Soft-delete a node from the tree. + * + * Soft-deleted nodes stay in the tree until trim()/balance() is invoked, and + * are not included in traversals/finds. * * @param element * The node to delete @@ -140,7 +132,7 @@ public class BinarySearchTree { public void deleteNode(T element) { elementCount--; - rootElement.delete(element, comparator); + root.delete(element, comparator); } /** @@ -149,7 +141,7 @@ public class BinarySearchTree { * @return The root of the tree. */ public ITreePart getRoot() { - return rootElement; + return root; } /** @@ -160,7 +152,7 @@ public class BinarySearchTree { * @return Whether or not the node is in the tree. */ public boolean isInTree(T element) { - return rootElement.contains(element, comparator); + return root.contains(element, comparator); } /** @@ -171,16 +163,14 @@ public class BinarySearchTree { * @param traversalPredicate * The function to use until it fails */ - public void traverse(TreeLinearizationMethod linearizationMethod, - Predicate traversalPredicate) { + public void traverse(TreeLinearizationMethod linearizationMethod, Predicate traversalPredicate) { if (linearizationMethod == null) { - throw new NullPointerException( - "Linearization method must not be null"); + throw new NullPointerException("Linearization method must not be null"); } else if (traversalPredicate == null) { throw new NullPointerException("Predicate must not be nulls"); } - rootElement.forEach(linearizationMethod, traversalPredicate); + root.forEach(linearizationMethod, traversalPredicate); } /** @@ -196,9 +186,9 @@ public class BinarySearchTree { }); // Clear the tree - rootElement = null; + root = null; // Add the nodes to the tree in the order they were inserted nodes.forEach(node -> addNode(node)); } -} \ No newline at end of file +} 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 8ceb554..d647742 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 @@ -34,27 +34,13 @@ public class BinarySearchTreeLeaf implements ITreePart { data = element; } - /* - * Can't add things to a leaf. (non-Javadoc) - * - * @see bjc.utils.data.bst.ITreePart#add(java.lang.Object, - * java.util.Comparator) - */ @Override public void add(T element, Comparator comparator) { throw new IllegalArgumentException("Can't add to a leaf."); } - /* - * Just transform our data. (non-Javadoc) - * - * @see - * bjc.utils.data.bst.ITreePart#collapse(java.util.function.Function, - * java.util.function.BiFunction) - */ @Override - public E collapse(Function leafTransformer, - BiFunction branchCollapser) { + public E collapse(Function leafTransformer, BiFunction branchCollapser) { if (leafTransformer == null) { throw new NullPointerException("Transformer must not be null"); } @@ -62,33 +48,16 @@ public class BinarySearchTreeLeaf implements ITreePart { return leafTransformer.apply(data); } - /* - * Only check our data. (non-Javadoc) - * - * @see bjc.utils.data.bst.ITreePart#contains(java.lang.Object, - * java.util.Comparator) - */ @Override public boolean contains(T element, Comparator comparator) { return this.data.equals(element); } - /* - * Just get the data (non-Javadoc) - * - * @see bjc.utils.data.bst.ITreePart#data() - */ @Override public T data() { return data; } - /* - * Just mark ourselves as "not here" (non-Javadoc) - * - * @see bjc.utils.data.bst.ITreePart#delete(java.lang.Object, - * java.util.Comparator) - */ @Override public void delete(T element, Comparator comparator) { if (data.equals(element)) { @@ -96,13 +65,6 @@ public class BinarySearchTreeLeaf implements ITreePart { } } - /* - * Just walk our data and only succede if the walk does, because - * there's nowhere left to go. (non-Javadoc) - * - * @see bjc.utils.data.bst.ITreePart#directedWalk(bjc.utils.data.bst. - * DirectedWalkFunction) - */ @Override public boolean directedWalk(DirectedWalkFunction treeWalker) { if (treeWalker == null) { @@ -121,16 +83,8 @@ public class BinarySearchTreeLeaf implements ITreePart { } } - /* - * Just check our data. (non-Javadoc) - * - * @see - * bjc.utils.data.bst.ITreePart#forEach(bjc.utils.data.bst.ITreePart. - * TreeLinearizationMethod, java.util.function.Predicate) - */ @Override - public boolean forEach(TreeLinearizationMethod linearizationMethod, - Predicate traversalPredicate) { + public boolean forEach(TreeLinearizationMethod linearizationMethod, Predicate traversalPredicate) { if (traversalPredicate == null) { throw new NullPointerException("Predicate must not be null"); } 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 9817f91..fa3add0 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 @@ -19,39 +19,33 @@ import java.util.function.Predicate; * The data type stored in the tree. */ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { - /** + /* * The left child of this node */ - private ITreePart leftBranch; + private ITreePart left; - /** + /* * The right child of this node */ - private ITreePart rightBranch; + private ITreePart right; /** * Create a new node with the specified data and children. * * @param element * The data to store in this node. - * @param left + * @param lft * The left child of this node. - * @param right + * @param rght * The right child of this node. */ - public BinarySearchTreeNode(T element, ITreePart left, - ITreePart right) { + public BinarySearchTreeNode(T element, ITreePart lft, + ITreePart rght) { super(element); - this.leftBranch = left; - this.rightBranch = right; + this.left = lft; + this.right = rght; } - /* - * Either adds it to the left/right, or undeletes itself. (non-Javadoc) - * - * @see bjc.utils.data.bst.TreeLeaf#add(java.lang.Object, - * java.util.Comparator) - */ @Override public void add(T element, Comparator comparator) { if (comparator == null) { @@ -60,65 +54,57 @@ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { switch (comparator.compare(data, element)) { case -1: - if (leftBranch == null) { - leftBranch = new BinarySearchTreeNode<>(element, null, - null); + if (left == null) { + left = new BinarySearchTreeNode<>(element, null, null); } else { - leftBranch.add(element, comparator); + left.add(element, comparator); } + break; case 0: if (isDeleted) { isDeleted = false; } else { - throw new IllegalArgumentException( - "Can't add duplicate values"); + throw new IllegalArgumentException("Can't add duplicate values"); } + break; case 1: - if (rightBranch == null) { - rightBranch = new BinarySearchTreeNode<>(element, null, - null); + if (right == null) { + right = new BinarySearchTreeNode<>(element, null, null); } else { - rightBranch.add(element, comparator); + right.add(element, comparator); } + break; default: - throw new IllegalStateException( - "Error: Comparator yielded invalid value"); + throw new IllegalStateException("Error: Comparator yielded invalid value"); } } @Override - public E collapse(Function nodeCollapser, - BiFunction branchCollapser) { + public E collapse(Function nodeCollapser, BiFunction branchCollapser) { if (nodeCollapser == null || branchCollapser == null) { throw new NullPointerException("Collapser must not be null"); } E collapsedNode = nodeCollapser.apply(data); - if (leftBranch != null) { - E collapsedLeftBranch = leftBranch.collapse(nodeCollapser, - branchCollapser); - if (rightBranch != null) { - E collapsedRightBranch = rightBranch - .collapse(nodeCollapser, branchCollapser); + if (left != null) { + E collapsedLeftBranch = left.collapse(nodeCollapser, branchCollapser); - E collapsedBranches = branchCollapser - .apply(collapsedLeftBranch, collapsedRightBranch); + if (right != null) { + E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser); - return branchCollapser.apply(collapsedNode, - collapsedBranches); + E collapsedBranches = branchCollapser.apply(collapsedLeftBranch, collapsedRightBranch); + + return branchCollapser.apply(collapsedNode, collapsedBranches); } - return branchCollapser.apply(collapsedNode, - collapsedLeftBranch); + return branchCollapser.apply(collapsedNode, collapsedLeftBranch); } - if (rightBranch != null) { - E collapsedRightBranch = rightBranch.collapse(nodeCollapser, - branchCollapser); + if (right != null) { + E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser); - return branchCollapser.apply(collapsedNode, - collapsedRightBranch); + return branchCollapser.apply(collapsedNode, collapsedRightBranch); } return collapsedNode; @@ -153,12 +139,12 @@ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { directedWalk(currentElement -> { switch (comparator.compare(data, element)) { case -1: - return leftBranch == null ? FAILURE : LEFT; + return left == null ? FAILURE : LEFT; case 0: isDeleted = true; return FAILURE; case 1: - return rightBranch == null ? FAILURE : RIGHT; + return right == null ? FAILURE : RIGHT; default: return FAILURE; } @@ -175,9 +161,9 @@ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { case SUCCESS: return true; case LEFT: - return leftBranch.directedWalk(treeWalker); + return left.directedWalk(treeWalker); case RIGHT: - return rightBranch.directedWalk(treeWalker); + return right.directedWalk(treeWalker); case FAILURE: return false; default: @@ -186,25 +172,20 @@ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { } @Override - public boolean forEach(TreeLinearizationMethod linearizationMethod, - Predicate traversalPredicate) { + public boolean forEach(TreeLinearizationMethod linearizationMethod, Predicate traversalPredicate) { if (linearizationMethod == null) { - throw new NullPointerException( - "Linearization method must not be null"); + throw new NullPointerException("Linearization method must not be null"); } else if (traversalPredicate == null) { throw new NullPointerException("Predicate must not be null"); } switch (linearizationMethod) { case PREORDER: - return preorderTraverse(linearizationMethod, - traversalPredicate); + return preorderTraverse(linearizationMethod, traversalPredicate); case INORDER: - return inorderTraverse(linearizationMethod, - traversalPredicate); + return inorderTraverse(linearizationMethod, traversalPredicate); case POSTORDER: - return postorderTraverse(linearizationMethod, - traversalPredicate); + return postorderTraverse(linearizationMethod, traversalPredicate); default: throw new IllegalArgumentException( "Passed an incorrect TreeLinearizationMethod " @@ -212,10 +193,7 @@ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { } } - private boolean inorderTraverse( - TreeLinearizationMethod linearizationMethod, - Predicate traversalPredicate) { - + private boolean inorderTraverse( TreeLinearizationMethod linearizationMethod, Predicate traversalPredicate) { if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) { return false; } @@ -224,24 +202,19 @@ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { return false; } - if (!traverseRightBranch(linearizationMethod, - traversalPredicate)) { + if (!traverseRightBranch(linearizationMethod, traversalPredicate)) { return false; } return true; } - private boolean postorderTraverse( - TreeLinearizationMethod linearizationMethod, - Predicate traversalPredicate) { - + private boolean postorderTraverse(TreeLinearizationMethod linearizationMethod, Predicate traversalPredicate) { if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) { return false; } - if (!traverseRightBranch(linearizationMethod, - traversalPredicate)) { + if (!traverseRightBranch(linearizationMethod, traversalPredicate)) { return false; } @@ -253,10 +226,7 @@ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { } - private boolean preorderTraverse( - TreeLinearizationMethod linearizationMethod, - Predicate traversalPredicate) { - + private boolean preorderTraverse(TreeLinearizationMethod linearizationMethod, Predicate traversalPredicate) { if (!traverseElement(traversalPredicate)) { return false; } @@ -265,8 +235,7 @@ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { return false; } - if (!traverseRightBranch(linearizationMethod, - traversalPredicate)) { + if (!traverseRightBranch(linearizationMethod, traversalPredicate)) { return false; } @@ -275,37 +244,37 @@ public class BinarySearchTreeNode extends BinarySearchTreeLeaf { private boolean traverseElement(Predicate traversalPredicate) { boolean nodeSuccesfullyTraversed; + if (isDeleted) { nodeSuccesfullyTraversed = true; } else { nodeSuccesfullyTraversed = traversalPredicate.test(data); } + return nodeSuccesfullyTraversed; } - private boolean traverseLeftBranch( - TreeLinearizationMethod linearizationMethod, - Predicate traversalPredicate) { + private boolean traverseLeftBranch(TreeLinearizationMethod linearizationMethod, Predicate traversalPredicate) { boolean leftSuccesfullyTraversed; - if (leftBranch == null) { + + if (left == null) { leftSuccesfullyTraversed = true; } else { - leftSuccesfullyTraversed = leftBranch - .forEach(linearizationMethod, traversalPredicate); + leftSuccesfullyTraversed = left.forEach(linearizationMethod, traversalPredicate); } + return leftSuccesfullyTraversed; } - private boolean traverseRightBranch( - TreeLinearizationMethod linearizationMethod, - Predicate traversalPredicate) { + private boolean traverseRightBranch(TreeLinearizationMethod linearizationMethod, Predicate traversalPredicate) { boolean rightSuccesfullyTraversed; - if (rightBranch == null) { + + if (right == null) { rightSuccesfullyTraversed = true; } else { - rightSuccesfullyTraversed = rightBranch - .forEach(linearizationMethod, traversalPredicate); + rightSuccesfullyTraversed = right.forEach(linearizationMethod, traversalPredicate); } + return rightSuccesfullyTraversed; } -} \ No newline at end of file +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/ITreePart.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/ITreePart.java index cbd7229..c574196 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/ITreePart.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/ITreePart.java @@ -74,11 +74,11 @@ public interface ITreePart { /** * Execute a directed walk through the tree. * - * @param treeWalker + * @param walker * The function to use to direct the walk through the tree. * @return Whether the directed walk finished successfully. */ - public boolean directedWalk(DirectedWalkFunction treeWalker); + public boolean directedWalk(DirectedWalkFunction walker); /** * Execute a provided function for each element of tree it succesfully @@ -86,11 +86,11 @@ public interface ITreePart { * * @param linearizationMethod * The way to linearize the tree for executing - * @param traversalPredicate - * The function to apply to each element, where it returning + * @param predicate + * 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 traversalPredicate); + Predicate predicate); } -- cgit v1.2.3