-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBPTreeInnerNode.java
More file actions
380 lines (343 loc) · 10.3 KB
/
Copy pathBPTreeInnerNode.java
File metadata and controls
380 lines (343 loc) · 10.3 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
import java.io.Serializable;
import java.util.ArrayList;
public class BPTreeInnerNode<T extends Comparable<T>> extends BPTreeNode<T> implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private BPTreeNode<T>[] children;
/**
* create BPTreeNode given order.
*
* @param n
*/
@SuppressWarnings("unchecked")
public BPTreeInnerNode(int n) {
super(n);
keys = new Comparable[n];
children = new BPTreeNode[n + 1];
}
/**
* get child with specified index
*
* @return Node which is child at specified index
*/
public BPTreeNode<T> getChild(int index) {
return children[index];
}
/**
* creating child at specified index
*/
public void setChild(int index, BPTreeNode<T> child) {
children[index] = child;
}
/**
* get the first child of this node.
*
* @return first child node.
*/
public BPTreeNode<T> getFirstChild() {
return children[0];
}
/**
* get the last child of this node
*
* @return last child node.
*/
public BPTreeNode<T> getLastChild() {
return children[numberOfKeys];
}
/**
* @return the minimum keys values in InnerNode
*/
public int minKeys() {
if (this.isRoot())
return 1;
return (order + 2) / 2 - 1;
}
/**
* insert given key in the corresponding index.
*
* @param key key to be inserted
* @param recordReference reference which that inserted key is located
* @param parent parent of that inserted node
* @param ptr index of pointer in the parent node pointing to the current
* node
* @return value to be pushed up to the parent.
*/
public PushUp<T> insert(T key, Ref recordReference, BPTreeInnerNode<T> parent, int ptr) {
int index = findIndex(key);
PushUp<T> pushUp = children[index].insert(key, recordReference, this, index);
if (pushUp == null)
return null;
if (this.isFull()) {
BPTreeInnerNode<T> newNode = this.split(pushUp);
Comparable<T> newKey = newNode.getFirstKey();
newNode.deleteAt(0, 0);
return new PushUp<T>(newNode, newKey);
} else {
index = 0;
while (index < numberOfKeys && getKey(index).compareTo(key) < 0)
++index;
this.insertRightAt(index, pushUp.key, pushUp.newNode);
return null;
}
}
/**
* split the inner node and adjust values and pointers.
*
* @param pushup key to be pushed up to the parent in case of splitting.
* @return Inner node after splitting
*/
@SuppressWarnings("unchecked")
public BPTreeInnerNode<T> split(PushUp<T> pushup) {
int keyIndex = this.findIndex((T) pushup.key);
int midIndex = numberOfKeys / 2 - 1;
if (keyIndex > midIndex) // split nodes evenly
++midIndex;
int totalKeys = numberOfKeys + 1;
// move keys to a new node
BPTreeInnerNode<T> newNode = new BPTreeInnerNode<T>(order);
for (int i = midIndex; i < totalKeys - 1; ++i) {
newNode.insertRightAt(i - midIndex, this.getKey(i), this.getChild(i + 1));
numberOfKeys--;
}
newNode.setChild(0, this.getChild(midIndex));
// insert the new key
if (keyIndex < totalKeys / 2)
this.insertRightAt(keyIndex, pushup.key, pushup.newNode);
else
newNode.insertRightAt(keyIndex - midIndex, pushup.key, pushup.newNode);
return newNode;
}
/**
* find the correct place index of specified key in that node.
*
* @param key to be looked for
* @return index of that given key
*/
public int findIndex(T key) {
for (int i = 0; i < numberOfKeys; ++i) {
int cmp = getKey(i).compareTo(key);
if (cmp > 0)
return i;
}
return numberOfKeys;
}
/**
* insert at given index a given key
*
* @param index where it inserts the key
* @param key to be inserted at index
*/
private void insertAt(int index, Comparable<T> key) {
for (int i = numberOfKeys; i > index; --i) {
this.setKey(i, this.getKey(i - 1));
this.setChild(i + 1, this.getChild(i));
}
this.setKey(index, key);
numberOfKeys++;
}
/**
* insert key and adjust left pointer with given child.
*
* @param index where key is inserted
* @param key to be inserted in that index
* @param leftChild child which this node points to with pointer at left of that
* index
*/
public void insertLeftAt(int index, Comparable<T> key, BPTreeNode<T> leftChild) {
insertAt(index, key);
this.setChild(index + 1, this.getChild(index));
this.setChild(index, leftChild);
}
/**
* insert key and adjust right pointer with given child.
*
* @param index where key is inserted
* @param key to be inserted in that index
* @param rightChild child which this node points to with pointer at right of
* that index
*/
public void insertRightAt(int index, Comparable<T> key, BPTreeNode<T> rightChild) {
insertAt(index, key);
this.setChild(index + 1, rightChild);
}
public Comparable minRightKey(int i) {
Comparable out;
BPTreeNode current = this.getChild(i + 1);
while (current instanceof BPTreeInnerNode) {
BPTreeInnerNode tmp = ((BPTreeInnerNode) current);
current = tmp.getChild(0);
}
out = current.getFirstKey();
return out;
}
/**
* delete key and return true or false if it is deleted or not
*/
public boolean delete(T key, BPTreeInnerNode<T> parent, int ptr, Ref ref) {
boolean done = false;
for (int i = 0; !done && i < numberOfKeys; ++i)
if (keys[i].compareTo(key) > 0)
done = children[i].delete(key, this, i, ref);
if (!done)
done = children[numberOfKeys].delete(key, this, numberOfKeys, ref);
for (int i = 0; i < numberOfKeys; i++) {
if (keys[i].compareTo(key) == 0) {
this.setKey(i, this.minRightKey(i));
}
}
if (this.numberOfKeys == 0)
this.setKey(0, this.minRightKey(0));
if (numberOfKeys < this.minKeys()) {
if (isRoot()) {
this.getFirstChild().setRoot(true);
this.setRoot(false);
return done;
}
// 1.try to borrow
if (borrow(parent, ptr))
return done;
// 2.merge
merge(parent, ptr);
}
// for (int i = 0; i < numberOfKeys; i++) {
// if (keys[i].compareTo(key) == 0) {
// this.setKey(i, this.minRightKey(i));
// }
//
// }
// if (this.numberOfKeys == 0)
// this.setKey(0, this.minRightKey(0));
return done;
}
/**
* borrow from the right sibling or left sibling in case of overflow.
*
* @param parent of the current node
* @param ptr index of pointer in the parent node pointing to the current
* node
* @return true or false if it can borrow form right sibling or left sibling or
* it can not
*/
public boolean borrow(BPTreeInnerNode<T> parent, int ptr) {
// check left sibling
if (ptr > 0) {
BPTreeInnerNode<T> leftSibling = (BPTreeInnerNode<T>) parent.getChild(ptr - 1);
if (leftSibling.numberOfKeys > leftSibling.minKeys()) {
this.insertLeftAt(0, parent.getKey(ptr - 1), leftSibling.getLastChild());
parent.deleteAt(ptr - 1);
parent.insertRightAt(ptr - 1, leftSibling.getLastKey(), this);
leftSibling.deleteAt(leftSibling.numberOfKeys - 1);
return true;
}
}
// check right sibling
if (ptr < parent.numberOfKeys) {
BPTreeInnerNode<T> rightSibling = (BPTreeInnerNode<T>) parent.getChild(ptr + 1);
if (rightSibling.numberOfKeys > rightSibling.minKeys()) {
this.insertRightAt(this.numberOfKeys, parent.getKey(ptr), rightSibling.getFirstChild());
parent.deleteAt(ptr);
parent.insertRightAt(ptr, rightSibling.getFirstKey(), rightSibling);
rightSibling.deleteAt(0, 0);
return true;
}
}
return false;
}
/**
* try to merge with left or right sibling in case of overflow
*
* @param parent of the current node
* @param ptr index of pointer in the parent node pointing to the current
* node
*/
public void merge(BPTreeInnerNode<T> parent, int ptr) {
if (ptr > 0) {
// merge with left
BPTreeInnerNode<T> leftSibling = (BPTreeInnerNode<T>) parent.getChild(ptr - 1);
leftSibling.merge(parent.getKey(ptr - 1), this);
parent.deleteAt(ptr - 1);
} else {
// merge with right
BPTreeInnerNode<T> rightSibling = (BPTreeInnerNode<T>) parent.getChild(ptr + 1);
this.merge(parent.getKey(ptr), rightSibling);
parent.deleteAt(ptr);
}
}
/**
* merge the current node with the passed node and pulling the passed key from
* the parent to be inserted with the merged node
*
* @param parentKey the pulled key from the parent to be inserted in the
* merged node
* @param foreignNode the node to be merged with the current node
*/
public void merge(Comparable<T> parentKey, BPTreeInnerNode<T> foreignNode) {
this.insertRightAt(numberOfKeys, parentKey, foreignNode.getFirstChild());
for (int i = 0; i < foreignNode.numberOfKeys; ++i)
this.insertRightAt(numberOfKeys, foreignNode.getKey(i), foreignNode.getChild(i + 1));
}
/**
* delete the key at the specified index with the option to delete the right or
* left pointer
*
* @param keyIndex the index whose key will be deleted
* @param childPtr 0 for deleting the left pointer and 1 for deleting the right
* pointer
*/
public void deleteAt(int keyIndex, int childPtr) // 0 for left and 1 for right
{
for (int i = keyIndex; i < numberOfKeys - 1; ++i) {
keys[i] = keys[i + 1];
children[i + childPtr] = children[i + childPtr + 1];
}
if (childPtr == 0)
children[numberOfKeys - 1] = children[numberOfKeys];
numberOfKeys--;
}
/**
* searches for the record reference of the specified key
*/
@Override
public Ref search(T key) {
return children[findIndex(key)].search(key);
}
/**
* delete the key at the given index and deleting its right child
*/
public void deleteAt(int index) {
deleteAt(index, 1);
}
@Override
public boolean deleteLeft(T key, BPTreeInnerNode<T> parent, int ptr, Ref ref) {
// TODO Auto-generated method stub
boolean done = false;
for (int i = 0; !done && i < numberOfKeys; ++i) {
if (keys[i].compareTo(key) == 0 || keys[i].compareTo(key) > 0) {
done = children[i].deleteLeft(key, this, i, ref);
}
}
if (!done)
done = children[numberOfKeys].deleteLeft(key, this, numberOfKeys, ref);
for (int i = 0; i < numberOfKeys; i++) {
if (keys[i].compareTo(key) == 0) {
this.setKey(i, this.minRightKey(i));
}
}
if (numberOfKeys < this.minKeys()) {
if (isRoot()) {
this.getFirstChild().setRoot(true);
this.setRoot(false);
return done;
}
// 1.try to borrow
if (borrow(parent, ptr))
return done;
// 2.merge
merge(parent, ptr);
}
return done;
}
}