-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTree.java
697 lines (606 loc) · 23 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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
package nonlinear;
import java.util.*;
public class BinaryTree {
Node root;
public BinaryTree(Node root){
this.root = root;
}
/**
* Prints all the values of Nodes by Iterating through full tree in a Depth First Search - In Order Traversal( Left-Root-Right)
*/
public void inOrderTraversal(){
inOrder(this.root);
}
private void inOrder(Node node){
if(node == null) return;
inOrder(node.left);
System.out.print(node.data +" ");
inOrder(node.right);
}
/**
* Prints all the values of Nodes by Iterating through full tree in a Depth First Search - Pre Order Traversal( Root-Left-Right)
*/
public void preOrderTraversal(){
preOrder(this.root);
}
private void preOrder(Node node){
if(node == null) return;
System.out.print(node.data +" ");
preOrder(node.left);
preOrder(node.right);
}
/**
* Prints all the values of Nodes by Iterating through full tree in a Depth First Search - In Order Traversal( Left-Right-Root)
*/
public void postOrderTraversal(){
postOrder(root);
}
private void postOrder(Node node){
if(node == null) return;
postOrder(node.left);
postOrder(node.right);
System.out.print(node.data +" ");
}
/**
* Prints all the nodes level by level in same line with a space
*/
public void levelOrderTraversal(){
if(root == null) return;
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
Node node = queue.poll();
System.out.print(node.data +" ");
if(node.left != null)queue.add(node.left);
if(node.right != null)queue.add(node.right);
}
}
/**
* Prints all the nodes level by level in separate lines for each level
*/
public void levelOrderTraversalLineByLine(){
if(root == null) return;
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
int size = queue.size(); // current queue size in current level
for(int i=0;i<size;i++){
Node node = queue.poll();
System.out.print(node.data +" "); // prints in same line
if(node.left != null)queue.add(node.left);
if(node.right != null)queue.add(node.right);
}
System.out.println(); // Adds a new line after a level is processed
}
}
/**
* Prints all the nodes navigating level by level in spiral form in same line starting from root and right to left in next level, left to right in next level, etc.
*/
public void levelOrderTraversalSpiralForm(){
if(root == null) return;
Deque<Node> dll = new ArrayDeque<>();
dll.addLast(root);
boolean fromRight = false;
while(!dll.isEmpty()){
int size = dll.size(); // current dll size in current level
for(int i=0;i<size;i++){
Node node = fromRight ? dll.removeLast() : dll.removeFirst();
System.out.print(node.data +" "); // prints in same line
if(fromRight){
if(node.right != null){
dll.addFirst(node.right);
}
if(node.left != null){
dll.addFirst(node.left);
}
}else{
if(node.left != null){
dll.addLast(node.left);
}
if(node.right != null){
dll.addLast(node.right);
}
}
}
fromRight = !fromRight;
}
}
/**
* Searches for a given value in the Tree by iterating all the nodes
* @param value to be searched in Tree
* @return Node which matches the given value Or null if Tree is null or not found.
* Time Complexity - O(n)
*/
public Node search(int value){
if(this.root == null) return null;
return find(value, root);
}
private Node find(int value, Node node){
if(node == null) return null;
if(node.data == value) return node;
Node foundNodeLeftSide = find(value, node.left);
if(foundNodeLeftSide != null) return foundNodeLeftSide;
Node foundNodeRightSide = find(value, node.right);
if(foundNodeRightSide != null) return foundNodeRightSide;
return null;
}
/**
* Method to calculate the height of the Tree
* @return current height of the tree
*/
public int getHeight(){
return maxHeight(root);
}
private int maxHeight(Node node){
if(node == null) return 0;
return 1 + Math.max(maxHeight(node.left), maxHeight(node.right));
}
/**
* Method to calculate the minimum value present in the Tree
* @return current minimum of the tree
* Time Complexity - O(n)
*/
public int getMin(){
if(root == null) return -1;
return getMinimumValue(root);
}
private int getMinimumValue(Node node){
if(node == null) return Integer.MAX_VALUE; // Return max value for null to ensure it doesn't affect the min comparison
int leftMin = getMinimumValue(node.left);
int rightMin = getMinimumValue(node.right);
return Math.min(node.data, Math.min(leftMin, rightMin));
}
/**
* Method to calculate the maximum value present in the Tree
* @return current maximum of the tree
* Time Complexity - O(n)
*/
public int getMax(){
if(root == null) return -1;
return getMaximumValue(root);
}
private int getMaximumValue(Node node){
if(node == null) return Integer.MIN_VALUE; // Return min value for null to ensure it doesn't affect the max comparison
int leftMax = getMaximumValue(node.left);
int rightMax = getMaximumValue(node.right);
return Math.max(node.data, Math.max(leftMax, rightMax));
}
/**
* Inserts a new node with given value at the end of the BinaryTree. Uses Level Order Traversal(BFS) and finds the next available spot to make it a complete binary tree.
* @param value
*/
public void insertLevelOrder(int value){
Node node = new Node(value);
if(root == null) {
this.root = node;
return;
}
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
Node currentNode = queue.poll();
if(currentNode.left == null){
currentNode.left = node;
return;
}else{
queue.add(currentNode.left);
}
if(currentNode.right == null){
currentNode.right = node;
return;
}else{
queue.add(currentNode.right);
}
}
}
/**
* Print the nodes at the given distance to Root, Example: level 1 prints the first level nodes which are left and right of root.
*/
public void printNodesAtLevel(int level){
if(level < 0 || root == null) return;
if(level == 0){
System.out.print(root.data);
return;
}
Queue<Node> queue = new LinkedList<>();
queue.add(root);
int currLevel = 0;
while(!queue.isEmpty()){
int size = queue.size();
for(int i=0;i<size;i++){
Node node = queue.poll();
if(level == currLevel){
System.out.print(node.data + " ");
}
if(node.left != null)queue.add(node.left);
if(node.right != null)queue.add(node.right);
}
currLevel++;
}
}
/**
* Prints left view of the BinaryTree
* - Prints the first Node at each level in separate lines
*/
public void printLeftView(){
if(root == null) return;
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
int size = queue.size();
for(int i=0;i<size;i++){
Node node = queue.poll();
if(i == 0){
System.out.print(node.data + " ");
}
if(node.left != null)queue.add(node.left);
if(node.right != null)queue.add(node.right);
}
System.out.println();
}
}
/**
* Prints right view of the BinaryTree
* - Prints the last Node at each level in separate lines
*/
public void printRightView(){
if(root == null) return;
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
int size = queue.size();
for(int i=0;i<size;i++){
Node node = queue.poll();
if(i == size-1){
System.out.print(node.data + " ");
}
if(node.left != null)queue.add(node.left);
if(node.right != null)queue.add(node.right);
}
System.out.println();
}
}
/**
* Prints top view of the BinaryTree
* Prints the Nodes in same line separated by a space
*/
public void printTopView(){
}
/**
* Prints bottom view of the BinaryTree
* Prints the Nodes in same line separated by a space
*/
public void printBottomView(){
}
/**
* Naive approach which requires multiple traversals to return Least Common Ancestor for the given two numbers present in the Tree
* Builds two paths for two numbers from root to theat number and then iterates over those paths to find the last common node and returns it.
* @param n1
* @param n2
* @return
*/
public Node naiveLCA(int n1, int n2){
if(root == null) return null;
List<Node> n1Path = new ArrayList<>();
List<Node> n2Path = new ArrayList<>();
n1Path.add(root);
n2Path.add(root);
//Fill both paths for n1 and n2 starting from root.
fillPath(n1Path, n1, root);
fillPath(n2Path, n2, root);
//Iterate both paths and find the last common node which is the lca for two given numbers n1 and n2.
int i=0;
while(i<n1Path.size() && i<n2Path.size()){
if(n1Path.get(i) != n2Path.get(i)) break;
i++;
}
return n1Path.get(i-1);
}
private void fillPath(List<Node> path, int n, Node node){
if(node == null) return;
if(isExists(n,node)){
path.add(node);
}
fillPath(path,n,node.left);
fillPath(path,n,node.right);
}
private boolean isExists(int n, Node node){
if(node == null) return false;
if(node.data == n) return true;
return isExists(n, node.left) || isExists(n, node.right);
}
/**
* Efficient Approach with single traversal
* Returns the Least Common Ancestor for the given two numbers present in the Tree.
* This assumes both n1 and n2 are present in the tree and doesn't return null when either one of them is not present
* @param n1
* @param n2
* @return
*/
public Node getLCA(int n1, int n2){
return findLCA(root, n1, n2);
}
/**
* Possible Scenarios
* 1. If current node is either n1 or n2 then this is the LCA for those two numbers
* 2. If leftLCA and rightLCA are not null, meaning n1 and n2 present on each side then return current node
* 3. If either one of them is not null then that means both n1 and n2 are present on that side , so return the non-null side
* 4. If both or null return null
* @param node
* @param n1
* @param n2
* @return
*/
private Node findLCA(Node node, int n1, int n2){
if(node == null) return null;
if(node.data == n1 || node.data == n2) return node;
Node leftLCA = findLCA(node.left, n1, n2);
Node rightLCA = findLCA(node.right, n1, n2);
if(leftLCA != null && rightLCA != null) return node;
return leftLCA != null ? leftLCA : rightLCA;
}
Node prev = null; // global temp variable for storing a reference to prev while building a DLL from Tree
Node head = null; // global variable for head of the DLL
/**
* Method to convert the BinaryTree to a DoublyLinkedList in the order of InOrder Traversal, Uses existing reference of Tree Node left and right for prev and next in DLL.
* @return
*/
public Node convertToDDL(){
inOrderForDDL(root);
return head;
}
private void inOrderForDDL(Node node){
if(node == null) return;
inOrderForDDL(node.left);
if(prev == null){
head = node;
}else{
node.left = prev;
prev.right = node;
}
prev = node;
inOrderForDDL(node.right);
}
int diameter = Integer.MIN_VALUE;
/**
* Returns the diameter which is the maximum distance in between any two nodes in the Tree
* @return
*/
public int getDiameter(){
diameter = Integer.MIN_VALUE; // to reset for multiple calls on same instance of Tree
calculateDiameter(root);
return diameter;
}
private int calculateDiameter(Node node){
if(node == null) return 0;
int leftHeight = calculateDiameter(node.left);
int rightHeight = calculateDiameter(node.right);
diameter = Math.max(diameter, 1 + leftHeight + rightHeight);
return 1 + Math.max(leftHeight, rightHeight);
}
public Node getSuccessorOfInOrderTraversal(Node node){
if(node == null) return null;
if(node.right != null){
return getFirstInInorderTraverse(node.right);
}else{
// traverse up the tree until we find a node where its parent.left == itself (node.left.parent = node) => return node
Node parent = node.parent;
while(parent!=null && parent.left != node){
node = parent;
parent = parent.parent;
}
return parent;
}
}
public Node getPredecessorOfInOrderTraversal(Node node){
if(node == null) return null;
if(node.left != null){
return getLastInInorderTraverse(node.left);
}else{
// traverse up the tree until we find a node where its parent.right == itself ( node.right.parent == node)
Node parent = node.parent;
while(parent!=null && parent.right != node){
node = parent;
parent = parent.parent;
}
return parent;
}
}
public Node getFirstInInorderTraverse(Node node){
if (node == null) return null;
while (node.left != null) {
node = node.left;
}
return node;
}
public Node getLastInInorderTraverse(Node node){
if (node == null) return null;
while (node.right != null) {
node = node.right;
}
return node;
}
/**
* Inserts given newNode after node - as per In Order traversal
* @param node - non null
* @param newNode - non null
* returns true if insert is success - false otherwise
*/
public boolean subTreeInsertAfter(Node node, Node newNode){
if(node == null || newNode == null) return false;
if(node.right ==null){
node.right = newNode;
newNode.parent = node;
}else if(node.right!=null){
// insert left of getFirstInInorderTraverse(node.right)
Node leftMostNode = getFirstInInorderTraverse(node.right);
leftMostNode.left = newNode;
newNode.parent = leftMostNode;
}
return true;
}
/**
* Inserts given newNode before node - as per In Order traversal
* @param node - non null
* @param newNode - non null
* returns true if insert is success - false otherwise
*/
public boolean subTreeInsertBefore(Node node, Node newNode){
if(node == null || newNode == null) return false;
if(node.left ==null){
node.left = newNode;
newNode.parent = node;
}else if(node.left!=null){
// insert right of getLastInInorderTraverse(node.right)
Node rightMostNode = getLastInInorderTraverse(node.left);
rightMostNode.right = newNode;
newNode.parent = rightMostNode;
}
return true;
}
/**
* deletes given node by preserving the inorder traversal
* @param node - non null
*/
public boolean delete(Node node){
if(node == null) return false;
if(node.left == null && node.right == null){
// detach from parent
if(node.parent.left == node) node.parent.left = null;
if(node.parent.right == node) node.parent.right = null;
}else if(node.left!=null){
// swap with predecessor of node and recurse delete on swapped node
Node predecessorNode = getPredecessorOfInOrderTraversal(node);
int temp = predecessorNode.data;
predecessorNode.data = node.data;
node.data = temp;
delete(predecessorNode);
}else{
// swap with successor of node and recurse delete on swapped node
Node successorNode = getSuccessorOfInOrderTraversal(node);
int temp = successorNode.data;
successorNode.data = node.data;
node.data = temp;
delete(successorNode);
}
return true;
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree(new Node(1));
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.root.right.left = new Node(6);
tree.root.right.right = new Node(7);
System.out.println();
System.out.println("-- inOrderTraversal --");
tree.inOrderTraversal();
System.out.println();
System.out.println("-- preOrderTraversal --");
tree.preOrderTraversal();
System.out.println();
System.out.println("-- postOrderTraversal --");
tree.postOrderTraversal();
System.out.println();
System.out.println("-- levelOrderTraversal --");
tree.levelOrderTraversal();
System.out.println();
System.out.println("-- levelOrderTraversalLineByLine --");
tree.levelOrderTraversalLineByLine();
System.out.println("-- levelOrderTraversalSpiralForm -- ");
tree.levelOrderTraversalSpiralForm();
System.out.println();
System.out.println("-- getHeight -- ");
System.out.println(tree.getHeight());
System.out.println("-- getMin -- ");
System.out.println(tree.getMin());
System.out.println("-- getMax -- ");
System.out.println(tree.getMax());
System.out.println("-- printLeftView -- ");
tree.printLeftView();
System.out.println("-- printRightView -- ");
tree.printRightView();
System.out.println("-- getDiameter()");
System.out.println(tree.getDiameter());
Node root = new Node(20);
Node node10 = new Node(10);
Node node30 = new Node(30);
Node node5 = new Node(5);
Node node15 = new Node(15);
Node node25 = new Node(25);
Node node35 = new Node(35);
// Connect nodes to form the tree
root.left = node10;
root.right = node30;
node10.parent = root;
node30.parent = root;
node10.left = node5;
node10.right = node15;
node5.parent = node10;
node15.parent = node10;
node30.left = node25;
node30.right = node35;
node25.parent = node30;
node35.parent = node30;
BinaryTree bTreeWithParent = new BinaryTree(root);
BinaryTree.printTreeHelper(root, 0);
Node successor = bTreeWithParent.getSuccessorOfInOrderTraversal(node10);
System.out.println("Successor of node 10: " + (successor != null ? successor.data: "null"));
successor = bTreeWithParent.getSuccessorOfInOrderTraversal(node5);
System.out.println("Successor of node 5: " + (successor != null ? successor.data : "null"));
successor = bTreeWithParent.getSuccessorOfInOrderTraversal(bTreeWithParent.root);
System.out.println("Successor of root node 20: " + (successor != null ? successor.data : "null"));
successor = bTreeWithParent.getSuccessorOfInOrderTraversal(node35);
System.out.println("Successor of node 35: " + (successor != null ? successor.data : "null"));
successor = bTreeWithParent.getSuccessorOfInOrderTraversal(node15);
System.out.println("Successor of node 15: " + (successor != null ? successor.data : "null"));
Node predecessor = bTreeWithParent.getPredecessorOfInOrderTraversal(node10);
System.out.println("predecessor of node 10: " + (predecessor != null ? predecessor.data: "null"));
predecessor = bTreeWithParent.getPredecessorOfInOrderTraversal(bTreeWithParent.root);
System.out.println("predecessor of node 20(root): " + (predecessor != null ? predecessor.data : "null"));
predecessor = bTreeWithParent.getPredecessorOfInOrderTraversal(node25);
System.out.println("predecessor of node 25: " + (predecessor != null ? predecessor.data : "null"));
bTreeWithParent.subTreeInsertAfter(node35, new Node(40));
bTreeWithParent.subTreeInsertAfter(root, new Node(24));
System.out.println("After 40 InsertAfter 35 && 24 InsertAfter root(20)");
BinaryTree.printTreeHelper(root, 0);
bTreeWithParent.subTreeInsertBefore(node5, new Node(50));
bTreeWithParent.subTreeInsertBefore(root, new Node(55));
System.out.println("After 50 Insertbefore 5 && 55 insertbefore root(20)");
BinaryTree.printTreeHelper(bTreeWithParent.root, 0);
bTreeWithParent.delete(node10);
System.out.println("After deleting node 10");
BinaryTree.printTreeHelper(bTreeWithParent.root, 0);
bTreeWithParent.delete(root);
System.out.println("After deleting node 20 (root)");
BinaryTree.printTreeHelper(bTreeWithParent.root, 0);
}
//Prints root first as leftmost and righSubtree at top of root and leftsubtree as down of root
private static void printTreeHelper(Node node, int level) {
if (node == null) {
return;
}
// Print right subtree first (this will appear at the top when printed)
printTreeHelper(node.right, level + 1);
// Print the current node with appropriate spacing
printSpaces(4 * level);
System.out.println(node.data);
// Print left subtree
printTreeHelper(node.left, level + 1);
}
private static void printSpaces(int count) {
for (int i = 0; i < count; i++) {
System.out.print(" ");
}
}
}
class Node{
Node left;
Node right;
Node parent;
int data;
public Node(int value){
this.data = value;
}
}