Binary Tree

 
class Node {
  Node left;

  Node right;

  int value;

  public Node(int value) {
    this.value = value;
  }
}

class BinaryTree {
  public void insert(Node node, int value) {
    if (value < node.value) {
      if (node.left != null) {
        insert(node.left, value);
      } else {
        System.out.println("  Inserted " + value + " to left of " + node.value);
        node.left = new Node(value);
      }
    } else if (value > node.value) {
      if (node.right != null) {
        insert(node.right, value);
      } else {
        System.out
            .println("  Inserted " + value + " to right of " + node.value);
        node.right = new Node(value);
      }
    }
  }

  public void printInOrder(Node node) {
    if (node != null) {
      printInOrder(node.left);
      System.out.println("  Traversed " + node.value);
      printInOrder(node.right);
    }
  }

  /**
   * uses in-order traversal when the origin is less than the node's value
   * 
   * uses reverse-order traversal when the origin is greater than the node's
   * order
   */
  public void printFrontToBack(Node node, int camera) {
    if (node == null)
      return;
    if (node.value > camera) {
      // print in order
      printFrontToBack(node.left, camera);
      System.out.println("  Traversed " + node.value);
      printFrontToBack(node.right, camera);
    } else if (node.value < camera) {
      // print reverse order
      printFrontToBack(node.right, camera);
      System.out.println("  Traversed " + node.value);
      printFrontToBack(node.left, camera);
    } else {
      // order doesn't matter
      printFrontToBack(node.left, camera);
      printFrontToBack(node.right, camera);
    }
  }

}

public class Main {

  public static void main(String[] args) {
    Node root = new Node(5);
    System.out.println("Binary Tree Example");
    System.out.println("Building tree with root value " + root.value);
    BinaryTree binaryTree = new BinaryTree();
    binaryTree.insert(root, 1);
    binaryTree.insert(root, 8);
    binaryTree.insert(root, 6);
    binaryTree.insert(root, 3);
    binaryTree.insert(root, 9);
    System.out.println("Traversing tree in order");
    binaryTree.printInOrder(root);
    System.out.println("Traversing tree front-to-back from location 7");
    binaryTree.printFrontToBack(root, 7);
  }

}
  

Output:


Binary Tree Example
Building tree with root value 5
  Inserted 1 to left of 5
  Inserted 8 to right of 5
  Inserted 6 to left of 8
  Inserted 3 to right of 1
  Inserted 9 to right of 8
Traversing tree in order
  Traversed 1
  Traversed 3
  Traversed 5
  Traversed 6
  Traversed 8
  Traversed 9
Traversing tree front-to-back from location 7
  Traversed 6
  Traversed 8
  Traversed 9
  Traversed 5
  Traversed 3
  Traversed 1
Home 
  Java Book 
    Runnable examples  

Algorithm:
  1. Binary search
  2. Binary Search Insert
  3. Recursive Binary Search Implementation in Java
  4. Linear Searching double Arrays
  5. Bubble sort
  6. Heap sort
  7. Merge sort
  8. Fast Merge Sort
  9. Fast Quick Sort
  10. Simple version of quick sort
  11. Quicksort for sorting arrays
  12. Quick Sort Implementation with median-of-three partitioning and cutoff for small arrays
  13. Quick sort with median-of-three partitioning
  14. Insert sort
  15. Insert Sort for objects
  16. Selection sort
  17. Shell sort
  18. Fibonacci
  19. Hanoi puzzle
  20. Table of fahrenheit and celsius temperatures
  21. Growable integer array
  22. Linked List class
  23. Binary Tree
  24. Tree with generic user object