-
Notifications
You must be signed in to change notification settings - Fork 0
/
bplustree.java
514 lines (450 loc) · 14.6 KB
/
bplustree.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
//import all the package which need to use here
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.*;
import java.util.regex.*;
import java.io.*;
//use generics PECS principle to define the class bplustree
//extend to get, super to insert put
public class bplustree<K extends Comparable<? super K>, V> {
private static final int Default_MaxNumOfChild = 128; //set a default m-1(m means the order of tree)
private int MaxNumOfChild; //set a variable m-1, integer type
private Node root; //set the root node of the B Plus Tree
//define the public class bplustree here
public bplustree() {
this.MaxNumOfChild = Default_MaxNumOfChild;
}
//define a public function initialize function here
//to execute initialize function, which is necessary
//for constructing a B Plus Tree
public void initialize(int MaxNumOfChild) {
if (MaxNumOfChild <= 2)
throw new IllegalArgumentException("Illegal branching factor: "
+ MaxNumOfChild);
this.MaxNumOfChild = MaxNumOfChild;
root = new LeafNode();
}
//define a public function key search here
public V search(K key) {
return root.getValue(key);
}
//make public the search range function defined later here
//return the getRange function result
public List<V> searchRange(K key1, K key2) {
return root.getRange(key1, key2);
}
public void insert(K key, V value) {
root.insertValue(key, value);
}
public void delete(K key) {
root.deleteValue(key);
}
//Make the linked list, as what B+ tree required.
public String toString() {
Queue<List<Node>> queue = new LinkedList<List<Node>>();
queue.add(Arrays.asList(root));
StringBuilder sb = new StringBuilder();
while (!queue.isEmpty()) {
Queue<List<Node>> nextQueue = new LinkedList<List<Node>>();
while (!queue.isEmpty()) {
List<Node> nodes = queue.remove();
sb.append('{');
Iterator<Node> it = nodes.iterator();
while (it.hasNext()) {
Node node = it.next();
sb.append(node.toString());
if (it.hasNext())
sb.append(", ");
if (node instanceof bplustree.InternalNode)
nextQueue.add(((InternalNode) node).children);
}
sb.append('}');
if (!queue.isEmpty())
sb.append(", ");
else
sb.append('\n');
}
queue = nextQueue;
}
return sb.toString();
}
//Define an abstract class Node
private abstract class Node {
List<K> keys; //Define keys as List
int keyNumber() {
return keys.size();
}
abstract V getValue(K key);
abstract void deleteValue(K key);
abstract void insertValue(K key, V value);
abstract K getFirstLeafKey();
abstract List<V> getRange(K key1, K key2);
abstract void merge(Node sibling);
abstract Node split();
abstract boolean isOverflow();
abstract boolean isUnderflow();
public String toString() {
return keys.toString();
}
}
//Define a private class Internal Node here
private class InternalNode extends Node {
List<Node> children; //Use a dynamic list children to store internal node
//Initialze a keys dynamic list and children dynamic list.
InternalNode() {
this.keys = new ArrayList<K>();
this.children = new ArrayList<Node>();
}
//Return value according to key
V getValue(K key) {
return getChild(key).getValue(key);
}
//Define deleteValue function here to execute Delete(key) command.
void deleteValue(K key) {
Node child = getChild(key);
child.deleteValue(key);
if (child.isUnderflow()) {
Node childLeftSibling = getChildLeftSibling(key);
Node childRightSibling = getChildRightSibling(key);
Node left = childLeftSibling != null ? childLeftSibling : child;
Node right = childLeftSibling != null ? child
: childRightSibling;
left.merge(right);
deleteChild(right.getFirstLeafKey());
if (left.isOverflow()) { //check whether the number of left is >m-1
Node sibling = left.split(); //if it is >m-1, then move the first leaf key to be a child
insertChild(sibling.getFirstLeafKey(), sibling);
}
if (root.keyNumber() == 0) //check whether the root is deleted
root = left; //if it is the case, set the node left to be the root
}
}
//Insert the key into the keys list, and check whether other nodes need to move
//Since it is the internal node class, as B+ tree required, only store keys, not values.
void insertValue(K key, V value) {
Node child = getChild(key);
child.insertValue(key, value);
if (child.isOverflow()) {
Node sibling = child.split();
insertChild(sibling.getFirstLeafKey(), sibling);
}
if (root.isOverflow()) {
Node sibling = split();
InternalNode newRoot = new InternalNode();
newRoot.keys.add(sibling.getFirstLeafKey());
newRoot.children.add(this);
newRoot.children.add(sibling);
root = newRoot;
}
}
//Return the first leaf key
K getFirstLeafKey() {
return children.get(0).getFirstLeafKey();
}
List<V> getRange(K key1, K key2) {
return getChild(key1).getRange(key1, key2);
}
//Merge the sibling
void merge(Node sibling) {
InternalNode node = (InternalNode) sibling;
keys.add(node.getFirstLeafKey());
keys.addAll(node.keys);
children.addAll(node.children);
}
//Define a command split, to separate the left and right sibling
Node split() {
int from = keyNumber() / 2 + 1, to = keyNumber();
InternalNode sibling = new InternalNode();
sibling.keys.addAll(keys.subList(from, to));
sibling.children.addAll(children.subList(from, to + 1));
keys.subList(from - 1, to).clear();
children.subList(from, to + 1).clear();
return sibling; //return the sibling
}
//Check whether overflow
boolean isOverflow() {
return children.size() > MaxNumOfChild;
}
//Check whether underflow
boolean isUnderflow() {
return children.size() < (MaxNumOfChild + 1) / 2;
}
//Find the index of given key in keys list use binarySearch.
//childIndex set to loc+1 if result>=0, otherwise set to -loc-1.
//return the children node in children list use the childIndex.
Node getChild(K key) {
int loc = Collections.binarySearch(keys, key);
int childIndex = loc >= 0 ? loc + 1 : -loc - 1;
return children.get(childIndex);
}
//Define a function deleteChild here.
//Use binarySearch to find the location of specific node
//remove the value according to key
void deleteChild(K key) {
int loc = Collections.binarySearch(keys, key);
if (loc >= 0) {
keys.remove(loc);
children.remove(loc + 1);
}
}
//Insert given key and child to specific position.
void insertChild(K key, Node child) {
int loc = Collections.binarySearch(keys, key);
int childIndex = loc >= 0 ? loc + 1 : -loc - 1;
if (loc >= 0) {
children.set(childIndex, child);
} else {
keys.add(childIndex, key);
children.add(childIndex + 1, child);
}
}
//Use binarySearch to check whether there is already has the given key number
//If there is already one, return the corresponding element in the list children.
Node getChildLeftSibling(K key) {
int loc = Collections.binarySearch(keys, key);
int childIndex = loc >= 0 ? loc + 1 : -loc - 1;
if (childIndex > 0)
return children.get(childIndex - 1);
return null;
}
Node getChildRightSibling(K key) {
int loc = Collections.binarySearch(keys, key);
int childIndex = loc >= 0 ? loc + 1 : -loc - 1;
if (childIndex < keyNumber())
return children.get(childIndex + 1);
return null;
}
}
//Define a private class LeafNode
private class LeafNode extends Node {
List<V> values;
LeafNode next;
LeafNode() {
keys = new ArrayList<K>();
values = new ArrayList<V>();
}
//Get the value of the given key
//Use binarySearch function here to find the index of the specific key
V getValue(K key) {
int loc = Collections.binarySearch(keys, key);
return loc >= 0 ? values.get(loc) : null;
}
void deleteValue(K key) {
int loc = Collections.binarySearch(keys, key);
if (loc >= 0) {
keys.remove(loc);
values.remove(loc);
}
}
//Define insertValue function here
void insertValue(K key, V value) {
int loc = Collections.binarySearch(keys, key);
int valueIndex = loc >= 0 ? loc : -loc - 1;
if (loc >= 0) {
values.set(valueIndex, value);
} else {
keys.add(valueIndex, key);
values.add(valueIndex, value);
}
if (root.isOverflow()) {
Node sibling = split();
InternalNode newRoot = new InternalNode();
newRoot.keys.add(sibling.getFirstLeafKey());
newRoot.children.add(this);
newRoot.children.add(sibling);
root = newRoot;
}
}
//Return the first element in keys List.
K getFirstLeafKey() {
return keys.get(0);
}
//Define a getRange function here use to execute range search.
//The function finally return every value whose key is key1<=key<=key2 in the List format.
//import package ahead to use relative function of Iterator
//use while to check every node
//use iterator() to ask the container to return Iterator
//use hasNext() to check whether there is any element in kIt
//use next() to get the next element in the sequence
List<V> getRange(K key1, K key2) {
List<V> result = new LinkedList<V>();
LeafNode node = this;
while (node != null) {
Iterator<K> kIt = node.keys.iterator();
Iterator<V> vIt = node.values.iterator();
while (kIt.hasNext()) {
K key = kIt.next();
V value = vIt.next();
int cmp1 = key.compareTo(key1);
int cmp2 = key.compareTo(key2);
if ((cmp1 >= 0) && (cmp2 <= 0))
result.add(value);
else if (cmp2 > 0)
return result;
}
node = node.next;
}
return result;
}
void merge(Node sibling) {
LeafNode node = (LeafNode) sibling;
keys.addAll(node.keys); //Add list to list
values.addAll(node.values);
next = node.next;
}
//Split the sibling
Node split() {
LeafNode sibling = new LeafNode();
int from = (keyNumber() + 1) / 2, to = keyNumber();
sibling.keys.addAll(keys.subList(from, to));
sibling.values.addAll(values.subList(from, to));
keys.subList(from, to).clear();
values.subList(from, to).clear();
sibling.next = next;
next = sibling;
return sibling;
}
//Use boolean store result of whether the number of values exceed m-1
//m is the tree order, the number of children should not be larger than m-1
//which is one of the rule of B+ TREE.
boolean isOverflow() {
return values.size() > MaxNumOfChild - 1;
}
//Same reasons as above.
boolean isUnderflow() {
return values.size() < MaxNumOfChild / 2;
}
}
public static void main(String[] args)
{
try{
String filePath = args[0];
FileInputStream fin = new FileInputStream(filePath);
InputStreamReader reader = new InputStreamReader(fin);
BufferedReader buffReader = new BufferedReader(reader);
String strTmp = "";
BufferedWriter out = new BufferedWriter(new FileWriter("output_file.txt"));
//to initialize the B Plus Tree
String str = buffReader.readLine();
Integer instart = null;
Integer inend = null;
for (int i=0; i<str.length(); i++) {
if (str.substring(i).startsWith("(")) {
instart = i + 1;
}
else if (str.substring(i).startsWith(")")) {
inend = i;
}
}
String incontent = str.substring(instart, inend);
Integer treeorder = Integer.parseInt(incontent) + 1;
bplustree<Integer, Double> bpt = new bplustree<Integer, Double>();
bpt.initialize(treeorder);
//System.out.println(treeorder);
while((strTmp = buffReader.readLine())!=null){
// System.out.println(strTmp);
//insert command
if (strTmp.startsWith("Insert")) {
int insertlen = strTmp.length();
Integer istartnum = null;
Integer isendnum = null;
for (int i=0; i < insertlen; i++) {
if (strTmp.substring(i).startsWith("(")) {
istartnum = i + 1;
}
else if (strTmp.substring(i).startsWith(")")) {
isendnum = i;
}
}
String iscontent = strTmp.substring(istartnum, isendnum);
String iscSplit[] = iscontent.split(",");
String istr1 = iscSplit[0].trim();
String istr2 = iscSplit[1].trim();
Integer ikey = Integer.parseInt(istr1);
Double ivalue = Double.parseDouble(istr2);
bpt.insert(ikey, ivalue); //insert to the tree
}//end of insert command
//search command
else if (strTmp.startsWith("Search")) {
boolean status = strTmp.contains(",");
if (status) {
//search range function here
Integer srstart = null;
Integer srend = null;
for (int i=0; i < strTmp.length(); i++) {
if (strTmp.substring(i).startsWith("(")) {
srstart = i + 1;
}
else if (strTmp.substring(i).startsWith(")")) {
srend = i;
}
}
String srcontent = strTmp.substring(srstart, srend);
String srcSplit[] = srcontent.split(",");
Integer srkey1 = Integer.parseInt(srcSplit[0].trim());
Integer srkey2 = Integer.parseInt(srcSplit[1].trim());
List<Double> srresult = bpt.searchRange(srkey1, srkey2);
//List to string
StringBuilder sb = new StringBuilder();
for(int i = 0; i < srresult.size(); i++) {
sb.append(srresult.get(i)).append(",");
}
String realstr = sb.toString().substring(0, sb.toString().length()-1);
//System.out.println(realstr);
out.write(realstr);
out.write("\n");
} else {
Integer sstart = null;
Integer send = null;
for (int i=0; i < strTmp.length(); i++) {
if (strTmp.substring(i).startsWith("(")) {
sstart = i + 1;
}
else if (strTmp.substring(i).startsWith(")")) {
send = i;
}
}
String scontent = strTmp.substring(sstart, send);
Integer skey = Integer.parseInt(scontent.trim());
Double searchresult = bpt.search(skey);
if (searchresult != null) {
//System.out.println(searchresult);
String chresult = Double.toString(searchresult);
out.write(chresult);
out.write("\n");
} else {
//System.out.println("Null");
out.write("Null");
out.write("\n");
}
}
}
//Delete command
else if (strTmp.startsWith("Delete")) {
Integer dstart = null;
Integer dend = null;
for (int i=0; i < strTmp.length(); i++) {
if (strTmp.substring(i).startsWith("(")) {
dstart = i + 1;
}
else if (strTmp.substring(i).startsWith(")")) {
dend = i;
}
}
String dcontent = strTmp.substring(dstart, dend);
Integer dkey = Integer.parseInt(dcontent);
bpt.delete(dkey);
}
}
buffReader.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}