Skip to content

Commit 3bc8095

Browse files
committed
corrected comments, tweaked method naming
1 parent 6db2dd7 commit 3bc8095

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

B-Tree/BTree.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

B-Tree/Tests/Tests/BTreeTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class BTreeTests: XCTestCase {
108108
}
109109

110110
do {
111-
try bTree.checkBalanced()
111+
try bTree.checkBalance()
112112
} catch {
113113
XCTFail("BTree is not balanced")
114114
}
@@ -126,7 +126,7 @@ class BTreeTests: XCTestCase {
126126
XCTAssertNil(bTree[20])
127127

128128
do {
129-
try bTree.checkBalanced()
129+
try bTree.checkBalance()
130130
} catch {
131131
XCTFail("BTree is not balanced")
132132
}
@@ -140,7 +140,7 @@ class BTreeTests: XCTestCase {
140140
XCTAssertNil(bTree[1])
141141

142142
do {
143-
try bTree.checkBalanced()
143+
try bTree.checkBalance()
144144
} catch {
145145
XCTFail("BTree is not balanced")
146146
}
@@ -156,7 +156,7 @@ class BTreeTests: XCTestCase {
156156
XCTAssertNil(bTree[9])
157157

158158
do {
159-
try bTree.checkBalanced()
159+
try bTree.checkBalance()
160160
} catch {
161161
XCTFail("BTree is not balanced")
162162
}
@@ -173,7 +173,7 @@ class BTreeTests: XCTestCase {
173173
XCTAssertNil(bTree[9])
174174

175175
do {
176-
try bTree.checkBalanced()
176+
try bTree.checkBalance()
177177
} catch {
178178
XCTFail("BTree is not balanced")
179179
}
@@ -189,7 +189,7 @@ class BTreeTests: XCTestCase {
189189
}
190190

191191
do {
192-
try bTree.checkBalanced()
192+
try bTree.checkBalance()
193193
} catch {
194194
XCTFail("BTree is not balanced")
195195
}
@@ -215,7 +215,7 @@ enum BTreeError: Error {
215215
}
216216

217217
extension BTreeNode {
218-
func checkBalanced(isRoot root: Bool) throws {
218+
func checkBalance(isRoot root: Bool) throws {
219219
if numberOfKeys > owner.order * 2 {
220220
throw BTreeError.tooManyNodes
221221
} else if !root && numberOfKeys < owner.order {
@@ -224,7 +224,7 @@ extension BTreeNode {
224224

225225
if !isLeaf {
226226
for child in children! {
227-
try child.checkBalanced(isRoot: false)
227+
try child.checkBalance(isRoot: false)
228228
}
229229
}
230230
}
@@ -242,7 +242,7 @@ extension BTree where Key: SignedInteger, Value: SignedInteger {
242242
}
243243
}
244244

245-
func checkBalanced() throws {
246-
try rootNode.checkBalanced(isRoot: true)
245+
func checkBalance() throws {
246+
try rootNode.checkBalance(isRoot: true)
247247
}
248248
}

0 commit comments

Comments
 (0)