Created
August 3, 2019 18:47
-
-
Save ajinkyajawale14499/402f883104065192cf6f2ba40e102b99 to your computer and use it in GitHub Desktop.
preorder tree traversal
This file contains hidden or 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 PostorderTree { | |
public void postOrderRecursive(Node root) { | |
if (root != null) { | |
postOrderRecursive(root.left); | |
postOrderRecursive(root.right); | |
System.out.print(root.data + " "); | |
} | |
} | |
public void preorderIteration(Node root) { | |
Stack<Node> s1 = new Stack<Node>(); | |
Stack<Node> s2 = new Stack<Node>(); | |
// push the root node into first stack. | |
s1.push(root); | |
while (s1.isEmpty() == false) { | |
// take out the root and insert into second stack. | |
Node temp = s1.pop(); | |
s2.push(temp); | |
// now we have the root, push the left and right child of root into | |
// the first stack. | |
if(temp.left!=null){ | |
s1.push(temp.left); | |
} | |
if(temp.right!=null){ | |
s1.push(temp.right); | |
} | |
} | |
//once the all node are traversed, take out the nodes from second stack and print it. | |
System.out.println("Preorder Traversal: "); | |
while(s2.isEmpty()==false){ | |
System.out.print(s2.pop()); | |
} | |
} | |
public static void main(String[] args) { | |
Node root = new Node(1); | |
root.left = new Node(2); | |
root.right = new Node(3); | |
root.left.left = new Node(4); | |
root.left.right = new Node(5); | |
root.right.left = new Node(6); | |
root.right.right = new Node(7); | |
PostorderTree i = new PostorderTree(); | |
i.postOrderRecursive(root); | |
System.out.println(); | |
i.postOrderRecursive(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