-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst.java
271 lines (234 loc) · 6.03 KB
/
bst.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
class BinaryTreeNode<T> {
public BinaryTreeNode(T data) {
this.data = data;
}
public T data;
public BinaryTreeNode<T> left;
public BinaryTreeNode<T> right;
}
class Node<T> {
T data;
Node<T> next;
Node(T data){
this.data = data;
next = null;
}
}
class QueueUsingLL<T> {
private Node<T> front;
private Node<T> rear;
private int size;
public QueueUsingLL() {
front = null;
rear = null;
size = 0;
}
int size(){
return size;
}
boolean isEmpty(){
return size == 0;
}
T front() throws QueueEmptyException{
if(size == 0){
throw new QueueEmptyException();
}
return front.data;
}
void enqueue(T element){
Node<T> newNode = new Node<>(element);
if(rear == null){
front = newNode;
rear = newNode;
}else{
rear.next = newNode;
rear = newNode;
}
size++;
}
T dequeue() throws QueueEmptyException{
if(size == 0){
throw new QueueEmptyException();
}
T temp = front.data;
front = front.next;
if(size == 1){
rear = null;
}
size--;
return temp;
}
}
public class Pair<T,V> {
public T first;
public V second;
}
import java.util.Scanner;
public class BinaryTreeUse {
public static void printTree(BinaryTreeNode<Integer> root) {
if (root == null) {
return;
}
String toBePrinted = root.data + "";
if (root.left != null) {
toBePrinted += "L:" + root.left.data + ",";
}
if (root.right != null) {
toBePrinted += "R:" + root.right.data;
}
System.out.println(toBePrinted);
printTree(root.left);
printTree(root.right);
}
public static BinaryTreeNode<Integer> takeInput(Scanner s) {
int rootData;
System.out.println("Enter root data");
rootData = s.nextInt();
if (rootData == -1) {
return null;
}
BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(rootData);
root.left = takeInput(s);
root.right = takeInput(s);
return root;
}
public static BinaryTreeNode<Integer> takeInputLevelWise() {
Scanner s = new Scanner(System.in);
QueueUsingLL<BinaryTreeNode<Integer>> pendingNodes = new QueueUsingLL<>();
System.out.println("Enter root data");
int rootData = s.nextInt();
if (rootData == -1) {
return null;
}
BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(rootData);
pendingNodes.enqueue(root);
while (!pendingNodes.isEmpty()) {
BinaryTreeNode<Integer> front;
try {
front = pendingNodes.dequeue();
} catch (QueueEmptyException e) {
return null;
}
System.out.println("Enter left child of " + front.data);
int leftChild = s.nextInt();
if (leftChild != -1) {
BinaryTreeNode<Integer> child = new BinaryTreeNode<Integer>(leftChild);
pendingNodes.enqueue(child);
front.left = child;
}
System.out.println("Enter right child of " + front.data);
int rightChild = s.nextInt();
if (rightChild != -1) {
BinaryTreeNode<Integer> child = new BinaryTreeNode<Integer>(rightChild);
pendingNodes.enqueue(child);
front.right = child;
}
}
return root;
}
public static int countNodes(BinaryTreeNode<Integer> root) {
if (root == null) {
return 0;
}
int ans = 1;
ans += countNodes(root.left);
ans += countNodes(root.right);
return ans;
}
public static void mirror(BinaryTreeNode<Integer> root) {
if (root == null) {
return;
}
mirror(root.left);
mirror(root.right);
BinaryTreeNode<Integer> temp = root.left;
root.left = root.right;
root.right = temp;
return;
}
public static int diameter(BinaryTreeNode<Integer> root) {
if (root == null) {
return 0;
}
int option1 = height(root.left) + height(root.right);
int option2 = diameter(root.left);
int option3 = diameter(root.right);
return Math.max(option1, Math.max(option2, option3));
}
public static int height(BinaryTreeNode<Integer> root) {
if (root == null) {
return 0;
}
int lh = height(root.left);
int rh = height(root.right);
return 1 + Math.max(lh, rh);
}
public static Pair<Integer, Integer> heightDiameter(BinaryTreeNode<Integer> root) {
if (root == null) {
Pair<Integer,Integer> output = new Pair<>();
output.first = 0;
output.second = 0;
return output;
}
Pair<Integer, Integer> lo = heightDiameter(root.left);
Pair<Integer, Integer> ro = heightDiameter(root.right);
int height = 1 + Math.max(lo.first, ro.first);
int option1 = lo.first + ro.first;
int option2 = lo.second;
int option3 = ro.second;
int diameter = Math.max(option1, Math.max(option2, option3));
Pair<Integer,Integer> output = new Pair<>();
output.first = height;
output.second = diameter;
return output;
}
public static void inorder(BinaryTreeNode<Integer> root) {
if (root == null) {
return;
}
inorder(root.left);
System.out.print(root.data + " ");
inorder(root.right);
}
public static BinaryTreeNode<Integer> buildTreeHelper(int in[], int pre[], int inS, int inE, int preS, int preE) {
if (inS > inE) {
return null;
}
int rootData = pre[preS];
BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(rootData);
int rootInIndex = -1;
for (int i = inS; i <= inE; i++) {
if (in[i] == rootData) {
rootInIndex = i;
break;
}
}
if (rootInIndex == -1) {
return null;
}
int leftInS = inS;
int leftInE = rootInIndex - 1;
int leftPreS = preS + 1;
int leftPreE = leftInE - leftInS + leftPreS;
int rightInS = rootInIndex + 1;
int rightInE = inE;
int rightPreS = leftPreE + 1;
int rightPreE = preE;
root.left = buildTreeHelper(in, pre, leftInS, leftInE, leftPreS, leftPreE);
root.right = buildTreeHelper(in, pre, rightInS, rightInE, rightPreS, rightPreE);
return root;
}
public static BinaryTreeNode<Integer> buildTree(int in[], int pre[]) {
return buildTreeHelper(in, pre, 0, in.length - 1, 0, pre.length -1);
}
public static void main(String[] args) {
// BinaryTreeNode<Integer> root = takeInputLevelWise();
// printTree(root);
// System.out.println("diameter:" + heightDiameter(root).second);
// System.out.println("Height:" + heightDiameter(root).first);
int in[] = {4,2,5,1,3,7};
int pre[] = {1,2,4,5,3,7};
BinaryTreeNode<Integer> root = buildTree(in, pre);
printTree(root);
}
}