-
Notifications
You must be signed in to change notification settings - Fork 13
/
Tree_BinaryTreeInOrderTraversal.java
211 lines (168 loc) · 6.2 KB
/
Tree_BinaryTreeInOrderTraversal.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
// https://practice.geeksforgeeks.org/problems/inorder-traversal/1
// https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/
// https://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/
// https://github.com/mission-peace/interview/blob/master/src/com/interview/tree/TreeTraversals.java
// https://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/
// https://www.youtube.com/watch?v=VsxLHGUqAKs&feature=emb_logo
// https://www.youtube.com/watch?v=nzmtCFNae9k&t=200s (GOOD Video Example)
// https://www.youtube.com/watch?v=wGXB9OWhPTg (BEST Video in Morris Traversal)
// https://leetcode.com/problems/binary-tree-inorder-traversal/solution/ (A good post which discusses all 3 approaches)
public class Tree_BinaryTreeInOrderTraversal {
static class Tree {
/* recursive inorder traversal with return type as list */
ArrayList<Integer> inOrder(Node root) {
ArrayList<Integer> op = new ArrayList<>();
inorder(root, op);
return op;
}
private void inorder(Node root, ArrayList<Integer> op) {
if (root == null) return;
inorder(root.left, op);
op.add(root.data);
inorder(root.right, op);
}
/* recursive inorder traversal with no return type */
void printInOrderRecursive(Node root) {
if (root == null) return;
printInOrderRecursive(root.left);
System.out.print(root.data+ " ");
printInOrderRecursive(root.right);
}
/* non recursive inorder traversal using stack
* time complexity : O(N)
* space complexity : O(N) */
void printInOrderNonRecursive(Node root) {
if (root == null) return;
StringBuilder sb = new StringBuilder();
Stack<Node> stack = new Stack<>();
Node currNode = root;
while (currNode != null || !stack.isEmpty()) { /* when currNode is null and stack is also empty, then traversing is complete */
while (currNode != null) { /* until currNode is null, push that node to stack and go deeper in left side */
stack.push(currNode);
currNode = currNode.left;
}
/* at this point, currNode must be null */
currNode = stack.pop();
sb.append(currNode.data).append(" ");
/* we have visited the node and its left subtree. Now, it's right subtree's turn */
currNode = currNode.right;
}
System.out.println(sb);
sb.setLength(0);
}
// https://www.youtube.com/watch?v=wGXB9OWhPTg
/* why we need a stack or recursion :
* when we are visiting from current node tp the left side,
* we need a way to comeback to the current node after left side is already visited
* this is wy we store the current node first at stack and then visit the left child */
/* Morris Traversal : which uses neither recursion nor stack
* space complexity : O(N)
* time complexity : O(1) */
void printInOrderUsingMorrisTraversal(Node root) {
StringBuilder sb = new StringBuilder();
Node currNode = root;
while (currNode != null) {
if (currNode.left == null) {
/* if current node's left is null, then there is nothing
* explore on the left side,so we print the current and
* go deeper in the right side */
sb.append(currNode.data).append(" ");
currNode = currNode.right;
}
else {
/* if a current node has left child,
* find the inorder predecessor of that node
*
* inorder predecessor is the right most node in the left subtree
* */
Node inOrderPredecessor = currNode.left;
/* to find predecessor, keep going right till right node is not null
* or right node is not current */
while (inOrderPredecessor.right != currNode && inOrderPredecessor.right != null) {
inOrderPredecessor = inOrderPredecessor.right;
}
/* after the above while loop is end, then either
* predecessor's right is current or predecessor's right will be null */
if (inOrderPredecessor.right == null) {
/* if predecessor's right is null that means we have not been in
* the left side we should visit on the left child.
* before visiting to the left child, we establish the link between
* inOrderPredecessor's right and currNode */
inOrderPredecessor.right = currNode;
currNode = currNode.left;
}
else {
/* if predecessor's right is current it means that we've already visited the left side
* so we break the link between inOrderPredecessor and current node, then print the current data
* and then go deeper on the right side */
inOrderPredecessor.right = null;
sb.append(currNode.data).append(" ");
currNode = currNode.right;
}
}
}
System.out.println(sb);
sb.setLength(0);
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-- > 0) {
String s = br.readLine().trim();
Node root = buildTree(s);
Tree tree = new Tree();
System.out.println("Recursive : ");
tree.printInOrderRecursive(root);
System.out.println();
System.out.println("Non recursive : ");
tree.printInOrderNonRecursive(root);
System.out.println("Using Morris traversal : ");
tree.printInOrderUsingMorrisTraversal(root);
}
}
private static Node buildTree(String str) {
if(str.length()==0 || str.charAt(0)=='N') {
return null;
}
String[] ip = str.split("\\s+");
Node root = new Node(Integer.parseInt(ip[0]));
Queue<Node> queue = new LinkedList<>();
queue.add(root);
int i = 1;
while(!queue.isEmpty() && i < ip.length) {
Node currNode = queue.peek();
queue.remove();
String currVal = ip[i];
if (!currVal.equals("N")) {
currNode.left = new Node(Integer.parseInt(currVal));
queue.add(currNode.left);
}
i++;
if(i >= ip.length) break;
currVal = ip[i];
if(!currVal.equals("N")) {
currNode.right = new Node(Integer.parseInt(currVal));
queue.add(currNode.right);
}
i++;
}
return root;
}
static class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
left = right = null;
}
}
}