summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/funcdata/bst/BinarySearchTree.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/bjc/funcdata/bst/BinarySearchTree.java')
-rw-r--r--src/main/java/bjc/funcdata/bst/BinarySearchTree.java21
1 files changed, 9 insertions, 12 deletions
diff --git a/src/main/java/bjc/funcdata/bst/BinarySearchTree.java b/src/main/java/bjc/funcdata/bst/BinarySearchTree.java
index e22a8da..99b67cd 100644
--- a/src/main/java/bjc/funcdata/bst/BinarySearchTree.java
+++ b/src/main/java/bjc/funcdata/bst/BinarySearchTree.java
@@ -6,7 +6,7 @@ import java.util.List;
import java.util.function.Predicate;
import bjc.funcdata.FunctionalList;
-import bjc.funcdata.IList;
+import bjc.funcdata.ListEx;
/**
* A binary search tree, with some mild support for functional traversal.
@@ -24,7 +24,7 @@ public class BinarySearchTree<T> {
private int elementCount;
/* The root element of the tree */
- private ITreePart<T> root;
+ private TreePart<T> root;
/**
* Create a new tree using the specified way to compare elements.
@@ -33,8 +33,7 @@ public class BinarySearchTree<T> {
* The thing to use for comparing elements
*/
public BinarySearchTree(final Comparator<T> cmp) {
- if (cmp == null)
- throw new NullPointerException("Comparator must not be null");
+ if (cmp == null) throw new NullPointerException("Comparator must not be null");
elementCount = 0;
comparator = cmp;
@@ -49,11 +48,8 @@ public class BinarySearchTree<T> {
public void addNode(final T element) {
elementCount++;
- if (root == null) {
- root = new BinarySearchTreeNode<>(element, null, null);
- } else {
- root.add(element, comparator);
- }
+ if (root == null) root = new BinarySearchTreeNode<>(element, null, null);
+ else root.add(element, comparator);
}
/**
@@ -70,7 +66,7 @@ public class BinarySearchTree<T> {
*
* @return Whether the adjusted pivot is with the list.
*/
- private boolean adjustedPivotInBounds(final IList<T> elements, final int pivot,
+ private boolean adjustedPivotInBounds(final ListEx<T> elements, final int pivot,
final int pivotAdjustment) {
return ((pivot - pivotAdjustment) >= 0)
&& ((pivot + pivotAdjustment) < elements.getSize());
@@ -82,7 +78,7 @@ public class BinarySearchTree<T> {
* Takes O(N) time, but also O(N) space.
*/
public void balance() {
- final IList<T> elements = new FunctionalList<>();
+ final ListEx<T> elements = new FunctionalList<>();
/* Add each element to the list in sorted order. */
root.forEach(TreeLinearizationMethod.INORDER, element -> elements.add(element));
@@ -139,7 +135,7 @@ public class BinarySearchTree<T> {
*
* @return The root of the tree.
*/
- public ITreePart<T> getRoot() {
+ public TreePart<T> getRoot() {
return root;
}
@@ -184,6 +180,7 @@ public class BinarySearchTree<T> {
*/
traverse(TreeLinearizationMethod.PREORDER, node -> {
nodes.add(node);
+
return true;
});