-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathmaxDataInBinaryTree.java
115 lines (89 loc) Β· 2.72 KB
/
maxDataInBinaryTree.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
package Lecture20;
import java.util.Scanner;
public class maxDataInBinaryTree {
private class Node {
int data;
Node left;
Node right;
Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
private Node root;
private int size = 0;
public maxDataInBinaryTree() {
Scanner sc = new Scanner(System.in);
this.root = takeTreeInput(sc, null, false);
}
private Node takeTreeInput(Scanner sc, Node parent, boolean isLeftorRight) {
if (parent == null) {
System.out.print("Enter data for root node: ");
} else {
if (isLeftorRight) {
System.out.print("Enter data for left child of " + parent.data+ ": ");
} else {
System.out.print("Enter data for right child of: " + parent.data + ": ");
}
}
int data = sc.nextInt();
Node node = new Node(data, null, null);
this.size++;
boolean choice = false;
System.out.print("Do you have left child for " + node.data + " -> true/false: ");
choice = sc.nextBoolean();
if (choice) {
node.left = this.takeTreeInput(sc, node, true);
}
choice = false;
System.out.print("Do you have right child for " + node.data + " -> true/false: ");
choice = sc.nextBoolean();
if (choice) {
node.right = this.takeTreeInput(sc, node, false);
}
return node;
}
public int max() {
return this.maxNodeData(this.root);
}
private int maxNodeData(Node node) {
if (node == null) {
return Integer.MIN_VALUE;
}
int leftSubtreeMax = maxNodeData(node.left);
int rightSubtreeMax = maxNodeData(node.right);
int maxVal = Math.max(leftSubtreeMax,
(Math.max(node.data, rightSubtreeMax)));
return maxVal;
}
// Driver program
public static void main(String[] args) {
maxDataInBinaryTree tree = new maxDataInBinaryTree();
int maxData = tree.max();
System.out.println("=============\n");
System.out.println("max value is: " + maxData);
}
}
/* output
Enter data for root node: 20
Do you have left child for 20 -> true/false: true
Enter data for left child of 20: 10
Do you have left child for 10 -> true/false: true
Enter data for left child of 10: 28
Do you have left child for 28 -> true/false: false
Do you have right child for 28 -> true/false: false
Do you have right child for 10 -> true/false: true
Enter data for right child of: 10: 16
Do you have left child for 16 -> true/false: false
Do you have right child for 16 -> true/false: false
Do you have right child for 20 -> true/false: true
Enter data for right child of: 20: 30
Do you have left child for 30 -> true/false: true
Enter data for left child of 30: 42
Do you have left child for 42 -> true/false: false
Do you have right child for 42 -> true/false: false
Do you have right child for 30 -> true/false: false
=============
max value is: 42
*/