29
29
// MARK: - BTreeNode class
30
30
31
31
class BTreeNode < Key: Comparable , Value> {
32
+ /**
33
+ * The tree that owns the node.
34
+ */
32
35
unowned var owner : BTree < Key , Value >
33
36
34
37
fileprivate var keys = [ Key] ( )
35
- var values = [ Value] ( )
38
+ fileprivate var values = [ Value] ( )
36
39
var children : [ BTreeNode ] ?
37
40
38
41
var isLeaf : Bool {
@@ -147,7 +150,7 @@ extension BTreeNode {
147
150
* - child: the child to be split
148
151
* - index: the index of the key, which will be moved up to the parent
149
152
*/
150
- fileprivate func split( _ child: BTreeNode , at index: Int ) {
153
+ private func split( _ child: BTreeNode , at index: Int ) {
151
154
let middleIndex = child. numberOfKeys / 2
152
155
keys. insert ( child. keys [ middleIndex] , at: index)
153
156
values. insert ( child. values [ middleIndex] , at: index)
@@ -188,7 +191,7 @@ private enum BTreeNodePosition {
188
191
}
189
192
190
193
extension BTreeNode {
191
- fileprivate var inorderPredecessor : BTreeNode {
194
+ private var inorderPredecessor : BTreeNode {
192
195
if isLeaf {
193
196
return self
194
197
} else {
@@ -221,7 +224,7 @@ extension BTreeNode {
221
224
values [ index] = predecessor. values. last!
222
225
children![ index] . remove ( keys [ index] )
223
226
if children![ index] . numberOfKeys < owner. order {
224
- fix ( children![ index] , at: index)
227
+ fix ( child : children![ index] , at: index)
225
228
}
226
229
}
227
230
} else if key < keys [ index] {
@@ -230,7 +233,7 @@ extension BTreeNode {
230
233
if let leftChild = children ? [ index] {
231
234
leftChild. remove ( key)
232
235
if leftChild. numberOfKeys < owner. order {
233
- fix ( leftChild, at: index)
236
+ fix ( child : leftChild, at: index)
234
237
}
235
238
} else {
236
239
print ( " The key: \( key) is not in the tree. " )
@@ -241,7 +244,7 @@ extension BTreeNode {
241
244
if let rightChild = children ? [ ( index + 1 ) ] {
242
245
rightChild. remove ( key)
243
246
if rightChild. numberOfKeys < owner. order {
244
- fix ( rightChild, at: ( index + 1 ) )
247
+ fix ( child : rightChild, at: ( index + 1 ) )
245
248
}
246
249
} else {
247
250
print ( " The key: \( key) is not in the tree " )
@@ -259,16 +262,16 @@ extension BTreeNode {
259
262
* - child: the child to be fixed
260
263
* - index: the index of the child to be fixed in the current node
261
264
*/
262
- fileprivate func fix( _ child: BTreeNode , at index: Int ) {
265
+ private func fix( child: BTreeNode , at index: Int ) {
263
266
264
267
if ( index - 1 ) >= 0 && children![ ( index - 1 ) ] . numberOfKeys > owner. order {
265
- move ( keyAt : ( index - 1 ) , to: child, from: children![ ( index - 1 ) ] , at: . left)
268
+ move ( keyAtIndex : ( index - 1 ) , to: child, from: children![ ( index - 1 ) ] , at: . left)
266
269
} else if ( index + 1 ) < children!. count && children![ ( index + 1 ) ] . numberOfKeys > owner. order {
267
- move ( keyAt : index, to: child, from: children![ ( index + 1 ) ] , at: . right)
270
+ move ( keyAtIndex : index, to: child, from: children![ ( index + 1 ) ] , at: . right)
268
271
} else if ( index - 1 ) >= 0 {
269
- merge ( child, at : index, to: . left)
272
+ merge ( child: child , atIndex : index, to: . left)
270
273
} else {
271
- merge ( child, at : index, to: . right)
274
+ merge ( child: child , atIndex : index, to: . right)
272
275
}
273
276
}
274
277
@@ -282,7 +285,7 @@ extension BTreeNode {
282
285
* - node: the node to move the key from
283
286
* - position: the position of the from node relative to the targetNode
284
287
*/
285
- fileprivate func move( keyAt index: Int , to targetNode: BTreeNode ,
288
+ private func move( keyAtIndex index: Int , to targetNode: BTreeNode ,
286
289
from node: BTreeNode , at position: BTreeNodePosition ) {
287
290
switch position {
288
291
case . left:
@@ -321,7 +324,7 @@ extension BTreeNode {
321
324
* - index: the index of the child in the current node
322
325
* - position: the position of the node to merge into
323
326
*/
324
- fileprivate func merge( _ child: BTreeNode , at index: Int , to position: BTreeNodePosition ) {
327
+ private func merge( child: BTreeNode , atIndex index: Int , to position: BTreeNodePosition ) {
325
328
switch position {
326
329
case . left:
327
330
// We can merge to the left sibling
@@ -407,21 +410,21 @@ extension BTreeNode: CustomStringConvertible {
407
410
408
411
// MARK: - BTree class
409
412
410
- open class BTree < Key: Comparable , Value> {
413
+ public class BTree < Key: Comparable , Value> {
411
414
/**
412
415
* The order of the B-Tree
413
416
*
414
417
* The number of keys in every node should be in the [order, 2*order] range,
415
418
* except the root node which is allowed to contain less keys than the value of order.
416
419
*/
417
- open let order : Int
420
+ public let order : Int
418
421
419
- /**
422
+ /**
420
423
* The root node of the tree
421
424
*/
422
425
var rootNode : BTreeNode < Key , Value > !
423
426
424
- fileprivate( set) open var numberOfKeys = 0
427
+ fileprivate( set) public var numberOfKeys = 0
425
428
426
429
/**
427
430
* Designated initializer for the tree
@@ -463,7 +466,7 @@ extension BTree {
463
466
* - key: the key of the value to be returned
464
467
*/
465
468
public subscript ( key: Key ) -> Value ? {
466
- return valueForKey ( key)
469
+ return value ( for : key)
467
470
}
468
471
}
469
472
@@ -476,7 +479,7 @@ extension BTree {
476
479
* - Parameters:
477
480
* - key: the key of the value to be returned
478
481
*/
479
- public func valueForKey ( _ key: Key ) -> Value ? {
482
+ public func value ( for key: Key ) -> Value ? {
480
483
guard rootNode. numberOfKeys > 0 else {
481
484
return nil
482
485
}
@@ -509,7 +512,7 @@ extension BTree {
509
512
* - Precondition:
510
513
* The root node of the tree contains `order` * 2 keys.
511
514
*/
512
- fileprivate func splitRoot( ) {
515
+ private func splitRoot( ) {
513
516
let middleIndexOfOldRoot = rootNode. numberOfKeys / 2
514
517
515
518
let newRoot = BTreeNode < Key , Value > (
@@ -552,7 +555,7 @@ extension BTree {
552
555
* - Parameters:
553
556
* - key: the key to remove
554
557
*/
555
- public func removeKey ( _ key: Key ) {
558
+ public func remove ( _ key: Key ) {
556
559
guard rootNode. numberOfKeys > 0 else {
557
560
return
558
561
}
0 commit comments