Пример #1
0
        public void AllTest()
        {
            //Arrange
            var bst = new BinarySearchTree<int, string>();
            var testData = new[]
            {
                new KeyValuePair<int, string>(5, "A"),
                new KeyValuePair<int, string>(3, "B"),
                new KeyValuePair<int, string>(6, "C"),
                new KeyValuePair<int, string>(2, "D"),
                new KeyValuePair<int, string>(7, "E"),
                new KeyValuePair<int, string>(4, "F"),
            };

            //Act
            testData.All(t =>
            {
                bst.Add(t.Key, t.Value);
                return true;
            });
            var resultList = bst.All().ToList();

            //Assert
            Assert.AreEqual(resultList.Count, 6);

            Assert.AreEqual(resultList[0].Key, 2);
            Assert.AreEqual(resultList[1].Key, 3);
            Assert.AreEqual(resultList[2].Key, 4);
            Assert.AreEqual(resultList[3].Key, 5);
            Assert.AreEqual(resultList[4].Key, 6);
            Assert.AreEqual(resultList[5].Key, 7);
        }
Пример #2
0
        public void AddTest()
        {
            var bst = new BinarySearchTree<int, string>();
            bst.Add(5, "A");
            bst.Add(4, "B");
            bst.Add(6, "C");
            bst.Add(3, "D");
            bst.Add(7, "E");

            Assert.AreEqual(bst.Size, 5);
        }
Пример #3
0
 public void DeleteMinTest()
 {
     //Arrange
     var bst = new BinarySearchTree<int, string>();
     bst.Add(5, "A");
     bst.Add(3, "B");
     bst.Add(6, "C");
     bst.Add(2, "D");
     bst.Add(7, "E");
     bst.Add(4, "F");
     //Act
     bst.DeleteMin();
     var testResult = bst.Min();
     //Assert
     Assert.AreEqual(testResult.Key, 3);
 }
Пример #4
0
 public void Insert(int newData)
 {
     if (_data == null)
         _data = newData;
     else
     {
         if (newData <= _data)
             if (_left != null)
                 _left.Insert(newData);
             else
                 _left = new BinarySearchTree(newData);
         else
             if (_right != null)
                 _right.Insert(newData);
             else
                 _right = new BinarySearchTree(newData);
     }
 }
Пример #5
0
        public void DeleteTest()
        {
            //Arrange
            var bst = new BinarySearchTree<int, string>();
            bst.Add(5, "A");
            bst.Add(3, "B");
            bst.Add(6, "C");
            bst.Add(2, "D");
            bst.Add(7, "E");
            bst.Add(4, "F");
            //Act
            bst.Delete(3);
            var testResult = bst.GetNodeByKey(5).LeftNode;

            //Assert
            Assert.AreEqual(testResult.Key, 4);
        }
Пример #6
0
 public void MaxTest()
 {
     //Arrange
     var bst = new BinarySearchTree<int, string>();
     bst.Add(5, "A");
     bst.Add(4, "B");
     bst.Add(6, "C");
     bst.Add(0, "D");
     bst.Add(7, "E");
     //Act
     var testResult = bst.Max();
     //Assert
     Assert.AreEqual(testResult.Key, 7);
 }
Пример #7
0
        public void LevelOrderTraversalTest()
        {
            //Arrange
            var bst = new BinarySearchTree<int, string>();

            //43 10 78 84 75 65 89 34 29 54

            var testData = new[]
            {
                new KeyValuePair<int, string>(43, "43"),
                new KeyValuePair<int, string>(10, "10"),
                new KeyValuePair<int, string>(78, "78"),
                new KeyValuePair<int, string>(84, "84"),
                new KeyValuePair<int, string>(75, "75"),
                new KeyValuePair<int, string>(65, "65"),
                new KeyValuePair<int, string>(89, "89"),
                new KeyValuePair<int, string>(34, "34"),
                new KeyValuePair<int, string>(29, "29"),
                new KeyValuePair<int, string>(54, "54"),
            };

            //Act
            testData.All(t =>
            {
                bst.Add(t.Key, t.Value);
                return true;
            });
            var result = bst.LevelOrderTraversal();

            //Assert
            Assert.AreEqual(result.Count(), 10);

            Assert.AreEqual(result.ElementAt(0).Key, 43);
            Assert.AreEqual(result.ElementAt(1).Key, 10);
            Assert.AreEqual(result.ElementAt(2).Key, 78);
            Assert.AreEqual(result.ElementAt(3).Key, 34);
            Assert.AreEqual(result.ElementAt(4).Key, 75);
            Assert.AreEqual(result.ElementAt(5).Key, 84);
            Assert.AreEqual(result.ElementAt(6).Key, 29);
            Assert.AreEqual(result.ElementAt(7).Key, 65);
            Assert.AreEqual(result.ElementAt(8).Key, 89);
            Assert.AreEqual(result.ElementAt(9).Key, 54);
        }
Пример #8
0
        public void LevelOrderTraversalAfterRemovingTest()
        {
            //Arrange
            var bst = new BinarySearchTree<int, string>();

            //88 48 91 26 55 11 43 65 12 57 23 62
            var testData = new[]
            {
                new KeyValuePair<int, string>(88, "88"),
                new KeyValuePair<int, string>(48, "48"),
                new KeyValuePair<int, string>(91, "91"),
                new KeyValuePair<int, string>(26, "26"),
                new KeyValuePair<int, string>(55, "55"),
                new KeyValuePair<int, string>(11, "11"),
                new KeyValuePair<int, string>(43, "43"),
                new KeyValuePair<int, string>(65, "65"),
                new KeyValuePair<int, string>(12, "12"),
                new KeyValuePair<int, string>(57, "57"),
                new KeyValuePair<int, string>(23, "23"),
                new KeyValuePair<int, string>(62, "62"),
            };

            //Act
            testData.All(t =>
            {
                bst.Add(t.Key, t.Value);
                return true;
            });
            var result = bst.LevelOrderTraversal();

            //Assert
            Assert.AreEqual(result.Count(), 12);

            Assert.AreEqual(result.ElementAt(0).Key, 88);
            Assert.AreEqual(result.ElementAt(1).Key, 48);
            Assert.AreEqual(result.ElementAt(2).Key, 91);
            Assert.AreEqual(result.ElementAt(3).Key, 26);
            Assert.AreEqual(result.ElementAt(4).Key, 55);
            Assert.AreEqual(result.ElementAt(5).Key, 11);
            Assert.AreEqual(result.ElementAt(6).Key, 43);
            Assert.AreEqual(result.ElementAt(7).Key, 65);
            Assert.AreEqual(result.ElementAt(8).Key, 12);
            Assert.AreEqual(result.ElementAt(9).Key, 57);
            Assert.AreEqual(result.ElementAt(10).Key, 23);
            Assert.AreEqual(result.ElementAt(11).Key, 62);

            //91 12 48
            bst.Delete(91);
            bst.Delete(12);
            bst.Delete(48);
            result = bst.LevelOrderTraversal();

            //Assert
            Assert.AreEqual(result.Count(), 9);

            Assert.AreEqual(result.ElementAt(0).Key, 88);
            Assert.AreEqual(result.ElementAt(1).Key, 55);
            Assert.AreEqual(result.ElementAt(2).Key, 26);
            Assert.AreEqual(result.ElementAt(3).Key, 65);
            Assert.AreEqual(result.ElementAt(4).Key, 11);
            Assert.AreEqual(result.ElementAt(5).Key, 43);
            Assert.AreEqual(result.ElementAt(6).Key, 57);
            Assert.AreEqual(result.ElementAt(7).Key, 23);
            Assert.AreEqual(result.ElementAt(8).Key, 62);
        }
Пример #9
0
 private static BinarySearchTreeNode Delete(BinarySearchTreeNode node)
 {
     Splay(BinarySearchTree.Next(node));
     Splay(node);
     return(BinarySearchTree.Delete(node));
 }
Пример #10
0
 public static BinarySearchTreeNode Insert(long key, BinarySearchTreeNode root)
 {
     BinarySearchTree.Insert(key, root);
     return(Find(key, root));
 }
        static void Main(string[] args)
        {
            /********************************************************** IMPLEMENT QUEUE WITH TWO STACKS ***********************************************************************/
            ImplementQueueWithTwoStacks iqTS = new ImplementQueueWithTwoStacks();

            iqTS.Insert(10);
            iqTS.Insert(20);
            iqTS.Insert(30);
            iqTS.Insert(40);

            Console.WriteLine(iqTS.Delete()); // 10
            Console.WriteLine(iqTS.Delete()); // 20
            Console.WriteLine(iqTS.Delete()); // 30
            /********************************************************** IMPLEMENT QUEUE WITH TWO STACKS ***********************************************************************/

            // Console.WriteLine(new InfixToPostFixConversion(7).ConvertToPostfix("a+b*(c-d)"));
            Console.WriteLine(new InfixToPostFixConversion("a+b*(c^d-e)^(f+g*h)-i".Length).ConvertToPostfix("a+b*(c^d-e)^(f+g*h)-i")); // abcd^e-fgh*+^*+i-

            /**************************************************************** Trying to implement complete binary tree   *****************************************************************/
            TreePractice tr = new TreePractice();

            tr.AddNode_BinaryTree(10, tr.root);
            tr.AddNode_BinaryTree(20, tr.root);
            tr.AddNode_BinaryTree(30, tr.root);
            tr.AddNode_BinaryTree(40, tr.root);
            tr.AddNode_BinaryTree(50, tr.root);
            tr.AddNode_BinaryTree(60, tr.root);
            /**************************************************************** Trying to implement complete binary tree   *****************************************************************/

            TreeTraversals trTraversal = new TreeTraversals();

            trTraversal.Inorder(tr.root);

            Console.WriteLine(new TreePractice().CountNumberOfNodes(tr.root));

            Console.WriteLine(new TreePractice().CountNumberOfNodes_Meth2(tr.root));

            Console.WriteLine(new TreePractice().CountNumberOfLeaves(tr.root));

            Console.WriteLine("No of leaves:" + new TreePractice().CountNumberofLeaves_Meth2(tr.root));

            Console.WriteLine("No of non-leaves:" + new TreePractice().CountNumberofNonLeaves_Meth2(tr.root));

            /************************************************* Binary Search Tree ************************************/
            BinarySearchTree bTree = new BinarySearchTree();

            /* Add elements in Binary Search Tree */
            bTree.AddNode(10, bTree.root);
            bTree.AddNode(20, bTree.root);
            bTree.AddNode(30, bTree.root);
            bTree.AddNode(40, bTree.root);
            bTree.AddNode(25, bTree.root);
            bTree.AddNode(15, bTree.root);
            bTree.AddNode(5, bTree.root);
            bTree.AddNode(8, bTree.root);
            bTree.AddNode(9, bTree.root);

            Console.WriteLine("Maximum Element: " + bTree.FindMax(bTree.root));

            Console.WriteLine("Minimum Element: " + bTree.FindMin(bTree.root));

            bTree.DeleteNode(25, bTree.root);
            NodePractice afterDelete = bTree.DeleteNode(40, bTree.root);

            // bTree.DeleteNode(10, bTree.root);

            Console.WriteLine(afterDelete.data);

            trTraversal.Inorder(bTree.root); // should print all elements in a sorted order

            Console.ReadLine();
        }