예제 #1
0
 // Traverse the tree to check the number of nodes under the tree T including node T itself
 public int NodeNum(BiTree T)
 {
     if (T == null)
     {
         return(0);
     }
     else
     {
         if ((T.lchild != null) && (T.rchild != null))
         {
             return(T.NodeNum(T.lchild) + T.NodeNum(T.rchild) + 1);
         }
         else if (T.lchild != null)
         {
             return(T.NodeNum(T.lchild) + 1);
         }
         else if (T.rchild != null)
         {
             return(T.NodeNum(T.rchild) + 1);
         }
         else
         {
             return(1);
         }
     }
 }
예제 #2
0
        static void Main(string[] args)
        {
            char[] delimiterChars = { ' ' }; // set up the dilimiter char array, which includes space

            Console.WriteLine("Enter a collection of numbers in the range [0, 100], separated by spaces:");

            string input = Console.ReadLine(); // read inputs from console

            int[] array = null;                // store input integer arrays

            // Considering some error inputs like null or not an integer, write the console out
            if (input == null)
            {
                Console.WriteLine("User input format is null!");
            }
            else
            {
                // do a split of string to get strings of integers
                string[] words = input.Split(delimiterChars, System.StringSplitOptions.RemoveEmptyEntries);
                if (words.Length == 0)
                {
                    Console.WriteLine("User input length 0!"); // user enters 0 qualified string
                }
                else
                {
                    array = new int[words.Length];
                    for (int i = 0; i < words.Length; i++)
                    {
                        if (Int32.TryParse(words[i], out array[i]))
                        {
                            Console.WriteLine(array[i]); // do parse to translate strings into integers
                        }
                        else
                        {
                            Console.WriteLine("User input not a number!");
                        }
                    }
                }
            }

            // Next, We want to build our binary search Tree.


            if (array == null)
            {
                Console.WriteLine("User input error!"); // no integer is entered
            }
            else
            {
                BiTree T = new BiTree(array[0]);    // establish the binary search tree
                for (int i = 1; i < array.Length; i++)
                {
                    T.Insert(T, array[i]);
                }

                int      count = T.NodeNum(T);
                IntArray A     = new IntArray(count); // read the integers from tree to a designed int array class IntArray

                T.PreOrder(T, A);
                string tmp = "Tree contents: "; // show the tree contents

                //
                for (int i = 0; i < A.cnt; i++)
                {
                    tmp += A.arr[i] + " ";   // combine the integers from int array into a string, tmp
                }

                // display all the required like contents, node number, levels ...
                Console.WriteLine(tmp);
                Console.WriteLine("Tree statistics:");
                Console.WriteLine("  Number of nodes: {0}", count);
                Console.WriteLine("  Number of levels: {0}", T.level(T));
                Console.WriteLine("  Minimum number of levels that a tree with {0} nodes could have = {1}", count, Math.Ceiling(Math.Log((double)count + 1) / Math.Log(2)));
                Console.WriteLine("Done!");
            }
        }