Skip to content

Commit

Permalink
#1 - start insert and generics
Browse files Browse the repository at this point in the history
  • Loading branch information
obriensystems committed Jan 28, 2024
1 parent bcf4f97 commit 886731d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@

public interface BinaryTree<T> {

void add(T value);
void delete(T value);
BinaryTree<T> getLeft();
BinaryTree<T> getRight();
Integer depth();
List<T> inOrderTraversal();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import java.util.ArrayList;
import java.util.List;

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

private BinaryTreeNode<T> left;
private BinaryTreeNode<T> right;
private BinaryTree<T> left;
private BinaryTree<T> right;
private T data;

public static void main(String[] args) {
// TODO Auto-generated method stub

public BinaryTreeImpl() {
data = null;
}

public BinaryTreeImpl(T value) {
data = value;
}

@Override
Expand All @@ -21,4 +25,48 @@ public List<T> inOrderTraversal() {
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;

}
}
}

@Override
public void delete(T value) {
// TODO Auto-generated method stub

}

@Override
public Integer depth() {
// TODO Auto-generated method stub
return null;
}



@Override
public BinaryTree<T> getLeft() {
return left;
}

@Override
public BinaryTree<T> getRight() {
return right;
}



public static void main(String[] args) {
// TODO Auto-generated method stub

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ public void testContructor() {
List<Integer> list = tree.inOrderTraversal();
assertNotNull(list);

tree.add(10);
tree.add(16);
BinaryTree<Integer> subTree = tree.getRight();
assertNotNull(subTree);

}
}

0 comments on commit 886731d

Please sign in to comment.