-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinTrees.java
More file actions
339 lines (312 loc) · 11 KB
/
BinTrees.java
File metadata and controls
339 lines (312 loc) · 11 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import java.util.*;
public class BinTrees {
static class Node {
int data;
Node left, right;
Node(int data) {
this.data = data;
left = right = null;
}
}
static class BinaryTree {
static int idx = -1;
public Node buildTree(int nodes[]) { // O(n)
idx++;
if (nodes[idx] == -1) {
return null;
}
Node newNode = new Node(nodes[idx]);
newNode.left = buildTree(nodes);
newNode.right = buildTree(nodes);
return newNode;
}
public void preorder(Node root) { // O(n)
if (root == null) {
System.out.print(" -1 ");
return;
}
System.out.print(root.data + " ");
preorder(root.left);
preorder(root.right);
}
public void inorder(Node root) { // O(n)
if (root == null) {
System.out.print(" -1 ");
return;
}
inorder(root.left);
System.out.print(root.data + " ");
inorder(root.right);
}
public void postorder(Node root) { // O(n)
if (root == null) {
System.out.print(" -1 ");
return;
}
postorder(root.left);
postorder(root.right);
System.out.print(root.data + " ");
}
public void levelOrder(Node root) { // O(n)
if (root == null) {
return;
}
Queue<Node> queue = new java.util.LinkedList<>();
queue.add(root);
queue.add(null); // Level delimiter
while (!queue.isEmpty()) {
Node currNode = queue.remove();
if (currNode == null) {
System.out.println(); // End of current level
if (queue.isEmpty()) {
break;
} else {
queue.add(null); // Add level delimiter for next level
}
} else {
System.out.print(currNode.data + " ");
if (currNode.left != null) {
queue.add(currNode.left);
}
if (currNode.right != null) {
queue.add(currNode.right);
}
}
}
}
public int height(Node root) { // O(n)
if (root == null) {
return 0;
}
int leftHeight = height(root.left);
int rightHeight = height(root.right);
return Math.max(leftHeight, rightHeight) + 1;
}
public int countNodes(Node root) { // O(n)
if (root == null) {
return 0;
}
int leftCount = countNodes(root.left);
int rightCount = countNodes(root.right);
return leftCount + rightCount + 1;
}
public int sumNodes(Node root) { // O(n)
if (root == null) {
return 0;
}
int leftSum = sumNodes(root.left);
int rightSum = sumNodes(root.right);
return leftSum + rightSum + root.data;
}
public int diameter(Node root) { // O(n^2)
if (root == null) {
return 0;
}
int leftDiameter = diameter(root.left);
int rightDiameter = diameter(root.right);
int leftHeight = height(root.left);
int rightHeight = height(root.right);
int selfDiameter = leftHeight + rightHeight + 1;
return Math.max(selfDiameter, Math.max(leftDiameter, rightDiameter));
}
static class TreeInfo {
int height;
int diameter;
TreeInfo(int height, int diameter) {
this.height = height;
this.diameter = diameter;
}
}
public TreeInfo diameterOptimized(Node root) { // O(n)
if (root == null) {
return new TreeInfo(0, 0);
}
TreeInfo left = diameterOptimized(root.left);
TreeInfo right = diameterOptimized(root.right);
int height = Math.max(left.height, right.height) + 1;
int selfDiameter = left.height + right.height + 1;
int diameter = Math.max(selfDiameter, Math.max(left.diameter, right.diameter));
return new TreeInfo(height, diameter);
}
public static boolean isIdentical(Node root, Node subRoot) { // O(m)
if (root == null && subRoot == null) {
return true;
}
if (root == null || subRoot == null) {
return false;
}
if (root.data == subRoot.data) {
return isIdentical(root.left, subRoot.left) && isIdentical(root.right, subRoot.right);
}
return false;
}
public static boolean isSubtree(Node root, Node subRoot) { // O(n*m)
if (root == null) {
return false;
}
if (root.data == subRoot.data) {
if (isIdentical(root, subRoot)) {
return true;
}
}
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
}
static class HDNode {
Node node;
int hd;
public HDNode(Node node, int hd) {
this.node = node;
this.hd = hd;
}
}
public static void TopView(Node root) {
Queue<HDNode> queue = new LinkedList<>();
HashMap<Integer, Node> map = new HashMap<>();
int min = 0, max = 0;
queue.add(new HDNode(root, 0));
queue.add(null);
while (!queue.isEmpty()) {
HDNode curr = queue.remove();
if (curr == null) {
if (queue.isEmpty()) {
break;
} else {
queue.add(null);
}
} else {
if (!map.containsKey(curr.hd)) {
map.put(curr.hd, curr.node);
}
if (curr.node.left != null) {
queue.add(new HDNode(curr.node.left, curr.hd - 1));
min = Math.min(min, curr.hd - 1);
}
if (curr.node.right != null) {
queue.add(new HDNode(curr.node.right, curr.hd + 1));
max = Math.max(max, curr.hd + 1);
}
}
}
for (int i = min; i <= max; i++) {
System.out.print(map.get(i).data + " ");
}
System.out.println();
}
}
public static void KLevel(Node root, int k) {
if (root == null) {
return;
}
if (k == 0) {
System.out.print(root.data + " ");
return;
}
KLevel(root.left, k - 1);
KLevel(root.right, k - 1);
}
public static boolean getPath(Node root, int n, ArrayList<Node> path) {
if (root == null) {
return false;
}
path.add(root);
if (root.data == n) {
return true;
}
boolean foundLeft = getPath(root.left, n, path);
boolean foundRight = getPath(root.right, n, path);
if (foundLeft || foundRight) {
return true;
}
path.remove(path.size() - 1);
return false;
}
public static Node lastCommonAncestor2(Node root, int n1, int n2) { // O(n) time O(1) space
if (root == null) {
return null;
}
if (root.data == n1 || root.data == n2) {
return root;
}
Node leftLCA = lastCommonAncestor2(root.left, n1, n2);
Node rightLCA = lastCommonAncestor2(root.right, n1, n2);
if (leftLCA != null && rightLCA != null) {
return root;
}
if (leftLCA != null) {
return leftLCA;
}
return rightLCA;
}
public static void lastCommonAncestor(Node root, int n1, int n2) { // O(n) space O(n)
ArrayList<Node> path1 = new ArrayList<>();
ArrayList<Node> path2 = new ArrayList<>();
getPath(root, n1, path1);
getPath(root, n2, path2);
int i = 0;
for (; i < path1.size() && i < path2.size(); i++) {
if (path1.get(i) != path2.get(i)) {
break;
}
}
Node lca = path1.get(i - 1);
System.out.println("LCA: " + lca.data);
}
public static int distanceFromLCA(Node lca , int n , int distance){
if(lca == null){
return -1;
}
if(lca.data == n){
return distance;
}
int leftDistance = distanceFromLCA(lca.left , n , distance + 1);
if(leftDistance != -1){
return leftDistance;
}
return distanceFromLCA(lca.right , n , distance + 1);
}
public static int minDistanceNodes(Node root , int n1 , int n2){
Node lca = lastCommonAncestor2(root , n1 , n2);
int d1 = distanceFromLCA(lca , n1 , 0);
int d2 = distanceFromLCA(lca , n2 , 0);
return d1 + d2;
}
public static void KthAncestor(Node root, int n, int k) {
ArrayList<Node> path = new ArrayList<>();
getPath(root, n, path);
int index = path.size() - 1 - k;
if (index >= 0) {
System.out.println(k + "th ancestor is: " + path.get(index).data);
} else {
System.out.println("No ancestor found");
}
}
public static void main(String[] args) {
int nodes[] = { 1, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, -1, -1 };
BinaryTree tree = new BinaryTree();
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.right = new Node(6);
root.right.left = new Node(7);
BinaryTree subTree = new BinaryTree();
Node subRoot = new Node(3);
subRoot.left = new Node(7);
subRoot.right = new Node(6);
// tree.TopView(root);
// System.out.println("Is subtree: " + BinaryTree.isSubtree(root, subRoot));
// System.out.println("Height of tree: " + tree.height(root));
// System.out.println("diameter of tree: " + tree.diameter(root));
// System.out.println("diameter of tree optimized: " +
// tree.diameterOptimized(root).diameter);
// System.out.println(root.data);
// The binary tree is now built with 'root' as the root node.
// tree.preorder(root);
// tree.inorder(root);
// tree.levelOrder(root);
// KLevel(root, 2);
// System.out.println(lastCommonAncestor2(root, 4, 7).data);
System.out.println("Minimum distance between nodes: " + minDistanceNodes(root,4,6));
}
}