Esempio n. 1
0
        public static void AddRange_MultipleKeys_FormsCorrectBST()
        {
            var tree = new BinarySearchTree <int>();

            tree.AddRange(new List <int> {
                5, 3, 4, 2
            });

            var rootNode = tree.Search(5);

            Assert.AreEqual(5, rootNode !.Key);
            Assert.AreEqual(3, rootNode !.Left !.Key);
            Assert.IsNull(rootNode !.Right);

            var threeNode = tree.Search(3);

            Assert.AreEqual(3, threeNode !.Key);
            Assert.AreEqual(2, threeNode !.Left !.Key);
            Assert.AreEqual(4, threeNode !.Right !.Key);

            var twoNode = tree.Search(2);

            Assert.IsNull(twoNode !.Left);
            Assert.IsNull(twoNode !.Right);

            var fourNode = tree.Search(4);

            Assert.IsNull(fourNode !.Left);
            Assert.IsNull(fourNode !.Right);
        }
Esempio n. 2
0
        public void BSTUseTest()
        {
            string filepath = @"..\..\..\Resources\Communist.txt";

            string[] words = File.ReadAllText(filepath, Encoding.UTF8).Split(' ');

            Console.WriteLine($"Word count: {words.Length}");

            Stopwatch sw = new Stopwatch();

            sw.Start();

            BinarySearchTree <string, int> bst = new BinarySearchTree <string, int>();

            foreach (string word in words)
            {
                int val = bst.Search(word.ToLower());
                bst.Insert(word.ToLower(), ++val);
            }

            if (bst.Contains("unite"))
            {
                Console.WriteLine($"unite: {bst.Search("unite")}");
            }
            else
            {
                Console.WriteLine("No word 'unite' in the file.");
            }
            sw.Stop();
            Console.WriteLine($"BST, time:{sw.ElapsedMilliseconds} ms");
            Assert.IsTrue(bst.Search("unite") == 1);
        }
Esempio n. 3
0
        public static void Constructor_UseCustomComparer_FormsCorrectTree()
        {
            var cmpFunc = Comparer <string> .Create((x, y) => x.Length - y.Length);

            var tree  = new BinarySearchTree <string>(cmpFunc);
            var elems = new[] { "z", "yy", "vvv", "bbbb", "fffff", "pppppp" };

            tree.AddRange(elems);

            Assert.IsNotNull(tree.Search("vvv"));
            Assert.AreEqual("bbbb", tree.Search("vvv") !.Right !.Key);
        }
Esempio n. 4
0
        public static void Remove_NodesWithTwoChildren_CorrectlyRemovesNodes()
        {
            var tree = new BinarySearchTree <int>();

            tree.AddRange(new List <int> {
                5, 3, 4, 2, 7, 6, 8
            });

            var sevenRemoveResult = tree.Remove(7);

            Assert.IsTrue(sevenRemoveResult);
            Assert.IsNull(tree.Search(7));
            Assert.IsNull(tree.Search(6) !.Left);
            Assert.IsNotNull(tree.Search(6) !.Right);
            Assert.AreEqual(6, tree.Count);
        }
Esempio n. 5
0
        public static void Remove_RemoveRoot_CorrectlyRemovesRoot()
        {
            var tree = new BinarySearchTree <int>();

            tree.Add(5);
            tree.Remove(5);

            Assert.AreEqual(0, tree.Count);
            Assert.IsNull(tree.Search(5));

            tree.AddRange(new List <int> {
                5, 4, 6
            });
            tree.Remove(5);

            Assert.AreEqual(2, tree.Count);
            Assert.IsNull(tree.Search(5));
            Assert.IsNotNull(tree.Search(4));
            Assert.IsNotNull(tree.Search(6));
            Assert.AreEqual(6, tree.Search(4) !.Right !.Key);
        }
Esempio n. 6
0
        public static void Remove_NodesWithOneChild_CorrectlyRemovesNodes()
        {
            var tree = new BinarySearchTree <int>();

            tree.AddRange(new List <int> {
                5, 3, 4, 2, 7, 6, 8
            });

            tree.Remove(4);
            var threeRemoveResult = tree.Remove(3);

            Assert.IsTrue(threeRemoveResult);
            Assert.IsNull(tree.Search(3));
            Assert.IsNull(tree.Search(2) !.Left);
            Assert.IsNull(tree.Search(2) !.Right);
            Assert.AreEqual(5, tree.Count);

            tree.Remove(6);
            var sevenRemoveResult = tree.Remove(7);

            Assert.IsTrue(sevenRemoveResult);
            Assert.IsNull(tree.Search(7));
            Assert.IsNull(tree.Search(8) !.Left);
            Assert.IsNull(tree.Search(8) !.Right);
            Assert.AreEqual(3, tree.Count);
        }
Esempio n. 7
0
        public static void Search_MultipleKeys_FindsAllKeys()
        {
            var tree = new BinarySearchTree <int>();

            tree.AddRange(new List <int> {
                5, 3, 4, 2, 7, 6, 8
            });

            Assert.AreEqual(2, tree.Search(2) !.Key);
            Assert.AreEqual(3, tree.Search(3) !.Key);
            Assert.AreEqual(4, tree.Search(4) !.Key);
            Assert.AreEqual(5, tree.Search(5) !.Key);
            Assert.AreEqual(6, tree.Search(6) !.Key);
            Assert.AreEqual(7, tree.Search(7) !.Key);
            Assert.AreEqual(8, tree.Search(8) !.Key);
        }
        public void Search_NonExistingElement_ShouldReturnEmptyTree()
        {
            var bst = new BinarySearchTree <int>();

            bst.Insert(2);
            bst.Insert(1);
            bst.Insert(3);

            var result = bst.Search(5);
            var nodes  = new List <int>();

            result.EachInOrder(nodes.Add);

            var expectedNodes = new int[] { };

            CollectionAssert.AreEqual(expectedNodes, nodes);
            Assert.AreEqual(expectedNodes.Length, result.Count());
        }
        public void Search_ExistingElement_ShouldReturnSubTree()
        {
            var bst = new BinarySearchTree <int>();

            bst.Insert(20);
            bst.Insert(10);
            bst.Insert(5);
            bst.Insert(15);
            bst.Insert(17);
            bst.Insert(30);
            bst.Insert(25);
            bst.Insert(35);

            var result = bst.Search(10);
            var nodes  = new List <int>();

            result.EachInOrder(nodes.Add);

            var expectedNodes = new int[] { 5, 10, 15, 17 };

            CollectionAssert.AreEqual(expectedNodes, nodes);
        }
Esempio n. 10
0
        public static void Remove_LeafNodes_CorrectlyRemovesNodes()
        {
            var tree = new BinarySearchTree <int>();

            tree.AddRange(new List <int> {
                5, 3, 4, 2, 7, 6, 8
            });

            var twoRemoveResult = tree.Remove(2);

            Assert.IsTrue(twoRemoveResult);
            Assert.IsNull(tree.Search(2));
            Assert.IsNull(tree.Search(3) !.Left);
            Assert.IsNotNull(tree.Search(3) !.Right);
            Assert.AreEqual(6, tree.Count);

            var fourRemoveResult = tree.Remove(4);

            Assert.IsTrue(fourRemoveResult);
            Assert.IsNull(tree.Search(4));
            Assert.IsNull(tree.Search(3) !.Left);
            Assert.IsNull(tree.Search(3) !.Right);
            Assert.AreEqual(5, tree.Count);
        }