import java.util.Comparator;

public class BinarySearchTree<E> extends BinaryTree<E> {
	protected Comparator<E> comp;

	public BinarySearchTree(Comparator<E> inComp) {
		super();
		comp = inComp;
	}

	public boolean contains(E elem) {
		BinaryTreeNode<E> current = root;
		while(current != null) {
			System.out.println("Contains considering node with data " + current.getData());
			if(current.getData().equals(elem)) {
				return true;
			}
			if(comp.compare(current.getData(), elem) >= 0) {
				current = current.getLeft();
			} else {
				current = current.getRight();
			}
		}
		return false;
	}

	public void add(E elem) {
		BinaryTreeNode<E> current = root;
		while(current != null) {
			System.out.println("Add considering node with data " + current.getData());
			if(comp.compare(current.getData(), elem) >= 0) {
				if(current.getLeft() == null) {
					current.setLeft(new BinaryTreeNode<E>(elem));
					return;
				}
				current = current.getLeft();
			} else {
				if(current.getRight() == null) {
					current.setRight(new BinaryTreeNode<E>(elem));
					return;
				}
				current = current.getRight();
			}
		}
		root = new BinaryTreeNode<E>(elem);
	}

	public static void main(String[] args) {
		BinarySearchTree<Integer> test = new BinarySearchTree<>(new NaturalComparator<Integer>());
		test.add(18);
		test.add(9);
		test.add(24);
		test.add(5);
		test.add(12);
		test.add(22);
		test.add(30);
		test.add(29);
		test.add(35);

		System.out.println("=================");
		System.out.println("Searching for 14: " + test.contains(14));
		System.out.println("Adding 23:");
		test.add(23);
		System.out.println("Searching for 23: " + test.contains(23));
	}

}
