-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBPlusTree.java
More file actions
468 lines (421 loc) · 16.7 KB
/
Copy pathBPlusTree.java
File metadata and controls
468 lines (421 loc) · 16.7 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
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
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
import java.util.ArrayList;
import java.util.ListIterator;
/**
* BPlusTree Class Assumptions: 1. No duplicate keys inserted 2. Order D:
* D<=number of keys in a node <=2*D 3. All keys are non-negative
* TODO: Rename to BPlusTree
*/
public class BPlusTree<K extends Comparable<K>, T> {
public Node<K,T> root;
public static final int D = 2;
public T search(K key) {
// Search from root
return tree_search(root, key);
}
public T tree_search(Node<K,T> startNode, K key) {
// If the starting node is a leafNode
if (startNode.isLeafNode) {
// Get the position of the key in the list of keys
int position = startNode.keys.indexOf(key);
if (position == -1) return null;
// Get its value from the list of values.
return (T)((LeafNode)startNode).values.get(position);
}
// If not, find the right subtree to start the search.
else {
// If the key is smaller than all of the keys in the current node, start searching from the leftmost child.
if (key.compareTo(startNode.keys.get(0)) < 0) {
return tree_search((Node<K,T>)((IndexNode)startNode).children.get(0), key);
}
// If the key is greater then all of the keys in the current node, start searching from the rightmost child.
else if (key.compareTo(startNode.keys.get(startNode.keys.size() - 1)) >= 0) {
return tree_search((Node<K,T>)((IndexNode)startNode).children.get(((IndexNode)startNode).children.size() - 1), key);
}
// Else, find the index i in the list of keys such that K(i) <= key < K(i+1), then search from children(i+1).
else {
ListIterator<K> iterator = startNode.keys.listIterator();
while (iterator.hasNext()) {
if (iterator.next().compareTo(key) > 0) {
int position = iterator.previousIndex();
return tree_search((Node<K,T>)((IndexNode)startNode).children.get(position), key);
}
}
}
}
return null;
}
/**
* TODO Insert a key/value pair into the BPlusTree
*
* @param key
* @param value
*/
public void insert(K key, T value) {
//Check if the root has been created or not.
if (root==null){
//create root node and first leaf
//set up root node
Node lnode=new LeafNode(key,value);
root=lnode;
}
else{
//find the right position and insert
//with recursive method
recinsert(key,value,root,0);
}
}
public Entry<K, Node<K,T>> recinsert(K key, T value, Node N, int depth){
//find position
int i=0;
while ((i<N.keys.size())&&(key.compareTo((K)N.keys.get(i))>=0)){
i++;
}
//check for special case. Since leave node does not have m+1 values.
if (N.isLeafNode){
i--;}
//create return variable
Entry<K,Node<K,T>> entry;
if (!N.isLeafNode){
//recur through the node
entry=recinsert(key,value,(Node)((IndexNode)N).children.get(i),depth+1);
if (entry!=null){
((IndexNode)N).insertSorted(entry, i);
}
//check overflow and return
if (N.isOverflowed()){
//check if it's the root node
if (depth!=0){
entry=this.splitIndexNode((IndexNode)N);
return entry;
}else
{
entry=this.splitIndexNode((IndexNode)N);
//
IndexNode newroot=new IndexNode(entry.getKey(), root, entry.getValue());
root=newroot;
return null;
}
}
}else {
//if reached leaf node
//insert key and value
((LeafNode)N).insertSorted(key, value);
//check overflow
//return entry if node has been split
if (N.isOverflowed()){
if (depth!=0){
entry=this.splitLeafNode((LeafNode)N);
}
else{
entry=this.splitLeafNode((LeafNode)N);
IndexNode newroot=new IndexNode(entry.getKey(), root, entry.getValue());
root=newroot;
}
return entry;
}
}
return null;
}
/**
* TODO Split a leaf node and return the new right node and the splitting
* key as an Entry<splitingKey, RightNode>
*
* @param leaf, any other relevant data
* @return the key/node pair as an Entry
*/
public Entry<K, Node<K,T>> splitLeafNode(LeafNode<K,T> leaf) {
int n=leaf.keys.size();
//create new node
LeafNode nLeaf=new LeafNode(leaf.keys.subList(D, n),leaf.values.subList(D, n));
//get the key for parent node
K tkey=leaf.keys.get(D);
//remove excess node
for (int i=D;i<n;i++){
leaf.keys.remove(D);
leaf.values.remove(D);
}
//connect nodes
if (leaf.nextLeaf != null) {
leaf.nextLeaf.previousLeaf = nLeaf;
nLeaf.nextLeaf = leaf.nextLeaf;
leaf.nextLeaf = nLeaf;
nLeaf.previousLeaf = leaf;
}
else {
leaf.nextLeaf=nLeaf;
nLeaf.previousLeaf=leaf;
}
//create return entry
Entry<K, Node<K,T>> reentry=new SimpleEntry<K,Node<K,T>>(tkey,nLeaf);
return reentry;
}
/**
* TODO split an indexNode and return the new right node and the splitting
* key as an Entry<slitingKey, RightNode>
*
* @param index, any other relevant data
* @return new key/node pair as an Entry
*/
public Entry<K, Node<K,T>> splitIndexNode(IndexNode<K,T> index) {
int n=index.keys.size();
int m=index.children.size();
//create new index node for return
IndexNode nindex=new IndexNode(index.keys.subList(D+1, n),index.children.subList(D+1, m));
K tkey=index.keys.get(D);
//delete copied node
for (int i=D;i<n;i++){
index.keys.remove(D);
}
for (int i=D+1;i<m;i++){
index.children.remove(D+1);
}
//create return node
Entry<K, Node<K,T>> reentry=new SimpleEntry<K,Node<K,T>>(tkey,nindex);
return reentry;
}
/**
* TODO Delete a key/value pair from this B+Tree
*
* @param key
*/
public void delete(K key) {
//using recursion function here
recdelete(key,root);
//default if no root just do nothing
if (root!=null){
if (root.keys.size()==0){
root=(Node)((IndexNode)root).children.get(0);
}}
}
public boolean recdelete(K key, Node N){
int i=0;
//find the position to delete
i=this.binarysearch(N.keys, key);
//help on leafnode
if (N.isLeafNode){
i--;
if (key.compareTo((K)N.keys.get(i))==0){
//found node to delete
N.keys.remove(i);
((LeafNode)N).values.remove(i);
}
}else{
//go into index node and continue finding the node to delete
if (recdelete(key, ((Node)((IndexNode)N).children.get(i)))){
Node Secnode;
Node Node1=null;
Node Node2=null;
//create three nodes for relocation if one node is underflow
int size1=-1;
int size2=-1;
//obtain left node if and only if there is a left node
if (i>0){
Node1=((Node)((IndexNode)N).children.get(i-1));
size1=Node1.keys.size();
}
int k=0;
if (((Node)(((IndexNode)N).children.get(i))).isLeafNode){k=1;}
//obtain right node if and only if there is a right node, also use k to help positon the pointer if the children is leafnode
if (i<N.keys.size()-k){
Node2=((Node)((IndexNode)N).children.get(i+1));
size2=Node2.keys.size();
}
//determine which node has more nodes to redistribute
Secnode=size1>size2?Node1:Node2;
//do leaf redistribute if children is leaf node
if (((Node)(((IndexNode)N).children.get(i))).isLeafNode){
int pos=-1;
//determine left and right
if (size1>size2){
pos=this.handleLeafNodeUnderflow((LeafNode)Secnode,(LeafNode)(((IndexNode)N).children.get(i)),(IndexNode)N);
}else{
pos=this.handleLeafNodeUnderflow((LeafNode)(((IndexNode)N).children.get(i)),(LeafNode)Secnode,(IndexNode)N);
}
if (pos!=-1){
((IndexNode)N).children.remove(pos+1);
((IndexNode)N).keys.remove(pos);
}
}
//do index node redistribute otherwise
else{
int pos=-1;
//determine left and right
if (size1>size2){
pos=this.handleIndexNodeUnderflow((IndexNode)Secnode,(IndexNode)(((IndexNode)N).children.get(i)),(IndexNode)N);
}else{
pos=this.handleIndexNodeUnderflow((IndexNode)(((IndexNode)N).children.get(i)),(IndexNode)Secnode,(IndexNode)N);
}
if (pos!=-1){
((IndexNode)N).children.remove(pos+1);
((IndexNode)N).keys.remove(pos);
}
}
}
}
//return current node status, if underflow, the parent will do handle
return N.isUnderflowed();
}
//search tree for finding the right node
public int binarysearch(ArrayList<K> keys, K dkey ){
int s=0;
int ed=keys.size()-1;
if (dkey.compareTo(keys.get(s))<0){
return s;
}else if (dkey.compareTo(keys.get(ed))>=0){
return ed;
}
while (s<=ed){
//key is inside s and ed
int mid= s+(ed-s)/2;
if (dkey.compareTo(keys.get(mid))<0) ed=mid-1;
else if (dkey.compareTo(keys.get(mid))>0) s=mid+1;
else return mid;
}
return s;
}
/**
* TODO Handle LeafNode Underflow (merge or redistribution)
*
* @param left
* : the smaller node
* @param right
* : the bigger node
* @param parent
* : their parent index node
* @return the splitkey position in parent if merged so that parent can
* delete the splitkey later on. -1 otherwise
*/
public int handleLeafNodeUnderflow(LeafNode<K,T> left, LeafNode<K,T> right,
IndexNode<K,T> parent) {
//position of the subnode in upper node
int pos;
//where should the transfer begin on right node
pos=this.binarysearch(parent.keys, right.keys.get(0));
pos--;
//take record of three nodes properties for later uses
int l1=left.values.size();
int l2=right.values.size();
int p1=parent.keys.size();
//if not enough keys on left and right
if (l1+l2<2*D){
K tkey=parent.keys.get(pos);
//pull down and redistribute nodes
for (int i=0;i<l2;i++){
left.keys.add(right.keys.get(0));
right.keys.remove(0);
left.values.add(right.values.get(0));
right.values.remove(0);
}
left.nextLeaf=right.nextLeaf;
right.nextLeaf.previousLeaf=left;
right=null;
return pos;
}else{
//if there is enough keys, do redistribution
//detemine direction of the redistribution
if (l2>l1){
int numtomove=D-l1;
for (int i=0;i<numtomove;i++){
left.keys.add(right.keys.get(0));
right.keys.remove(0);
left.values.add(right.values.get(0));
right.values.remove(0);
}
}
else{
int numtomove=D-l2;
for (int i=0;i<numtomove;i++){
right.keys.add(0,left.keys.get(left.keys.size()-1));
left.keys.remove(left.keys.size()-1);
right.values.add(0,left.values.get(left.values.size()-1));
left.values.remove(left.values.size()-1);
}
}
//modified parent node with the correct keys
parent.keys.remove(pos);
parent.keys.add(pos,right.keys.get(0));
}
return -1;
}
/**
* TODO Handle IndexNode Underflow (merge or redistribution)
*
* @param left
* : the smaller node
* @param right
* : the bigger node
* @param parent
* : their parent index node
* @return the splitkey position in parent if merged so that parent can
* delete the splitkey later on. -1 otherwise
*/
public int handleIndexNodeUnderflow(IndexNode<K,T> left,
IndexNode<K,T> right, IndexNode<K,T> parent) {
int pos=0;
//where should the transfer begin on right node
if (right!=null){
pos=this.binarysearch(parent.keys, right.keys.get(0));
pos--;
}
int l1=0;
//make sure there exists a node on left or right
if (left!=null){
l1=left.keys.size();
}
int l2=0;
if (right!=null){
l2=right.keys.size();
}
//if there is not enough keys on left and right
if (l1+l2<2*D){
//move keys from left to right
left.keys.add(parent.keys.get(pos));
for (int i=0;i<l2;i++){
left.keys.add(right.keys.get(0));
right.keys.remove(0);
left.children.add(right.children.get(0));
right.children.remove(0);
}
if (l2>0){
left.children.add(right.children.get(0));
right.children.remove(0);}
//destory right
right=null;
//give parent position to delete the key and children
return pos;
}else{
//if there is enough keys
//detemine redistribution direction
if (l2>l1){
left.keys.add(parent.keys.get(pos));
int numtomove=D-l1;
for (int i=0;i<numtomove;i++){
left.keys.add(right.keys.get(0));
right.keys.remove(0);
left.children.add(right.children.get(0));
right.children.remove(0);
}
parent.keys.remove(pos);
parent.keys.add(pos,left.keys.get(left.keys.size()));
left.keys.remove(left.keys.size());
}
else{
right.keys.add(0,parent.keys.get(pos));
int numtomove=D-l2;
for (int i=0;i<numtomove;i++){
right.keys.add(0,left.keys.get(left.keys.size()-1));
left.keys.remove(left.keys.size()-1);
right.children.add(0,left.children.get(left.children.size()-1));
left.children.remove(left.children.size()-1);
}
//modified parent node with the correct key
parent.keys.remove(pos);
parent.keys.add(pos,right.keys.get(0));
right.keys.remove(0);
}
return -1;
}
}
}