Created
March 3, 2013 17:33
-
-
Save meikj/5077121 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Node { | |
private Node left; | |
private Node right; | |
private int value; | |
public Node(int value) { | |
this.left = null; | |
this.right = null; | |
this.value = value; | |
} | |
public Node(Node left, Node right, int value) { | |
this.left = left; | |
this.right = right; | |
this.value = value; | |
} | |
public Node getLeft() { return left; } | |
public Node getRight() { return right; } | |
public int getValue() { return value; } | |
public static void preOrder(Node root) { | |
if(root == null) return; | |
System.out.print(root.getValue() + " "); | |
preOrder(root.getLeft()); | |
preOrder(root.getRight()); | |
} | |
public static void inOrder(Node root) { | |
if(root == null) return; | |
inOrder(root.getLeft()); | |
System.out.print(root.getValue() + " "); | |
inOrder(root.getRight()); | |
} | |
public static void postOrder(Node root) { | |
if(root == null) return; | |
postOrder(root.getLeft()); | |
postOrder(root.getRight()); | |
System.out.print(root.getValue() + " "); | |
} | |
public static void main(String[] args) { | |
Node node2 = new Node(new Node(10), new Node(3), 2); | |
Node node5 = new Node(null, node2, 5); | |
Node node7 = new Node(new Node(14), null, 7); | |
Node node1 = new Node(new Node(6), new Node(4), 1); | |
Node node9 = new Node(node7, node1, 9); | |
Node root = new Node(node5, node9, 2); | |
System.out.print("Pre-order: "); | |
preOrder(root); | |
System.out.println(); | |
System.out.print("In-order: "); | |
inOrder(root); | |
System.out.println(); | |
System.out.print("Post-order: "); | |
postOrder(root); | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment