2929// MARK: - BTreeNode class
3030
3131class BTreeNode < Key: Comparable , Value> {
32+ /**
33+ * The tree that owns the node.
34+ */
3235 unowned var owner : BTree < Key , Value >
3336
3437 fileprivate var keys = [ Key] ( )
35- var values = [ Value] ( )
38+ fileprivate var values = [ Value] ( )
3639 var children : [ BTreeNode ] ?
3740
3841 var isLeaf : Bool {
@@ -147,7 +150,7 @@ extension BTreeNode {
147150 * - child: the child to be split
148151 * - index: the index of the key, which will be moved up to the parent
149152 */
150- fileprivate func split( _ child: BTreeNode , at index: Int ) {
153+ private func split( _ child: BTreeNode , at index: Int ) {
151154 let middleIndex = child. numberOfKeys / 2
152155 keys. insert ( child. keys [ middleIndex] , at: index)
153156 values. insert ( child. values [ middleIndex] , at: index)
@@ -188,7 +191,7 @@ private enum BTreeNodePosition {
188191}
189192
190193extension BTreeNode {
191- fileprivate var inorderPredecessor : BTreeNode {
194+ private var inorderPredecessor : BTreeNode {
192195 if isLeaf {
193196 return self
194197 } else {
@@ -221,7 +224,7 @@ extension BTreeNode {
221224 values [ index] = predecessor. values. last!
222225 children![ index] . remove ( keys [ index] )
223226 if children![ index] . numberOfKeys < owner. order {
224- fix ( children![ index] , at: index)
227+ fix ( child : children![ index] , at: index)
225228 }
226229 }
227230 } else if key < keys [ index] {
@@ -230,7 +233,7 @@ extension BTreeNode {
230233 if let leftChild = children ? [ index] {
231234 leftChild. remove ( key)
232235 if leftChild. numberOfKeys < owner. order {
233- fix ( leftChild, at: index)
236+ fix ( child : leftChild, at: index)
234237 }
235238 } else {
236239 print ( " The key: \( key) is not in the tree. " )
@@ -241,7 +244,7 @@ extension BTreeNode {
241244 if let rightChild = children ? [ ( index + 1 ) ] {
242245 rightChild. remove ( key)
243246 if rightChild. numberOfKeys < owner. order {
244- fix ( rightChild, at: ( index + 1 ) )
247+ fix ( child : rightChild, at: ( index + 1 ) )
245248 }
246249 } else {
247250 print ( " The key: \( key) is not in the tree " )
@@ -259,16 +262,16 @@ extension BTreeNode {
259262 * - child: the child to be fixed
260263 * - index: the index of the child to be fixed in the current node
261264 */
262- fileprivate func fix( _ child: BTreeNode , at index: Int ) {
265+ private func fix( child: BTreeNode , at index: Int ) {
263266
264267 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)
266269 } 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)
268271 } else if ( index - 1 ) >= 0 {
269- merge ( child, at : index, to: . left)
272+ merge ( child: child , atIndex : index, to: . left)
270273 } else {
271- merge ( child, at : index, to: . right)
274+ merge ( child: child , atIndex : index, to: . right)
272275 }
273276 }
274277
@@ -282,7 +285,7 @@ extension BTreeNode {
282285 * - node: the node to move the key from
283286 * - position: the position of the from node relative to the targetNode
284287 */
285- fileprivate func move( keyAt index: Int , to targetNode: BTreeNode ,
288+ private func move( keyAtIndex index: Int , to targetNode: BTreeNode ,
286289 from node: BTreeNode , at position: BTreeNodePosition ) {
287290 switch position {
288291 case . left:
@@ -321,7 +324,7 @@ extension BTreeNode {
321324 * - index: the index of the child in the current node
322325 * - position: the position of the node to merge into
323326 */
324- fileprivate func merge( _ child: BTreeNode , at index: Int , to position: BTreeNodePosition ) {
327+ private func merge( child: BTreeNode , atIndex index: Int , to position: BTreeNodePosition ) {
325328 switch position {
326329 case . left:
327330 // We can merge to the left sibling
@@ -407,21 +410,21 @@ extension BTreeNode: CustomStringConvertible {
407410
408411// MARK: - BTree class
409412
410- open class BTree < Key: Comparable , Value> {
413+ public class BTree < Key: Comparable , Value> {
411414 /**
412415 * The order of the B-Tree
413416 *
414417 * The number of keys in every node should be in the [order, 2*order] range,
415418 * except the root node which is allowed to contain less keys than the value of order.
416419 */
417- open let order : Int
420+ public let order : Int
418421
419- /**
422+ /**
420423 * The root node of the tree
421424 */
422425 var rootNode : BTreeNode < Key , Value > !
423426
424- fileprivate( set) open var numberOfKeys = 0
427+ fileprivate( set) public var numberOfKeys = 0
425428
426429 /**
427430 * Designated initializer for the tree
@@ -463,7 +466,7 @@ extension BTree {
463466 * - key: the key of the value to be returned
464467 */
465468 public subscript ( key: Key ) -> Value ? {
466- return valueForKey ( key)
469+ return value ( for : key)
467470 }
468471}
469472
@@ -476,7 +479,7 @@ extension BTree {
476479 * - Parameters:
477480 * - key: the key of the value to be returned
478481 */
479- public func valueForKey ( _ key: Key ) -> Value ? {
482+ public func value ( for key: Key ) -> Value ? {
480483 guard rootNode. numberOfKeys > 0 else {
481484 return nil
482485 }
@@ -509,7 +512,7 @@ extension BTree {
509512 * - Precondition:
510513 * The root node of the tree contains `order` * 2 keys.
511514 */
512- fileprivate func splitRoot( ) {
515+ private func splitRoot( ) {
513516 let middleIndexOfOldRoot = rootNode. numberOfKeys / 2
514517
515518 let newRoot = BTreeNode < Key , Value > (
@@ -552,7 +555,7 @@ extension BTree {
552555 * - Parameters:
553556 * - key: the key to remove
554557 */
555- public func removeKey ( _ key: Key ) {
558+ public func remove ( _ key: Key ) {
556559 guard rootNode. numberOfKeys > 0 else {
557560 return
558561 }
0 commit comments