-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathBST.java
More file actions
249 lines (216 loc) · 5.06 KB
/
BST.java
File metadata and controls
249 lines (216 loc) · 5.06 KB
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
package Algorithms_Java;
import java.io.*;
import java.util.Stack;
public class BST {
public static class Node {
int data;
Node left;
Node right;
Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
public static class Pair {
Node node;
int state;
Pair(Node node, int state) {
this.node = node;
this.state = state;
}
}
public static Node construct(Integer[] arr) {
Node root = new Node(arr[0], null, null);
Pair rtp = new Pair(root, 1);
Stack<Pair> st = new Stack<>();
st.push(rtp);
int idx = 0;
while (st.size() > 0) {
Pair top = st.peek();
if (top.state == 1) {
idx++;
if (arr[idx] != null) {
top.node.left = new Node(arr[idx], null, null);
Pair lp = new Pair(top.node.left, 1);
st.push(lp);
} else {
top.node.left = null;
}
top.state++;
} else if (top.state == 2) {
idx++;
if (arr[idx] != null) {
top.node.right = new Node(arr[idx], null, null);
Pair rp = new Pair(top.node.right, 1);
st.push(rp);
} else {
top.node.right = null;
}
top.state++;
} else {
st.pop();
}
}
return root;
}
public static void display(Node node) {
if (node == null) {
return;
}
String str = "";
str += node.left == null ? "." : node.left.data + "";
str += " <- " + node.data + " -> ";
str += node.right == null ? "." : node.right.data + "";
System.out.println(str);
display(node.left);
display(node.right);
}
public static int size(Node node) {
if (node == null) {
return 0;
}
int leftSize = size(node.left);
int rightSize = size(node.right);
int mySize = leftSize + rightSize + 1;
return mySize;
}
public static int sum(Node node) {
if (node == null) {
return 0;
}
int leftSum = sum(node.left);
int rightSum = sum(node.right);
int mySum = leftSum + rightSum + node.data;
return mySum;
}
public static int max(Node node) {
if (node.right != null) {
return max(node.right);
} else
return node.data;
}
public static int min(Node node) {
if (node.left != null) {
return min(node.left);
} else
return node.data;
}
public static boolean find(Node node, int data) {
if (node == null) {
return false;
}
if (data > node.data) {
return find(node.right, data);
} else if (data < node.data) {
return find(node.left, data);
} else
return true;
}
public static Node add(Node node, int data) {
if (node == null) {
Node nn = new Node(data, null, null);
return nn;
}
if (data > node.data) {
node.right = add(node.right, data);
} else if (data < node.data)
node.left = add(node.left, data);
else {
}
return node;
}
public static Node remove(Node node, int data) {
if (node == null) {
return null;
}
if (data > node.data) {
node.right = remove(node.right, data);
} else if (data < node.data) {
node.left = remove(node.left, data);
} else {
if (node.left != null && node.right != null) {
int leftKaMax = max(node.left);
node.data = leftKaMax;
node.left = remove(node.left, leftKaMax);
return node;
} else if (node.left != null) {
return node.left;
} else if (node.right != null) {
return node.right;
} else {
return null;
}
}
return node;
}
static int sum = 0;
public static void rwsol(Node node) {
if (node == null)
return;
rwsol(node.right);
int origData = node.data;
node.data = sum;
sum += origData;
rwsol(node.left);
}
public static int lca(Node node, int d1, int d2) {
if (node == null) {
return 0;
}
if (node.data < d1 && node.data < d2) {
return lca(node.right, d1, d2);
} else if (node.data > d1 && node.data > d2) {
return lca(node.left, d1, d2);
} else
return node.data;
}
public static void pir(Node node, int d1, int d2) {
if (node == null) {
return;
}
pir(node.left, d1, d2);
if (node.data >= d1 && node.data <= d2) {
System.out.println(node.data);
}
pir(node.right, d1, d2);
}
public static void printTargetPair(Node root, Node node, int target) {
if (node == null) {
return;
}
printTargetPair(root, node.left, target);
int restValue = target - node.data;
if (node.data < restValue) {
if (find(root, restValue)) {
System.out.println(node.data + " " + restValue);
}
}
printTargetPair(root, node.right, target);
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Integer[] arr = new Integer[n];
String[] values = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
if (values[i].equals("n") == false) {
arr[i] = Integer.parseInt(values[i]);
} else {
arr[i] = null;
}
}
int data = Integer.parseInt(br.readLine());
Node root = construct(arr);
int size = size(root);
int sum = sum(root);
int max = max(root);
int min = min(root);
boolean found = find(root, data);
System.out.println(size);
System.out.println(sum);
System.out.println(max);
System.out.println(min);
System.out.println(found);
}
}