@@ -136,7 +136,7 @@ extension BTreeNode {
136
136
} else {
137
137
children![ index] . insert ( value, for: key)
138
138
if children![ index] . numberOfKeys > owner. order * 2 {
139
- split ( children![ index] , at : index)
139
+ split ( child : children![ index] , atIndex : index)
140
140
}
141
141
}
142
142
}
@@ -150,7 +150,7 @@ extension BTreeNode {
150
150
* - child: the child to be split
151
151
* - index: the index of the key, which will be moved up to the parent
152
152
*/
153
- private func split( _ child: BTreeNode , at index: Int ) {
153
+ private func split( child: BTreeNode , atIndex index: Int ) {
154
154
let middleIndex = child. numberOfKeys / 2
155
155
keys. insert ( child. keys [ middleIndex] , at: index)
156
156
values. insert ( child. values [ middleIndex] , at: index)
@@ -224,7 +224,7 @@ extension BTreeNode {
224
224
values [ index] = predecessor. values. last!
225
225
children![ index] . remove ( keys [ index] )
226
226
if children![ index] . numberOfKeys < owner. order {
227
- fix ( child : children![ index] , at : index)
227
+ fix ( childWithTooFewKeys : children![ index] , atIndex : index)
228
228
}
229
229
}
230
230
} else if key < keys [ index] {
@@ -233,7 +233,7 @@ extension BTreeNode {
233
233
if let leftChild = children ? [ index] {
234
234
leftChild. remove ( key)
235
235
if leftChild. numberOfKeys < owner. order {
236
- fix ( child : leftChild, at : index)
236
+ fix ( childWithTooFewKeys : leftChild, atIndex : index)
237
237
}
238
238
} else {
239
239
print ( " The key: \( key) is not in the tree. " )
@@ -244,7 +244,7 @@ extension BTreeNode {
244
244
if let rightChild = children ? [ ( index + 1 ) ] {
245
245
rightChild. remove ( key)
246
246
if rightChild. numberOfKeys < owner. order {
247
- fix ( child : rightChild, at : ( index + 1 ) )
247
+ fix ( childWithTooFewKeys : rightChild, atIndex : ( index + 1 ) )
248
248
}
249
249
} else {
250
250
print ( " The key: \( key) is not in the tree " )
@@ -253,16 +253,17 @@ extension BTreeNode {
253
253
}
254
254
255
255
/**
256
- * Fixes the `child` at `index`.
257
- * If `child` contains too many children then it moves a child to one of
258
- * `child`'s neighbouring nodes.
259
- * If `child` contains too few children then it merges it with one of its neighbours.
256
+ * Fixes `childWithTooFewKeys` by either moving a key to it from
257
+ * one of its neighbouring nodes, or by merging.
258
+ *
259
+ * - Precondition:
260
+ * `childWithTooFewKeys` must have less keys than the order of the tree.
260
261
*
261
262
* - Parameters:
262
263
* - child: the child to be fixed
263
264
* - index: the index of the child to be fixed in the current node
264
265
*/
265
- private func fix( child: BTreeNode , at index: Int ) {
266
+ private func fix( childWithTooFewKeys child: BTreeNode , atIndex index: Int ) {
266
267
267
268
if ( index - 1 ) >= 0 && children![ ( index - 1 ) ] . numberOfKeys > owner. order {
268
269
move ( keyAtIndex: ( index - 1 ) , to: child, from: children![ ( index - 1 ) ] , at: . left)
0 commit comments