forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVLTreeTests.swift
191 lines (160 loc) · 4.43 KB
/
AVLTreeTests.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
//
// AVLTreeTestsTests.swift
// AVLTreeTestsTests
//
// Created by Barbara Rodeker on 2/19/16.
// Copyright © 2016 Swift Algorithm Club. All rights reserved.
//
import XCTest
class AVLTreeTests: XCTestCase {
var tree: AVLTree<Int, String>?
func testSwift4() {
// last checked with Xcode 9.0b4
#if swift(>=4.0)
print("Hello, Swift 4!")
#endif
}
override func setUp() {
super.setUp()
tree = AVLTree()
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testAVLTreeBalancedAutoPopulate() {
self.tree?.autopopulateWithNodes(10)
do {
try self.tree?.inOrderCheckBalanced(self.tree?.root)
} catch _ {
XCTFail("Tree is not balanced after autopopulate")
}
}
func testAVLTreeBalancedInsert() {
self.tree?.autopopulateWithNodes(5)
for i in 6...10 {
self.tree?.insert(key: i)
do {
try self.tree?.inOrderCheckBalanced(self.tree?.root)
} catch _ {
XCTFail("Tree is not balanced after inserting " + String(i))
}
}
}
func testAVLTreeBalancedDelete() {
self.tree?.autopopulateWithNodes(5)
for i in 1...6 {
self.tree?.delete(key: i)
do {
try self.tree?.inOrderCheckBalanced(self.tree?.root)
} catch _ {
XCTFail("Tree is not balanced after deleting " + String(i))
}
}
}
func testEmptyInitialization() {
let tree = AVLTree<Int, String>()
XCTAssertEqual(tree.size, 0)
XCTAssertNil(tree.root)
}
func testSingleInsertionPerformance() {
self.measure {
self.tree?.insert(key: 5, payload: "E")
}
}
func testMultipleInsertionsPerformance() {
self.measure {
self.tree?.autopopulateWithNodes(50)
}
}
func testSearchExistentOnSmallTreePerformance() {
self.measure {
print(self.tree?.search(input: 2))
}
}
func testSearchExistentElementOnLargeTreePerformance() {
self.measure {
self.tree?.autopopulateWithNodes(500)
print(self.tree?.search(input: 400))
}
}
func testMinimumOnPopulatedTree() {
self.tree?.autopopulateWithNodes(500)
let min = self.tree?.root?.minimum()
XCTAssertNotNil(min, "Minimum function not working")
}
func testMinimumOnSingleTreeNode() {
let treeNode = TreeNode(key: 1, payload: "A")
let min = treeNode.minimum()
XCTAssertNotNil(min, "Minimum on single node should be returned")
XCTAssertEqual(min?.payload, treeNode.payload)
}
func testDeleteExistentKey() {
self.tree?.delete(key: 1)
XCTAssertNil(self.tree?.search(input: 1), "Key should not exist anymore")
}
func testDeleteNotExistentKey() {
self.tree?.delete(key: 1056)
XCTAssertNil(self.tree?.search(input: 1056), "Key should not exist")
}
func testInsertSize() {
let tree = AVLTree<Int, String>()
for i in 0...5 {
tree.insert(key: i, payload: "")
XCTAssertEqual(tree.size, i + 1, "Insert didn't update size correctly!")
}
}
func testDelete() {
let permutations = [
[5, 1, 4, 2, 3],
[2, 3, 1, 5, 4],
[4, 5, 3, 2, 1],
[3, 2, 5, 4, 1],
]
for p in permutations {
let tree = AVLTree<Int, String>()
tree.insert(key: 1, payload: "five")
tree.insert(key: 2, payload: "four")
tree.insert(key: 3, payload: "three")
tree.insert(key: 4, payload: "two")
tree.insert(key: 5, payload: "one")
var count = tree.size
for i in p {
tree.delete(key: i)
count -= 1
XCTAssertEqual(tree.size, count, "Delete didn't update size correctly!")
}
}
}
}
extension AVLTree where Key : SignedInteger {
func autopopulateWithNodes(_ count: Int) {
var k: Key = 1
for _ in 0...count {
self.insert(key: k)
k = k + 1
}
}
}
enum AVLTreeError: Error {
case notBalanced
}
extension AVLTree where Key : SignedInteger {
func height(_ node: Node?) -> Int {
if let node = node {
let lHeight = height(node.leftChild)
let rHeight = height(node.rightChild)
return max(lHeight, rHeight) + 1
}
return 0
}
func inOrderCheckBalanced(_ node: Node?) throws {
if let node = node {
guard abs(height(node.leftChild) - height(node.rightChild)) <= 1 else {
throw AVLTreeError.notBalanced
}
try inOrderCheckBalanced(node.leftChild)
try inOrderCheckBalanced(node.rightChild)
}
}
}