forked from maverickiam/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IterativeTreeTraversal_JAVA_1.java
76 lines (57 loc) · 1.84 KB
/
IterativeTreeTraversal_JAVA_1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// ? ============= USING HASH-MAP =============
import java.util.*;
// ! NOTE : while using this don'toverride the equals()&hashCode()method in Node Class
class Node {
int val;
Node left, right;
Node(int val) {
this.val = val;
left = right = null;
}
}
class IterativeMagic {
public static void traversalTrick(Node root) { // postorder
HashMap<Node, Integer> state = new HashMap<Node, Integer>();
Stack<Node> s = new Stack<Node>();
s.push(root);
while (!s.empty()) {
Node curNode = s.peek();
if (curNode == null) {
s.pop();
continue;
}
int curState;
if (!state.containsKey(curNode))// if no state exits till now - give it a state 0
state.put(curNode, 0);
curState = state.get(curNode);
switch (curState) {
case 0:
s.push(curNode.left);
break;
case 1:
s.push(curNode.right);
break;
case 2:
System.out.print(curNode.val + " ");
break;
default:
s.pop();
}
state.put(curNode, curState + 1); // change its current state : previous state's task done
}
}
}
public class IterativeTreeTraversal_JAVA_1 {
public static void main(String... args) {
Node tmp = new Node(7);
Node root = tmp;
root.left = new Node(3);
root.right = new Node(10);
root.left.left = new Node(2);
root.left.right = new Node(5);
root.left.left.left = new Node(1);
root.right.left = new Node(8);
root.right.right = new Node(5);
IterativeMagic.traversalTrick(tmp);
}
}