20
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
// SOFTWARE.
22
22
23
- public class TreeNode < Key: Comparable , Payload> {
23
+ open class TreeNode < Key: Comparable , Payload> {
24
24
public typealias Node = TreeNode < Key , Payload >
25
25
26
- public var payload : Payload ?
26
+ open var payload : Payload ?
27
27
28
- private var key : Key
28
+ fileprivate var key : Key
29
29
internal var leftChild : Node ?
30
30
internal var rightChild : Node ?
31
- private var height : Int
32
- weak private var parent : Node ?
31
+ fileprivate var height : Int
32
+ weak fileprivate var parent : Node ?
33
33
34
34
public init ( key: Key , payload: Payload ? , leftChild: Node ? , rightChild: Node ? , parent: Node ? , height: Int ) {
35
35
self . key = key
@@ -51,46 +51,46 @@ public class TreeNode<Key: Comparable, Payload> {
51
51
self . init ( key: key, payload: nil )
52
52
}
53
53
54
- public var isRoot : Bool {
54
+ open var isRoot : Bool {
55
55
return parent == nil
56
56
}
57
57
58
- public var isLeaf : Bool {
58
+ open var isLeaf : Bool {
59
59
return rightChild == nil && leftChild == nil
60
60
}
61
61
62
- public var isLeftChild : Bool {
62
+ open var isLeftChild : Bool {
63
63
return parent? . leftChild === self
64
64
}
65
65
66
- public var isRightChild : Bool {
66
+ open var isRightChild : Bool {
67
67
return parent? . rightChild === self
68
68
}
69
69
70
- public var hasLeftChild : Bool {
70
+ open var hasLeftChild : Bool {
71
71
return leftChild != nil
72
72
}
73
73
74
- public var hasRightChild : Bool {
74
+ open var hasRightChild : Bool {
75
75
return rightChild != nil
76
76
}
77
77
78
- public var hasAnyChild : Bool {
78
+ open var hasAnyChild : Bool {
79
79
return leftChild != nil || rightChild != nil
80
80
}
81
81
82
- public var hasBothChildren : Bool {
82
+ open var hasBothChildren : Bool {
83
83
return leftChild != nil && rightChild != nil
84
84
}
85
85
}
86
86
87
87
// MARK: - The AVL tree
88
88
89
- public class AVLTree < Key: Comparable , Payload> {
89
+ open class AVLTree < Key: Comparable , Payload> {
90
90
public typealias Node = TreeNode < Key , Payload >
91
91
92
- private ( set) public var root : Node ?
93
- private ( set) public var size = 0
92
+ fileprivate ( set) open var root : Node ?
93
+ fileprivate ( set) open var size = 0
94
94
95
95
public init ( ) { }
96
96
}
@@ -119,15 +119,15 @@ extension AVLTree {
119
119
set { insert ( key, newValue) }
120
120
}
121
121
122
- public func search( input: Key ) -> Payload ? {
122
+ public func search( _ input: Key ) -> Payload ? {
123
123
if let result = search ( input, root) {
124
124
return result. payload
125
125
} else {
126
126
return nil
127
127
}
128
128
}
129
129
130
- private func search( key: Key , _ node: Node ? ) -> Node ? {
130
+ fileprivate func search( _ key: Key , _ node: Node ? ) -> Node ? {
131
131
if let node = node {
132
132
if key == node. key {
133
133
return node
@@ -144,7 +144,7 @@ extension AVLTree {
144
144
// MARK: - Inserting new items
145
145
146
146
extension AVLTree {
147
- public func insert( key: Key , _ payload: Payload ? = nil ) {
147
+ public func insert( _ key: Key , _ payload: Payload ? = nil ) {
148
148
if let root = root {
149
149
insert ( key, payload, root)
150
150
} else {
@@ -153,7 +153,7 @@ extension AVLTree {
153
153
size += 1
154
154
}
155
155
156
- private func insert( input: Key , _ payload: Payload ? , _ node: Node ) {
156
+ fileprivate func insert( _ input: Key , _ payload: Payload ? , _ node: Node ) {
157
157
if input < node. key {
158
158
if let child = node. leftChild {
159
159
insert ( input, payload, child)
@@ -177,7 +177,7 @@ extension AVLTree {
177
177
// MARK: - Balancing tree
178
178
179
179
extension AVLTree {
180
- private func updateHeightUpwards( node: Node ? ) {
180
+ fileprivate func updateHeightUpwards( _ node: Node ? ) {
181
181
if let node = node {
182
182
let lHeight = node. leftChild? . height ?? 0
183
183
let rHeight = node. rightChild? . height ?? 0
@@ -186,22 +186,22 @@ extension AVLTree {
186
186
}
187
187
}
188
188
189
- private func lrDifference( node: Node ? ) -> Int {
189
+ fileprivate func lrDifference( _ node: Node ? ) -> Int {
190
190
let lHeight = node? . leftChild? . height ?? 0
191
191
let rHeight = node? . rightChild? . height ?? 0
192
192
return lHeight - rHeight
193
193
}
194
194
195
- private func balance( node: Node ? ) {
195
+ fileprivate func balance( _ node: Node ? ) {
196
196
guard let node = node else {
197
197
return
198
198
}
199
199
200
200
updateHeightUpwards ( node. leftChild)
201
201
updateHeightUpwards ( node. rightChild)
202
202
203
- var nodes = [ Node? ] ( count : 3 , repeatedValue : nil )
204
- var subtrees = [ Node? ] ( count : 4 , repeatedValue : nil )
203
+ var nodes = [ Node? ] ( repeating : nil , count : 3 )
204
+ var subtrees = [ Node? ] ( repeating : nil , count : 4 )
205
205
let nodeParent = node. parent
206
206
207
207
let lrFactor = lrDifference ( node)
@@ -295,7 +295,7 @@ extension AVLTree {
295
295
// MARK: - Displaying tree
296
296
297
297
extension AVLTree {
298
- private func display( node: Node ? , level: Int ) {
298
+ fileprivate func display( _ node: Node ? , level: Int ) {
299
299
if let node = node {
300
300
display ( node. rightChild, level: level + 1 )
301
301
print ( " " )
@@ -310,7 +310,7 @@ extension AVLTree {
310
310
}
311
311
}
312
312
313
- public func display( node: Node ) {
313
+ public func display( _ node: Node ) {
314
314
display ( node, level: 0 )
315
315
print ( " " )
316
316
}
@@ -319,7 +319,7 @@ extension AVLTree {
319
319
// MARK: - Delete node
320
320
321
321
extension AVLTree {
322
- public func delete( key: Key ) {
322
+ public func delete( _ key: Key ) {
323
323
if size == 1 {
324
324
root = nil
325
325
size -= 1
@@ -329,7 +329,7 @@ extension AVLTree {
329
329
}
330
330
}
331
331
332
- private func delete( node: Node ) {
332
+ fileprivate func delete( _ node: Node ) {
333
333
if node. isLeaf {
334
334
// Just remove and balance up
335
335
if let parent = node. parent {
@@ -351,11 +351,11 @@ extension AVLTree {
351
351
}
352
352
} else {
353
353
// Handle stem cases
354
- if let replacement = node. leftChild? . maximum ( ) where replacement !== node {
354
+ if let replacement = node. leftChild? . maximum ( ) , replacement !== node {
355
355
node. key = replacement. key
356
356
node. payload = replacement. payload
357
357
delete ( replacement)
358
- } else if let replacement = node. rightChild? . minimum ( ) where replacement !== node {
358
+ } else if let replacement = node. rightChild? . minimum ( ) , replacement !== node {
359
359
node. key = replacement. key
360
360
node. payload = replacement. payload
361
361
delete ( replacement)
0 commit comments