diff --git a/compsci-lib/src/main/java/dev/obrienlabs/compsci/collections/BinarySearchTreeImpl.java b/compsci-lib/src/main/java/dev/obrienlabs/compsci/collections/BinarySearchTreeImpl.java new file mode 100644 index 0000000..0cecf8f --- /dev/null +++ b/compsci-lib/src/main/java/dev/obrienlabs/compsci/collections/BinarySearchTreeImpl.java @@ -0,0 +1,23 @@ +package dev.obrienlabs.compsci.collections; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +public class BinarySearchTreeImpl> { + + /** + * BinarySearchTree + * @param unsorted + * @return + */ + public List sort(List unsorted) { + List sorted = new CopyOnWriteArrayList(); + + + return sorted; + } + + public static void main(String[] args) { + + } +} diff --git a/compsci-lib/src/main/java/dev/obrienlabs/compsci/collections/BinaryTree.java b/compsci-lib/src/main/java/dev/obrienlabs/compsci/collections/BinaryTree.java index d68a6f6..e8ad257 100644 --- a/compsci-lib/src/main/java/dev/obrienlabs/compsci/collections/BinaryTree.java +++ b/compsci-lib/src/main/java/dev/obrienlabs/compsci/collections/BinaryTree.java @@ -2,12 +2,17 @@ import java.util.List; -public interface BinaryTree { +public interface BinaryTree> { + void add(List list); void add(T value); void delete(T value); BinaryTree getLeft(); BinaryTree getRight(); + T getData(); + boolean search(T value); Integer depth(); List inOrderTraversal(); + List inOrderTraversalHelper(List list); + } diff --git a/compsci-lib/src/main/java/dev/obrienlabs/compsci/collections/BinaryTreeImpl.java b/compsci-lib/src/main/java/dev/obrienlabs/compsci/collections/BinaryTreeImpl.java index 5861ba3..55ca297 100644 --- a/compsci-lib/src/main/java/dev/obrienlabs/compsci/collections/BinaryTreeImpl.java +++ b/compsci-lib/src/main/java/dev/obrienlabs/compsci/collections/BinaryTreeImpl.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.function.Consumer; public class BinaryTreeImpl> implements BinaryTree { @@ -18,27 +19,71 @@ public BinaryTreeImpl(T value) { data = value; } + public T getData() { + return data; + } + @Override public List inOrderTraversal() { - List list = new ArrayList(); + return inOrderTraversalHelper(new ArrayList()); + } + + @Override + public List inOrderTraversalHelper(List list) { + if(null != left) + left.inOrderTraversalHelper(list); list.add(data); + if(null != right) + right.inOrderTraversalHelper(list); return list; } - + @Override public void add(T value) { // recursively insert value if(null == data) { data = value; } else { - if(null == right && data.compareTo(value) < 0) { - BinaryTree right = new BinaryTreeImpl(value); - this.right = right; - + // traverse until a null child exists + if(data.compareTo(value) < 0) { + if(null == right) { + right = new BinaryTreeImpl(value); + } else { + right.add(value); + } + } else { + if(null == left) { + left = new BinaryTreeImpl(value); + } else { + left.add(value); + } } } } + @Override + public void add(List list) { + list.stream().forEach(addConsumer); + } + + Consumer addConsumer = new Consumer() { + public void accept(T value) { + add(value); + } + }; + + @Override + public boolean search(T value) { + // TODO Auto-generated method stub + return false; + } + @Override + public String toString() { + StringBuffer buffer = new StringBuffer(); + inOrderTraversal().stream().forEach(i -> buffer.append(i).append(",")); + return buffer.toString(); + } + @Override public void delete(T value) { // TODO Auto-generated method stub @@ -69,4 +114,6 @@ public static void main(String[] args) { // TODO Auto-generated method stub } + + } diff --git a/compsci-lib/src/test/java/dev/obrienlabs/compsci/collections/BinaryTreeTest.java b/compsci-lib/src/test/java/dev/obrienlabs/compsci/collections/BinaryTreeTest.java index e3e41f6..443e4b7 100644 --- a/compsci-lib/src/test/java/dev/obrienlabs/compsci/collections/BinaryTreeTest.java +++ b/compsci-lib/src/test/java/dev/obrienlabs/compsci/collections/BinaryTreeTest.java @@ -2,11 +2,15 @@ import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.junit.Test; + public class BinaryTreeTest { @@ -17,10 +21,15 @@ public void testContructor() { List list = tree.inOrderTraversal(); assertNotNull(list); - tree.add(10); - tree.add(16); + // Add a list of numbers + List numbersToInsert = Stream.of(8,16,32,64,48,2,128).collect(Collectors.toList()); + tree.add(numbersToInsert); + BinaryTree subTree = tree.getRight(); assertNotNull(subTree); + assertTrue(subTree.getData().intValue() == 16); + System.out.println("right subtree:" + subTree.getData()); + System.out.println("tree: " + tree); } } \ No newline at end of file