forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BTree.swift
592 lines (502 loc) · 16 KB
/
BTree.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// The MIT License (MIT)
// Copyright (c) 2016 Viktor Szilárd Simkó (aqviktor[at]gmail.com)
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/*
* B-Tree
*
* A B-Tree is a self-balancing search tree, in which nodes can have more than two children.
*/
// MARK: - BTreeNode class
class BTreeNode<Key: Comparable, Value> {
/**
* The tree that owns the node.
*/
unowned var owner: BTree<Key, Value>
fileprivate var keys = [Key]()
fileprivate var values = [Value]()
var children: [BTreeNode]?
var isLeaf: Bool {
return children == nil
}
var numberOfKeys: Int {
return keys.count
}
init(owner: BTree<Key, Value>) {
self.owner = owner
}
convenience init(owner: BTree<Key, Value>, keys: [Key],
values: [Value], children: [BTreeNode]? = nil) {
self.init(owner: owner)
self.keys += keys
self.values += values
self.children = children
}
}
// MARK: BTreeNode extesnion: Searching
extension BTreeNode {
/**
* Returns the value for a given `key`, returns nil if the `key` is not found.
*
* - Parameters:
* - key: the key of the value to be returned
*/
func value(for key: Key) -> Value? {
var index = keys.startIndex
while (index + 1) < keys.endIndex && keys[index] < key {
index = (index + 1)
}
if key == keys[index] {
return values[index]
} else if key < keys[index] {
return children?[index].value(for: key)
} else {
return children?[(index + 1)].value(for: key)
}
}
}
// MARK: BTreeNode extension: Travelsals
extension BTreeNode {
/**
* Traverses the keys in order, executes `process` for every key.
*
* - Parameters:
* - process: the closure to be executed for every key
*/
func traverseKeysInOrder(_ process: (Key) -> Void) {
for i in 0..<numberOfKeys {
children?[i].traverseKeysInOrder(process)
process(keys[i])
}
children?.last?.traverseKeysInOrder(process)
}
}
// MARK: BTreeNode extension: Insertion
extension BTreeNode {
/**
* Inserts `value` for `key` to the node, or to one if its descendants.
*
* - Parameters:
* - value: the value to be inserted for `key`
* - key: the key for the `value`
*/
func insert(_ value: Value, for key: Key) {
var index = keys.startIndex
while index < keys.endIndex && keys[index] < key {
index = (index + 1)
}
if index < keys.endIndex && keys[index] == key {
values[index] = value
return
}
if isLeaf {
keys.insert(key, at: index)
values.insert(value, at: index)
owner.numberOfKeys += 1
} else {
children![index].insert(value, for: key)
if children![index].numberOfKeys > owner.order * 2 {
split(child: children![index], atIndex: index)
}
}
}
/**
* Splits `child` at `index`.
* The key-value pair at `index` gets moved up to the parent node,
* or if there is not an parent node, then a new parent node is created.
*
* - Parameters:
* - child: the child to be split
* - index: the index of the key, which will be moved up to the parent
*/
private func split(child: BTreeNode, atIndex index: Int) {
let middleIndex = child.numberOfKeys / 2
keys.insert(child.keys[middleIndex], at: index)
values.insert(child.values[middleIndex], at: index)
child.keys.remove(at: middleIndex)
child.values.remove(at: middleIndex)
let rightSibling = BTreeNode(
owner: owner,
keys: Array(child.keys[child.keys.indices.suffix(from: middleIndex)]),
values: Array(child.values[child.values.indices.suffix(from: middleIndex)])
)
child.keys.removeSubrange(child.keys.indices.suffix(from: middleIndex))
child.values.removeSubrange(child.values.indices.suffix(from: middleIndex))
children!.insert(rightSibling, at: (index + 1))
if child.children != nil {
rightSibling.children = Array(
child.children![child.children!.indices.suffix(from: (middleIndex + 1))]
)
child.children!.removeSubrange(child.children!.indices.suffix(from: (middleIndex + 1)))
}
}
}
// MARK: BTreeNode extension: Removal
/**
* An enumeration to indicate a node's position according to another node.
*
* Possible values:
* - left
* - right
*/
private enum BTreeNodePosition {
case left
case right
}
extension BTreeNode {
private var inorderPredecessor: BTreeNode {
if isLeaf {
return self
} else {
return children!.last!.inorderPredecessor
}
}
/**
* Removes `key` and the value associated with it from the node
* or one of its descendants.
*
* - Parameters:
* - key: the key to be removed
*/
func remove(_ key: Key) {
var index = keys.startIndex
while (index + 1) < keys.endIndex && keys[index] < key {
index = (index + 1)
}
if keys[index] == key {
if isLeaf {
keys.remove(at: index)
values.remove(at: index)
owner.numberOfKeys -= 1
} else {
let predecessor = children![index].inorderPredecessor
keys[index] = predecessor.keys.last!
values[index] = predecessor.values.last!
children![index].remove(keys[index])
if children![index].numberOfKeys < owner.order {
fix(childWithTooFewKeys: children![index], atIndex: index)
}
}
} else if key < keys[index] {
// We should go to left child...
if let leftChild = children?[index] {
leftChild.remove(key)
if leftChild.numberOfKeys < owner.order {
fix(childWithTooFewKeys: leftChild, atIndex: index)
}
} else {
print("The key:\(key) is not in the tree.")
}
} else {
// We should go to right child...
if let rightChild = children?[(index + 1)] {
rightChild.remove(key)
if rightChild.numberOfKeys < owner.order {
fix(childWithTooFewKeys: rightChild, atIndex: (index + 1))
}
} else {
print("The key:\(key) is not in the tree")
}
}
}
/**
* Fixes `childWithTooFewKeys` by either moving a key to it from
* one of its neighbouring nodes, or by merging.
*
* - Precondition:
* `childWithTooFewKeys` must have less keys than the order of the tree.
*
* - Parameters:
* - child: the child to be fixed
* - index: the index of the child to be fixed in the current node
*/
private func fix(childWithTooFewKeys child: BTreeNode, atIndex index: Int) {
if (index - 1) >= 0 && children![(index - 1)].numberOfKeys > owner.order {
move(keyAtIndex: (index - 1), to: child, from: children![(index - 1)], at: .left)
} else if (index + 1) < children!.count && children![(index + 1)].numberOfKeys > owner.order {
move(keyAtIndex: index, to: child, from: children![(index + 1)], at: .right)
} else if (index - 1) >= 0 {
merge(child: child, atIndex: index, to: .left)
} else {
merge(child: child, atIndex: index, to: .right)
}
}
/**
* Moves the key at the specified `index` from `node` to
* the `targetNode` at `position`
*
* - Parameters:
* - index: the index of the key to be moved in `node`
* - targetNode: the node to move the key into
* - node: the node to move the key from
* - position: the position of the from node relative to the targetNode
*/
private func move(keyAtIndex index: Int, to targetNode: BTreeNode,
from node: BTreeNode, at position: BTreeNodePosition) {
switch position {
case .left:
targetNode.keys.insert(keys[index], at: targetNode.keys.startIndex)
targetNode.values.insert(values[index], at: targetNode.values.startIndex)
keys[index] = node.keys.last!
values[index] = node.values.last!
node.keys.removeLast()
node.values.removeLast()
if !targetNode.isLeaf {
targetNode.children!.insert(node.children!.last!,
at: targetNode.children!.startIndex)
node.children!.removeLast()
}
case .right:
targetNode.keys.insert(keys[index], at: targetNode.keys.endIndex)
targetNode.values.insert(values[index], at: targetNode.values.endIndex)
keys[index] = node.keys.first!
values[index] = node.values.first!
node.keys.removeFirst()
node.values.removeFirst()
if !targetNode.isLeaf {
targetNode.children!.insert(node.children!.first!,
at: targetNode.children!.endIndex)
node.children!.removeFirst()
}
}
}
/**
* Merges `child` at `position` to the node at the `position`.
*
* - Parameters:
* - child: the child to be merged
* - index: the index of the child in the current node
* - position: the position of the node to merge into
*/
private func merge(child: BTreeNode, atIndex index: Int, to position: BTreeNodePosition) {
switch position {
case .left:
// We can merge to the left sibling
children![(index - 1)].keys = children![(index - 1)].keys +
[keys[(index - 1)]] + child.keys
children![(index - 1)].values = children![(index - 1)].values +
[values[(index - 1)]] + child.values
keys.remove(at: (index - 1))
values.remove(at: (index - 1))
if !child.isLeaf {
children![(index - 1)].children =
children![(index - 1)].children! + child.children!
}
case .right:
// We should merge to the right sibling
children![(index + 1)].keys = child.keys + [keys[index]] +
children![(index + 1)].keys
children![(index + 1)].values = child.values + [values[index]] +
children![(index + 1)].values
keys.remove(at: index)
values.remove(at: index)
if !child.isLeaf {
children![(index + 1)].children =
child.children! + children![(index + 1)].children!
}
}
children!.remove(at: index)
}
}
// MARK: BTreeNode extension: Conversion
extension BTreeNode {
/**
* Returns an array which contains the keys from the current node
* and its descendants in order.
*/
var inorderArrayFromKeys: [Key] {
var array = [Key] ()
for i in 0..<numberOfKeys {
if let returnedArray = children?[i].inorderArrayFromKeys {
array += returnedArray
}
array += [keys[i]]
}
if let returnedArray = children?.last?.inorderArrayFromKeys {
array += returnedArray
}
return array
}
}
// MARK: BTreeNode extension: Description
extension BTreeNode: CustomStringConvertible {
/**
* Returns a String containing the preorder representation of the nodes.
*/
var description: String {
var str = "\(keys)"
if !isLeaf {
for child in children! {
str += child.description
}
}
return str
}
}
// MARK: - BTree class
public class BTree<Key: Comparable, Value> {
/**
* The order of the B-Tree
*
* The number of keys in every node should be in the [order, 2*order] range,
* except the root node which is allowed to contain less keys than the value of order.
*/
public let order: Int
/**
* The root node of the tree
*/
var rootNode: BTreeNode<Key, Value>!
fileprivate(set) public var numberOfKeys = 0
/**
* Designated initializer for the tree
*
* - Parameters:
* - order: The order of the tree.
*/
public init?(order: Int) {
guard order > 0 else {
print("Order has to be greater than 0.")
return nil
}
self.order = order
rootNode = BTreeNode<Key, Value>(owner: self)
}
}
// MARK: BTree extension: Travelsals
extension BTree {
/**
* Traverses the keys in order, executes `process` for every key.
*
* - Parameters:
* - process: the closure to be executed for every key
*/
public func traverseKeysInOrder(_ process: (Key) -> Void) {
rootNode.traverseKeysInOrder(process)
}
}
// MARK: BTree extension: Subscript
extension BTree {
/**
* Returns the value for a given `key`, returns nil if the `key` is not found.
*
* - Parameters:
* - key: the key of the value to be returned
*/
public subscript (key: Key) -> Value? {
return value(for: key)
}
}
// MARK: BTree extension: Value for Key
extension BTree {
/**
* Returns the value for a given `key`, returns nil if the `key` is not found.
*
* - Parameters:
* - key: the key of the value to be returned
*/
public func value(for key: Key) -> Value? {
guard rootNode.numberOfKeys > 0 else {
return nil
}
return rootNode.value(for: key)
}
}
// MARK: BTree extension: Insertion
extension BTree {
/**
* Inserts the `value` for the `key` into the tree.
*
* - Parameters:
* - value: the value to be inserted for `key`
* - key: the key for the `value`
*/
public func insert(_ value: Value, for key: Key) {
rootNode.insert(value, for: key)
if rootNode.numberOfKeys > order * 2 {
splitRoot()
}
}
/**
* Splits the root node of the tree.
*
* - Precondition:
* The root node of the tree contains `order` * 2 keys.
*/
private func splitRoot() {
let middleIndexOfOldRoot = rootNode.numberOfKeys / 2
let newRoot = BTreeNode<Key, Value>(
owner: self,
keys: [rootNode.keys[middleIndexOfOldRoot]],
values: [rootNode.values[middleIndexOfOldRoot]],
children: [rootNode]
)
rootNode.keys.remove(at: middleIndexOfOldRoot)
rootNode.values.remove(at: middleIndexOfOldRoot)
let newRightChild = BTreeNode<Key, Value>(
owner: self,
keys: Array(rootNode.keys[rootNode.keys.indices.suffix(from: middleIndexOfOldRoot)]),
values: Array(rootNode.values[rootNode.values.indices.suffix(from: middleIndexOfOldRoot)])
)
rootNode.keys.removeSubrange(rootNode.keys.indices.suffix(from: middleIndexOfOldRoot))
rootNode.values.removeSubrange(rootNode.values.indices.suffix(from: middleIndexOfOldRoot))
if rootNode.children != nil {
newRightChild.children = Array(
rootNode.children![rootNode.children!.indices.suffix(from: (middleIndexOfOldRoot + 1))]
)
rootNode.children!.removeSubrange(
rootNode.children!.indices.suffix(from: (middleIndexOfOldRoot + 1))
)
}
newRoot.children!.append(newRightChild)
rootNode = newRoot
}
}
// MARK: BTree extension: Removal
extension BTree {
/**
* Removes `key` and the value associated with it from the tree.
*
* - Parameters:
* - key: the key to remove
*/
public func remove(_ key: Key) {
guard rootNode.numberOfKeys > 0 else {
return
}
rootNode.remove(key)
if rootNode.numberOfKeys == 0 && !rootNode.isLeaf {
rootNode = rootNode.children!.first!
}
}
}
// MARK: BTree extension: Conversion
extension BTree {
/**
* The keys of the tree in order.
*/
public var inorderArrayFromKeys: [Key] {
return rootNode.inorderArrayFromKeys
}
}
// MARK: BTree extension: Decription
extension BTree: CustomStringConvertible {
/**
* Returns a String containing the preorder representation of the nodes.
*/
public var description: String {
return rootNode.description
}
}