private static void TestBinarySearchTree()
        {
            var sb = new StringBuilder();

            var tree = new BinarySearchTree<int>(15);

            for (int i = 0; i < 30; i++)
            {
                tree.Add(i);
            }

            var clonedNode = (TreeNode<int>)tree.Root.Clone();

            sb.AppendLine(tree.ToString())
                .AppendLine("Tree root: " + tree.Root.ToString())
                .AppendLine("Tree contains 7? " + tree.Contains(7).ToString())
                .AppendLine("Cloned root: " + clonedNode.ToString())
                .AppendLine("Cloned Equals root? " + (clonedNode.Equals(tree.Root)).ToString())
                .AppendLine("Cloned == root? " + (clonedNode == tree.Root).ToString())
                .AppendLine("Cloned != root? " + (clonedNode != tree.Root).ToString())
                .AppendLine("12 deleted. New tree:");

            Console.Write(sb.ToString());

            tree.Delete(12);
            Console.WriteLine(tree.ToString());
        }
Ejemplo n.º 2
0
        private static void Main()
        {
            BinarySearchTree<int, string> tree = new BinarySearchTree<int, string>();

            tree.Add(1, "Pesho");
            tree.Add(2, "Gosho");
            tree.Add(3, "Minka");

            foreach (var item in tree)
            {
                Console.WriteLine(item);
            }

            tree.Remove(1);
            Console.WriteLine();
            foreach (var item in tree)
            {
                Console.WriteLine(item);
            }

            BinarySearchTree<int, string> tree2 = new BinarySearchTree<int, string>();

            tree2.Add(1, "Pesho");
            tree2.Add(2, "Gosho");
            tree2.Add(3, "Minka");
            tree.Add(1, "Pesho");

            Console.WriteLine(tree.Equals(tree2));
        }
Ejemplo n.º 3
0
		public static void Main (string[] args)
		{
			Console.WriteLine ("Hello World!");
			BinarySearchTree bst = new BinarySearchTree ();
			bst.Insert (23);
			bst.Insert (45);
			bst.Insert (16);
			bst.Insert (37);
			bst.Insert (3);
			bst.Insert (99);
			bst.Insert (22);
			Console.WriteLine ("Traversal");
			bst.InOrder (bst.root);
			Console.ReadLine ();
		}
Ejemplo n.º 4
0
 static void Main(string[] args)
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     tree.Insert(9);
     tree.Insert(7);
     tree.Insert(15);
     tree.Insert(6);
     tree.Insert(8);
     tree.Insert(10);
     tree.Insert(20);
     tree.Insert(21);
     tree.Insert(19);
     tree.PrintDFS();
     Console.WriteLine("Remove 15");
     tree.Remove(15);
     tree.PrintDFS();
 }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            BinarySearchTree<int> myTree = new BinarySearchTree<int>();

            myTree.Add(86);
            myTree.Add(12);
            myTree.Add(45);
            myTree.Add(34);
            myTree.Add(100);
            myTree.Add(89);
            myTree.Add(14);
            myTree.Add(55);

            myTree.Find(34);

            // myTree.RemoveNode(45);
            // myTree.RemoveNode(34);
            myTree.RemoveNode(89);

            foreach (var item in myTree)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();
            BinarySearchTree<int> myNewTree = (BinarySearchTree<int>)myTree.Clone();

            myTree.Root.value = 87;
            foreach (var item in myNewTree)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();
            foreach (var item in myTree)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine(myTree.Equals(myNewTree));
            Console.WriteLine(myNewTree.ToString());
            Console.WriteLine(myNewTree == myTree);
            Console.WriteLine(myNewTree != myTree);
            Console.WriteLine(myTree.GetHashCode());

        }
        private static void Main(string[] args)
        {
            BinarySearchTree<int> tree = new BinarySearchTree<int>();

            tree.Add(4);
            tree.Add(2);
            tree.Add(8);
            tree.Add(9);

            Console.WriteLine(tree);
            Console.WriteLine();
            var tree2 = tree.Clone();
            tree.Add(12);

            Console.WriteLine();
            Console.WriteLine(tree);
            Console.WriteLine();

            Console.WriteLine(tree2);
        }
        static void Main()
        {
            // Didn't finish this one before the deadline for the homework so I couldn't test if some of parts that I managed to
            // ... implement actually work.

            BinarySearchTree<int> tree = new BinarySearchTree<int>();   // Adding works
            tree.Add(7); tree.Add(6); tree.Add(5); tree.Add(4); tree.Add(3); tree.Add(2); tree.Add(1); tree.Add(6);
            tree.Add(5); tree.Add(4); tree.Add(3); tree.Add(2); tree.Add(6); tree.Add(5); tree.Add(4); tree.Add(3);
            tree.Add(6); tree.Add(5); tree.Add(4); tree.Add(6); tree.Add(5); tree.Add(6); tree.Add(7); tree.Add(15);
            tree.Add(14);tree.Add(13);tree.Add(12);tree.Add(11);tree.Add(10);tree.Add(14);tree.Add(13);tree.Add(12);
            tree.Add(11);tree.Add(14);tree.Add(13);tree.Add(12);tree.Add(14);tree.Add(13);tree.Add(14);tree.Add(20);
            tree.Add(19);tree.Add(16);tree.Add(16);tree.Add(17);tree.Add(18);tree.Add(19);tree.Add(21);tree.Add(20);
            tree.Add(22);

            // tree written down
            //                             7
            //                  6                     15
            //                5   7               14      20
            //            4    6              13      19      21
            //        3    5  6         12     14   16   20     22
            //    2    4  5  6      11    13  14   16 17
            //1    3  4  5  6   10   12  13  14         18
            // 2  3  4  5  6     11 12  13  14            19

            BinarySearchTree<int> clonedTree = (BinarySearchTree<int>)tree.Clone(); // Cloneing works as far as I can see by debugging

            // Find() goes into an infinite loop
            TreeNode<int> node = clonedTree.Find(100);  // Null
            PrintNode(node);
            node = clonedTree.Find(-100);   // Null
            PrintNode(node);
            node = clonedTree.Find(9);      // Null
            PrintNode(node);
            node = clonedTree.Find(13);     // Data: 13 (L: 12, R: 14) -> Depth: 4, Offset: -1 ? I think
            PrintNode(node);
        }
        static void Main(string[] args)
        {
            BinarySearchTree bst = new BinarySearchTree();
            bst.Insert(50);
            bst.Insert(33);
            bst.Insert(21);
            bst.Insert(55);
            bst.Insert(9);
            bst.Insert(13);
            bst.Insert(5);
            bst.Insert(57);
            bst.PrintTree();

            bst.Search(5);
            Console.ReadLine();
        }
Ejemplo n.º 9
0
        static void Main()
        {
            try
            {
                #region Test1: Creating of nodes

                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Test1: Creating of nodes");
                Console.ResetColor();
                Console.Write("Press any key to start the test...");
                Console.ReadKey();

                // Creates new empty tree
                var tree = new BinarySearchTree<int>();
                Console.WriteLine("\n\nA new tree was created...");

                // Adds some nodes in the tree
                Thread.Sleep(1500);
                tree.Add(2);
                tree.Add(7);
                tree.Add(1);
                tree.Add(5);
                tree.Add(22);
                tree.Add(-7);
                tree.Add(4);
                Console.WriteLine("{0} nodes were added in the tree.\n", tree.Nodes.Count);

                // Prints the tree
                Thread.Sleep(1500);
                Console.Write("The tree: ");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine(tree);
                Console.ResetColor();

                // Prints the nodes of the tree
                Thread.Sleep(1500);
                for (int i = 0; i < tree.Nodes.Count; i++)
                {
                    Console.Write("Node{0}: ", i);
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine("{0}", tree.Nodes[i]);
                    Console.ResetColor();
                    Thread.Sleep(400);
                }

                #endregion

                #region Test2: Searching

                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("\nTest2: Searching");
                Console.ResetColor();
                Console.Write("Press any key to start the test...");
                Console.ReadKey();

                // Search for some node in the tree
                Console.WriteLine();
                TreeNodeSearch(tree, 2);
                TreeNodeSearch(tree, 7);
                TreeNodeSearch(tree, 14);
                TreeNodeSearch(tree, 22);

                #endregion

                #region Test3: Cloning

                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("\nTest3: Cloning");
                Console.ResetColor();
                Console.Write("Press any key to start the test...");
                Console.ReadKey();

                // Cloning of the tree
                var clone = tree.Clone() as BinarySearchTree<int>;
                Console.WriteLine("\n\nA clone was created...");

                // Prints the clone
                Thread.Sleep(1500);
                Console.Write("The clone: ");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine(clone);
                Console.ResetColor();

                // Cloning check
                TreeCloningCheck(tree, clone, false);

                #endregion

                #region Test4: Deleting

                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("\nTest4: Deleting");
                Console.ResetColor();
                Console.Write("Press any key to start the test...");
                Console.ReadKey();

                // Delete two nodes from the tree
                tree.Delete(2);
                tree.Delete(5);
                Console.WriteLine("\n\nTwo nodes with value 2 and 5 were deleted from the tree.");

                // Prints the tree
                Thread.Sleep(1500);
                Console.Write("\nThe tree: ");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine(tree);
                Console.ResetColor();
                Thread.Sleep(1000);
                Console.Write("The clone: ");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine(clone);
                Console.ResetColor();

                // Cloning check
                TreeCloningCheck(tree, clone, true);

                // Gets a HashCode for the tree
                Thread.Sleep(1500);
                Console.WriteLine("\nThe HashCode for the tree: {0}", tree.GetHashCode());

                // Gets a HashCode for the clone
                Thread.Sleep(1000);
                Console.WriteLine("The HashCode for the clone: {0}\n", clone.GetHashCode());

                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 10
0
 private static void TreeNodeSearch(BinarySearchTree<int> tree, int number)
 {
     Thread.Sleep(1000);
     if (tree.Search(number) != null)
     {
         Console.WriteLine("\nNode with value:{0} was found in the tree.", number);
         Console.ForegroundColor = ConsoleColor.Gray;
         Console.WriteLine("{0,7}{1}", " ", tree.Search(number));
         Console.ResetColor();
     }
     else
     {
         Console.WriteLine("\nNode with value:{0} was not found in the tree.", number);
         Console.ResetColor();
     }
 }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            var bt = new BinarySearchTree();
            int x;

            while (true)
            {
                Console.WriteLine("1. Display tree");
                Console.WriteLine("2. Search");
                Console.WriteLine("3. Insert a new node");
                Console.WriteLine("4. Delete a node");
                Console.WriteLine("5. PreOrder Traversal");
                Console.WriteLine("6. InOrder Traversal");
                Console.WriteLine("7. PostOrder Traversal");
                Console.WriteLine("8. Height of tree");
                Console.WriteLine("9. Find Minimum key");
                Console.WriteLine("10. Find Maximum key");
                Console.WriteLine("11. Quit");

                var choice = Convert.ToInt32(Console.ReadLine());

                if (choice == 11)
                {
                    break;
                }

                switch (choice)
                {
                case 1:
                    bt.Display();
                    break;

                case 2:
                    Console.Write("Enter a key to be searched : ");
                    x = Convert.ToInt32(Console.ReadLine());

                    Console.WriteLine(bt.Search(x) ? "Key was found" : "Key not found");
                    break;

                case 3:
                    Console.Write("Enter a key to be Inserted : ");
                    x = Convert.ToInt32(Console.ReadLine());
                    bt.Insert(x);
                    break;

                case 4:
                    Console.Write("Enter a key to be Deleted : ");
                    x = Convert.ToInt32(Console.ReadLine());
                    bt.Delete(x);
                    break;

                case 5:
                    bt.PreOrder();
                    break;

                case 6:
                    bt.InOrder();
                    break;

                case 7:
                    bt.PostOrder();
                    break;

                case 8:
                    Console.WriteLine($"The HEIGHT of the tree is : {bt.Height()}");
                    break;

                case 9:
                    Console.WriteLine($"The MINIMUM key is : {bt.Min()}");
                    break;

                case 10:
                    Console.WriteLine($"The MAXIMUM key is : {bt.Max()}");
                    break;

                default:
                    Console.WriteLine("Invalid selection entered!!!");
                    break;
                }
            }
        }
Ejemplo n.º 12
0
        static void Main(string[] args)
        {
            BinarySearchTree tree = new BinarySearchTree(48);

            tree.AddItem(79);
            tree.AddItem(83);
            tree.AddItem(82);
            tree.AddItem(23);
            tree.AddItem(73);
            tree.AddItem(90);
            tree.AddItem(8);
            tree.AddItem(56);
            tree.AddItem(61);

            Console.WriteLine(tree.TreeCount());
            tree.TreePrint();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            tree.AddItem(52);
            tree.AddItem(73);
            tree.AddItem(68);
            tree.AddItem(85);
            tree.AddItem(58);
            tree.AddItem(18);
            tree.AddItem(49);
            tree.AddItem(47);
            tree.AddItem(87);
            tree.AddItem(61);

            Console.WriteLine(tree.TreeCount());
            tree.TreePrint();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            tree.AddItem(83);
            tree.AddItem(66);
            tree.AddItem(31);
            tree.AddItem(64);
            tree.AddItem(58);
            tree.AddItem(97);
            tree.AddItem(59);
            tree.AddItem(10);
            tree.AddItem(54);
            tree.AddItem(45);

            Console.WriteLine(tree.TreeCount());
            tree.TreePrint();

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Breadth First");

            List <int> nodes = tree.BreadthFirst();

            for (int i = 0; i < nodes.Count(); i++)
            {
                Console.Write(nodes[i] + "    ");
            }

            Console.ReadLine();
        }
Ejemplo n.º 13
0
        static void Main(string[] args)
        {
            BinarySearchTree bt = new BinarySearchTree();
            int choice, x;

            while (true)
            {
                Console.WriteLine("1.Display Tree");
                Console.WriteLine("2.Search");
                Console.WriteLine("3.Insert a new node");
                Console.WriteLine("4.Delete a node");
                Console.WriteLine("5.Preorder Traversal");
                Console.WriteLine("6.Inorder Traversal");
                Console.WriteLine("7.Postorder Traversal");
                Console.WriteLine("8.Height of tree");
                Console.WriteLine("9.Find Minimum key");
                Console.WriteLine("10.Find Maximum key");
                Console.WriteLine("11.Quit");
                Console.Write("Enter your choice:");
                choice = Convert.ToInt32(Console.ReadLine());
                if (choice == 11)
                {
                    break;
                }
                switch (choice)
                {
                case 1:
                    bt.Display();
                    break;

                case 2:
                    Console.Write("Enter the key to be searched:");
                    x = Convert.ToInt32(Console.ReadLine());
                    if (bt.Search(x))
                    {
                        Console.WriteLine("Key found");
                    }
                    else
                    {
                        Console.WriteLine("key not found");
                    }
                    break;

                case 3:
                    Console.Write("Enter the key to be inserted:");
                    x = Convert.ToInt32(Console.ReadLine());
                    bt.Insert(x);
                    break;

                case 4:
                    Console.Write("Enter the key to be deleted:");
                    x = Convert.ToInt32(Console.ReadLine());
                    bt.Delete(x);
                    break;

                case 5:
                    bt.PreOrder();
                    break;

                case 6:
                    bt.InOrder();
                    break;

                case 7:
                    bt.PostOrder();
                    break;

                case 8:
                    Console.WriteLine("Height of tree is " + bt.Height());
                    break;

                case 9:
                    Console.WriteLine("Minimum key is " + bt.Min());
                    break;

                case 10:
                    Console.WriteLine("Maximum key is " + bt.Max());
                    break;
                }
            }
        }
Ejemplo n.º 14
0
        public static void Main()
        {
            BinarySearchTree tree = new BinarySearchTree();

            Random r = new Random();

            WriteLine("Welcome! The purpose of this program is to generate numbers randomly " +
                      "\nand use a binary search tree to sort and find any given number.");
            string userInput = ReadLine();

            for (int index = 0; index < 13; index++)
            {
                int generated = r.Next(0, 100);
                tree.Insert(generated);
            }

            // Write("Would you like to traverse in-order? \"No\" skips. ");
            // userInput = ReadLine();

            //if (!string.Equals(userInput, "No", StringComparison.OrdinalIgnoreCase))
            //    tree.Inorder();

            //Write("Would you like to traverse pre-order? \"No\" skips. ");
            //userInput = ReadLine();

            //if (!string.Equals(userInput, "No", StringComparison.OrdinalIgnoreCase))
            //    tree.Preorder();

            //Write("Would you like to traverse post-order? \"No\" skips. ");
            //userInput = ReadLine();

            //if (!string.Equals(userInput, "No", StringComparison.OrdinalIgnoreCase))
            //    tree.Postorder();

            //Write("Would you like to print the smallest number? \"No\" skips. ");
            //userInput = ReadLine();

            //if (!string.Equals(userInput, "No", StringComparison.OrdinalIgnoreCase))
            //    tree.Minimum();

            //Write("Would you like to print the largest number? \"No\" skips. ");
            //userInput = ReadLine();

            //if (!string.Equals(userInput, "No", StringComparison.OrdinalIgnoreCase))
            //    tree.Maximum();

            //Write("Would you like to search for a particular number? \"Yes\" continues. ");
            //userInput = ReadLine();

            //if (string.Equals(userInput, "Yes", StringComparison.OrdinalIgnoreCase))
            //{
            //    Write("Please enter a number to search for: ");
            //    int number = int.Parse(ReadLine());
            //    tree.Contains(number);
            //}

            //Write("Would you like to delete a particular number? \"Yes\" continues. ");
            //userInput = ReadLine();

            //if (string.Equals(userInput, "Yes", StringComparison.OrdinalIgnoreCase))
            //{
            //    Write("Please enter a number to delete: ");
            //    int number = int.Parse(ReadLine());
            //    tree.Delete(number);
            //}

            tree.Inorder();
        }
Ejemplo n.º 15
0
 static void Main(string[] args)
 {
     BinarySearchTree tree = new BinarySearchTree();
 }
Ejemplo n.º 16
0
        private static void TreeCloningCheck(BinarySearchTree<int> tree, BinarySearchTree<int> clone, bool b)
        {
            Thread.Sleep(1500);
            Console.WriteLine("\nCloning check...{0,18}== test{0,8}!= test{0,8}Equal test", " ");
            Console.Write("{0}", (b) ? "The cloning after deletion is: " : "The cloning is: ");

            Thread.Sleep(1500);
            Console.ForegroundColor = tree == clone ? ConsoleColor.Green : ConsoleColor.Red;
            Console.Write(b ? "{0,10}" : "{0,25}", tree == clone ? "OK" : " X");

            Thread.Sleep(1000);
            Console.ForegroundColor = !(tree != clone) ? ConsoleColor.Green : ConsoleColor.Red;
            Console.Write("{0,15}", !(tree != clone) ? "OK" : " X");

            Thread.Sleep(1000);
            Console.ForegroundColor = tree.Equals(clone) ? ConsoleColor.Green : ConsoleColor.Red;
            Console.WriteLine("{0,18}", tree.Equals(clone) ? "OK" : " X");

            Console.ResetColor();
        }
 public BinarySearchTree(T nodeData)
 {
     this.NodeData  = nodeData;
     this.LeftTree  = null;
     this.RightTree = null;
 }