diff options
Diffstat (limited to 'base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java | 91 |
1 files changed, 48 insertions, 43 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)); } |
