Skip to content

Commit

Permalink
Improve BST tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpahm committed Nov 2, 2024
1 parent d8f94aa commit 6858750
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DSA/Data Structures/BinarySearchTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace DSA
public class BSTNode<T>(T Value) : IBinaryNode<T>
{
public T Value { get; set; } = Value;
public uint Count { get; set; } = 1;
public int Count { get; set; } = 1;
public BSTNode<T>? Left { get; set; }
public BSTNode<T>? Right { get; set; }

Expand Down
46 changes: 46 additions & 0 deletions DSATests/BinarySearchTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void RemoveTest()
Assert.AreEqual(10, tree.Count);
Assert.IsNotNull(tree.BSTRoot);
Assert.AreEqual(11, tree.BSTRoot.Value);
Assert.IsFalse(tree.Contains(10));

int[] ints = new int[tree.Count];
int[] correctInts = [3, 4, 5, 6, 7, 8, 9, 11, 12, 13];
Expand All @@ -82,6 +83,7 @@ public void RemoveTest()
Assert.AreEqual(9, tree.Count);
Assert.IsNotNull(tree.BSTRoot);
Assert.AreEqual(11, tree.BSTRoot.Value);
Assert.IsFalse(tree.Contains(3));

ints = new int[tree.Count];
correctInts = [4, 5, 6, 7, 8, 9, 11, 12, 13];
Expand All @@ -99,6 +101,7 @@ public void RemoveTest()
Assert.AreEqual(8, tree.Count);
Assert.IsNotNull(tree.BSTRoot);
Assert.AreEqual(11, tree.BSTRoot.Value);
Assert.IsFalse(tree.Contains(13));

ints = new int[tree.Count];
correctInts = [4, 5, 6, 7, 8, 9, 11, 12];
Expand All @@ -118,6 +121,9 @@ public void RemoveTest()
Assert.AreEqual(5, tree.Count);
Assert.IsNotNull(tree.BSTRoot);
Assert.AreEqual(11, tree.BSTRoot.Value);
Assert.IsFalse(tree.Contains(6));
Assert.IsFalse(tree.Contains(8));
Assert.IsFalse(tree.Contains(4));

ints = new int[tree.Count];
correctInts = [5, 7, 9, 11, 12, 13];
Expand All @@ -130,6 +136,46 @@ public void RemoveTest()
Assert.AreEqual(correctInts[i], ints[i]);
}

[TestMethod()]
public void DuplicateTest()
{
// Create usual test tree but with some duplicates (10x3, 8x2, 3x3)
tree = [10, 10, 10, 6, 12, 4, 8, 8, 11, 13, 3, 5, 3, 7, 3, 9];
Assert.AreEqual(16, tree.Count);
Assert.IsNotNull(tree.BSTRoot);
Assert.AreEqual(10, tree.BSTRoot.Value);
Assert.AreEqual(3, tree.BSTRoot.Count);

// Remove first copy of root
tree.Remove(10);
Assert.AreEqual(15, tree.Count);
Assert.IsNotNull(tree.BSTRoot);
Assert.AreEqual(10, tree.BSTRoot.Value);
Assert.AreEqual(2, tree.BSTRoot.Count);
Assert.IsTrue(tree.Contains(10));

// Remove root entirely
tree.Remove(10);
tree.Remove(10);
Assert.AreEqual(13, tree.Count);
Assert.IsNotNull(tree.BSTRoot);
Assert.AreEqual(11, tree.BSTRoot.Value);
Assert.AreEqual(1, tree.BSTRoot.Count);
Assert.IsFalse(tree.Contains(10));

// Make sure other duplicates are valid
Assert.IsTrue(tree.Contains(8));
Assert.IsTrue(tree.Contains(3));

var node = tree.Find(8);
Assert.IsNotNull(node);
Assert.AreEqual(2, node.Count);

node = tree.Find(3);
Assert.IsNotNull(node);
Assert.AreEqual(3, node.Count);
}

[TestMethod()]
public void ClearTest()
{
Expand Down

0 comments on commit 6858750

Please sign in to comment.