Skip to content

Commit cfb23c4

Browse files
author
Chris Pilcher
committed
Removing "open" access level that was automatically included in auto migration to swift 3. Removing underscore labels.
1 parent 4683bb9 commit cfb23c4

File tree

2 files changed

+68
-68
lines changed

2 files changed

+68
-68
lines changed

AVL Tree/AVLTree.swift

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
open class TreeNode<Key: Comparable, Payload> {
23+
public class TreeNode<Key: Comparable, Payload> {
2424
public typealias Node = TreeNode<Key, Payload>
2525

26-
open var payload: Payload?
26+
var payload: Payload?
2727

2828
fileprivate var key: Key
2929
internal var leftChild: Node?
@@ -51,35 +51,35 @@ open class TreeNode<Key: Comparable, Payload> {
5151
self.init(key: key, payload: nil)
5252
}
5353

54-
open var isRoot: Bool {
54+
var isRoot: Bool {
5555
return parent == nil
5656
}
5757

58-
open var isLeaf: Bool {
58+
var isLeaf: Bool {
5959
return rightChild == nil && leftChild == nil
6060
}
6161

62-
open var isLeftChild: Bool {
62+
var isLeftChild: Bool {
6363
return parent?.leftChild === self
6464
}
6565

66-
open var isRightChild: Bool {
66+
var isRightChild: Bool {
6767
return parent?.rightChild === self
6868
}
6969

70-
open var hasLeftChild: Bool {
70+
var hasLeftChild: Bool {
7171
return leftChild != nil
7272
}
7373

74-
open var hasRightChild: Bool {
74+
var hasRightChild: Bool {
7575
return rightChild != nil
7676
}
7777

78-
open var hasAnyChild: Bool {
78+
var hasAnyChild: Bool {
7979
return leftChild != nil || rightChild != nil
8080
}
8181

82-
open var hasBothChildren: Bool {
82+
var hasBothChildren: Bool {
8383
return leftChild != nil && rightChild != nil
8484
}
8585
}
@@ -89,8 +89,8 @@ open class TreeNode<Key: Comparable, Payload> {
8989
open class AVLTree<Key: Comparable, Payload> {
9090
public typealias Node = TreeNode<Key, Payload>
9191

92-
fileprivate(set) open var root: Node?
93-
fileprivate(set) open var size = 0
92+
fileprivate(set) var root: Node?
93+
fileprivate(set) var size = 0
9494

9595
public init() { }
9696
}
@@ -115,26 +115,26 @@ extension TreeNode {
115115

116116
extension AVLTree {
117117
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) }
120120
}
121121

122-
public func search(_ input: Key) -> Payload? {
123-
if let result = search(input, root) {
122+
public func search(input: Key) -> Payload? {
123+
if let result = search(key: input, node: root) {
124124
return result.payload
125125
} else {
126126
return nil
127127
}
128128
}
129129

130-
fileprivate func search(_ key: Key, _ node: Node?) -> Node? {
130+
fileprivate func search(key: Key, node: Node?) -> Node? {
131131
if let node = node {
132132
if key == node.key {
133133
return node
134134
} else if key < node.key {
135-
return search(key, node.leftChild)
135+
return search(key: key, node: node.leftChild)
136136
} else {
137-
return search(key, node.rightChild)
137+
return search(key: key, node: node.rightChild)
138138
}
139139
}
140140
return nil
@@ -144,31 +144,31 @@ extension AVLTree {
144144
// MARK: - Inserting new items
145145

146146
extension AVLTree {
147-
public func insert(_ key: Key, _ payload: Payload? = nil) {
147+
public func insert(key: Key, payload: Payload? = nil) {
148148
if let root = root {
149-
insert(key, payload, root)
149+
insert(input: key, payload: payload, node: root)
150150
} else {
151151
root = Node(key: key, payload: payload)
152152
}
153153
size += 1
154154
}
155155

156-
fileprivate func insert(_ input: Key, _ payload: Payload?, _ node: Node) {
156+
private func insert(input: Key, payload: Payload?, node: Node) {
157157
if input < node.key {
158158
if let child = node.leftChild {
159-
insert(input, payload, child)
159+
insert(input: input, payload: payload, node: child)
160160
} else {
161161
let child = Node(key: input, payload: payload, leftChild: nil, rightChild: nil, parent: node, height: 1)
162162
node.leftChild = child
163-
balance(child)
163+
balance(node: child)
164164
}
165165
} else {
166166
if let child = node.rightChild {
167-
insert(input, payload, child)
167+
insert(input: input, payload: payload, node: child)
168168
} else {
169169
let child = Node(key: input, payload: payload, leftChild: nil, rightChild: nil, parent: node, height: 1)
170170
node.rightChild = child
171-
balance(child)
171+
balance(node: child)
172172
}
173173
}
174174
}
@@ -177,37 +177,37 @@ extension AVLTree {
177177
// MARK: - Balancing tree
178178

179179
extension AVLTree {
180-
fileprivate func updateHeightUpwards(_ node: Node?) {
180+
fileprivate func updateHeightUpwards(node: Node?) {
181181
if let node = node {
182182
let lHeight = node.leftChild?.height ?? 0
183183
let rHeight = node.rightChild?.height ?? 0
184184
node.height = max(lHeight, rHeight) + 1
185-
updateHeightUpwards(node.parent)
185+
updateHeightUpwards(node: node.parent)
186186
}
187187
}
188188

189-
fileprivate func lrDifference(_ node: Node?) -> Int {
189+
fileprivate func lrDifference(node: Node?) -> Int {
190190
let lHeight = node?.leftChild?.height ?? 0
191191
let rHeight = node?.rightChild?.height ?? 0
192192
return lHeight - rHeight
193193
}
194194

195-
fileprivate func balance(_ node: Node?) {
195+
fileprivate func balance(node: Node?) {
196196
guard let node = node else {
197197
return
198198
}
199199

200-
updateHeightUpwards(node.leftChild)
201-
updateHeightUpwards(node.rightChild)
200+
updateHeightUpwards(node: node.leftChild)
201+
updateHeightUpwards(node: node.rightChild)
202202

203203
var nodes = [Node?](repeating: nil, count: 3)
204204
var subtrees = [Node?](repeating: nil, count: 4)
205205
let nodeParent = node.parent
206206

207-
let lrFactor = lrDifference(node)
207+
let lrFactor = lrDifference(node: node)
208208
if lrFactor > 1 {
209209
// left-left or left-right
210-
if lrDifference(node.leftChild) > 0 {
210+
if lrDifference(node: node.leftChild) > 0 {
211211
// left-left
212212
nodes[0] = node
213213
nodes[2] = node.leftChild
@@ -230,7 +230,7 @@ extension AVLTree {
230230
}
231231
} else if lrFactor < -1 {
232232
// right-left or right-right
233-
if lrDifference(node.rightChild) < 0 {
233+
if lrDifference(node: node.rightChild) < 0 {
234234
// right-right
235235
nodes[1] = node
236236
nodes[2] = node.rightChild
@@ -253,7 +253,7 @@ extension AVLTree {
253253
}
254254
} else {
255255
// Don't need to balance 'node', go for parent
256-
balance(node.parent)
256+
balance(node: node.parent)
257257
return
258258
}
259259

@@ -285,19 +285,19 @@ extension AVLTree {
285285
nodes[0]?.rightChild = subtrees[3]
286286
subtrees[3]?.parent = nodes[0]
287287

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
290290

291-
balance(nodes[2]?.parent)
291+
balance(node: nodes[2]?.parent)
292292
}
293293
}
294294

295295
// MARK: - Displaying tree
296296

297297
extension AVLTree {
298-
fileprivate func display(_ node: Node?, level: Int) {
298+
fileprivate func display(node: Node?, level: Int) {
299299
if let node = node {
300-
display(node.rightChild, level: level + 1)
300+
display(node: node.rightChild, level: level + 1)
301301
print("")
302302
if node.isRoot {
303303
print("Root -> ", terminator: "")
@@ -306,30 +306,30 @@ extension AVLTree {
306306
print(" ", terminator: "")
307307
}
308308
print("(\(node.key):\(node.height))", terminator: "")
309-
display(node.leftChild, level: level + 1)
309+
display(node: node.leftChild, level: level + 1)
310310
}
311311
}
312312

313-
public func display(_ node: Node) {
314-
display(node, level: 0)
313+
public func display(node: Node) {
314+
display(node: node, level: 0)
315315
print("")
316316
}
317317
}
318318

319319
// MARK: - Delete node
320320

321321
extension AVLTree {
322-
public func delete(_ key: Key) {
322+
public func delete(key: Key) {
323323
if size == 1 {
324324
root = nil
325325
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)
328328
size -= 1
329329
}
330330
}
331331

332-
fileprivate func delete(_ node: Node) {
332+
private func delete(node: Node) {
333333
if node.isLeaf {
334334
// Just remove and balance up
335335
if let parent = node.parent {
@@ -344,7 +344,7 @@ extension AVLTree {
344344
parent.rightChild = nil
345345
}
346346

347-
balance(parent)
347+
balance(node: parent)
348348
} else {
349349
// at root
350350
root = nil
@@ -354,11 +354,11 @@ extension AVLTree {
354354
if let replacement = node.leftChild?.maximum() , replacement !== node {
355355
node.key = replacement.key
356356
node.payload = replacement.payload
357-
delete(replacement)
357+
delete(node: replacement)
358358
} else if let replacement = node.rightChild?.minimum() , replacement !== node {
359359
node.key = replacement.key
360360
node.payload = replacement.payload
361-
delete(replacement)
361+
delete(node: replacement)
362362
}
363363
}
364364
}

AVL Tree/Tests/AVLTreeTests.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class AVLTreeTests: XCTestCase {
3737
self.tree?.autopopulateWithNodes(5)
3838

3939
for i in 6...10 {
40-
self.tree?.insert(i)
40+
self.tree?.insert(key: i)
4141
do {
4242
try self.tree?.inOrderCheckBalanced(self.tree?.root)
4343
} catch _ {
@@ -50,7 +50,7 @@ class AVLTreeTests: XCTestCase {
5050
self.tree?.autopopulateWithNodes(5)
5151

5252
for i in 1...6 {
53-
self.tree?.delete(i)
53+
self.tree?.delete(key: i)
5454
do {
5555
try self.tree?.inOrderCheckBalanced(self.tree?.root)
5656
} catch _ {
@@ -68,7 +68,7 @@ class AVLTreeTests: XCTestCase {
6868

6969
func testSingleInsertionPerformance() {
7070
self.measure {
71-
self.tree?.insert(5, "E")
71+
self.tree?.insert(key: 5, payload: "E")
7272
}
7373
}
7474

@@ -80,14 +80,14 @@ class AVLTreeTests: XCTestCase {
8080

8181
func testSearchExistentOnSmallTreePerformance() {
8282
self.measure {
83-
self.tree?.search(2)
83+
self.tree?.search(input: 2)
8484
}
8585
}
8686

8787
func testSearchExistentElementOnLargeTreePerformance() {
8888
self.measure {
8989
self.tree?.autopopulateWithNodes(500)
90-
self.tree?.search(400)
90+
self.tree?.search(input: 400)
9191
}
9292
}
9393

@@ -106,19 +106,19 @@ class AVLTreeTests: XCTestCase {
106106
}
107107

108108
func testDeleteExistentKey() {
109-
self.tree?.delete(1)
110-
XCTAssertNil(self.tree?.search(1), "Key should not exist anymore")
109+
self.tree?.delete(key: 1)
110+
XCTAssertNil(self.tree?.search(input: 1), "Key should not exist anymore")
111111
}
112112

113113
func testDeleteNotExistentKey() {
114-
self.tree?.delete(1056)
115-
XCTAssertNil(self.tree?.search(1056), "Key should not exist")
114+
self.tree?.delete(key: 1056)
115+
XCTAssertNil(self.tree?.search(input: 1056), "Key should not exist")
116116
}
117117

118118
func testInsertSize() {
119119
let tree = AVLTree<Int, String>()
120120
for i in 0...5 {
121-
tree.insert(i, "")
121+
tree.insert(key: i, payload: "")
122122
XCTAssertEqual(tree.size, i + 1, "Insert didn't update size correctly!")
123123
}
124124
}
@@ -134,15 +134,15 @@ class AVLTreeTests: XCTestCase {
134134
for p in permutations {
135135
let tree = AVLTree<Int, String>()
136136

137-
tree.insert(1, "five")
138-
tree.insert(2, "four")
139-
tree.insert(3, "three")
140-
tree.insert(4, "two")
141-
tree.insert(5, "one")
137+
tree.insert(key: 1, payload: "five")
138+
tree.insert(key: 2, payload: "four")
139+
tree.insert(key: 3, payload: "three")
140+
tree.insert(key: 4, payload: "two")
141+
tree.insert(key: 5, payload: "one")
142142

143143
var count = tree.size
144144
for i in p {
145-
tree.delete(i)
145+
tree.delete(key: i)
146146
count -= 1
147147
XCTAssertEqual(tree.size, count, "Delete didn't update size correctly!")
148148
}
@@ -154,7 +154,7 @@ extension AVLTree where Key : SignedInteger {
154154
func autopopulateWithNodes(_ count: Int) {
155155
var k: Key = 1
156156
for _ in 0...count {
157-
self.insert(k)
157+
self.insert(key: k)
158158
k = k + 1
159159
}
160160
}

0 commit comments

Comments
 (0)