-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFork.java
More file actions
352 lines (305 loc) · 8.81 KB
/
Fork.java
File metadata and controls
352 lines (305 loc) · 8.81 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
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
import java.lang.reflect.Array;
import java.util.Optional;
public class Fork<Key extends Comparable<Key>,Value> implements Bst<Key, Value>{
// immutable fields: key, value, left, right
private final Key key;
private final Value value;
private final Bst<Key, Value> left, right;
/**
* Initialize fields
* @param key - the key of the root
* @param value - the value of the root
* @param left - left part of the tree
* @param right - right part of the tree
*/
public Fork(Key key, Value value, Bst<Key, Value> left, Bst<Key, Value> right){
assert(left != null);
assert(right != null);
assert(left.smaller(key));
assert(right.bigger(key));
this.key = key;
this.value = value;
this.left = left;
this.right = right;
}
/**
* Tree is empty in Fork
*/
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
/**
* Get the node with the largest key
*/
@Override
public Optional<Entry<Key, Value>> largest() {
// TODO Auto-generated method stub
if (right.isEmpty())
return Optional.of(new Entry<Key, Value>(key, value));
else
return right.largest();
}
/**
* Get the node with the smallest key
*/
@Override
public Optional<Entry<Key, Value>> smallest() {
// TODO Auto-generated method stub
if (left.isEmpty())
return Optional.of(new Entry<Key, Value>(key, value));
else
return left.smallest();
}
/**
* Check if the keys of all nodes are smaller than k
*/
@Override
public boolean smaller(Key k) {
return largest().get().getKey().compareTo(k) < 0;
}
/**
* Check if the keys of all nodes are bigger than k
*/
@Override
public boolean bigger(Key k) {
return smallest().get().getKey().compareTo(k) > 0;
}
/**
* Check if node with Key k exists in the tree
*/
@Override
public boolean has(Key k) {
// TODO Auto-generated method stub
if(k.compareTo(key) == 0) // if exists return true
return true;
else if(k.compareTo(key) < 0) // else makes recursive call
return left.has(k);
else
return right.has(k);
}
/**
* Finds the value of the node with Key k
* @param k - the key that is being searched
*/
@Override
public Optional<Value> find(Key k) {
if(k.compareTo(key) == 0)
return Optional.of(value);
else if(k.compareTo(key) < 0)
return left.find(k);
else
return right.find(k);
}
/**
* Put new node in the tree
* @param k - the key of the node that is being put in the tree
* @param v - the value of the node that is being put in the tree
*/
@Override
public Bst<Key, Value> put(Key k, Value v) {
// TODO Auto-generated method stub
if(k.compareTo(key) == 0) // if node with the same key exists it replaces the node with the new one
return new Fork<Key, Value>(k, v, left, right);
else if(k.compareTo(key) < 0)
return new Fork<Key, Value>(key , value, left.put(k, v), right); // else put the node in the left part of the tree
else
return new Fork<Key, Value>(key, value, left, right.put(k, v)); // else put the node in the right part of the tree
}
/**
* Delete a node from the tree
* @param k - the key of the node that is being deleted from the tree
*/
@Override
public Optional<Bst<Key, Value>> delete(Key k) {
// TODO Auto-generated method stub
if(has(k)){
System.out.println("Hello");
if(k.compareTo(key) == 0)
if(left.isEmpty())
return Optional.of(right);
else if(right.isEmpty())
return Optional.of(left);
else
return Optional.of(new Fork<Key, Value>(left.largest().get().getKey(), left.largest().get().getValue(), left.deleteLargest().get(), right));
else if(k.compareTo(key) < 0)
return Optional.of(new Fork<Key, Value>(key, value, left.delete(k).get(), right));
else
return Optional.of(new Fork<Key, Value>(key, value, left, right.delete(k).get()));
}else{
return Optional.empty();
}
}
/**
* Delete the node with smallest key
*/
@Override
public Optional<Bst<Key, Value>> deleteSmallest() {
// TODO Auto-generated method stub
if (left.isEmpty())
return Optional.of(right);
else
return Optional.of(new Fork<Key,Value>(key, value, left.deleteSmallest().get(), right));
}
/**
* Delete the node with largest key
*/
@Override
public Optional<Bst<Key, Value>> deleteLargest() {
// TODO Auto-generated method stub
if (right.isEmpty())
return Optional.of(left);
else
return Optional.of(new Fork<Key,Value>(key, value, left, right.deleteLargest().get()));
}
/**
* Print the tree in a fancy way using fancyToString(0)
*/
@Override
public String fancyToString() {
// TODO Auto-generated method stub
return "\n\n\n" + fancyToString(0) + "\n\n\n";
}
/**
* @param d - the depth of the tree from which we print the tree
*/
@Override
public String fancyToString(int d) {
// TODO Auto-generated method stub
int step = 8; // depth step
String l = left.fancyToString(d+step);
String r = right.fancyToString(d+step);
return r + spaces(d) + key + ", " + value + "\n" + l;
}
/**
* Print spaces for the fancyToString method
* @param n - number of spaces
* @return spaces
*/
private String spaces(int n) {
String s = "";
for (int i = 0; i < n; i++)
s = s + " ";
return s;
}
/**
* Get the size of the tree - number of nodes
*/
@Override
public int size() {
// TODO Auto-generated method stub
if(left.isEmpty())
return 1 + right.size();
else if(right.isEmpty())
return 1 + left.size();
else
return 1 + left.size() + right.size();
}
/**
* Get the height of the tree
*/
@Override
public int height() {
// TODO Auto-generated method stub
if(left.isEmpty())
return 1 + right.height(); // return the height of the right part
else if(right.isEmpty())
return 1 + left.height(); // return the height of the left part
else
return Math.max(1 + right.height(), 1 + left.height()); // return both the height of the left and right part
}
/**
* Print the tree in ascending order
*/
@Override
public void printInOrder() {
// TODO Auto-generated method stub
left.printInOrder();
System.out.print("(" + key + ", " + value + ")");
right.printInOrder();
}
/**
* Save the tree in ascending order
* @param a - array of entries which stores the elements
*/
@Override
public void saveInOrder(Entry<Key, Value>[] a) {
// TODO Auto-generated method stub
this.saveInOrder(a, 0);
}
/**
* @param a - array of entries which stores the elements
* @param i - the index of the node
*/
@Override
public int saveInOrder(Entry<Key, Value>[] a, int i) {
// TODO Auto-generated method stub
i = left.saveInOrder(a, i);
a[i++]= new Entry<Key, Value>(key, value);
i = right.saveInOrder(a, i);
return i;
}
/**
* Transform a Bst tree into a balanced Bst tree
*/
public Bst<Key, Value> balanced() {
// TODO Auto-generated method stub
Entry<Key,Value> e = new Entry<>(key,value);
@SuppressWarnings("unchecked")
Entry<Key,Value>[] a = (Entry<Key,Value>[]) Array.newInstance(e.getClass(), size()); // initialize the array to store the elements
this.saveInOrder(a); // store the elements in ascending order
return helper(a, 0, a.length);
}
/**
*
* @param a - array stores the elements
* @param start - start of the new sub array
* @param stop - end of new sub array
* @return balanced tree
*/
private Bst<Key,Value> helper(Entry<Key,Value>[] a, int start, int stop){
if(a.length!=0 && start < stop){ // check the length of the array and the start to be less than the stop
Key mediumKey = a[(start + stop)/2].getKey(); // get the key of the medium node
Value mediumValue = a[(start + stop)/2].getValue(); // get the value of the medium node
int medium = (start + stop)/2; // get index of the medium node
// create new tree with the medium values of the current array and left and right parts- recursive calls with the left and right sub arrays
Fork<Key, Value> balancedTree = new Fork<Key, Value>(mediumKey, mediumValue, helper(a, start, medium), helper(a, medium+1, stop));
return balancedTree;
}else{
return new Empty<>();
}
}
/**
* Get the key of the current node
*/
@Override
public Optional<Key> getKey() {
// TODO Auto-generated method stub
return Optional.of(key);
}
/**
* Get the value of the current node
*/
@Override
public Optional<Value> getValue() {
// TODO Auto-generated method stub
return Optional.of(value);
}
/**
* Get the left sub tree of the current tree
*/
@Override
public Optional<Bst<Key, Value>> getLeft() {
// TODO Auto-generated method stub
return Optional.of(left);
}
/**
* Get the right sub tree of the current tree
*/
@Override
public Optional<Bst<Key, Value>> getRight() {
// TODO Auto-generated method stub
return Optional.of(right);
}
}