-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreesGraphs.java
396 lines (323 loc) · 11.9 KB
/
TreesGraphs.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import java.util.*;
import java.util.LinkedList;
public class TreesGraphs {
public static void main(String[] args) {
BST tree = new BST(20);
tree.insert(8);
tree.insert(22);
tree.insert(4);
tree.insert(12);
tree.insert(10);
tree.insert(14);
System.out.println(tree.getHeight(tree.root));
}
public static class BST {
private TreeNode root;
public BST() {
root = null;
}
public BST(int value) {
root = new TreeNode(value);
}
public void insert(int value) {
TreeNode newNode = new TreeNode(value);
if (root == null) root = newNode;
else {
root.insert(value);
}
}
public boolean search(int id) {
TreeNode current = root;
while (current != null) {
if (current.data == id) {
return true;
} else if (current.data > id) {
current = current.left;
} else {
current = current.right;
}
}
return false;
}
public void inOrderTraversal(TreeNode node) {
if (node != null) {
inOrderTraversal(node.left);
System.out.print(node.data + " ");
inOrderTraversal(node.right);
}
}
public void preOrderTraversal(TreeNode node) {
if (node != null) {
System.out.print(node.data + " ");
preOrderTraversal(node.left);
preOrderTraversal(node.right);
}
}
public void postOrderTraversal(TreeNode node) {
if (node != null) {
postOrderTraversal(node.left);
postOrderTraversal(node.right);
System.out.print(node.data + " ");
}
}
public int getHeight(TreeNode node) {
if (node == null) return 0;
return Math.max(getHeight(node.left), getHeight(node.right)) + 1;
}
//lowest common ancestor for binary tree
public static TreeNode LCA(TreeNode n1, TreeNode n2) {
HashMap<TreeNode, Boolean> ancestors = new HashMap<>();
while (n1 != null) {
ancestors.put(n2, Boolean.TRUE);
n1 = n1.parent;
}
while (n2 != null) {
if (ancestors.containsKey(n2) != ancestors.isEmpty()) return n2;
n2 = n2.parent;
}
return null;
}
public static int findLevel(TreeNode root, TreeNode node) {
if (root == null) return -1;
if (root.data == node.data) return 0;
int level = findLevel(root.left, node);
if (level == -1)
level = findLevel(root.right, node);
if (level != -1)
return level + 1;
return -1;
}
public int distance(TreeNode n1, TreeNode n2) {
TreeNode node = LCA(n1, n2);
int distLCA = findLevel(this.root, node);
int dist1 = findLevel(this.root, n1);
int dist2 = findLevel(this.root, n2);
return dist1 + dist2 - 2 * distLCA;
}
//lowest common ancestor for binary search tree
public TreeNode LCA_BST(TreeNode node, int val1, int val2) {
if (node == null) return null;
if (node.data > val1 && node.data > val2) {
return LCA_BST(node.left, val1, val2);
}
if (node.data < val1 && node.data < val2) {
return LCA_BST(node.right, val1, val2);
}
return node;
}
//4.1 Check if tree is balanced
//O(N) time and O(H) space, H = height
public int checkHeight(TreeNode root) {
if (root == null) return 0; //base case
//check if left is balanced
int lHeight = checkHeight(root.left);
if (lHeight == -1) return -1;
//check if right is balanced
int rHeight = checkHeight(root.right);
if (rHeight == -1) return -1;
//check if current node is balanced
int diff = lHeight - rHeight;
if (Math.abs(diff) > 1) return -1;
else return Math.max(lHeight, rHeight) + 1; //return height
}
public boolean isBalanced() {
return checkHeight(this.root) != -1;
}
//4.3
//insert middle
//insert into the left tree the left sub array
//insert into the right ...
//recurse
public TreeNode createMinBST(int arr[], int start, int end) {
if (end < start) return null;
int mid = (start + end) / 2;
TreeNode newNode = new TreeNode(arr[mid]);
newNode.left = createMinBST(arr, start, mid - 1);
newNode.right = createMinBST(arr, mid + 1, end);
return newNode;
}
public TreeNode createMinBST(int arr[]) {
return createMinBST(arr, 0, arr.length - 1);
}
//4.4
//DFS: recursive O(N) time O(logn) space
public void createLevelLinkedList(TreeNode root, ArrayList<LinkedList<TreeNode>> lists, int level) {
if (root == null) return;
LinkedList<TreeNode> list;
if (lists.size() == level) {
list = new LinkedList<>();
lists.add(list);
} else list = lists.get(level);
list.add(root);
createLevelLinkedList(root.left, lists, level + 1);
createLevelLinkedList(root.right, lists, level + 1);
}
public ArrayList<LinkedList<TreeNode>> createLevelLinkedList() {
ArrayList<LinkedList<TreeNode>> lists = new ArrayList<>();
createLevelLinkedList(this.root, lists, 0);
return lists;
}
//4.4
//BSF iterative O(N) time O(1) space
public ArrayList<LinkedList<TreeNode>> createLevelLinkedList_BFS(TreeNode root) {
ArrayList<LinkedList<TreeNode>> result = new ArrayList<>();
LinkedList<TreeNode> current = new LinkedList<>();
if (root != null) current.add(root);
while (current.size() > 0) {
result.add(current);
LinkedList<TreeNode> parents = current;
current = new LinkedList<>();
for (TreeNode node : parents) {
if (node.left != null) current.add(node.left);
if (node.right != null) current.add(node.right);
}
}
return result;
}
//4.5
//validate BST O(N) time O(logN) space
public boolean validateBST() {
return validateBST(this.root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
private boolean validateBST(TreeNode n, int min, int max) {
if (n == null) return true;
if (n.data <= min || n.data > max) return false;
return (validateBST(n.left, min, n.data) || validateBST(n.right, n.data, max));
}
//4.6
//In order successor
public TreeNode inOrderSucc(TreeNode n) {
if (n == null) return null;
if (n.right != null) return leftMostChild(n.right);
else {
TreeNode q = n;
TreeNode x = q.parent;
while (x != null && x.left != q) {
q = x;
x = x.parent;
}
return x;
}
}
public TreeNode leftMostChild(TreeNode n) {
if (n == null) return null;
while (n.left != null) {
n = n.left;
}
return n;
}
//4.8
//search t1 tree for node that matches t2 root
//compare from that node to see if t1 matches t2
//O(mn) m & n are the number of nodes in each tree
//or O(m + kn) where k is the number of matching nodes
//O(logm + logn) space
public boolean containsTree(TreeNode t1, TreeNode t2) {
if (t2 == null) return true;
return subTree(t1, t2);
}
private boolean subTree(TreeNode r1, TreeNode r2) {
if (r1 == null) return false;
if (r1.data == r2.data) {
if (matchTree(r1, r2)) return true;
}
return (subTree(r1.left, r2) || subTree(r1.right, r2));
}
private boolean matchTree(TreeNode r1, TreeNode r2) {
if (r2 == null && r1 == null) return true;
if (r1 == null || r2 == null) return false;
if (r1.data != r2.data) return false;
return (matchTree(r1.left, r2.left) && matchTree(r1.right, r2.right));
}
//4.9
//O(nlogn) time O(logn) space
public void findSum(TreeNode node, int sum) {
int height = getHeight(node);
int[] path = new int[height];
findSum(node, sum, path, 0);
}
public void findSum(TreeNode node, int sum, int[] path, int level) {
if (node == null) return;
//insert current node into path
path[level] = node.data;
//look for paths with a sum that ends at this node
int t = 0;
for (int i = level; i >= 0; i--) {
t += path[i];
if (t == sum) {
printPath(path, i, level);
}
}
//search nodes beneath this one
findSum(node.left, sum, path, level + 1);
findSum(node.right, sum, path, level + 1);
//remove current node from path
path[level] = Integer.MIN_VALUE;
}
public static void printPath(int[] path, int start, int end) {
for (int i = start; i <= end; i++) {
System.out.print(path[i] + " ");
}
System.out.println();
}
public int getDepth(int key)
{
TreeNode node = this.root;
int depth = 0;
while (node != null)
{
if (node.data == key) return depth;
if (node.data > key) node = node.left;
else node = node.right;
depth++;
}
return -1;
}
}
public static void BFS(TreeNode root) {
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode current = queue.remove();
System.out.println(current.data);
if (current.left != null) queue.add(current.left);
if (current.right != null) queue.add(current.right);
}
}
//just in order traversal
public static void DFS(TreeNode current) {
if (current.left != null) DFS(current.left);
System.out.println(current.data);
if (current.right != null) DFS(current.right);
}
/* 4.2
public enum State {
UNVISITED, VISITED, VISITING;
}
public static boolean BFS(Graph graph, Node start, Node end) {
LinkedList<Node> queue = new LinkedList<>();
for (Node v : graph.getNodes()) {
v.state = State.UNVISITED;
}
start.state = State.VISITING;
queue.add(start);
Node v;
while (!queue.isEmpty()) {
v = queue.removeFirst(); //dequeue
if (v != null) {
for (Node u : v.getAdjacent()) {
if (u.state == State.UNVISITED) {
if (u == end) return true;
else {
u.state = State.VISITING;
queue.add(u);
}
}
}
v.state = State.VISITED;
}
}
return false;
}
*/
}