Exemplo n.º 1
0
        public void CheckIfNodeIsInvalidatedAfterClearingAndAfterRemoval()
        {
            var tree = new BinarySearchTreeMap <int, int>();

            tree.Add(2, 2);
            tree.Add(1, 1);
            tree.Add(3, 3);

            // Tree looks like this:
            //   2
            //  / \
            // 1   3

            var node1 = tree.Root.Left;
            var node2 = tree.Root;
            var node3 = tree.Root.Right;

            tree.Remove(2);
            if (node2.Left != null || node2.Right != null)
            {
                Assert.Fail("2");
            }

            tree.Remove(3);
            if (node3.Left != null || node3.Right != null)
            {
                Assert.Fail("3");
            }

            tree.Remove(1);
            if (node1.Left != null || node1.Right != null)
            {
                Assert.Fail("1");
            }

            tree.Add(2, 2);
            tree.Add(1, 1);
            tree.Add(3, 3);

            node1 = tree.Root.Left;
            node2 = tree.Root;
            node3 = tree.Root.Right;

            tree.Clear();

            Assert.IsTrue(node1.Left == null && node1.Right == null &&
                          node2.Left == null && node2.Right == null &&
                          node3.Left == null && node3.Right == null &&
                          tree.Root == null &&
                          tree.Count == 0);
        }
Exemplo n.º 2
0
        public void SortedElementsAfterAddingAndRemoving()
        {
            BinarySearchTreeMap <int, int> tree = new BinarySearchTreeMap <int, int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree.Add(50, 50);
            tree.Add(75, 75);
            tree.Add(25, 25);

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.ContainsKey(el))
                    {
                        tree.Add(el, el);
                    }
                    el += i;
                }
            }

            bool removedEverything = true;

            //Removing every second number
            for (int i = 0; i < elementsCount; i += 2)
            {
                if (!tree.Remove(i))
                {
                    removedEverything = false;
                }
            }


            int  last              = -1;
            int  count             = 0;
            bool elementsAreSorted = true;

            foreach (var item in tree)
            {
                if (last > item.Key)
                {
                    elementsAreSorted = false;
                }
                last = item.Key;
                count++;
            }

            Trace.WriteLine(tree.Count);

            Assert.IsTrue(tree.Count == count &&
                          elementsAreSorted &&
                          removedEverything);
        }
Exemplo n.º 3
0
        public void RemoveAllExceptOne()
        {
            BinarySearchTreeMap <int, int> tree = new BinarySearchTreeMap <int, int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree.Add(50, 50);
            tree.Add(75, 75);
            tree.Add(25, 25);

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.ContainsKey(el))
                    {
                        tree.Add(el, el);
                    }
                    el += i;
                }
            }

            bool removedEverything = true;

            for (int i = 0; i < elementsCount - 1; i++)
            {
                if (!tree.Remove(i))
                {
                    removedEverything = false;
                }
            }

            Assert.IsTrue(tree.Count == 1 &&
                          removedEverything &&
                          tree.Root.Value == elementsCount - 1);
        }
        public static void DoTest()
        {
            // Binary Search Tree Map collection
            var bstMap = new BinarySearchTreeMap<int, string>(allowDuplicates: false);

            // Testing data
            KeyValuePair<int, string>[] values = new KeyValuePair<int, string>[10];

            // Prepare the values array
            for(int i = 1; i <= 10; ++i)
            {
                var keyValPair = new KeyValuePair<int, string>(i, String.Format("Integer: {0}", i));
                values[i - 1] = keyValPair;
            }

            //
            // Test singular insert
            for (int i = 0; i < 10; ++i)
                bstMap.Insert(values[i].Key, values[i].Value);

            Debug.Assert(bstMap.Count == values.Length, "Expected the same number of items.");

            bstMap.Clear();

            //
            // Test collection insert
            bstMap.Insert(values);

            //
            // Test enumeration of key-value pairs is still in oreder
            var enumerator = bstMap.GetInOrderEnumerator();
            for (int i = 0; i < 10; ++i)
            {
                if (enumerator.MoveNext())
                {
                    var curr = enumerator.Current;
                    if (curr.Key != values[i].Key || curr.Value != values[i].Value)
                        throw new Exception();
                }
            }

            //
            // Test against re-shuffled insertions (not like above order)
            bstMap = new BinarySearchTreeMap<int, string>(allowDuplicates: false);

            bstMap.Insert(4, "int4");
            bstMap.Insert(5, "int5");
            bstMap.Insert(7, "int7");
            bstMap.Insert(2, "int2");
            bstMap.Insert(1, "int1");
            bstMap.Insert(3, "int3");
            bstMap.Insert(6, "int6");
            bstMap.Insert(0, "int0");
            bstMap.Insert(8, "int8");
            bstMap.Insert(10, "int10");
            bstMap.Insert(9, "int9");

            Debug.Assert(bstMap.Count == values.Length, "Expected the same number of items.");

            //
            // ASSERT INSERTING DUPLICATES WOULD BREAK
            var insert_duplicate_passed = true;
            try
            {
                // 2 already exists in tree
                bstMap.Insert(2, "int2");
                insert_duplicate_passed = true;
            }
            catch
            {
                insert_duplicate_passed = false;
            }

            Debug.Assert(insert_duplicate_passed == false, "Fail! The tree doesn't allow duplicates");

            //
            // Test find
            Debug.Assert(bstMap.Find(5).Key == 5, "Wrong find result!");
            Debug.Assert(bstMap.FindMin().Key == 0, "Wrong min!");
            Debug.Assert(bstMap.FindMax().Key == 10, "Wrong max!");

            //
            // Assert find raises exception on non-existing elements
            bool threwKeyNotFoundError = false;

            try
            {
                bstMap.Find(999999999);
                threwKeyNotFoundError = false;
            }
            catch(KeyNotFoundException)
            {
                threwKeyNotFoundError = true;
            }

            Debug.Assert(true == threwKeyNotFoundError, "Expected to catch KeyNotFoundException.");

            //
            // PRINT TREE
            Console.WriteLine("*****************************");
            Console.WriteLine(" [*] BINARY SEARCH TREE TREE:\r\n");
            Console.WriteLine("*****************************");
            Console.WriteLine(bstMap.DrawTree());
            Console.WriteLine("\r\n");

            //
            // Assert count
            Debug.Assert(bstMap.Count == 11);

            //
            // Assert existence and nonexistence of some items
            Debug.Assert(bstMap.Contains(1) == true);
            Debug.Assert(bstMap.Contains(3) == true);
            Debug.Assert(bstMap.Contains(999) == false);

            //
            // Do some deletions
            bstMap.Remove(7);
            bstMap.Remove(1);
            bstMap.Remove(3);

            //
            // Assert count
            Debug.Assert(bstMap.Count == 8);

            //
            // Assert nonexistence of previously existing items
            Debug.Assert(bstMap.Contains(1) == false);
            Debug.Assert(bstMap.Contains(3) == false);

            //
            // Remove root key
            var oldRootKey = bstMap.Root.Key;
            bstMap.Remove(bstMap.Root.Key);

            //
            // Assert count
            Debug.Assert(bstMap.Count == 7);

            //
            // Assert nonexistence of old root's key
            Debug.Assert(bstMap.Contains(oldRootKey) == false);

            //
            // PRINT TREE
            Console.WriteLine("*****************************");
            Console.WriteLine(" [*] BINARY SEARCH TREE TREE:\r\n");
            Console.WriteLine("*****************************");
            Console.WriteLine(bstMap.DrawTree(includeValues: true));
            Console.WriteLine("\r\n");

            Console.ReadLine();
        }
Exemplo n.º 5
0
        public static void DoTest()
        {
            // Binary Search Tree Map collection
            var bstMap = new BinarySearchTreeMap <int, string>(allowDuplicates: false);

            // Testing data
            KeyValuePair <int, string>[] values = new KeyValuePair <int, string> [10];

            // Prepare the values array
            for (int i = 1; i <= 10; ++i)
            {
                var keyValPair = new KeyValuePair <int, string>(i, String.Format("Integer: {0}", i));
                values[i - 1] = keyValPair;
            }


            //
            // Test singular insert
            for (int i = 0; i < 10; ++i)
            {
                bstMap.Insert(values[i].Key, values[i].Value);
            }

            Debug.Assert(bstMap.Count == values.Length, "Expected the same number of items.");

            bstMap.Clear();


            //
            // Test collection insert
            bstMap.Insert(values);


            //
            // Test enumeration of key-value pairs is still in oreder
            var enumerator = bstMap.GetInOrderEnumerator();

            for (int i = 0; i < 10; ++i)
            {
                if (enumerator.MoveNext())
                {
                    var curr = enumerator.Current;
                    if (curr.Key != values[i].Key || curr.Value != values[i].Value)
                    {
                        throw new Exception();
                    }
                }
            }


            //
            // Test against re-shuffled insertions (not like above order)
            bstMap = new BinarySearchTreeMap <int, string>(allowDuplicates: false);

            bstMap.Insert(4, "int4");
            bstMap.Insert(5, "int5");
            bstMap.Insert(7, "int7");
            bstMap.Insert(2, "int2");
            bstMap.Insert(1, "int1");
            bstMap.Insert(3, "int3");
            bstMap.Insert(6, "int6");
            bstMap.Insert(0, "int0");
            bstMap.Insert(8, "int8");
            bstMap.Insert(10, "int10");
            bstMap.Insert(9, "int9");

            Debug.Assert(bstMap.Count == values.Length, "Expected the same number of items.");


            //
            // ASSERT INSERTING DUPLICATES WOULD BREAK
            var insert_duplicate_passed = true;

            try
            {
                // 2 already exists in tree
                bstMap.Insert(2, "int2");
                insert_duplicate_passed = true;
            }
            catch
            {
                insert_duplicate_passed = false;
            }

            Debug.Assert(insert_duplicate_passed == false, "Fail! The tree doesn't allow duplicates");


            //
            // Test find
            Debug.Assert(bstMap.Find(5).Key == 5, "Wrong find result!");
            Debug.Assert(bstMap.FindMin().Key == 0, "Wrong min!");
            Debug.Assert(bstMap.FindMax().Key == 10, "Wrong max!");


            //
            // Assert find raises exception on non-existing elements
            bool threwKeyNotFoundError = false;

            try
            {
                bstMap.Find(999999999);
                threwKeyNotFoundError = false;
            }
            catch (KeyNotFoundException)
            {
                threwKeyNotFoundError = true;
            }

            Debug.Assert(true == threwKeyNotFoundError, "Expected to catch KeyNotFoundException.");


            //
            // PRINT TREE
            Console.WriteLine("*****************************");
            Console.WriteLine(" [*] BINARY SEARCH TREE TREE:\r\n");
            Console.WriteLine("*****************************");
            Console.WriteLine(bstMap.DrawTree());
            Console.WriteLine("\r\n");


            //
            // Assert count
            Debug.Assert(bstMap.Count == 11);


            //
            // Assert existence and nonexistence of some items
            Debug.Assert(bstMap.Contains(1) == true);
            Debug.Assert(bstMap.Contains(3) == true);
            Debug.Assert(bstMap.Contains(999) == false);


            //
            // Do some deletions
            bstMap.Remove(7);
            bstMap.Remove(1);
            bstMap.Remove(3);


            //
            // Assert count
            Debug.Assert(bstMap.Count == 8);


            //
            // Assert nonexistence of previously existing items
            Debug.Assert(bstMap.Contains(1) == false);
            Debug.Assert(bstMap.Contains(3) == false);


            //
            // Remove root key
            var oldRootKey = bstMap.Root.Key;

            bstMap.Remove(bstMap.Root.Key);


            //
            // Assert count
            Debug.Assert(bstMap.Count == 7);


            //
            // Assert nonexistence of old root's key
            Debug.Assert(bstMap.Contains(oldRootKey) == false);


            //
            // PRINT TREE
            Console.WriteLine("*****************************");
            Console.WriteLine(" [*] BINARY SEARCH TREE TREE:\r\n");
            Console.WriteLine("*****************************");
            Console.WriteLine(bstMap.DrawTree(includeValues: true));
            Console.WriteLine("\r\n");

            Console.ReadLine();
        }//end-do-test
Exemplo n.º 6
0
        public void UpdatingElementsWithIndexerUsingTryGetValueMethodToGetValue()
        {
            BinarySearchTreeMap <int, int> tree = new BinarySearchTreeMap <int, int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree[50] = 50;
            tree[75] = 75;
            tree[25] = 25;

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.ContainsKey(el))
                    {
                        tree.Add(el, el);
                    }
                    el += i;
                }
            }

            bool removedEverything = true;

            //Removing every second number
            for (int i = 0; i < elementsCount; i += 2)
            {
                if (!tree.Remove(i))
                {
                    removedEverything = false;
                }
            }

            // Make all values negative
            for (int i = 0; i < elementsCount; i++)
            {
                int value;
                if (tree.TryGetValue(i, out value))
                {
                    tree[value] = -value;
                }
            }


            int  last              = -1;
            int  count             = 0;
            bool elementsAreSorted = true;

            foreach (var item in tree)
            {
                if (item.Value > 0)
                {
                    Assert.Fail();
                }
                if (last > item.Key)
                {
                    elementsAreSorted = false;
                }
                last = item.Key;
                count++;
            }

            Trace.WriteLine(tree.Count);

            Assert.IsTrue(tree.Count == count &&
                          elementsAreSorted &&
                          removedEverything);
        }
Exemplo n.º 7
0
        public void AddingAfterRemovingAllElements()
        {
            var tree = new BinarySearchTreeMap <int, int>();

            int elementsCount = 10000;

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.ContainsKey(el))
                    {
                        tree.Add(el, el);
                    }
                    el += i;
                }
            }

            if (tree.Count != elementsCount)
            {
                Assert.Fail();
            }

            //Removing every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    tree.Remove(el);
                    el += i;
                }
            }

            if (tree.Count != 0)
            {
                Assert.Fail();
            }

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.ContainsKey(el))
                    {
                        tree.Add(el, el);
                    }
                    el += i;
                }
            }

            int  last              = -1;
            int  count             = 0;
            bool elementsAreSorted = true;

            foreach (var item in tree)
            {
                if (last > item.Key)
                {
                    elementsAreSorted = false;
                }
                last = item.Key;
                count++;
            }

            Assert.IsTrue(tree.Count == elementsCount &&
                          elementsAreSorted &&
                          count == elementsCount);
        }