Exemplo n.º 1
0
        private static void Main()
        {
            Console.WriteLine("Represents the following tree");
            Console.WriteLine(@"             4             ");
            Console.WriteLine(@"            / \            ");
            Console.WriteLine(@"           /   \           ");
            Console.WriteLine(@"          /     \          ");
            Console.WriteLine(@"         /       \         ");
            Console.WriteLine(@"        2         6        ");
            Console.WriteLine(@"       / \       / \       ");
            Console.WriteLine(@"      /   \     /   \      ");
            Console.WriteLine(@"     /     \   /     \     ");
            Console.WriteLine(@"     1     3   5     7     ");
            var binaryTree = new BinaryTree<int>();
            binaryTree.Add(4);
            binaryTree.Add(2);
            binaryTree.Add(6);
            binaryTree.Add(1);
            binaryTree.Add(3);
            binaryTree.Add(5);
            binaryTree.Add(7);

            var preOrderTraversal = new PreOrderTraversal<int>();
            Console.Write("PreOrder: ");
            preOrderTraversal.Traverse(binaryTree.Root);
            Console.WriteLine();

            Console.Write("InOrder: ");
            var inOrderTraversal = new PreOrderTraversal<int>();
            inOrderTraversal.Traverse(binaryTree.Root);
            Console.WriteLine();

            Console.Write("PostOrder: ");
            var postOrderTraversal = new PostOrderTraversal<int>();
            postOrderTraversal.Traverse(binaryTree.Root);
            Console.WriteLine();

            Console.WriteLine("Value to find: 5");
            Node<int> nodeFound = binaryTree.Find(5);

            if (nodeFound == null)
                Console.WriteLine("Value not found");
            else
                Console.WriteLine("Value found: {0}", nodeFound.Value);
            Console.WriteLine();

            Console.WriteLine("Value to find: 100");
            Node<int> notFound = binaryTree.Find(100);
            Console.WriteLine(notFound == null ? "Worked as expected" : "Failed");
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            //declare and define the tree root for both
            Node treeRoot = new Node(20);

            //create new Binary Search Tree
            BinarySearchTree binarySearch = new BinarySearchTree();

            //create new binary tree
            BinaryTree biTree = new BinaryTree();

            //add to binary search tree
            Console.WriteLine("adding some nodes to binary search tree");
            binarySearch.Add(treeRoot, new Node(17));
            binarySearch.Add(treeRoot, new Node(15));
            binarySearch.Add(treeRoot, new Node(10));
            binarySearch.Add(treeRoot, new Node(9));
            binarySearch.Add(treeRoot, new Node(5));
            binarySearch.Add(treeRoot, new Node(3));

            //add to binary tree
            Console.WriteLine("adding some nodes to binary tree");
            biTree.Add(treeRoot, new Node(3));
            biTree.Add(treeRoot, new Node(5));
            biTree.Add(treeRoot, new Node(9));
            biTree.Add(treeRoot, new Node(10));
            biTree.Add(treeRoot, new Node(15));
            biTree.Add(treeRoot, new Node(17));

            // expected output 20, 3, 5, 9, 10, 15, 17
            binarySearch.PreOrder(treeRoot);
            biTree.PreOrder(treeRoot);


            // expected output
            biTree.InOrder(treeRoot);

            // expected output
            biTree.PostOrder(treeRoot);

            // expected output
            biTree.BreadthFirst(treeRoot);
        }
Exemplo n.º 3
0
        public static void BinaryTree()
        {
            Console.Clear();
            Console.WriteLine("This is a Binary Tree");
            Node       n1         = new Node(15);
            Node       n2         = new Node(20);
            Node       n3         = new Node(10);
            Node       n4         = new Node(70);
            Node       n5         = new Node(30);
            Node       n6         = new Node(40);
            Node       n7         = new Node(50);
            BinaryTree binaryTree = new BinaryTree(n1);

            binaryTree.Add(n1, n2);
            binaryTree.Add(n1, n3);
            binaryTree.Add(n1, n4);
            binaryTree.Add(n1, n5);
            binaryTree.Add(n1, n6);
            binaryTree.Add(n1, n7);

            Console.WriteLine("This is BreadthFirst");
            Console.WriteLine("The order should be:");
            Console.WriteLine("15 -> 20 -> 10 -> 70 -> 30 -> 40 -> 50");
            Console.WriteLine("The traversal starts now:");
            // 15 -> 20 -> 10 -> 70 -> 30 -> 40 -> 50 ->
            binaryTree.BreadthFirst(n1);
            Console.ReadLine();

            Console.Clear();
            Console.WriteLine("This is PreOrder");
            Console.WriteLine("The order should be:");
            Console.WriteLine("15 -> 20 -> 70 -> 30 -> 10 -> 40 -> 50");
            Console.WriteLine("The traversal starts now:");
            // 15 -> 20 -> 70 -> 30 -> 10 -> 40 -> 50
            binaryTree.PreOrder(n1);
            Console.ReadLine();

            Console.Clear();
            Console.WriteLine("This is InOrder");
            Console.WriteLine("The order should be:");
            Console.WriteLine("70 -> 20 -> 30 -> 15 -> 40 -> 10 -> 50");
            Console.WriteLine("The traversal starts now:");
            // 70 -> 20 -> 30 -> 15 -> 40 -> 10 -> 50
            binaryTree.InOrder(n1);
            Console.ReadLine();

            Console.Clear();
            Console.WriteLine("This is PostOrder");
            Console.WriteLine("The order should be:");
            Console.WriteLine("70 -> 30 -> 20 -> 40 -> 50 -> 10 -> 15");
            Console.WriteLine("The traversal starts now:");
            // 70 -> 30 -> 20 -> 40 -> 50 -> 10 -> 15
            binaryTree.PostOrder(n1);
            Console.WriteLine();

            Console.Clear();
            Console.WriteLine("Here is a search for a value in the tree");
            try
            {
                Node node = binaryTree.Search(n1, 20);
                Console.WriteLine(node.Value);
                Console.ReadLine();
                Console.WriteLine("Here is a search for a value not in the tree");
                Node node2 = binaryTree.Search(n1, 25);
                Console.WriteLine(node2.Value);
            }
            catch (NullReferenceException)
            {
                Console.WriteLine("A null reference exception was thrown");
                Console.ReadLine();
            }
        }
Exemplo n.º 4
0
        public static void Main(string[] args)
        {
            // Declare placeholder variables - used later by various methods
            string menuSelection = "", newNodeInput = "", nodeSearchValue = "";

            // Instantiate BinaryTree and BinarySearchTree objects, add initial values
            BinaryTree binTree = new BinaryTree(new Node(25));

            binTree.Add(binTree.Root, new Node(10));
            binTree.Add(binTree.Root, new Node(5));
            binTree.Add(binTree.Root, new Node(7));
            binTree.Add(binTree.Root, new Node(1));
            binTree.Add(binTree.Root, new Node(40));
            binTree.Add(binTree.Root, new Node(50));
            binTree.Add(binTree.Root, new Node(30));

            BinarySearchTree binSearchTree = new BinarySearchTree(new Node(25));

            binSearchTree.Add(binSearchTree.Root, new Node(10));
            binSearchTree.Add(binSearchTree.Root, new Node(5));
            binSearchTree.Add(binSearchTree.Root, new Node(7));
            binSearchTree.Add(binSearchTree.Root, new Node(1));
            binSearchTree.Add(binSearchTree.Root, new Node(40));
            binSearchTree.Add(binSearchTree.Root, new Node(50));
            binSearchTree.Add(binSearchTree.Root, new Node(30));

            // Loop until the user enters the "8" key to exit the application
            do
            {
                // Prompt user to select an option from the menu
                PrintMainMenu();
                menuSelection = Console.ReadLine();
                Console.Clear();

                switch (menuSelection)
                {
                case "1":     // Adds a Node to the Binary Tree
                    Console.WriteLine("What value you like add to the Binary Tree?");
                    newNodeInput = Console.ReadLine();
                    Console.Clear();

                    if (int.TryParse(newNodeInput, out int binTree_Add))
                    {
                        binTree.Add(binTree.Root, new Node(binTree_Add));
                        Console.WriteLine("Success!");
                    }
                    else
                    {
                        Console.WriteLine("Sorry, unable to add that value to the Binary Tree.");
                    }

                    PromptToReturnToMainMenu();
                    break;

                case "2":     // Adds a Node to the Binary Search Tree
                    Console.WriteLine("What value you like add to the Binary Search Tree?");
                    newNodeInput = Console.ReadLine();
                    Console.Clear();

                    if (int.TryParse(newNodeInput, out int binSearchTree_Add))
                    {
                        binSearchTree.Add(binSearchTree.Root, new Node(binSearchTree_Add));
                        Console.WriteLine("Success!");
                    }
                    else
                    {
                        Console.WriteLine("Sorry, unable to add that value to the Binary Search Tree.");
                    }

                    PromptToReturnToMainMenu();
                    break;

                case "3":     // Searches for a value in the Binary Tree
                    Console.WriteLine("What value you like search for in the Binary Tree?");
                    nodeSearchValue = Console.ReadLine();
                    Console.Clear();

                    if (int.TryParse(nodeSearchValue, out int binTree_Search))
                    {
                        Node foundBinTreeNode = binTree.Search(binTree.Root, binTree_Search);

                        if (foundBinTreeNode != null)
                        {
                            Console.WriteLine("Found!");
                        }
                        else
                        {
                            Console.WriteLine("That value does not exist within the Binary Tree");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Sorry, please input an integer to search for.");
                    }

                    PromptToReturnToMainMenu();
                    break;

                case "4":     // Searches for a value in the Binary Search Tree
                    Console.WriteLine("What value you like search for in the Binary Search Tree?");
                    nodeSearchValue = Console.ReadLine();
                    Console.Clear();

                    if (int.TryParse(nodeSearchValue, out int binSearchTree_Search))
                    {
                        Node foundBinSearchTreeNode = binSearchTree.Search(binSearchTree.Root, binSearchTree_Search);

                        if (foundBinSearchTreeNode != null)
                        {
                            Console.WriteLine("Found!");
                        }
                        else
                        {
                            Console.WriteLine("That value does not exist within the Binary Search Tree");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Sorry, please input an integer to search for.");
                    }

                    PromptToReturnToMainMenu();
                    break;

                case "5":     // Prints the values in the Binary Tree in "Preorder" sequence
                    binTree.PreOrder(binTree.Root);

                    PromptToReturnToMainMenu();
                    break;

                case "6":     // Prints the values in the Binary Tree in "Postorder" sequence
                    binTree.PostOrder(binTree.Root);

                    PromptToReturnToMainMenu();
                    break;

                case "7":     // Prints the values in the Binary Tree in "Inorder" sequence
                    binTree.InOrder(binTree.Root);

                    PromptToReturnToMainMenu();
                    break;

                case "8":     // Prints the values in the Binary Tree in "Breadth First" sequence
                    binTree.BreadthFirst(binTree.Root);

                    PromptToReturnToMainMenu();
                    break;

                case "9":     // Exits the Program
                    Environment.Exit(0);
                    break;

                default:     // Handles cases where user doesn't enter a valid menu option
                    Console.WriteLine("That did not match one of the menu options. Try again.\n");
                    break;
                }
            } while (menuSelection != "9");
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            BinaryTree BTree = new BinaryTree();

            BTree.Add(15);
            BTree.Add(5);
            BTree.Add(10);
            BTree.Add(75);
            BTree.Add(20);
            BTree.Add(11);
            BTree.Add(50);
            BTree.Add(1);
            BTree.Add(6);
            BTree.Add(32);
            BTree.Add(38);
            BTree.Add(26);
            BTree.Add(7);
            BTree.Add(200);

            Console.WriteLine(BTree.Contains(7));
            InOrder(BTree);
        }