@@ -11,39 +11,61 @@ import XCTest
11
11
class AVLTreeTests : XCTestCase {
12
12
13
13
var tree : AVLTree < Int , String > ?
14
-
14
+
15
15
override func setUp( ) {
16
16
super. setUp ( )
17
17
18
18
tree = AVLTree ( )
19
- tree? . insert ( 1 , " A " )
20
- tree? . insert ( 2 , " B " )
21
- tree? . insert ( 3 , " C " )
22
- tree? . insert ( 4 , " D " )
23
19
}
24
20
25
21
override func tearDown( ) {
26
22
// Put teardown code here. This method is called after the invocation of each test method in the class.
27
23
super. tearDown ( )
28
24
}
29
-
25
+
26
+ func testAVLTreeBalancedAutoPopulate( ) {
27
+ self . tree? . autopopulateWithNodes ( 10 )
28
+
29
+ do {
30
+ try self . tree? . inOrderCheckBalanced ( self . tree? . root)
31
+ } catch _ {
32
+ XCTFail ( " Tree is not balanced after autopopulate " )
33
+ }
34
+ }
35
+
36
+ func testAVLTreeBalancedInsert( ) {
37
+ self . tree? . autopopulateWithNodes ( 5 )
38
+
39
+ for i in 6 ... 10 {
40
+ self . tree? . insert ( i)
41
+ do {
42
+ try self . tree? . inOrderCheckBalanced ( self . tree? . root)
43
+ } catch _ {
44
+ XCTFail ( " Tree is not balanced after inserting " + String( i) )
45
+ }
46
+ }
47
+ }
48
+
49
+ func testAVLTreeBalancedDelete( ) {
50
+ self . tree? . autopopulateWithNodes ( 5 )
51
+
52
+ for i in 1 ... 6 {
53
+ self . tree? . delete ( i)
54
+ do {
55
+ try self . tree? . inOrderCheckBalanced ( self . tree? . root)
56
+ } catch _ {
57
+ XCTFail ( " Tree is not balanced after deleting " + String( i) )
58
+ }
59
+ }
60
+ }
61
+
30
62
func testEmptyInitialization( ) {
31
63
let tree = AVLTree < Int , String > ( )
32
64
33
65
XCTAssertEqual ( tree. size, 0 )
34
66
XCTAssertNil ( tree. root)
35
67
}
36
-
37
- func testNotEmptyInitialization( ) {
38
- XCTAssertNotNil ( self . tree? . root)
39
- XCTAssertNotEqual ( self . tree!. size, 0 )
40
- }
41
-
42
- func testInsertDuplicated( ) {
43
- self . tree? . insert ( 1 , " A " )
44
- XCTAssertEqual ( self . tree? . size, 5 , " Duplicated elements should be allowed " )
45
- }
46
-
68
+
47
69
func testSingleInsertionPerformance( ) {
48
70
self . measureBlock {
49
71
self . tree? . insert ( 5 , " E " )
@@ -92,6 +114,14 @@ class AVLTreeTests: XCTestCase {
92
114
self . tree? . delete ( 1056 )
93
115
XCTAssertNil ( self . tree? . search ( 1056 ) , " Key should not exist " )
94
116
}
117
+
118
+ func testInsertSize( ) {
119
+ let tree = AVLTree < Int , String > ( )
120
+ for i in 0 ... 5 {
121
+ tree. insert ( i, " " )
122
+ XCTAssertEqual ( tree. size, i + 1 , " Insert didn't update size correctly! " )
123
+ }
124
+ }
95
125
96
126
func testDelete( ) {
97
127
let permutations = [
@@ -109,9 +139,12 @@ class AVLTreeTests: XCTestCase {
109
139
tree. insert ( 3 , " three " )
110
140
tree. insert ( 4 , " two " )
111
141
tree. insert ( 5 , " one " )
112
-
142
+
143
+ var count = tree. size
113
144
for i in p {
114
145
tree. delete ( i)
146
+ count -= 1
147
+ XCTAssertEqual ( tree. size, count, " Delete didn't update size correctly! " )
115
148
}
116
149
}
117
150
}
@@ -126,3 +159,31 @@ extension AVLTree where Key : SignedIntegerType {
126
159
}
127
160
}
128
161
}
162
+
163
+ enum AVLTreeError : ErrorType {
164
+ case NotBalanced
165
+ }
166
+
167
+ extension AVLTree where Key : SignedIntegerType {
168
+ func height( node: Node ? ) -> Int {
169
+ if let node = node {
170
+ let lHeight = height ( node. leftChild)
171
+ let rHeight = height ( node. rightChild)
172
+
173
+ return max ( lHeight, rHeight) + 1
174
+ }
175
+ return 0
176
+ }
177
+
178
+ func inOrderCheckBalanced( node: Node ? ) throws {
179
+ if let node = node {
180
+ guard abs ( height ( node. leftChild) - height( node. rightChild) ) <= 1 else {
181
+ throw AVLTreeError . NotBalanced
182
+ }
183
+ try inOrderCheckBalanced ( node. leftChild)
184
+ try inOrderCheckBalanced ( node. rightChild)
185
+ }
186
+ }
187
+ }
188
+
189
+
0 commit comments