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 ++++++++++++---------- 1 file changed, 21 insertions(+), 18 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java') 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 * -- cgit v1.2.3