示例#1
0
        static void Main(string[] args)
        {
            Console.WriteLine("---------Welcome To Binary Search Tree--------");
            Console.WriteLine();
            BST <int> bST = new BST <int>(56);

            bST.Insert(30);
            bST.Insert(70);
            bST.Insert(22);
            bST.Insert(40);
            bST.Insert(60);
            bST.Insert(95);
            bST.Insert(11);
            bST.Insert(65);
            bST.Insert(3);
            bST.Insert(16);
            bST.Insert(63);
            bST.Insert(67);

            bST.GetSize();
            bST.Display();
            bool result = bST.Search(63, bST);

            Console.WriteLine();
            Console.WriteLine("The element 63 exists in the BST: " + bST.Search(63, bST));

            Console.Read();
        }
        static void Main(string[] args)
        {
            try
            {
                ObjectPersistence persister     = new ObjectPersistence();
                const string      TreeStoreFile = @"D:\SavedTree.bin";

                #region Construct the tree

                Console.WriteLine("Constructing tree...\t");

                BST bst = new BST();
                bst.Insert(20);
                bst.Insert(25);
                bst.Insert(45);
                bst.Insert(15);
                bst.Insert(67);
                bst.Insert(43);
                bst.Insert(80);
                bst.Insert(33);
                bst.Insert(67);
                bst.Insert(99);
                bst.Insert(91);

                #endregion

                #region Traverse the tree

                Console.WriteLine(@"----------------------------------------------------
Before serialization and de-serialization");

                Console.WriteLine("\nInorder Traversal (Left - Root - Right):");
                bst.Inorder(bst.GetRoot());
                Console.WriteLine("\nPreorder Traversal (Root - Left - Right): ");
                bst.Preorder(bst.GetRoot());
                Console.WriteLine("\nPostorder Traversal (Left - Right - Root): ");
                bst.Postorder(bst.GetRoot());

                #endregion

                #region Serialize and de-serialize the tree to and from a file

                Console.WriteLine(@"
----------------------------------------------------
Storing (serializing) tree to file...");

                persister.StoreBSTToFile(bst, TreeStoreFile);

                Console.WriteLine(string.Format("Tree saved to {0} in binary format\nDe-serializing in process...\t", TreeStoreFile));

                bst = persister.ReadBSTFromFile(TreeStoreFile);

                Console.WriteLine("\nAfter serialization and de-serialization");

                Console.WriteLine("\nInorder Traversal (Left - Root - Right):");
                bst.Inorder(bst.GetRoot());
                Console.WriteLine("\nPreorder Traversal (Root - Left - Right): ");
                bst.Preorder(bst.GetRoot());
                Console.WriteLine("\nPostorder Traversal (Left - Right - Root): ");
                bst.Postorder(bst.GetRoot());

                #endregion

                #region Finding maximum, minimum and size of BST

                Console.WriteLine(@"
----------------------------------------------------
Other details of the tree");

                Console.WriteLine("Minimum value in the tree: " + bst.GetMinimum(bst.GetRoot()));

                Console.WriteLine("Maximum value in the tree: " + bst.GetMaximum(bst.GetRoot()));

                Console.WriteLine("Size of the tree: " + bst.GetSize(bst.GetRoot()));

                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine("Oops!\n" + ex.Message);
            }

            //Keep the console running

            Console.WriteLine("\nPRESS ENTER TO TERMINATE...");
            Console.ReadLine();
        }