Ejemplo n.º 1
0
        //Build a Binary Search Tree
        static void Main(string[] args)
        {
            var bst = new BST();
            var arr = new int[] { 8, 6, 9, 3, 4, 5, 10, 3, 25, 40, 35, 55, 1 };

            foreach (var item in arr)
            {
                bst.Insert(item, 0);
            }
            bst.Insert(23, 0);
            bst.FindAndDelete(bst.Root(), 1);
            var successorNode = bst.inSuccessor(55);
            var min           = bst.Minimum(bst.Root());
            var max           = bst.Maximum(bst.Root());
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            /*
             *      Node:
             *      -Value
             *      -LeftChild
             *      -RightChild
             *      -Parent
             *
             *      -IsLeftChild
             *      -IsRightChild
             *      -ChildCount - count how many children
             */

            BST <int> binaryTree = new BST <int>();

            binaryTree.Insert(75);
            binaryTree.Insert(57);
            binaryTree.Insert(90);
            binaryTree.Insert(32);
            binaryTree.Insert(7);
            binaryTree.Insert(44);
            binaryTree.Insert(60);
            binaryTree.Insert(86);
            binaryTree.Insert(93);
            binaryTree.Insert(99);

            Console.WriteLine("end of insert");
            var min = binaryTree.Minimun();

            Console.WriteLine($"Finding smallest node...its value is: {min.Value}");
            var max = binaryTree.Maximum();

            Console.WriteLine($"Finding the max node...its value is: {max.Value}");

            /*TO DO: -Determine if a given node is a left child
             *       -Determine if a given node is a right chold
             *       -Add a delete*/

            ;
        }
Ejemplo n.º 3
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Enter a collection of numbers in the range [0,100], seprated by spaces: ");
            var userInput = Console.ReadLine();
            var tempArray = userInput.Split(' ');

            string[] inputArray = new HashSet <string>(tempArray).ToArray(); // removing duplicates
            BST      firstTree  = new BST();

            for (int i = 0; i < inputArray.Length; i++)
            {
                firstTree.Insert(int.Parse(inputArray[i]));
            }

            firstTree.TreeStatistcis();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var random = new Random();
            var bst    = new BST();
            var list   = new List <int>();

            for (int i = 0; i < 100; i++)
            {
                list.Add(random.Next(1, 200));
            }
            foreach (var item in list)
            {
                bst.Insert(item);
            }
            bst.Inorder();
            Console.ReadKey();

            foreach (var item in list)
            {
                bst.Delete(item);
            }
            bst.Inorder();
            Console.ReadKey();
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            BST  tree = new BST();
            Node A    = new Node();
            Node B    = new Node();
            Node C    = new Node();
            Node D    = new Node();
            Node E    = new Node();
            Node F    = new Node();

            tree.Insert(A, 9);
            tree.Insert(B, 7);
            tree.Insert(C, 10);
            tree.Insert(D, 6);
            tree.Insert(E, 11);
            tree.Insert(F, 1);


            tree.PrintTree(A);
        }
Ejemplo n.º 6
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();
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            BST binarySearchTree = new BST();

            binarySearchTree.Insert(56);
            binarySearchTree.Insert(30);
            binarySearchTree.Insert(70);
            binarySearchTree.Insert(22);
            binarySearchTree.Insert(40);
            binarySearchTree.Insert(11);
            binarySearchTree.Insert(3);
            binarySearchTree.Insert(16);
            binarySearchTree.Insert(60);
            binarySearchTree.Insert(95);
            binarySearchTree.Insert(65);
            binarySearchTree.Insert(63);
            binarySearchTree.Insert(67);
            binarySearchTree.Display();
            Console.WriteLine("size of binary search tree is : " + binarySearchTree.Size());
            Console.WriteLine("element " + binarySearchTree.Search(63) + " found in binary search tree");
        }