Skip to content

Commit 0634627

Browse files
committed
Add test case for old AVL Tree crash
1 parent e910258 commit 0634627

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed

AVL Tree/AVLTree.playground/Sources/AVLTree.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ extension AVLTree {
207207
let lrFactor = lrDifference(node)
208208
if lrFactor > 1 {
209209
// left-left or left-right
210-
if (lrDifference(node.leftChild) > 0) {
210+
if lrDifference(node.leftChild) > 0 {
211211
// left-left
212212
nodes[0] = node
213213
nodes[2] = node.leftChild
@@ -270,7 +270,6 @@ extension AVLTree {
270270
nodes[2]?.parent = nodeParent
271271
}
272272

273-
274273
nodes[2]?.leftChild = nodes[1]
275274
nodes[1]?.parent = nodes[2]
276275
nodes[2]?.rightChild = nodes[0]
@@ -289,7 +288,6 @@ extension AVLTree {
289288
updateHeightUpwards(nodes[1]) // Update height from left
290289
updateHeightUpwards(nodes[0]) // Update height from right
291290

292-
293291
balance(nodes[2]?.parent)
294292
}
295293
}
@@ -415,4 +413,4 @@ extension AVLTree: CustomStringConvertible {
415413
return "[]"
416414
}
417415
}
418-
}
416+
}

AVL Tree/AVLTree.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ extension AVLTree {
207207
let lrFactor = lrDifference(node)
208208
if lrFactor > 1 {
209209
// left-left or left-right
210-
if (lrDifference(node.leftChild) > 0) {
210+
if lrDifference(node.leftChild) > 0 {
211211
// left-left
212212
nodes[0] = node
213213
nodes[2] = node.leftChild
@@ -270,7 +270,6 @@ extension AVLTree {
270270
nodes[2]?.parent = nodeParent
271271
}
272272

273-
274273
nodes[2]?.leftChild = nodes[1]
275274
nodes[1]?.parent = nodes[2]
276275
nodes[2]?.rightChild = nodes[0]
@@ -289,7 +288,6 @@ extension AVLTree {
289288
updateHeightUpwards(nodes[1]) // Update height from left
290289
updateHeightUpwards(nodes[0]) // Update height from right
291290

292-
293291
balance(nodes[2]?.parent)
294292
}
295293
}
@@ -415,4 +413,4 @@ extension AVLTree: CustomStringConvertible {
415413
return "[]"
416414
}
417415
}
418-
}
416+
}

AVL Tree/Tests/AVLTreeTests.swift

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,38 +68,61 @@ class AVLTreeTests: XCTestCase {
6868
self.tree?.search(400)
6969
}
7070
}
71+
72+
func testMinimumOnPopulatedTree() {
73+
self.tree?.autopopulateWithNodes(500)
74+
let min = self.tree?.root?.minimum()
75+
XCTAssertNotNil(min, "Minimum function not working")
76+
}
77+
78+
func testMinimumOnSingleTreeNode() {
79+
let treeNode = TreeNode(key: 1, payload: "A")
80+
let min = treeNode.minimum()
7181

72-
func testMinimumOnPopulatedTree() {
73-
self.tree?.autopopulateWithNodes(500)
74-
let min = self.tree?.root?.minimum()
75-
XCTAssertNotNil(min, "Minimum function not working")
76-
}
77-
78-
func testMinimumOnSingleTreeNode() {
79-
let treeNode = TreeNode(key: 1, payload: "A")
80-
let min = treeNode.minimum()
81-
82-
XCTAssertNotNil(min, "Minimum on single node should be returned")
83-
XCTAssertEqual(min?.payload,treeNode.payload)
84-
}
82+
XCTAssertNotNil(min, "Minimum on single node should be returned")
83+
XCTAssertEqual(min?.payload,treeNode.payload)
84+
}
85+
86+
func testDeleteExistentKey() {
87+
self.tree?.delete(1)
88+
XCTAssertNil(self.tree?.search(1), "Key should not exist anymore")
89+
}
90+
91+
func testDeleteNotExistentKey() {
92+
self.tree?.delete(1056)
93+
XCTAssertNil(self.tree?.search(1056), "Key should not exist")
94+
}
95+
96+
func testDelete() {
97+
let permutations = [
98+
[5, 1, 4, 2, 3],
99+
[2, 3, 1, 5, 4],
100+
[4, 5, 3, 2, 1],
101+
[3, 2, 5, 4, 1],
102+
]
85103

86-
func testDeleteExistentKey() {
87-
self.tree?.delete(1)
88-
XCTAssertNil(self.tree?.search(1), "Key should not exist anymore")
89-
}
104+
for p in permutations {
105+
let tree = AVLTree<Int, String>()
90106

91-
func testDeleteNOTExistentKey() {
92-
self.tree?.delete(1056)
93-
XCTAssertNil(self.tree?.search(1056), "Key should not exist")
107+
tree.insert(1, "five")
108+
tree.insert(2, "four")
109+
tree.insert(3, "three")
110+
tree.insert(4, "two")
111+
tree.insert(5, "one")
112+
113+
for i in p {
114+
tree.delete(i)
115+
}
94116
}
95-
117+
}
96118
}
97119

98120
extension AVLTree where Key : SignedIntegerType {
99121
func autopopulateWithNodes(count : Int) {
100122
var k : Key = 1
101123
for _ in 0...count {
102-
self.insert(k++)
124+
self.insert(k)
125+
k = k + 1
103126
}
104127
}
105128
}

0 commit comments

Comments
 (0)