Skip to content

Instantly share code, notes, and snippets.

@thmain
Created August 20, 2015 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/2147e0ca90e7415ec625 to your computer and use it in GitHub Desktop.
Save thmain/2147e0ca90e7415ec625 to your computer and use it in GitHub Desktop.
import java.util.Stack;
public class PreOrderTree {
public void preOrderRecursive(Node root) {
if (root != null) {
System.out.print(root.data + " ");
preOrderRecursive(root.left);
preOrderRecursive(root.right);
}
}
public void preorderIteration(Node root) {
Stack<Node> s = new Stack<Node>();
while (true) {
// First print the root node and then add left node
while (root != null) {
System.out.print(root.data + " ");
s.push(root);
root = root.left;
}
// check if Stack is emtpy, if yes, exit from everywhere
if (s.isEmpty()) {
return;
}
// pop the element from the stack and go right to the tree
root = s.pop();
root = root.right;
}
}
public static void main(String[] args) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(5);
root.left.right = new Node(15);
PreOrderTree i = new PreOrderTree();
i.preOrderRecursive(root);
System.out.println();
i.preorderIteration(root);
}
}
class Node {
int data;
Node left;
Node right;
public Node(int data) {
this.data = data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment