コード例 #1
0
        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");
        }
コード例 #2
0
        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");
        }
コード例 #3
0
        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());
        }
コード例 #4
0
        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");
        }
コード例 #5
0
        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");
        }