-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathBinaryTree.java
488 lines (457 loc) · 10.4 KB
/
BinaryTree.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/**
* Data-Structures-in-Java
* BinaryTree.java
*/
package com.deepak.data.structures.Tree;
import java.util.LinkedList;
import java.util.Queue;
/**
* Binary Tree implementation
*
* @author Deepak
*
* @param <E>
*/
public class BinaryTree<E extends Comparable<E>> {
/* Root of tree and counter to keep track of size */
private Node<E> root;
private int size = 0;
/**
* Method to add a node to tree
*
* @param value
*/
public void addNode(E value) {
/* If size is zero, make it a root node */
if (size == 0) {
root = new Node<E>(value);
size++;
} else {
/* Add the node */
addNode(root, value);
}
}
/**
* Method to add the node in a tree
*
* @param rootNode
* @param value
* @return {@link Node}
*/
private Node<E> addNode(Node<E> rootNode, E value) {
/* If root is null, return null */
if (rootNode == null) {
return null;
}
/* Create a new node */
Node<E> newNode = new Node<E>(value);
/* Compare with the root, if small, insert left else insert right */
if ((newNode.data).compareTo(rootNode.data) <= 0) {
if (rootNode.left != null) {
rootNode.left = addNode(rootNode.left, value);
} else {
rootNode.left = newNode;
}
} else {
if (rootNode.right != null) {
rootNode.right = addNode(rootNode.right, value);
} else {
rootNode.right = newNode;
}
}
/* Increase the size and return rootNode */
size++;
return rootNode;
}
/**
* Method to find the root of the tree
*
* @return {@link Node}
*/
public Node<E> root() {
/* If tree is empty, return null
* else return return root node */
if (isEmpty()) {
return null;
} else {
return root;
}
}
/**
* Method to check if tree is empty
*
* @return {@link boolean}
*/
public boolean isEmpty() {
return size() == 0;
}
/**
* Method to check size of tree
*
* @return {@link int}
*/
public int size() {
return size(root);
}
/**
* Find the size of the tree from a given node
*
* @param root
* @return {@link int}
*/
private int size(Node<E> root) {
/* If root is null, return 0 else
* size of left subtree + size of right subtree
* + 1 for root */
if (root == null) {
return 0;
} else {
return (size(root.left)) + 1 + (size(root.right));
}
}
/**
* Method to check if given node is root
*
* @param node
* @return {@link boolean}
*/
public boolean isRoot(Node<E> node) {
/* If given node is root, return true */
if (root == node) {
return true;
}
return false;
}
/**
* Method to find the parent of a given node
*
* @param node
* @return {@link Node}
*/
public Node<E> findParent(Node<E> node) {
return findParent(node.data, root, null);
}
/**
* Method to find the parent of a given node
*
* @param data
* @param root
* @param parent
* @return {@link Node}
*/
private Node<E> findParent(E data, Node<E> root, Node<E> parent) {
/* If root is null, there is no parent */
if (root == null) {
return null;
}
/* Compare the root with the data. If they are not equal,
* first find parent in left subtree, if still parent is not found
* check right subtree */
if ((root.data).compareTo(data) != 0) {
parent = findParent(data, root.left, root);
if (parent == null) {
parent = findParent(data, root.right, root);
}
}
return parent;
}
/**
* Method to check if a given node has parent
*
* @param node
* @return {@link boolean}
*/
public boolean hasParent(Node<E> node) {
/* If parent is not null, exits */
return findParent(node) != null;
}
/**
* Method to check if a given node has left node
*
* @param node
* @return {@link boolean}
*/
public boolean hasLeft(Node<E> node) {
if (node.left != null) {
return true;
}
return false;
}
/**
* Method to find the left node of a given node
*
* @param node
* @return {@link Node}
*/
public Node<E> left(Node<E> node) {
/* If left exists, then return that */
if (hasLeft(node)) {
return node.left;
}
return null;
}
/**
* Method to check if right node exits for a given node
*
* @param node
* @return {@link boolean}
*/
public boolean hasRight(Node<E> node) {
if (node.right != null) {
return true;
}
return false;
}
/**
* Method to find the right node for a given node
*
* @param node
* @return {@link Node}
*/
public Node<E> right(Node<E> node) {
if (hasRight(node)) {
return node.right;
}
return null;
}
/**
* Method to check if a given node is a leaf
*
* @param node
* @return {@link boolean}
*/
public boolean isLeaf(Node<E> node) {
/* If no left or right child, its a leaf node */
return !hasLeft(node) && !hasRight(node);
}
/**
* Method to get depth of the tree
* This is considered as Level as well
*
* @param node
* @return {@link int}
*/
public int getDepth(Node<E> node) {
/* If given node is null, size is zero */
if (node == null) {
return 0;
}
/* Get the depth of left and right.
* Whichever is greater, return that and add
* one for root */
int left = getDepth(node.left);
int right = getDepth(node.right);
return left > right ? left + 1 : right + 1;
}
/**
* Method to print the tree InOrder
* InOrder : Left -> Root -> Right
*
* @param node
*/
public void printInOrder(Node<E> node) {
/* If root is null, return else print
* in order specified above */
if (node == null) {
return;
}
printInOrder(node.left);
System.out.print(node.data + " ");
printInOrder(node.right);
}
/**
* Method to print the tree PreOrder
* PreOrder : Root -> Left -> Right
*
* @param node
*/
public void printPreorder(Node<E> node) {
/* If root is null, return else print
* in order specified above */
if (node == null) {
return;
}
System.out.print(node.data + " ");
printPreorder(node.left);
printPreorder(node.right);
}
/**
* Method to print the tree PostOrder
* PostOrder : Left -> Right -> Root
*
* @param node
*/
public void printPostorder(Node<E> node) {
/* If root is null, return else print
* in order specified above */
if (node == null) {
return;
}
printPostorder(node.left);
printPostorder(node.right);
System.out.print(node.data + " ");
}
/**
* Method to print the tree level by level
*
* Approach => Use 2 queues, Keep polling from queue 1 and
* keep adding to queue 2. Now, for next level, keep polling
* from queue 2 and keep adding from queue 1. Stop when both
* queues are empty.
* Basically elements at any level will be in one queue
*
* @param root
*/
public void printByLevelUsingTwoQueues(Node<E> root) {
/* If root is null, return */
if (root == null) {
return;
}
/* Define two queues and add root to queue 1 */
Queue<Node<E>> queue1 = new LinkedList<>();
Queue<Node<E>> queue2 = new LinkedList<>();
queue1.add(root);
/* Keep going until both are empty */
while (!queue1.isEmpty() || !queue2.isEmpty()) {
/* While queue 1 is not empty, keep polling and printing */
while (!queue1.isEmpty()) {
root = queue1.poll();
System.out.println(root.data + " ");
/* Add children to the other queue */
if (root.left != null) {
queue2.add(root.left);
}
if (root.right != null) {
queue2.add(root.right);
}
}
/* We are done with one level. Line space */
System.out.println();
while (!queue2.isEmpty()) {
/* Same logic as queue 1 goes with queue 2 */
root = queue2.poll();
System.out.println(root.data + " ");
if (root.left != null) {
queue1.add(root.left);
}
if (root.right != null) {
queue1.add(root.right);
}
}
/* Line space when entire tree is printed */
System.out.println();
}
}
/**
* Method to print the tree level by level
*
* Approach => Use one queue and a delimiter i.e null.
* Add delimiter after every level. As soon as you
* encounter a null, print a new line and add null
* to the end of the queue
*
* @param root
*/
public void printByLevelUsingOneQueueAndDelimiter(Node<E> root) {
/* If root is null, return */
if (root == null) {
return;
}
/* Define a queue, add root to it.
* Since root itself is a level, add null */
Queue<Node<E>> queue = new LinkedList<>();
queue.add(root);
queue.add(null);
/* Keep going until queue is empty */
while (!queue.isEmpty()) {
/* Poll the node and print its content.
* Keep adding elements from the next level */
root = queue.poll();
if (root != null) {
System.out.println(root.data + " ");
if (root.left != null) {
queue.add(root.left);
}
if (root.right != null) {
queue.add(root.right);
}
} else {
/* If we encounter null, print a new line and add null at the end */
if (!queue.isEmpty()) {
System.out.println();
queue.add(null);
}
}
}
}
/**
* Method to print the tree level by level
*
* Approach => Use one queue with count. Keep count of nodes at every level.
* As soon as this count is 0 print a new line.
*
* @param root
*/
public void printByLevelUsingOneQueueAndCounter(Node<E> root) {
/* If root is null, return */
if (root == null) {
return;
}
/* Define a queue and add root to it */
Queue<Node<E>> queue = new LinkedList<>();
queue.add(root);
/* Keep track of level and elements in level */
int levelCount = 1;
int currentCount = 0;
/* Keep going until queue is empty */
while (!queue.isEmpty()) {
while (levelCount > 0) {
/* Get the data, print the content and add children.
* Increase the element counter and decrease the level counter */
root = queue.poll();
System.out.println(root.data + " ");
if (root.left != null) {
currentCount++;
queue.add(root.left);
}
if (root.right != null) {
currentCount++;
queue.add(root.right);
}
levelCount--;
}
/* Print new line, make level counter to current counter
* and set current counter to 0 */
System.out.println();
levelCount = currentCount;
currentCount = 0;
}
}
/**
* Tree node class
*
* @author Deepak
*
* @param <T>
*/
public class Node<T> {
/* Left child, right child and data */
private Node<T> left;
private Node<T> right;
private T data;
/**
* Constructor to create a tree node
*
* @param data
*/
public Node(T data) {
this.data = data;
this.left = null;
this.right = null;
}
@Override
public String toString() {
return "Node [data=" + data + "]";
}
}
}