class BinaryTree {
private BinaryNode root;
public BinaryTree(int value) {
root = new BinaryNode (value);
}
public BinaryTree() {
root = null;
}
private boolean contains2 (BinaryNode ptr, int value) {
boolean retval;
if (ptr != null) {
if (ptr.value > value)
retval = contains2 (ptr.leftSon, value);
else if (ptr.value < value)
retval = contains2 (ptr.rightSon, value);
else
retval = true;
}
else
retval = false;
return retval;
}
public boolean contains(int value) {
BinaryNode ptr;
ptr = root;
return contains2(ptr, value);
}
private void insert2 (BinaryNode ptr, int value) {
BinaryNode tmp;
if (ptr != null) {
if (value < ptr.value) {
if (ptr.leftSon == null) {
tmp = new BinaryNode (value);
ptr.leftSon = tmp;
}
else
insert2 (ptr.leftSon, value);
}
else {
if (ptr.rightSon == null) {
tmp = new BinaryNode (value);
ptr.rightSon = tmp;
}
else
insert2 (ptr.rightSon, value);
}
}
else {
root = new BinaryNode (value);
}
}
public void insert(int value) {
BinaryNode ptr;
ptr = root;
insert2(ptr, value);
}
private void inorder2 (BinaryNode ptr) {
if (ptr != null) {
inorder2 (ptr.leftSon);
System.out.println (ptr.value);
inorder2 (ptr.rightSon);
}
}
public void inorder() {
BinaryNode ptr;
ptr = root;
inorder2 (ptr);
}
private static class BinaryNode {
private BinaryNode leftSon, rightSon;
private int value;
public BinaryNode(int value) {
this.value = value;
this.leftSon = null;
this.rightSon = null;
}
}
}
class TestTree {
public static void main(String[] args) {
BinaryTree myTree = new BinaryTree(6);
myTree.insert(5);
myTree.insert(4);
myTree.insert(12);
myTree.insert(11);
myTree.insert(10);
myTree.inorder();
System.out.println(myTree.contains(10));
System.out.println(myTree.contains(5));
System.out.println(myTree.contains(4));
System.out.println(myTree.contains(107));
System.out.println(myTree.contains(11));
System.out.println(myTree.contains(208));
}
}