From 82951e37e10b282d9a7c89f4662990b64949c943 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 29 Feb 2016 10:41:17 -0500 Subject: General code cleanup --- .../bjc/utils/funcdata/bst/BinarySearchTree.java | 39 ++++----- .../utils/funcdata/bst/DirectedWalkFunction.java | 25 +++--- .../java/bjc/utils/funcdata/bst/ITreePart.java | 94 +++++++++++++++------- .../main/java/bjc/utils/funcdata/bst/TreeNode.java | 25 +++--- 4 files changed, 111 insertions(+), 72 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 7bf0007..7665797 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 @@ -5,6 +5,7 @@ import java.util.Comparator; import java.util.List; import java.util.function.Predicate; +import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.bst.ITreePart.TreeLinearizationMethod; /** @@ -49,6 +50,7 @@ public class BinarySearchTree { */ public void addNode(T dat) { nCount++; + if (root == null) { root = new TreeNode(dat, null, null); } else { @@ -61,42 +63,33 @@ public class BinarySearchTree { * time, but also O(N) space. */ public void balance() { - ArrayList elms = new ArrayList<>(nCount); + FunctionalList elms = new FunctionalList<>(); root.forEach(TreeLinearizationMethod.INORDER, e -> elms.add(e)); root = null; - int piv = elms.size() / 2; + int piv = elms.getSize() / 2; int adj = 0; - while ((piv - adj) >= 0 && (piv + adj) < elms.size()) { + while ((piv - adj) >= 0 && (piv + adj) < elms.getSize()) { if (root == null) { - root = new TreeNode(elms.get(piv), null, null); + root = new TreeNode(elms.getByIndex(piv), null, null); } else { - root.add(elms.get(piv + adj), comp); - root.add(elms.get(piv - adj), comp); + root.add(elms.getByIndex(piv + adj), comp); + root.add(elms.getByIndex(piv - adj), comp); } adj++; } if ((piv - adj) >= 0) { - root.add(elms.get(piv - adj), comp); - } else if ((piv + adj) < elms.size()) { - root.add(elms.get(piv + adj), comp); + root.add(elms.getByIndex(piv - adj), comp); + } else if ((piv + adj) < elms.getSize()) { + root.add(elms.getByIndex(piv + adj), comp); } } - /** - * Get the root of the tree. - * - * @return The root of the tree. - */ - public ITreePart getRoot() { - return root; - } - /** * Soft-delete a node from the tree. Soft-deleted nodes stay in the * tree until trim()/balance() is invoked, and are not included in @@ -106,9 +99,19 @@ public class BinarySearchTree { */ public void deleteNode(T dat) { nCount--; + root.delete(dat, comp); } + /** + * Get the root of the tree. + * + * @return The root of the tree. + */ + public ITreePart getRoot() { + return root; + } + /** * Check if a node is in the tree * diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java index 18e80be..12c87b3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java @@ -2,9 +2,11 @@ package bjc.utils.funcdata.bst; /** * Represents a function for doing a directed walk of a binary tree. + * * @author ben * * @param + * The type of element stored in the walked tree */ @FunctionalInterface public interface DirectedWalkFunction { @@ -15,28 +17,31 @@ public interface DirectedWalkFunction { * */ public enum DirectedWalkResult { - /** - * Specifies that the function has succesfully completed - * - */ - SUCCESS, /** * Specifies that the function has failed. */ FAILURE, - /** + /** * Specifies that the function wants to move left in the tree next. */ - LEFT, + LEFT, /** - * Specifies that the function wants to move right in the tree next. + * Specifies that the function wants to move right in the tree + * next. */ - RIGHT + RIGHT, + /** + * Specifies that the function has succesfully completed + * + */ + SUCCESS } /** * Perform a directed walk on a node of a tree. - * @param data The data stored in the node currently being visited + * + * @param data + * The data stored in the node currently being visited * @return The way the function wants the walk to go next. */ public DirectedWalkResult walk(T data); 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 0c8f12e..dfcfedd 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 @@ -7,75 +7,109 @@ import java.util.function.Predicate; /** * A interface for the fundamental things that want to be part of a tree. + * * @author ben * - * @param The data contained in this part of the tree. + * @param + * The data contained in this part of the tree. */ public interface ITreePart { /** * Represents the ways to linearize a tree for traversal. + * * @author ben * */ public enum TreeLinearizationMethod { /** - * Visit the left side of this tree part, the tree part itself, and then the right part. + * Visit the left side of this tree part, the tree part itself, and + * then the right part. */ - INORDER, + INORDER, /** - * Visit the left side of this tree part, the right side, and then the tree part itself. + * Visit the left side of this tree part, the right side, and then + * the tree part itself. */ - POSTORDER, + POSTORDER, /** - * Visit the tree part itself, then the left side of tthis tree part and then the right part. + * Visit the tree part itself, then the left side of tthis tree + * part and then the right part. */ PREORDER } - + /** * Add a element below this tree part somewhere. - * @param dat The element to add below this tree part - * @param comp The thing to use for comparing values to find where to insert the tree part. + * + * @param dat + * The element to add below this tree part + * @param comp + * The thing to use for comparing values to find where to + * insert the tree part. */ - void add(T dat, Comparator comp); + public void add(T dat, Comparator comp); + /** - * Collapses this tree part into a single value. - * Does not change the underlying tree. - * @param f The function to use to transform data into mapped form. - * @param bf The function to use to collapse data in mapped form into a single value. + * Collapses this tree part into a single value. Does not change the + * underlying tree. + * + * @param f + * The function to use to transform data into mapped form. + * @param bf + * The function to use to collapse data in mapped form into + * a single value. * @return A single value from collapsing the tree. */ - E collapse(Function f, BiFunction bf); + public E collapse(Function f, BiFunction bf); + /** * Check if this tre part or below it contains the specified data item - * @param data The data item to look for. - * @param cmp 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. + * + * @param data + * The data item to look for. + * @param cmp + * 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. */ - boolean contains(T data, Comparator cmp); + public boolean contains(T data, Comparator cmp); + /** * Get the data associated with this tree part. + * * @return The data associated with this tree part. */ - T data(); + public T data(); + /** * Remove the given node from this tree part and any of its children. - * @param dat The data item to remove. - * @param cmp The comparator to use to search for the data item. + * + * @param dat + * The data item to remove. + * @param cmp + * The comparator to use to search for the data item. */ - void delete(T dat, Comparator cmp); + public void delete(T dat, Comparator cmp); + /** * Execute a directed walk through the tree. - * @param ds The function to use to direct the walk through the tree. + * + * @param ds + * The function to use to direct the walk through the tree. * @return Whether the directed walk finished successfully. */ - boolean directedWalk(DirectedWalkFunction ds); + public boolean directedWalk(DirectedWalkFunction ds); + /** - * Execute a provided function for each element of tree it succesfully completes for - * @param tlm The way to linearize the tree for executing - * @param c The function to apply to each element, where it returning false - * terminates traversal early + * Execute a provided function for each element of tree it succesfully + * completes for + * + * @param tlm + * The way to linearize the tree for executing + * @param c + * The function to apply to each element, where it returning + * false terminates traversal early * @return Whether the traversal finished succesfully */ - boolean forEach(TreeLinearizationMethod tlm, Predicate c); + public boolean forEach(TreeLinearizationMethod tlm, Predicate c); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeNode.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeNode.java index 40cc53f..e8c6c8b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeNode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeNode.java @@ -111,20 +111,17 @@ public class TreeNode extends TreeLeaf { @Override public void delete(T dat, Comparator cmp) { - directedWalk(new DirectedWalkFunction() { - @Override - public DirectedWalkResult walk(T ds) { - switch (cmp.compare(data, dat)) { - case -1: - return left == null ? FAILURE : LEFT; - case 0: - deleted = true; - return FAILURE; - case 1: - return right == null ? FAILURE : RIGHT; - default: - return DirectedWalkResult.FAILURE; - } + directedWalk(ds -> { + switch (cmp.compare(data, dat)) { + case -1: + return left == null ? FAILURE : LEFT; + case 0: + deleted = true; + return FAILURE; + case 1: + return right == null ? FAILURE : RIGHT; + default: + return FAILURE; } }); } -- cgit v1.2.3