@@ -136,7 +136,7 @@ extension BTreeNode {
136136 } else {
137137 children![ index] . insert ( value, for: key)
138138 if children![ index] . numberOfKeys > owner. order * 2 {
139- split ( children![ index] , at : index)
139+ split ( child : children![ index] , atIndex : index)
140140 }
141141 }
142142 }
@@ -150,7 +150,7 @@ extension BTreeNode {
150150 * - child: the child to be split
151151 * - index: the index of the key, which will be moved up to the parent
152152 */
153- private func split( _ child: BTreeNode , at index: Int ) {
153+ private func split( child: BTreeNode , atIndex index: Int ) {
154154 let middleIndex = child. numberOfKeys / 2
155155 keys. insert ( child. keys [ middleIndex] , at: index)
156156 values. insert ( child. values [ middleIndex] , at: index)
@@ -224,7 +224,7 @@ extension BTreeNode {
224224 values [ index] = predecessor. values. last!
225225 children![ index] . remove ( keys [ index] )
226226 if children![ index] . numberOfKeys < owner. order {
227- fix ( child : children![ index] , at : index)
227+ fix ( childWithTooFewKeys : children![ index] , atIndex : index)
228228 }
229229 }
230230 } else if key < keys [ index] {
@@ -233,7 +233,7 @@ extension BTreeNode {
233233 if let leftChild = children ? [ index] {
234234 leftChild. remove ( key)
235235 if leftChild. numberOfKeys < owner. order {
236- fix ( child : leftChild, at : index)
236+ fix ( childWithTooFewKeys : leftChild, atIndex : index)
237237 }
238238 } else {
239239 print ( " The key: \( key) is not in the tree. " )
@@ -244,7 +244,7 @@ extension BTreeNode {
244244 if let rightChild = children ? [ ( index + 1 ) ] {
245245 rightChild. remove ( key)
246246 if rightChild. numberOfKeys < owner. order {
247- fix ( child : rightChild, at : ( index + 1 ) )
247+ fix ( childWithTooFewKeys : rightChild, atIndex : ( index + 1 ) )
248248 }
249249 } else {
250250 print ( " The key: \( key) is not in the tree " )
@@ -253,16 +253,17 @@ extension BTreeNode {
253253 }
254254
255255 /**
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.
260261 *
261262 * - Parameters:
262263 * - child: the child to be fixed
263264 * - index: the index of the child to be fixed in the current node
264265 */
265- private func fix( child: BTreeNode , at index: Int ) {
266+ private func fix( childWithTooFewKeys child: BTreeNode , atIndex index: Int ) {
266267
267268 if ( index - 1 ) >= 0 && children![ ( index - 1 ) ] . numberOfKeys > owner. order {
268269 move ( keyAtIndex: ( index - 1 ) , to: child, from: children![ ( index - 1 ) ] , at: . left)
0 commit comments