Repository navigation
Expand file tree
/
Copy pathBTreeInnerNode.java
More file actions
215 lines (175 loc) · 5.94 KB
/
Copy pathBTreeInnerNode.java
File metadata and controls
215 lines (175 loc) · 5.94 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
class BTreeInnerNode<TKey extends Comparable<TKey>> extends BTreeNode<TKey> {
protected final static int INNERORDER = 4;
protected Object[] children;
public BTreeInnerNode() {
this.keys = new Object[INNERORDER + 1];
this.children = new Object[INNERORDER + 2];
}
@SuppressWarnings("unchecked")
public BTreeNode<TKey> getChild(int index) {
return (BTreeNode<TKey>)this.children[index];
}
public void setChild(int index, BTreeNode<TKey> child) {
this.children[index] = child;
if (child != null)
child.setParent(this);
}
@Override
public TreeNodeType getNodeType() {
return TreeNodeType.InnerNode;
}
@Override
public int search(TKey key) {
int index = 0;
for (index = 0; index < this.getKeyCount(); ++index) {
int cmp = this.getKey(index).compareTo(key);
if (cmp == 0) {
return index + 1;
}
else if (cmp > 0) {
return index;
}
}
return index;
}
/* The codes below are used to support insertion operation */
private void insertAt(int index, TKey key, BTreeNode<TKey> leftChild, BTreeNode<TKey> rightChild) {
// move space for the new key
for (int i = this.getKeyCount() + 1; i > index; --i) {
this.setChild(i, this.getChild(i - 1));
}
for (int i = this.getKeyCount(); i > index; --i) {
this.setKey(i, this.getKey(i - 1));
}
// insert the new key
this.setKey(index, key);
this.setChild(index, leftChild);
this.setChild(index + 1, rightChild);
this.keyCount += 1;
}
/**
* When splits a internal node, the middle key is kicked out and be pushed to parent node.
*/
@Override
protected BTreeNode<TKey> split() {
int midIndex = this.getKeyCount() / 2;
BTreeInnerNode<TKey> newRNode = new BTreeInnerNode<TKey>();
for (int i = midIndex + 1; i < this.getKeyCount(); ++i) {
newRNode.setKey(i - midIndex - 1, this.getKey(i));
this.setKey(i, null);
}
for (int i = midIndex + 1; i <= this.getKeyCount(); ++i) {
newRNode.setChild(i - midIndex - 1, this.getChild(i));
newRNode.getChild(i - midIndex - 1).setParent(newRNode);
this.setChild(i, null);
}
this.setKey(midIndex, null);
newRNode.keyCount = this.getKeyCount() - midIndex - 1;
this.keyCount = midIndex;
return newRNode;
}
@Override
protected BTreeNode<TKey> pushUpKey(TKey key, BTreeNode<TKey> leftChild, BTreeNode<TKey> rightNode) {
// find the target position of the new key
int index = this.search(key);
// insert the new key
this.insertAt(index, key, leftChild, rightNode);
// check whether current node need to be split
if (this.isOverflow()) {
return this.dealOverflow();
}
else {
return this.getParent() == null ? this : null;
}
}
/* The codes below are used to support delete operation */
private void deleteAt(int index) {
int i = 0;
for (i = index; i < this.getKeyCount() - 1; ++i) {
this.setKey(i, this.getKey(i + 1));
this.setChild(i + 1, this.getChild(i + 2));
}
this.setKey(i, null);
this.setChild(i + 1, null);
--this.keyCount;
}
@Override
protected void processChildrenTransfer(BTreeNode<TKey> borrower, BTreeNode<TKey> lender, int borrowIndex) {
int borrowerChildIndex = 0;
while (borrowerChildIndex < this.getKeyCount() + 1 && this.getChild(borrowerChildIndex) != borrower)
++borrowerChildIndex;
if (borrowIndex == 0) {
// borrow a key from right sibling
TKey upKey = borrower.transferFromSibling(this.getKey(borrowerChildIndex), lender, borrowIndex);
this.setKey(borrowerChildIndex, upKey);
}
else {
// borrow a key from left sibling
TKey upKey = borrower.transferFromSibling(this.getKey(borrowerChildIndex - 1), lender, borrowIndex);
this.setKey(borrowerChildIndex - 1, upKey);
}
}
@Override
protected BTreeNode<TKey> processChildrenFusion(BTreeNode<TKey> leftChild, BTreeNode<TKey> rightChild) {
int index = 0;
while (index < this.getKeyCount() && this.getChild(index) != leftChild)
++index;
TKey sinkKey = this.getKey(index);
// merge two children and the sink key into the left child node
leftChild.fusionWithSibling(sinkKey, rightChild);
// remove the sink key, keep the left child and abandon the right child
this.deleteAt(index);
// check whether need to propagate borrow or fusion to parent
if (this.isUnderflow()) {
if (this.getParent() == null) {
// current node is root, only remove keys or delete the whole root node
if (this.getKeyCount() == 0) {
leftChild.setParent(null);
return leftChild;
}
else {
return null;
}
}
return this.dealUnderflow();
}
return null;
}
@Override
protected void fusionWithSibling(TKey sinkKey, BTreeNode<TKey> rightSibling) {
BTreeInnerNode<TKey> rightSiblingNode = (BTreeInnerNode<TKey>)rightSibling;
int j = this.getKeyCount();
this.setKey(j++, sinkKey);
for (int i = 0; i < rightSiblingNode.getKeyCount(); ++i) {
this.setKey(j + i, rightSiblingNode.getKey(i));
}
for (int i = 0; i < rightSiblingNode.getKeyCount() + 1; ++i) {
this.setChild(j + i, rightSiblingNode.getChild(i));
}
this.keyCount += 1 + rightSiblingNode.getKeyCount();
this.setRightSibling(rightSiblingNode.rightSibling);
if (rightSiblingNode.rightSibling != null)
rightSiblingNode.rightSibling.setLeftSibling(this);
}
@Override
protected TKey transferFromSibling(TKey sinkKey, BTreeNode<TKey> sibling, int borrowIndex) {
BTreeInnerNode<TKey> siblingNode = (BTreeInnerNode<TKey>)sibling;
TKey upKey = null;
if (borrowIndex == 0) {
// borrow the first key from right sibling, append it to tail
int index = this.getKeyCount();
this.setKey(index, sinkKey);
this.setChild(index + 1, siblingNode.getChild(borrowIndex));
this.keyCount += 1;
upKey = siblingNode.getKey(0);
siblingNode.deleteAt(borrowIndex);
}
else {
// borrow the last key from left sibling, insert it to head
this.insertAt(0, sinkKey, siblingNode.getChild(borrowIndex + 1), this.getChild(0));
upKey = siblingNode.getKey(borrowIndex);
siblingNode.deleteAt(borrowIndex);
}
return upKey;
}
}