23
23
public class TreeNode < Key: Comparable , Payload> {
24
24
public typealias Node = TreeNode < Key , Payload >
25
25
26
- public var payload : Payload ?
26
+ 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
+ var isRoot : Bool {
55
55
return parent == nil
56
56
}
57
57
58
- public var isLeaf : Bool {
58
+ var isLeaf : Bool {
59
59
return rightChild == nil && leftChild == nil
60
60
}
61
61
62
- public var isLeftChild : Bool {
62
+ var isLeftChild : Bool {
63
63
return parent? . leftChild === self
64
64
}
65
65
66
- public var isRightChild : Bool {
66
+ var isRightChild : Bool {
67
67
return parent? . rightChild === self
68
68
}
69
69
70
- public var hasLeftChild : Bool {
70
+ var hasLeftChild : Bool {
71
71
return leftChild != nil
72
72
}
73
73
74
- public var hasRightChild : Bool {
74
+ var hasRightChild : Bool {
75
75
return rightChild != nil
76
76
}
77
77
78
- public var hasAnyChild : Bool {
78
+ var hasAnyChild : Bool {
79
79
return leftChild != nil || rightChild != nil
80
80
}
81
81
82
- public var hasBothChildren : Bool {
82
+ 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) var root : Node ?
93
+ fileprivate ( set) var size = 0
94
94
95
95
public init ( ) { }
96
96
}
@@ -115,26 +115,26 @@ extension TreeNode {
115
115
116
116
extension AVLTree {
117
117
subscript( key: Key ) -> Payload ? {
118
- get { return search ( key) }
119
- set { insert ( key, newValue) }
118
+ get { return search ( input : key) }
119
+ set { insert ( key: key , payload : newValue) }
120
120
}
121
121
122
122
public func search( input: Key ) -> Payload ? {
123
- if let result = search ( input, root) {
123
+ if let result = search ( key : input, node : 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
134
134
} else if key < node. key {
135
- return search ( key, node. leftChild)
135
+ return search ( key: key , node : node. leftChild)
136
136
} else {
137
- return search ( key, node. rightChild)
137
+ return search ( key: key , node : node. rightChild)
138
138
}
139
139
}
140
140
return nil
@@ -144,31 +144,31 @@ 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
- insert ( key, payload, root)
149
+ insert ( input : key, payload: payload , node : root)
150
150
} else {
151
151
root = Node ( key: key, payload: payload)
152
152
}
153
153
size += 1
154
154
}
155
155
156
- private func insert( input: Key ,
F987
_ payload: Payload ? , _ node: Node ) {
156
+ private func insert( input: Key , payload: Payload ? , node: Node ) {
157
157
if input < node. key {
158
158
if let child = node. leftChild {
159
- insert ( input, payload, child)
159
+ insert ( input: input , payload: payload , node : child)
160
160
} else {
161
161
let child = Node ( key: input, payload: payload, leftChild: nil , rightChild: nil , parent: node, height: 1 )
162
162
node. leftChild = child
163
- balance ( child)
163
+ balance ( node : child)
164
164
}
165
165
} else {
166
166
if let child = node. rightChild {
167
- insert ( input, payload, child)
167
+ insert ( input: input , payload: payload , node : child)
168
168
} else {
169
169
let child = Node ( key: input, payload: payload, leftChild: nil , rightChild: nil , parent: node, height: 1 )
170
170
node. rightChild = child
171
- balance ( child)
171
+ balance ( node : child)
172
172
}
173
173
}
174
174
}
@@ -177,37 +177,37 @@ 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
184
184
node. height = max ( lHeight, rHeight) + 1
185
- updateHeightUpwards ( node. parent)
185
+ updateHeightUpwards ( node: node . parent)
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
- updateHeightUpwards ( node. leftChild)
201
- updateHeightUpwards ( node. rightChild)
200
+ updateHeightUpwards ( node: node . leftChild)
201
+ updateHeightUpwards ( node: 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
- let lrFactor = lrDifference ( node)
207
+ let lrFactor = lrDifference ( node: node )
208
208
if lrFactor > 1 {
209
209
// left-left or left-right
210
- if lrDifference ( node. leftChild) > 0 {
210
+ if lrDifference ( node: node . leftChild) > 0 {
211
211
// left-left
212
212
nodes [ 0 ] = node
213
213
nodes [ 2 ] = node. leftChild
@@ -230,7 +230,7 @@ extension AVLTree {
230
230
}
231
231
} else if lrFactor < - 1 {
232
232
// right-left or right-right
233
- if lrDifference ( node. rightChild) < 0 {
233
+ if lrDifference ( node: node . rightChild) < 0 {
234
234
// right-right
235
235
nodes [ 1 ] = node
236
236
nodes [ 2 ] = node. rightChild
@@ -253,7 +253,7 @@ extension AVLTree {
253
253
}
254
254
} else {
255
255
// Don't need to balance 'node', go for parent
256
- balance ( node. parent)
256
+ balance ( node: node . parent)
257
257
return
258
258
}
259
259
@@ -285,19 +285,19 @@ extension AVLTree {
285
285
nodes [ 0 ] ? . rightChild = subtrees [ 3 ]
286
286
subtrees [ 3 ] ? . parent = nodes [ 0 ]
287
287
288
- updateHeightUpwards ( nodes [ 1 ] ) // Update height from left
289
- updateHeightUpwards ( nodes [ 0 ] ) // Update height from right
288
+ updateHeightUpwards ( node : nodes [ 1 ] ) // Update height from left
289
+ updateHeightUpwards ( node : nodes [ 0 ] ) // Update height from right
290
290
291
- balance ( nodes [ 2 ] ? . parent)
291
+ balance ( node : nodes [ 2 ] ? . parent)
292
292
}
293
293
}
294
294
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
- display ( node. rightChild, level: level + 1 )
300
+ display ( node: node . rightChild, level: level + 1 )
301
301
print ( " " )
302
302
if node. isRoot {
303
303
print ( " Root -> " , terminator: " " )
@@ -306,12 +306,12 @@ extension AVLTree {
306
306
print ( " " , terminator: " " )
307
307
}
308
308
print ( " ( \( node. key) : \( node. height) ) " , terminator: " " )
309
- display ( node. leftChild, level: level + 1 )
309
+ display ( node: node . leftChild, level: level + 1 )
310
310
}
311
311
}
312
312
313
313
public func display( node: Node ) {
314
- display ( node, level: 0 )
314
+ display ( node: node , level: 0 )
315
315
print ( " " )
316
316
}
317
317
}
@@ -323,8 +323,8 @@ extension AVLTree {
323
323
if size == 1 {
324
324
root = nil
325
325
size -= 1
326
- } else if let node = search ( key, root) {
327
- delete ( node)
326
+ } else if let node = search ( key: key , node : root) {
327
+ delete ( node: node )
328
328
size -= 1
329
329
}
330
330
}
@@ -344,21 +344,21 @@ extension AVLTree {
344
344
parent. rightChild = nil
345
345
}
346
346
347
- balance ( parent)
347
+ balance ( node : parent)
348
348
} else {
349
349
// at root
350
350
root = nil
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
- delete ( replacement)
358
- } else if let replacement = node. rightChild? . minimum ( ) where replacement !== node {
357
+ delete ( node : replacement)
358
+ } else if let replacement = node. rightChild? . minimum ( ) , replacement !== node {
359
359
node. key = replacement. key
360
360
node. payload = replacement. payload
361
- delete ( replacement)
361
+ delete ( node : replacement)
362
362
}
363
363
}
364
364
}
0 commit comments