/media/sda-magnetic/david/Dokumente-15/fernuni-hagen/cs-i-ii/old-cs-2-03/java-src/Binarytreeprogram.java


class BinaryTree {
    private BinaryNode root;

    public BinaryTree(int value) {
       root = new BinaryNode (value);
    }

    public BinaryTree() {  
       root = null;
    }

    private boolean contains2 (BinaryNode ptr, int value) {
      if (ptr != null) {
         if (ptr.value < value) 
           contains2 (ptr.leftSon, value);
         else if (ptr.value > value)
           contains2 (ptr.rightSon, value);
         else 
           return true;
      }
      return false;
    }
    
    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();
    }
}