Skip to content

Commit 054ab88

Browse files
author
Gaurav Sonwani
committed
Added preOrder and postOrder Traversal of tree
1 parent ec9d672 commit 054ab88

File tree

1 file changed

+112
-4
lines changed

1 file changed

+112
-4
lines changed

Tree/BinaryTree.java

+112-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package Tree;
22

3-
import java.util.LinkedList;
4-
import java.util.Queue;
53
import java.util.Stack;
64

75
public class BinaryTree {
@@ -74,6 +72,101 @@ public void inOrderMorrisTraversal(BinaryTreeNode root) {
7472
}
7573
}
7674

75+
/* Recursive approach of preOrder Traversal of Tree
76+
* Time Complexity: O(n)
77+
* Space Complexity: O(n) (implicit)
78+
*/
79+
public void preOrder(BinaryTreeNode root) {
80+
if (root == null) return;
81+
System.out.print(root.data + " ");
82+
preOrder(root.left);
83+
preOrder(root.right);
84+
}
85+
86+
/* preOrder Traversal of Tree with using any extra space, Morris Traversal
87+
* Time Complexity: O(n)
88+
* Space Complexity: O(1)
89+
*/
90+
public void preOrderMorrisTraversal(BinaryTreeNode root) {
91+
while (root != null) {
92+
if (root.left == null) {
93+
System.out.print(root.data + " ");
94+
root = root.right;
95+
} else {
96+
BinaryTreeNode prev = root.left;
97+
while (prev.right != null && prev.right != root)
98+
prev = prev.right;
99+
if (prev.right == root) {
100+
prev.right = null;
101+
root = root.right;
102+
} else {
103+
System.out.print(root.data + " ");
104+
prev.right = root;
105+
root = root.left;
106+
}
107+
}
108+
}
109+
}
110+
111+
/* Recursive approach of postOrder Traversal of Tree
112+
* Time Complexity: O(n)
113+
* Space Complexity: O(n) (implicit)
114+
*/
115+
public void postOrder(BinaryTreeNode root) {
116+
if (root == null) return;
117+
postOrder(root.left);
118+
postOrder(root.right);
119+
System.out.print(root.data + " ");
120+
}
121+
122+
/* Iterative approach of psotOrder Traversal of Tree using two stack
123+
* Time Complexity: O(n)
124+
* Space Complexity: O(n) (explicit)
125+
*/
126+
public void iterativePostOrderTraversalTwoStack(BinaryTreeNode rootref) {
127+
if (rootref == null) return;
128+
Stack<BinaryTreeNode> stack1 = new Stack<BinaryTreeNode>();
129+
Stack<BinaryTreeNode> stack2 = new Stack<BinaryTreeNode>();
130+
stack1.push(rootref);
131+
while (!stack1.empty()) {
132+
BinaryTreeNode temp = stack1.pop();
133+
stack2.push(temp);
134+
if (temp.left != null) stack1.push(temp.left);
135+
if (temp.right != null) stack1.push(temp.right);
136+
}
137+
while (!stack2.empty()) {
138+
System.out.print(stack2.pop().data + " ");
139+
}
140+
}
141+
142+
/* Iterative approach of psotOrder Traversal of Tree using one stack
143+
* Time Complexity: O(n)
144+
* Space Complexity: O(n) (explicit)
145+
*/
146+
public void iterativePostOrderTraversalOneStack(BinaryTreeNode rootref) {
147+
if (rootref == null) return;
148+
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
149+
stack.push(rootref);
150+
BinaryTreeNode prev = rootref;
151+
while (!stack.empty()) {
152+
BinaryTreeNode top = stack.peek();
153+
if (top.left == null && top.right == null ||
154+
top.left == prev || top.right == prev) {
155+
prev = stack.pop();
156+
System.out.print(prev.data + " ");
157+
158+
} else if (top.left != null && top.right != null &&
159+
(top.left != prev || top.right != prev)) {
160+
stack.push(top.right);
161+
stack.push(top.left);
162+
} else if (top.right != null && top.right != prev) {
163+
stack.push(top.right);
164+
} else if (top.left != null && top.left != prev) {
165+
stack.push(top.left);
166+
}
167+
}
168+
}
169+
77170
public static void main(String[] args) {
78171
BinaryTree binaryTree = new BinaryTree();
79172
binaryTree.root = new BinaryTreeNode(1);
@@ -86,13 +179,28 @@ public static void main(String[] args) {
86179
binaryTree.root.left.right.left = new BinaryTreeNode(8);
87180
binaryTree.root.right.right.left = new BinaryTreeNode(9);
88181

89-
System.out.println("\nInOrder");
182+
System.out.println("\nInOrderRecursively");
90183
binaryTree.inOrder(binaryTree.root);
91184

92-
System.out.println("\nInOrderWithoutRecurion");
185+
System.out.println("\nInOrderWithoutRecursion");
93186
binaryTree.inOrderWithoutRecursion(binaryTree.root);
187+
94188
System.out.println("\nInOrderMorrisTraversal");
95189
binaryTree.inOrderMorrisTraversal(binaryTree.root);
96190

191+
System.out.println("\nPreOrderRecursively");
192+
binaryTree.preOrder(binaryTree.root);
193+
194+
System.out.println("\npreOrderMorrisTraversal");
195+
binaryTree.preOrderMorrisTraversal(binaryTree.root);
196+
197+
System.out.println("\npostOrderRecursively");
198+
binaryTree.postOrder(binaryTree.root);
199+
200+
System.out.println("\niterativePostOrderTraversalOneStack");
201+
binaryTree.iterativePostOrderTraversalOneStack(binaryTree.root);
202+
203+
System.out.println("\niterativePostOrderTraversalOneStack");
204+
binaryTree.iterativePostOrderTraversalOneStack(binaryTree.root);
97205
}
98206
}

0 commit comments

Comments
 (0)