Ejemplo n.º 1
0
        private static void BinarySearchTreeDemo()
        {
            BinarySearchTree tree = new BinarySearchTree();
            int choice            = 1;

            do
            {
                Console.Clear();
                Console.WriteLine("1.Display the Tree\n2.Insert a node\n3.Delete a node\n4.Search a node\n5.Find Number of elements\n6.Find Height of the tree\n7.InOrderTraverse" +
                                  "\n8.PreOrderTraverse\n9.PostOrderTraverse\n10.LevelOrderTraverse\n11.Exit\nPlease enter a choice");
                choice = Convert.ToInt32(Console.ReadLine());
                switch (choice)
                {
                case 1:
                    tree.display(tree.Root, 1);
                    Console.ReadKey();
                    break;

                case 2:
                    Console.WriteLine("Enter the element to add\n");
                    int  value  = Convert.ToInt32(Console.ReadLine());
                    bool result = tree.Insert(ref tree.Root, value);
                    if (result)
                    {
                        Console.WriteLine("Added successfully");
                    }
                    else
                    {
                        Console.WriteLine("Could not add the element");
                    }
                    Console.ReadKey();
                    break;

                case 3:
                    Console.WriteLine("Enter the element to delete\n");
                    int  valueToDelete = Convert.ToInt32(Console.ReadLine());
                    bool delResult     = tree.Delete(ref tree.Root, valueToDelete);
                    if (delResult)
                    {
                        Console.WriteLine("Deleted successfully");
                    }
                    else
                    {
                        Console.WriteLine("Could not delete the element");
                    }
                    Console.ReadKey();
                    break;

                case 4:
                    Console.WriteLine("Enter the element to search\n");
                    int  valueToSearch = Convert.ToInt32(Console.ReadLine());
                    bool searchResult  = tree.Search(ref tree.Root, valueToSearch);
                    if (searchResult)
                    {
                        Console.WriteLine("The element exists");
                    }
                    else
                    {
                        Console.WriteLine("The element doesn't exists");
                    }
                    Console.ReadKey();
                    break;

                case 5:
                    int numberOfElements = tree.FindNumberOfElements(ref tree.Root);
                    Console.WriteLine("The total number of elements in the tree is " + numberOfElements);
                    Console.ReadKey();
                    break;

                case 6:
                    int heightOfTree = tree.FindHeightOfTree(ref tree.Root);
                    Console.WriteLine("The total height of the tree is " + heightOfTree);
                    Console.ReadKey();
                    break;

                case 7:
                    tree.InOrderTraverse(tree.Root);
                    Console.ReadKey();
                    break;

                case 8:
                    tree.PreOrderTraverse(tree.Root);
                    Console.ReadKey();
                    break;

                case 9:
                    tree.PostOrderTraverse(tree.Root);
                    Console.ReadKey();
                    break;

                case 10:
                    tree.LevelOrderTraverse(tree.Root);
                    Console.ReadKey();
                    break;

                case 11:
                    break;

                default:
                    Console.WriteLine("Please enter a valid choice");
                    Console.ReadKey();
                    break;
                }
            } while (choice != 11);
        }