public void TesBalancedTree() { var root = new TreeNode(3); root.Left = new TreeNode(9); root.Right = new TreeNode(20); root.Right.Left = new TreeNode(15); root.Right.Right = new TreeNode(7); var r = BalancedTree.IsBalanced(root); Assert.AreEqual(r, true); root = new TreeNode(1); root.Left = new TreeNode(2); root.Right = new TreeNode(2); root.Left.Left = new TreeNode(3); root.Left.Right = new TreeNode(3); root.Left.Left.Left = new TreeNode(4); root.Left.Left.Right = new TreeNode(4); r = BalancedTree.IsBalanced(root); Assert.AreEqual(r, false); }
public void RemovingFromAnEmptyBalancedTreeShouldThrowException() { BalancedTree <int> tree = new BalancedTree <int>(); Assert.IsTrue(tree.IsEmpty, "Tree is empty"); _ = Assert.Throws(typeof(TreeUnderflowException), () => tree.Remove(42), "Removing from an empty tree should throw exception"); }
public void RemovingAValueThatDoesNotExistShouldReturnFalse() { BalancedTree <int> tree = new BalancedTree <int>(); tree.Add(42); Assert.IsFalse(tree.IsEmpty, "Tree is not empty"); Assert.IsFalse(tree.Remove(0), "Should not have been able to remove value not in the tree"); }
public static void TestTreeBalance() { BalancedTree b = new BalancedTree(); Console.WriteLine(b.CheckBalance<int>(GetBalanced1())); Console.WriteLine(b.CheckBalance<int>(GetBalanced2())); Console.WriteLine(b.CheckBalance<int>(GetBalanced3())); Console.WriteLine(b.CheckBalance<int>(GetUnBalanced1())); Console.WriteLine(b.CheckBalance<int>(GetUnBalanced2())); }
public void AddToEmptyBalancedTreeShouldHaveSpecificProperties() { BalancedTree <int> tree = new BalancedTree <int>(); tree.Add(42); Assert.IsFalse(tree.IsEmpty, "Tree is not empty"); Assert.AreEqual(1, tree.Size(), "Tree should have one node"); Assert.AreEqual(0, tree.Height(), "Tree should have height of 1"); Assert.AreEqual("42", tree.ToString(), "Tree should have comma separated list of values"); }
public void BalancedTree_AddTest() { var dictionary = CreateRandomDictionary(2000); var tree = new BalancedTree<int, string>(17); var dictionary2 = new Dictionary<int, string>(); Assert.IsFalse(tree.Any()); foreach (var kvp in dictionary) { dictionary2.Add(kvp.Key, kvp.Value); tree.Add(kvp.Key, kvp.Value); Assert.IsTrue(tree.SequenceEqual(dictionary2.OrderBy(kvp1 => kvp1.Key))); } }
public void TestInsertion() { List <int> data = new List <int> { 10, 20, 30, 40, 50, 25 }; BalancedTree tree = new BalancedTree(); foreach (int datum in data) { tree.Insert(datum); } Assert.IsTrue(tree.PreOrder() == "302010254050"); }
public void BalancedTree_RemoveTest() { var dictionary = CreateRandomDictionary(2000); var tree = new BalancedTree<int, string>(17, dictionary); var keys = dictionary.Keys.ToList(); foreach (var key in keys) { dictionary.Remove(key); tree.Remove(key); Assert.IsTrue(tree.SequenceEqual(dictionary.OrderBy(kvp => kvp.Key))); } Assert.IsFalse(tree.Any()); }
public void TestBalance() { List <int> data = new List <int> { 14, 7, 23, 8 }; BalancedTree tree = new BalancedTree(); foreach (int datum in data) { tree.Insert(datum); } Assert.IsTrue(tree.Balance == 1); data = new List <int> { 2, 1 }; tree = new BalancedTree(); foreach (int datum in data) { tree.Insert(datum); } Assert.IsTrue(tree.Balance == 1); //The following tests were for development purposes, after balancing is enacted in tree's they will need to be mocked //data = new List<int> //{ // 7,1,3,4 //}; //tree = new BalancedTree(); //foreach (int datum in data) //{ // tree.Insert(datum); //} //Assert.IsTrue(tree.Balance == 3); //data = new List<int> //{ // 7,9,10 //}; //tree = new BalancedTree(); //foreach (int datum in data) //{ // tree.Insert(datum); //} //Assert.IsTrue(tree.Balance == -2); }
public void TestRemoval() { List <int> data = new List <int> { 9, 5, 10, 0, 6, 11, -1, 1, 2 }; BalancedTree tree = new BalancedTree(); foreach (int datum in data) { tree.Insert(datum); } Assert.IsTrue(tree.PreOrder() == "910-15261011"); tree.Remove(10); string result = tree.PreOrder(); Assert.IsTrue(result == "10-1952611"); }
static void Main(string[] args) { BalancedTree <int> tree = new BalancedTree <int>(); List <int> insertList = new List <int> { 9, 5, 10, 0, 6, 11, -1, 1, 2 }; foreach (var item in insertList) { tree.Root = tree.Insert(tree.Root, item); } tree.PrintSorted(); tree.Root = tree.DeleteNode(tree.Root, -1); tree.PrintSorted(); }
public void AddingSeveralValuesToBalancedTreeShouldMaintainBalance() { BalancedTree <int> tree = new BalancedTree <int>(); tree.Add(42); // tree should now look like this: // 42 tree.Add(17); // tree should now look like this: // 42 // / // 17 tree.Add(0); // tree should now look like this: // 42 // / \ // 17 0 tree.Add(213401); // tree should now look like this: // 42 // / \ // 17 0 // / // 213401 tree.Add(-1); // tree should now look like this: // 42 // / \ // 17 0 // / / // 213401 -1 tree.Add(9); // tree should now look like this: // 42 // / \ // 17 0 // / \ / // 213401 9 -1 Assert.IsFalse(tree.IsEmpty, "Tree is not empty"); Assert.AreEqual(6, tree.Size(), "Tree should have six nodes"); Assert.AreEqual(2, tree.Height(), "Tree should have height of 2"); Assert.AreEqual("42, 17, 213401, 9, 0, -1", tree.ToString(), "Tree should have comma separated list of values"); // adding one more should add the node to the "0" on the right tree.Add(-69); Assert.IsFalse(tree.IsEmpty, "Tree is not empty"); Assert.AreEqual(7, tree.Size(), "Tree should have seven nodes"); Assert.AreEqual(2, tree.Height(), "Tree should have height of 2"); Assert.AreEqual("42, 17, 213401, 9, 0, -1, -69", tree.ToString(), "Tree should have comma separated list of values"); }
public void RemovingAValueThatExistsShouldMaintainBalanceAndReturnTrue() { BalancedTree <int> tree = new BalancedTree <int>(); tree.Add(42); // remove the root node - should return true and make the tree empty Assert.IsTrue(tree.Remove(42), "Should be able to remove 42"); Assert.IsTrue(tree.IsEmpty, "Tree should be empty again"); tree.Add(42); tree.Add(17); tree.Add(0); tree.Add(213401); tree.Add(-1); tree.Add(9); // tree should now look like this: // 42 // / \ // 17 0 // / \ / // 213401 9 -1 Assert.IsFalse(tree.IsEmpty, "Tree is not empty"); Assert.AreEqual(6, tree.Size(), "Tree should have six nodes"); Assert.AreEqual(2, tree.Height(), "Tree should have height of 2"); Assert.AreEqual("42, 17, 213401, 9, 0, -1", tree.ToString(), "Tree should have comma separated list of values"); // simple case - remove a leaf Assert.IsTrue(tree.Remove(-1), "Should have been able to remove 9"); Assert.IsFalse(tree.IsEmpty, "Tree is not empty"); Assert.AreEqual(5, tree.Size(), "Tree should have five nodes"); Assert.AreEqual(2, tree.Height(), "Tree should have height of 2"); Assert.AreEqual("42, 17, 213401, 9, 0", tree.ToString(), "Tree should have comma separated list of values"); // tree should now look like this: // 42 // / \ // 17 0 // / \ // 213401 9 // remove 213401 - should move 9 to the left child of 17 Assert.IsTrue(tree.Remove(213401), "Should have been able to remove 213401"); // tree should now look like this: // 42 // / \ // 17 0 // / // 9 Assert.IsFalse(tree.IsEmpty, "Tree is not empty"); Assert.AreEqual(4, tree.Size(), "Tree should have four nodes"); Assert.AreEqual(2, tree.Height(), "Tree should have height of 2"); Assert.AreEqual("42, 17, 9, 0", tree.ToString(), "Tree should have comma separated list of values"); // add a few more nodes back into the tree and remove a node with two subtrees tree.Add(-1); tree.Add(69); tree.Add(213401); // tree should now look like this: // 42 // / \ // 17 0 // / \ / \ // 9 69 -1 213401 Assert.IsTrue(tree.Remove(17), "Should have been able to remove 17"); // tree should now look like this: // 42 // / \ // 69 0 // / / \ // 9 -1 213401 Assert.IsFalse(tree.IsEmpty, "Tree is not empty"); Assert.AreEqual(6, tree.Size(), "Tree should have six nodes"); Assert.AreEqual(2, tree.Height(), "Tree should have height of 2"); Assert.AreEqual("42, 69, 9, 0, -1, 213401", tree.ToString(), "Tree should have comma separated list of values"); }
public void BalancedTree_ReversedTreeTest() { var dictionary = CreateRandomDictionary(20000); var reversedTree = new BalancedTree<int, string>(24, dictionary, true); Assert.IsTrue(reversedTree.SequenceEqual(dictionary.OrderByDescending(kvp => kvp.Key))); }