Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            //************************* TESTING 1 2 3 ****************************

            BSTTree <T> tree = new BSTTree <T>();

            Console.WriteLine(" \n******************* ");
            Console.WriteLine(" ***** TESTING ***** ");
            Console.WriteLine(" ******************* \n");

            Console.WriteLine("[!] Inserting 9, 4, 24, 12, and 20: ");

            tree.Insert(9);
            tree.Insert(4);
            tree.Insert(24);
            tree.Insert(12);
            tree.Insert(20);

            Console.Write("\n");

            Console.Write("In Order: ");
            tree.InOrder();
            Console.Write("PreOrder(Root, Left, Right): ");
            tree.PreOrder();
            Console.Write("PostOrder(Left, Right, Root): ");
            tree.PostOrder();

            Console.WriteLine("\n[!] Now inserting 78, 32, and 41: ");

            tree.Insert(78);
            tree.Insert(32);
            tree.Insert(41);

            Console.Write("\n");

            Console.Write("In Order: ");
            tree.InOrder();
            Console.Write("PreOrder(Root, Left, Right): ");
            tree.PreOrder();
            Console.Write("PostOrder(Left, Right, Root): ");
            tree.PostOrder();

            Console.WriteLine("\nDoes the tree have 99 in it? (No): " + tree.Contains(99));
            Console.WriteLine("How about 12? (Yes): " + tree.Contains(12));

            Console.Write("\n");
            //********************** ACTUAL ASSIGNMENT ***************************

            BSTTree <T> tree2 = new BSTTree <T>();

            Console.WriteLine(" ******************* ");
            Console.WriteLine(" ****** C# BST ***** ");
            Console.WriteLine(" ******************* \n");

            Console.WriteLine("Please enter a list of integer number (1-100): ");
            string inline = Console.ReadLine();

            Console.WriteLine("User entered this data: " + inline);
            char[] delimiterChars = { ' ', ',', '_', ':', '\t' }; //Part of Crandall's in class code

            string[] words = inline.Split(delimiterChars);

            Console.WriteLine("Length of array: {0}", words.Length);

            foreach (string s in words) //Part of Crandall's in class code
            {
                int newVal = 0;
                if (Int32.TryParse(s, out newVal))
                {
                    tree2.Insert(newVal);
                }
            }

            Console.WriteLine("\n[!] Here's Your Tree: \n");

            Console.Write("In Order: ");
            tree2.InOrder();
            Console.Write("PreOrder(Root, Left, Right): ");
            tree2.PreOrder();
            Console.Write("PostOrder(Left, Right, Root): ");
            tree2.PostOrder();

            Console.WriteLine("\n[!] Tree Info: \n");

            Console.WriteLine("Nodes: {0}", tree2.Count());
            Console.WriteLine("Levels: {0}", tree2.Levels());
            Console.WriteLine("Min Height given # of Nodes: {0}", tree2.MinHeight()); //log2(n)+1

            //im done c:
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // string to hold input from user
            string tempArray = null;

            // string to split input from user into
            string[] Array = null;
            // root node for BST
            BSTNode root = null;
            // tree variable to hold nodes and use functions
            BSTTree tree = new BSTTree();

            // Get User's input
            Console.WriteLine("Enter a collection of numbers in the range [0, 100], separated by spaces:");
            tempArray = Console.ReadLine();
            // Split user's input on every space, put into a string array
            Array = tempArray.Split(null);

            // Convert string array into array of ints, found on https://stackoverflow.com/questions/1297231/convert-string-to-int-in-one-line-of-code-using-linq
            // by user Simon Fox
            int[] userInput = Array.Select(s => int.Parse(s)).ToArray();

            // To check for duplicates
            List <int> tempCheck = new List <int>();

            // Insert user input into BST, nested loop to check for duplicates and break if it is a duplicate, otherwise it inserts.
            for (int i = 0; i < Array.Length; i++)
            {
                if (tempCheck.Contains(userInput[i]) == false)
                {
                    root = tree.insert(root, userInput[i]);
                    tempCheck.Add(userInput[i]);
                }
            }

            // Outputs numbers in sorted order through an in-order traversal function
            Console.Write("Tree contents: ");
            tree.inOrderTraversal(root);
            Console.WriteLine("");

            Console.WriteLine("Tree statistics:");
            // calls count function and displays number of nodes given from that function
            int track = tree.count(root);

            Console.Write("Number of nodes: " + track);

            Console.WriteLine("");

            // calls treeDepth function to get depth of tree and reports it to user
            int depth = tree.treeDepth(root);

            Console.Write("Number of levels: " + depth);

            double minimumLevels = 0;

            // we learned minimum levels is n=log(base 2)x+1 in cpts 122, where n is minimum levels and x is number of nodes so using math functions we can determine that.
            minimumLevels = Math.Floor(Math.Log(track + 1, 2));

            Console.WriteLine("");
            Console.Write("Minimum number of levels that a tree with " + track + " nodes could have = " + minimumLevels);
            Console.WriteLine("");
        }