Esempio n. 1
0
        public void MinLevelTest()
        {
            BST        bst   = new BST();
            List <int> myLst = new List <int>
            {
                20,
                30,
                15,
                1,
                5,
                55,
                600,
                43
            };

            foreach (int x in myLst)
            {
                bst.Root = bst.Insert(bst.Root, x);
            }
            double result = 0.0;

            result = bst.MinLevel(bst.Root);

            Assert.AreEqual(bst.MinLevel(bst.Root), result);
        }
Esempio n. 2
0
        /// <Main>
        /// Main function where everything is essentially run and called.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            List <string> myLst   = new List <string>(); //List to hold input from user
            string        usrList = String.Empty;
            BST           myTree  = new BST();

            Console.WriteLine("Please enter a list of integers seperated by spaces [0,100]: \n"); //Get string from user
            usrList = Console.ReadLine();
            Console.Write("\n");
            myLst = usrList.Split(' ').ToList();      //Splits the string into a List of strings

            foreach (string temp in myLst.Distinct()) //Iterate through each string in the List<strings> **** Distinct will make sure the list is unique
            {
                int result = 0;
                if (int.TryParse(temp, out int value)) //Parse string into an integer
                {
                    result      = value;
                    myTree.Root = myTree.Insert(myTree.Root, result);
                }
            }

            //Stats
            Console.WriteLine("\nTree Stats: \n");
            Console.Write("inOrder Traversal: ");
            myTree.InOrderTraversal(myTree.Root);
            Console.WriteLine("\n   Number of nodes: " + myTree.Count);
            Console.WriteLine(string.Format("   Height of tree: {0}", myTree.Levels(myTree.Root)));
            Console.WriteLine(string.Format("   Theoretical: Minimum levels for a tree with {0} nodes is {1}\n", myTree.Count, myTree.MinLevel(myTree.Root)));
            Console.WriteLine("\n");
            Console.WriteLine("Program complete.\n");
        }
Esempio n. 3
0
        public void InOrderTest()
        {
            BST bst = new BST();

            try
            {
                bst.InOrderTraversal(bst.Root); //Empty inorder traversal
                Assert.Fail();                  // raises AssertionException
            }
            catch (Exception)
            {}
            bst.Insert(bst.Root, 30);
            bst.InOrderTraversal(bst.Root); //non empty tree
            Assert.IsNotNull(bst);
        }
Esempio n. 4
0
        public void InsertTest()
        {
            BST        bst   = new BST();
            List <int> myLst = new List <int>();

            myLst.Add(20);
            myLst.Add(30);
            myLst.Add(10);
            foreach (int x in myLst)
            {
                bst.Root = bst.Insert(bst.Root, x);
            }

            Assert.AreEqual(20, bst.Root.Data);
            Assert.AreEqual(30, bst.Root.PRight.Data);
            Assert.AreEqual(10, bst.Root.PLeft.Data);
        }
Esempio n. 5
0
        public void LevelsTest()
        {
            BST        bst   = new BST();
            List <int> myLst = new List <int>
            {
                20,
                30,
                15,
                1,
                5,
                55,
                600,
                43
            };

            foreach (int x in myLst)
            {
                bst.Root = bst.Insert(bst.Root, x);
            }

            Assert.AreEqual(4, bst.Levels(bst.Root));
        }
Esempio n. 6
0
        /// <summary>
        /// Main entrance of program.
        /// This program will take in a user input and create a BST
        /// with that input.  NO DUPLICATES WOULD BE ACCEPTED.
        /// Will display stats of the BST after calculations are performed.
        /// </summary>
        /// <param name="args">Args.</param>
        public static void Main(string[] args)
        {
            // Gets users input of integers in a string.
            string userInput = GetInput();

            // Converts string input into an array if integers and inserts these number into a BST.
            int[] arr = ConvertInput(userInput);
            BST   bst = new BST();

            foreach (var number in arr)
            {
                bst.Insert(number);
            }

            // Calculates stats of BST and displays the stats.
            int levels       = bst.GetLevels();
            int itemCount    = bst.GetItemCount();
            int minLevelsReq = bst.GetMinLevels();

            // Displays BST in order.
            Console.WriteLine("{0}", bst.DisplayInSortedOrder());

            Console.WriteLine("Levels: {0} \nItemCount: {1} \nMinLevelsRequired: {2}", levels, itemCount, minLevelsReq);
        }