Skip to content

Commit

Permalink
Add test case for old AVL Tree crash
Browse files Browse the repository at this point in the history
  • Loading branch information
hollance committed Mar 18, 2016
1 parent e910258 commit 0634627
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
6 changes: 2 additions & 4 deletions AVL Tree/AVLTree.playground/Sources/AVLTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ extension AVLTree {
let lrFactor = lrDifference(node)
if lrFactor > 1 {
// left-left or left-right
if (lrDifference(node.leftChild) > 0) {
if lrDifference(node.leftChild) > 0 {
// left-left
nodes[0] = node
nodes[2] = node.leftChild
Expand Down Expand Up @@ -270,7 +270,6 @@ extension AVLTree {
nodes[2]?.parent = nodeParent
}


nodes[2]?.leftChild = nodes[1]
nodes[1]?.parent = nodes[2]
nodes[2]?.rightChild = nodes[0]
Expand All @@ -289,7 +288,6 @@ extension AVLTree {
updateHeightUpwards(nodes[1]) // Update height from left
updateHeightUpwards(nodes[0]) // Update height from right


balance(nodes[2]?.parent)
}
}
Expand Down Expand Up @@ -415,4 +413,4 @@ extension AVLTree: CustomStringConvertible {
return "[]"
}
}
}
}
6 changes: 2 additions & 4 deletions AVL Tree/AVLTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ extension AVLTree {
let lrFactor = lrDifference(node)
if lrFactor > 1 {
// left-left or left-right
if (lrDifference(node.leftChild) > 0) {
if lrDifference(node.leftChild) > 0 {
// left-left
nodes[0] = node
nodes[2] = node.leftChild
Expand Down Expand Up @@ -270,7 +270,6 @@ extension AVLTree {
nodes[2]?.parent = nodeParent
}


nodes[2]?.leftChild = nodes[1]
nodes[1]?.parent = nodes[2]
nodes[2]?.rightChild = nodes[0]
Expand All @@ -289,7 +288,6 @@ extension AVLTree {
updateHeightUpwards(nodes[1]) // Update height from left
updateHeightUpwards(nodes[0]) // Update height from right


balance(nodes[2]?.parent)
}
}
Expand Down Expand Up @@ -415,4 +413,4 @@ extension AVLTree: CustomStringConvertible {
return "[]"
}
}
}
}
67 changes: 45 additions & 22 deletions AVL Tree/Tests/AVLTreeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,38 +68,61 @@ class AVLTreeTests: XCTestCase {
self.tree?.search(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()

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)
}
XCTAssertNotNil(min, "Minimum on single node should be returned")
XCTAssertEqual(min?.payload,treeNode.payload)
}

func testDeleteExistentKey() {
self.tree?.delete(1)
XCTAssertNil(self.tree?.search(1), "Key should not exist anymore")
}

func testDeleteNotExistentKey() {
self.tree?.delete(1056)
XCTAssertNil(self.tree?.search(1056), "Key should not exist")
}

func testDelete() {
let permutations = [
[5, 1, 4, 2, 3],
[2, 3, 1, 5, 4],
[4, 5, 3, 2, 1],
[3, 2, 5, 4, 1],
]

func testDeleteExistentKey() {
self.tree?.delete(1)
XCTAssertNil(self.tree?.search(1), "Key should not exist anymore")
}
for p in permutations {
let tree = AVLTree<Int, String>()

func testDeleteNOTExistentKey() {
self.tree?.delete(1056)
XCTAssertNil(self.tree?.search(1056), "Key should not exist")
tree.insert(1, "five")
tree.insert(2, "four")
tree.insert(3, "three")
tree.insert(4, "two")
tree.insert(5, "one")

for i in p {
tree.delete(i)
}
}

}
}

extension AVLTree where Key : SignedIntegerType {
func autopopulateWithNodes(count : Int) {
var k : Key = 1
for _ in 0...count {
self.insert(k++)
self.insert(k)
k = k + 1
}
}
}

0 comments on commit 0634627

Please sign in to comment.