Esempio n. 1
0
        static void Main(string[] args)
        {
            var  bst = new BinarySearchTree();
            char c;

            do
            {
                PrintMenu();
                if (!TryGetChar(out c))
                {
                    BadInput();
                }
                else
                {
                    switch (c)
                    {
                    case 'a':
                        if (!TryGetInt(out int i, "Enter the value to add"))
                        {
                            BadInput();
                        }
                        else
                        {
                            bst.Insert(i);
                        }
                        break;

                    case 'p':
                        BTreePrinter.Print(bst.Root);
                        break;

                    case 'b':
                        bst.PrintBfs();
                        break;

                    case 'd':
                        bst.PrintDfs();
                        break;
                    }
                }
            } while (c != 'q');
        }
Esempio n. 2
0
 private static BinarySearchTree<string> ConstructTree(params string[] values)
 {
     string[] numbers = values.Select(x => x.ToString()).ToArray();
     BinarySearchTree<string> bst = new BinarySearchTree<string>();
     foreach (string currentNumber in numbers)
     {
         bst.Insert(currentNumber);
     }
     return bst;
 }
Esempio n. 3
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);
        }
Esempio n. 4
0
 private static BinarySearchTree<int> ConstructTree()
 {
     string fileName = "BinarySearchTreeBasic.txt";
     string[] numbers = ExtractContent(fileName);
     BinarySearchTree<int> bst = new BinarySearchTree<int>();
     foreach (string currentNumber in numbers)
     {
         bst.Insert(int.Parse(currentNumber));
     }
     return bst;
 }
Esempio n. 5
0
        static void Main(string[] args)
        {
            // var node = new Node(33);
            // node.Left = new Node(16).Left = new Node(13).Right = new Node(15);
            // node.Left.Right.Left = new Node(17);
            // node.Left.Right.Right = new Node(25);
            // node.Left.Right.Right.Left = new Node(19);
            // node.Left.Right.Right.Right = new Node(21);

            // node.Right = new Node(50).Right = new Node(58).Right = new Node(66);
            // node.Right.Left = new Node(34);
            // node.Right.Right.Left = new Node(51).Right = new Node(55);

            var trees = new BinarySearchTree();

            trees.Insert(33);
            trees.Insert(16);
            trees.Insert(13);
            trees.Insert(18);
            trees.Insert(15);
            trees.Insert(17);
            trees.Insert(25);
            trees.Insert(19);
            trees.Insert(21);
            trees.Insert(50);
            trees.Insert(34);
            trees.Insert(58);
            trees.Insert(51);
            trees.Insert(55);
            trees.Insert(66);

            trees.Delete(18);

            var trees1 = new BinarySearchTree <int>();

            trees1.Add(33);
            trees1.Add(16);
            trees1.Add(13);
            trees1.Add(18);
            trees1.Add(15);
            trees1.Add(17);
            trees1.Add(25);
            trees1.Add(19);
            trees1.Add(21);
            trees1.Add(50);
            trees1.Add(34);
            trees1.Add(58);
            trees1.Add(51);
            trees1.Add(55);
            trees1.Add(66);

            trees1.Remove(13);
            Console.WriteLine("Hello World!");
        }