The Implementation of A B Plus Tree

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
import java.util.*;

public class BPlusTree<K extends Comparable<? super K>, V> {

public static void main(String[] args) {
ArrayList<Integer> words = new ArrayList<>();

for (int i = 0; i < 10000; i++) {
words.add(i);
}

BPlusTree<Integer, Integer> tree = new BPlusTree<>();

for (Integer word : words) {
if (!tree.contains(word)) {
tree.insert(word, 0);
}

tree.set(word, tree.search(word).intValue() + 1);
}

System.out.println(tree);

System.out.println(tree.searchRange(10, RangePolicy.INCLUSIVE, 20, RangePolicy.INCLUSIVE));

for (int i = 0; i < 10000; i++) {
tree.delete(i);
}

System.out.println(tree);
}

// The branching factor used when none specified in constructor.
private static final int DEFAULT_BRANCHING_FACTOR = 128;

// The branching factor for the B+ tree, that measures the capacity of nodes
// (i.e., the number of children nodes) for internal nodes in the tree.
private int branchingFactor;

// The root node of the B+ tree.
private Node root;

public BPlusTree() {
this(DEFAULT_BRANCHING_FACTOR);
}

public BPlusTree(int branchingFactor) {
this.branchingFactor = branchingFactor;

// initially, root is a leaf node which is able to keep values. It will split when the
// value list is full. After that the root node only serves as a lookup table and becomes
// a internal node.
root = new LeafNode();
}

public V search(K key) {
return root.getValue(key);
}

public boolean contains(K key) {
return root.getValue(key) != null;
}

public List<V> searchRange(K key1, RangePolicy policy1, K key2, RangePolicy policy2) {
return root.getRange(key1, policy1, key2, policy2);
}

public void insert(K key, V value) {
root.insertValue(key, value);
}

public void set(K key, V value) {
root.insertValue(key, value);
}

public void delete(K key) {
root.deleteValue(key);
}

enum RangePolicy {
EXCLUSIVE, INCLUSIVE
}

private abstract class Node {
List<K> keys;

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, RangePolicy policy1, K key2, RangePolicy policy2);

abstract void merge(Node sibling);

abstract Node split();

abstract boolean isOverflow();

abstract boolean isUnderflow();

public String toString() {
return keys.toString();
}
}

private class LeafNode extends Node {
List<V> values;
LeafNode next;

LeafNode() {
keys = new ArrayList<>();
values = new ArrayList<>();
}

@Override
V getValue(K key) {
int loc = Collections.binarySearch(keys, key);
return loc >= 0 ? values.get(loc) : null;
}

@Override
void deleteValue(K key) {
int loc = Collections.binarySearch(keys, key);

// not found, return directly.
if (loc < 0) {
return;
}

keys.remove(loc);
values.remove(loc);
}

@Override
void insertValue(K key, V value) {

// Collections.binarySearch() returns the index of the search key if present or the insertion point if
// not found.

int loc = Collections.binarySearch(keys, key);

// loc >= 0 node binding with the given key found, otherwise not found and -loc-1 is the insertion point.
int valueIndex = loc >= 0 ? loc : -loc - 1;

// key exists, update it
if (loc >= 0) {
values.set(valueIndex, value);
}
// key not exists, add a new node
else {

// For index node, children node is always 1 more than keys
// But leaf nodes are aways of same size about keys and values.

keys.add(valueIndex, key);
values.add(valueIndex, value);
}

// This happens only when root node is still a leaf node, or the tree just consists of one level.
if (this == root && root.isOverflow()) {
Node sibling = split();

IndexNode newRoot = new IndexNode();

newRoot.keys.add(sibling.getFirstLeafKey());
newRoot.children.add(this);
newRoot.children.add(sibling);

root = newRoot;
}
}

@Override
K getFirstLeafKey() {
return keys.get(0);
}

@Override
List<V> getRange(K key1, RangePolicy policy1, K key2, RangePolicy policy2) {
List<V> result = new LinkedList<>();
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);

// (key2 >= key && EXCLUSIVE) || (key2 > key && INCLUSIVE)
boolean condition1 = (policy2 == RangePolicy.EXCLUSIVE && cmp2 >= 0) || (policy2 == RangePolicy.INCLUSIVE && cmp2 > 0);

// key overstep the right boundary of the given range
if (condition1) {
return result;
}

// (key1 > key && EXCLUSIVE) || (key1 >= key && INCLUSIVE)
boolean condition2 = ((policy1 == RangePolicy.EXCLUSIVE && cmp1 > 0) || (policy1 == RangePolicy.INCLUSIVE && cmp1 >= 0));

// (key2 < key && EXCLUSIVE) || (key2 <= key && INCLUSIVE)
boolean condition3 = ((policy2 == RangePolicy.EXCLUSIVE && cmp2 < 0) || (policy2 == RangePolicy.INCLUSIVE && cmp2 <= 0));

// key locates within the given range
if (condition2 && condition3) {
result.add(value);
}
}

node = node.next;
}

return result;
}

@Override
void merge(Node sibling) {

// leaf nodes always merge leaf nodes, index nodes will never be passed in.

LeafNode node = (LeafNode) sibling;

// Don't be afraid of overflow issue, it will automatically split if necessary.

keys.addAll(node.keys);
values.addAll(node.values);

next = node.next;
}

@Override
Node split() {
// create a new sibling, it would be much easier than transferring to an existing sibling.
LeafNode sibling = new LeafNode();

// move half of the keys to next sibling
int from = (keyNumber() + 1) / 2;
int 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;
}

@Override
boolean isOverflow() {
return values.size() > branchingFactor - 1;
}

@Override
boolean isUnderflow() {
return values.size() < branchingFactor / 2;
}
}

private class IndexNode extends Node {
List<Node> children;

IndexNode() {
this.keys = new ArrayList<>();
this.children = new ArrayList<>();
}

@Override
V getValue(K key) {
return getChild(key).getValue(key);
}

@Override
void deleteValue(K key) {
Node child = getChild(key);
child.deleteValue(key);

if (child.isUnderflow()) {
Node childLeftSibling = getChildLeftSibling(key);
Node childRightSibling = getChildRightSibling(key);

// left merge child or child merge right
Node left = childLeftSibling != null ? childLeftSibling : child;

Node right = childLeftSibling != null ? child : childRightSibling;

left.merge(right);

deleteChild(right.getFirstLeafKey());

if (left.isOverflow()) {
Node sibling = left.split();
insertChild(sibling.getFirstLeafKey(), sibling);
}

if (root.keyNumber() == 0) {
root = left;
}
}
}

@Override
void insertValue(K key, V value) {
Node child = getChild(key);

// recursively descend until reach the left node at the bottom of the tree.
child.insertValue(key, value);

// overflow check is always conducted by the parent node because it enables
// parent update the index easily..
if (child.isOverflow()) {
Node sibling = child.split();
insertChild(sibling.getFirstLeafKey(), sibling);
}

// every time after modification, check out if the root node is too full.
// the reason is that root node has no parent, hence the overflow is conducted
// by itself.
if (this == root && root.isOverflow()) {
Node sibling = split();

IndexNode newRoot = new IndexNode();

newRoot.keys.add(sibling.getFirstLeafKey());

newRoot.children.add(this);
newRoot.children.add(sibling);

root = newRoot;
}
}

@Override
K getFirstLeafKey() {
return children.get(0).getFirstLeafKey();
}

@Override
List<V> getRange(K key1, RangePolicy policy1, K key2, RangePolicy policy2) {
return getChild(key1).getRange(key1, policy1, key2, policy2);
}

@Override
void merge(Node sibling) {

// Index nodes always merge index nodes, leaf nodes will never be passed in.

IndexNode node = (IndexNode) sibling;

keys.add(node.getFirstLeafKey());
keys.addAll(node.keys);

children.addAll(node.children);
}

@Override
Node split() {
int from = keyNumber() / 2 + 1, to = keyNumber();

IndexNode sibling = new IndexNode();

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;
}

@Override
boolean isOverflow() {
return children.size() > branchingFactor;
}

@Override
boolean isUnderflow() {
return children.size() < (branchingFactor + 1) / 2;
}

Node getChild(K key) {
int loc = Collections.binarySearch(keys, key);

int childIndex = loc >= 0 ? loc + 1 : -loc - 1;

return children.get(childIndex);
}

void deleteChild(K key) {
int loc = Collections.binarySearch(keys, key);

// 404, return directly
if (loc < 0) {
return;
}

keys.remove(loc);
children.remove(loc + 1);
}

void insertChild(K key, Node child) {
// child is the newly created node, key is the first node key of the child node
int loc = Collections.binarySearch(keys, key);

int childIndex = loc >= 0 ? loc + 1 : -loc - 1;

// index already exists, update it
if (loc >= 0) {
children.set(childIndex, child);
}
// index not exits, create one as a reference to the child node
else {

// For index node, children node is always 1 more than keys
// But leaf nodes are aways of same size about keys and values.

keys.add(childIndex, key);
children.add(childIndex + 1, child);
}
}

Node getChildLeftSibling(K key) {

// two children associated with a single index key, < the key is the left child, >= the key is the right child.

// loc >= 0 means found, the position next to it is the insertion point.
// loc < 0 means not found, -loc-1 is the insertion point
int loc = Collections.binarySearch(keys, key);

// 4 situations

// 1. key in keys, then loc >= 0
// 2. key < keys.get(0), then loc = -1
// 3. key == keys.get(0), then loc = 0
// 4. key in range (keys.get(0), keys.get(size-1)], then 1<=loc<=size when key found, or -size-1<=loc<=-2 if not found

// key exits
if (loc >= 0) {
// children.get(loc)>= key and children.get(loc-1) < key, so it is the left sibling
return children.get(loc - 1);
}

// keyIndex == 0 means key < keys.get(0)
// keyIndex == 1 means keys.get(1) > key >= keys.get(0)

// key not exits, but the insertion point is not the head of the key list
if (loc < -1) {
// -loc-1 is the insertion point which is larger than given key, so return the left sibling of children.get(-loc-1)
return children.get(-loc - 2);
}

return null;
}

Node getChildRightSibling(K key) {

// see also above getChildLeftSibling()

int loc = Collections.binarySearch(keys, key);

int childIndex = loc >= 0 ? loc + 1 : -loc - 1;

// children.size = keys.size + 1

// childrenIndex == key.size() means values binding with given key is in children.get(childrenIndex+1)
// which is the right most node without any right sibling nodes.
if (childIndex < keyNumber()) {
return children.get(childIndex + 1);
}

return null;
}
}
}

References

  1. https://github.com/jiaguofang/b-plus-tree