Skip to content

Commit

Permalink
#2 - BinaryTree add/traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
obriensystems committed Feb 11, 2024
1 parent 886731d commit 91a54d7
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.obrienlabs.compsci.collections;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class BinarySearchTreeImpl<T extends Comparable<? super T>> {

/**
* BinarySearchTree
* @param unsorted
* @return
*/
public List<T> sort(List<T> unsorted) {
List<T> sorted = new CopyOnWriteArrayList<T>();


return sorted;
}

public static void main(String[] args) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import java.util.List;

public interface BinaryTree<T> {
public interface BinaryTree<T extends Comparable<? super T>> {

void add(List<T> list);
void add(T value);
void delete(T value);
BinaryTree<T> getLeft();
BinaryTree<T> getRight();
T getData();
boolean search(T value);
Integer depth();
List<T> inOrderTraversal();
List<T> inOrderTraversalHelper(List<T> list);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class BinaryTreeImpl<T extends Comparable<? super T>> implements BinaryTree<T> {

Expand All @@ -18,27 +19,71 @@ public BinaryTreeImpl(T value) {
data = value;
}

public T getData() {
return data;
}

@Override
public List<T> inOrderTraversal() {
List<T> list = new ArrayList<T>();
return inOrderTraversalHelper(new ArrayList<T>());
}

@Override
public List<T> inOrderTraversalHelper(List<T> 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<T> right = new BinaryTreeImpl(value);
this.right = right;

// traverse until a null child exists
if(data.compareTo(value) < 0) {
if(null == right) {
right = new BinaryTreeImpl<T>(value);
} else {
right.add(value);
}
} else {
if(null == left) {
left = new BinaryTreeImpl<T>(value);
} else {
left.add(value);
}
}
}
}

@Override
public void add(List<T> list) {
list.stream().forEach(addConsumer);
}

Consumer<T> addConsumer = new Consumer<T>() {
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
Expand Down Expand Up @@ -69,4 +114,6 @@ public static void main(String[] args) {
// TODO Auto-generated method stub

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {


Expand All @@ -17,10 +21,15 @@ public void testContructor() {
List<Integer> list = tree.inOrderTraversal();
assertNotNull(list);

tree.add(10);
tree.add(16);
// Add a list of numbers
List<Integer> numbersToInsert = Stream.of(8,16,32,64,48,2,128).collect(Collectors.toList());
tree.add(numbersToInsert);

BinaryTree<Integer> subTree = tree.getRight();
assertNotNull(subTree);
assertTrue(subTree.getData().intValue() == 16);
System.out.println("right subtree:" + subTree.getData());
System.out.println("tree: " + tree);

}
}

0 comments on commit 91a54d7

Please sign in to comment.