Java tutorial
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class TreeNode { TreeNode left, right; int data; TreeNode() { data = 0; left = right = null; } TreeNode(int n) { data = n; left = right = null; } void disp() { System.out.println(data + " "); } void setLeft(TreeNode n) { left = n; } void setRight(TreeNode n) { right = n; } TreeNode getLeft() { return left; } TreeNode getRight() { return right; } void setData(int d) { data = d; } int getData() { return data; } } class BinaryTree { TreeNode root; BinaryTree() { root = null; } void insert(int data) { root = insert(root, data); } TreeNode insert(TreeNode node, int data) { if (node == null) { node = new TreeNode(data); } else { if (data <= node.data) { node.left = insert(node.left, data); } else { node.right = insert(node.right, data); } } return (node); } void inorder() { inorder(root); System.out.println(); } void inorder(TreeNode r) { if (r != null) { inorder(r.getLeft()); System.out.print(r.getData() + " "); inorder(r.getRight()); } } void preorder() { preorder(root); System.out.println(); } void preorder(TreeNode r) { if (r != null) { System.out.print(r.getData() + " "); preorder(r.getLeft()); preorder(r.getRight()); } } void postorder() { postorder(root); System.out.println(); } void postorder(TreeNode r) { if (r != null) { postorder(r.getLeft()); postorder(r.getRight()); System.out.print(r.getData() + " "); } } } public class Main { public static void main(String[] argv) throws IOException { BinaryTree bt = new BinaryTree(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int val; char ch = ' '; String clearbuffer; do { System.out.print("Enter a number: "); val = Integer.parseInt(br.readLine()); bt.insert(val); System.out.print("Do you wish to enter more values (Y/N)....."); ch = (char) br.read(); clearbuffer = br.readLine(); } while (ch == 'y' || ch == 'Y'); System.out.println("Postorder traversal of given tree is: "); bt.postorder(); System.out.println("Preorder traversal of given tree is: "); bt.preorder(); System.out.println("Inorder traversal of given tree is: "); bt.inorder(); } }