Skip to content

Commit

Permalink
add tests for delete
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdihasnat committed Dec 6, 2024
1 parent 0f61b35 commit 7044a83
Showing 1 changed file with 147 additions and 1 deletion.
148 changes: 147 additions & 1 deletion Algorithms.Tests/DataStructures/AVLTree.fs
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,150 @@ type AVLTreeTests() =
|> verifyProperties

Assert.AreEqual(tree1.Root |> Option.map (fun n -> n.Value),
tree2.Root |> Option.map (fun n -> n.Value))
tree2.Root |> Option.map (fun n -> n.Value))
[<TestMethod>]
member _.``Delete maintains AVL properties``() =
let tree =
empty
|> insert 5
|> verifyProperties
|> insert 3
|> verifyProperties
|> insert 7
|> verifyProperties
|> insert 1
|> verifyProperties
|> insert 9
|> verifyProperties
|> insert 4
|> verifyProperties
|> insert 6
|> verifyProperties
|> insert 8
|> verifyProperties
|> insert 2
|> verifyProperties
|> delete 5 // Delete root
|> verifyProperties
|> delete 1 // Delete leaf
|> verifyProperties
|> delete 7 // Delete node with one child
|> verifyProperties
|> delete 3 // Delete node with two children
|> verifyProperties

Assert.IsTrue(tree.Root.IsSome)

[<TestMethod>]
member _.``Delete from empty tree returns empty tree``() =
let tree =
empty
|> delete 1
|> verifyProperties

Assert.IsTrue(tree.Root.IsNone)

[<TestMethod>]
member _.``Delete non-existent value maintains tree structure``() =
let tree1 =
empty
|> insert 2
|> verifyProperties
|> insert 1
|> verifyProperties
|> insert 3
|> verifyProperties

let tree2 =
tree1
|> delete 4
|> verifyProperties

Assert.AreEqual(
tree1.Root |> Option.map (fun n -> n.Value),
tree2.Root |> Option.map (fun n -> n.Value)
)

[<TestMethod>]
member _.``Complex deletion cases maintain balance``() =
let tree =
empty
|> insert 50 // Create a more complex tree
|> verifyProperties
|> insert 25
|> verifyProperties
|> insert 75
|> verifyProperties
|> insert 10
|> verifyProperties
|> insert 35
|> verifyProperties
|> insert 60
|> verifyProperties
|> insert 90
|> verifyProperties
|> insert 5
|> verifyProperties
|> insert 15
|> verifyProperties
|> insert 30
|> verifyProperties
|> insert 40
|> verifyProperties
|> insert 55
|> verifyProperties
|> insert 65
|> verifyProperties
|> insert 80
|> verifyProperties
|> insert 95
|> verifyProperties

// Test various deletion patterns
|> delete 50 // Delete root with two children
|> verifyProperties
|> delete 25 // Delete inner node with two children
|> verifyProperties
|> delete 5 // Delete leaf
|> verifyProperties
|> delete 95 // Delete leaf on opposite side
|> verifyProperties
|> delete 35 // Delete node with one child
|> verifyProperties
|> delete 75 // Delete node requiring rebalancing
|> verifyProperties

Assert.IsTrue(tree.Root.IsSome)

[<TestMethod>]
member _.``Sequential deletion maintains balance``() =
let mutable tree = empty

// Build tree with sequential inserts
for i in 1..10 do
tree <- insert i tree
tree <- verifyProperties tree

// Delete in reverse order
for i in seq{10..(-1)..1} do
tree <- delete i tree
tree <- verifyProperties tree

Assert.IsTrue(tree.Root.IsNone)

[<TestMethod>]
member _.``Random operations maintain AVL properties``() =
let rng = System.Random(42)
let mutable tree = empty

// Random inserts
for _ in 1..20 do
let value = rng.Next(1, 100)
tree <- insert value tree
tree <- verifyProperties tree

// Random deletes
for _ in 1..10 do
let value = rng.Next(1, 100)
tree <- delete value tree
tree <- verifyProperties tree

0 comments on commit 7044a83

Please sign in to comment.