From ae0d399d9b6a4d29285002bc3836be82f8c7df70 Mon Sep 17 00:00:00 2001 From: Michael OBrien Date: Sun, 11 Feb 2024 11:33:30 -0500 Subject: [PATCH] #2 - traversal using nodes --- .../compsci/collections/BinaryTree.java | 7 +++--- .../compsci/collections/BinaryTreeImpl.java | 25 +++++++++++++++---- .../compsci/collections/BinaryTreeTest.java | 6 +++-- 3 files changed, 28 insertions(+), 10 deletions(-) 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 e8ad257..580cfed 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 @@ -12,7 +12,8 @@ public interface BinaryTree> { T getData(); boolean search(T value); Integer depth(); - List inOrderTraversal(); - List inOrderTraversalHelper(List list); - + List> inOrderTraversal(); + List> inOrderTraversalHelper(List> list); + List> inOrderTraversalOnlyLeft(); + List> inOrderTraversalOnlyLeftHelper(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 55ca297..0bf4a53 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 @@ -24,19 +24,34 @@ public T getData() { } @Override - public List inOrderTraversal() { - return inOrderTraversalHelper(new ArrayList()); + public List> inOrderTraversal() { + return inOrderTraversalHelper(new ArrayList>()); } @Override - public List inOrderTraversalHelper(List list) { + public List> inOrderTraversalHelper(List> list) { if(null != left) left.inOrderTraversalHelper(list); - list.add(data); + list.add(this); if(null != right) right.inOrderTraversalHelper(list); return list; } + + @Override + public List> inOrderTraversalOnlyLeft() { + return inOrderTraversalOnlyLeftHelper(new ArrayList>()); + } + + @Override + public List> inOrderTraversalOnlyLeftHelper(List> list) { + if(null != left) + left.inOrderTraversalOnlyLeftHelper(list); + list.add(this); + if(null != right) + right.inOrderTraversalOnlyLeftHelper(list); + return list; + } @Override public void add(T value) { @@ -80,7 +95,7 @@ public boolean search(T value) { @Override public String toString() { StringBuffer buffer = new StringBuffer(); - inOrderTraversal().stream().forEach(i -> buffer.append(i).append(",")); + inOrderTraversal().stream().forEach(i -> buffer.append(i.getData()).append(",")); return buffer.toString(); } 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 443e4b7..e2e8077 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 @@ -18,12 +18,14 @@ public class BinaryTreeTest { @Test public void testContructor() { BinaryTree tree = new BinaryTreeImpl(); - List list = tree.inOrderTraversal(); - assertNotNull(list); + // Add a list of numbers List numbersToInsert = Stream.of(8,16,32,64,48,2,128).collect(Collectors.toList()); tree.add(numbersToInsert); + + List> list = tree.inOrderTraversal(); + assertNotNull(list); BinaryTree subTree = tree.getRight(); assertNotNull(subTree);