diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java | 39 |
1 files changed, 21 insertions, 18 deletions
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<T> { */ public void addNode(T dat) { nCount++; + if (root == null) { root = new TreeNode<T>(dat, null, null); } else { @@ -61,43 +63,34 @@ public class BinarySearchTree<T> { * time, but also O(N) space. */ public void balance() { - ArrayList<T> elms = new ArrayList<>(nCount); + FunctionalList<T> 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<T>(elms.get(piv), null, null); + root = new TreeNode<T>(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<T> 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 * traversals/finds. @@ -106,10 +99,20 @@ public class BinarySearchTree<T> { */ public void deleteNode(T dat) { nCount--; + root.delete(dat, comp); } /** + * Get 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 * * @param dat |
