Created
August 20, 2015 05:43
-
-
Save thmain/2147e0ca90e7415ec625 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
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