-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathBinaryTree.java
More file actions
206 lines (183 loc) · 6.94 KB
/
BinaryTree.java
File metadata and controls
206 lines (183 loc) · 6.94 KB
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
package Tree;
import java.util.Stack;
public class BinaryTree {
BinaryTreeNode root;
BinaryTree() {
}
BinaryTree(int d) {
root = new BinaryTreeNode(d);
}
/* Recursive approach of inOrder Traversal of Tree
* Time Complexity: O(n)
* Space Complexity: O(n) (implicit)
*/
public void inOrder(BinaryTreeNode root) {
if (root == null) return;
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
/* Iterative approach of inOrder Traversal of Tree
* Time Complexity: O(n)
* Space Complexity: O(n) (explicit)
*/
public void inOrderWithoutRecursion(BinaryTreeNode root) {
if (root == null) return;
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
while (root != null) {
stack.push(root);
root = root.left;
}
while (!stack.empty()) {
BinaryTreeNode temp = stack.pop();
System.out.print(temp.data + " ");
if (temp.right != null)
root = temp.right;
while (root != null) {
stack.push(root);
root = root.left;
}
}
}
/* inOrder Traversal of Tree with using any extra space, Morris Traversal
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
public void inOrderMorrisTraversal(BinaryTreeNode root) {
while (root != null) {
if (root.left == null) {
System.out.print(root.data + " ");
root = root.right;
} else {
BinaryTreeNode prev = root.left;
while (prev.right != null && prev.right != root)
prev = prev.right;
if (prev.right == null) {
prev.right = root;
root = root.left;
} else {
prev.right = null;
System.out.print(root.data + " ");
root = root.right;
}
}
}
}
/* Recursive approach of preOrder Traversal of Tree
* Time Complexity: O(n)
* Space Complexity: O(n) (implicit)
*/
public void preOrder(BinaryTreeNode root) {
if (root == null) return;
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
/* preOrder Traversal of Tree with using any extra space, Morris Traversal
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
public void preOrderMorrisTraversal(BinaryTreeNode root) {
while (root != null) {
if (root.left == null) {
System.out.print(root.data + " ");
root = root.right;
} else {
BinaryTreeNode prev = root.left;
while (prev.right != null && prev.right != root)
prev = prev.right;
if (prev.right == root) {
prev.right = null;
root = root.right;
} else {
System.out.print(root.data + " ");
prev.right = root;
root = root.left;
}
}
}
}
/* Recursive approach of postOrder Traversal of Tree
* Time Complexity: O(n)
* Space Complexity: O(n) (implicit)
*/
public void postOrder(BinaryTreeNode root) {
if (root == null) return;
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data + " ");
}
/* Iterative approach of psotOrder Traversal of Tree using two stack
* Time Complexity: O(n)
* Space Complexity: O(n) (explicit)
*/
public void iterativePostOrderTraversalTwoStack(BinaryTreeNode rootref) {
if (rootref == null) return;
Stack<BinaryTreeNode> stack1 = new Stack<BinaryTreeNode>();
Stack<BinaryTreeNode> stack2 = new Stack<BinaryTreeNode>();
stack1.push(rootref);
while (!stack1.empty()) {
BinaryTreeNode temp = stack1.pop();
stack2.push(temp);
if (temp.left != null) stack1.push(temp.left);
if (temp.right != null) stack1.push(temp.right);
}
while (!stack2.empty()) {
System.out.print(stack2.pop().data + " ");
}
}
/* Iterative approach of psotOrder Traversal of Tree using one stack
* Time Complexity: O(n)
* Space Complexity: O(n) (explicit)
*/
public void iterativePostOrderTraversalOneStack(BinaryTreeNode rootref) {
if (rootref == null) return;
Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
stack.push(rootref);
BinaryTreeNode prev = rootref;
while (!stack.empty()) {
BinaryTreeNode top = stack.peek();
if (top.left == null && top.right == null ||
top.left == prev || top.right == prev) {
prev = stack.pop();
System.out.print(prev.data + " ");
} else if (top.left != null && top.right != null &&
(top.left != prev || top.right != prev)) {
stack.push(top.right);
stack.push(top.left);
} else if (top.right != null && top.right != prev) {
stack.push(top.right);
} else if (top.left != null && top.left != prev) {
stack.push(top.left);
}
}
}
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
binaryTree.root = new BinaryTreeNode(1);
binaryTree.root.left = new BinaryTreeNode(2);
binaryTree.root.right = new BinaryTreeNode(3);
binaryTree.root.left.left = new BinaryTreeNode(4);
binaryTree.root.left.right = new BinaryTreeNode(5);
binaryTree.root.right.right = new BinaryTreeNode(6);
binaryTree.root.left.left.right = new BinaryTreeNode(7);
binaryTree.root.left.right.left = new BinaryTreeNode(8);
binaryTree.root.right.right.left = new BinaryTreeNode(9);
System.out.println("\nInOrderRecursively");
binaryTree.inOrder(binaryTree.root);
System.out.println("\nInOrderWithoutRecursion");
binaryTree.inOrderWithoutRecursion(binaryTree.root);
System.out.println("\nInOrderMorrisTraversal");
binaryTree.inOrderMorrisTraversal(binaryTree.root);
System.out.println("\nPreOrderRecursively");
binaryTree.preOrder(binaryTree.root);
System.out.println("\npreOrderMorrisTraversal");
binaryTree.preOrderMorrisTraversal(binaryTree.root);
System.out.println("\npostOrderRecursively");
binaryTree.postOrder(binaryTree.root);
System.out.println("\niterativePostOrderTraversalOneStack");
binaryTree.iterativePostOrderTraversalOneStack(binaryTree.root);
System.out.println("\niterativePostOrderTraversalOneStack");
binaryTree.iterativePostOrderTraversalOneStack(binaryTree.root);
}
}