static void Main(string[] args)
        {
            BinaryTree binaryTree = new BinaryTree(new Node(1));

            binaryTree.Add(new Node(2), binaryTree.Root);
            binaryTree.Add(new Node(3), binaryTree.Root);
            binaryTree.Add(new Node(4), binaryTree.Root);
            binaryTree.Add(new Node(5), binaryTree.Root);

            BinarySearchTree BST = new BinarySearchTree(new Node(50));

            BST.Add(new Node(25), BST.Root);
            BST.Add(new Node(10), BST.Root);
            BST.Add(new Node(75), BST.Root);
            BST.Add(new Node(60), BST.Root);
            BST.Add(new Node(72), BST.Root);
            BST.Add(new Node(80), BST.Root);

            Console.WriteLine("BreadthFirst()");
            //1-2-3-4-5
            binaryTree.BreadthFirst(binaryTree.Root);
            Console.WriteLine("------");
            //50-25-75-10-60-80-72
            BST.BreadthFirst(BST.Root);
            Console.ReadLine();
            Console.Clear();

            Console.WriteLine("PreOrder()");
            //1-2-4-5-3
            binaryTree.PreOrder(binaryTree.Root);
            Console.WriteLine("------");
            //50-25-10-75-60-72-80
            BST.PreOrder(BST.Root);
            Console.ReadLine();
            Console.Clear();

            Console.WriteLine("InOrder()");
            //4-2-5-1-3
            binaryTree.InOrder(binaryTree.Root);
            Console.WriteLine("------");
            //10-25-50-60-72-75-80
            BST.InOrder(BST.Root);
            Console.ReadLine();
            Console.Clear();

            Console.WriteLine("PostOrder()");
            //4-5-2-3-1
            binaryTree.PostOrder(binaryTree.Root);
            Console.WriteLine("------");
            //10-25-72-60-80-75-50
            BST.PostOrder(BST.Root);
        }
        static void Main(string[] args)
        {
            BinaryTree bt = new BinaryTree();

            Node rootNode = new Node(1);

            bt.Add(rootNode, new Node(2));
            bt.Add(rootNode, new Node(3));
            bt.Add(rootNode, new Node(4));
            bt.Add(rootNode, new Node(5));

            Console.WriteLine("PreOrder: 1, 2, 4, 5, 3");
            bt.PreOrder(rootNode);

            Console.WriteLine("InOrder: 4, 2, 5, 1, 3");
            bt.InOrder(rootNode);

            Console.WriteLine("PostOrder: 4, 5, 2, 3, 1");
            bt.PostOrder(rootNode);

            Console.WriteLine("Breadth First: 1, 2, 3, 4, 5");
            bt.BreadthFirst(rootNode);

            Console.WriteLine("Press ENTER for BST Methods");
            Console.ReadLine();
            Console.Clear();

            BinarySearchTree bst     = new BinarySearchTree();
            Node             newRoot = new Node(10);

            bst.Add(newRoot, new Node(5));
            bst.Add(newRoot, new Node(15));
            bst.Add(newRoot, new Node(3));
            bst.Add(newRoot, new Node(17));
            bst.Add(newRoot, new Node(7));
            bst.Add(newRoot, new Node(13));

            Console.WriteLine("PreOrder: 10, 5, 3, 7, 15, 13, 17");
            bst.PreOrder(newRoot);

            Console.WriteLine("InOrder: 3, 5, 7, 10, 13, 15, 17");
            bst.InOrder(newRoot);

            Console.WriteLine("PostOrder: 3, 7, 5, 13, 17, 15, 10");
            bst.PostOrder(newRoot);

            Console.WriteLine("BreadthFirst: 10, 5, 15, 3, 7, 13, 17");
            bst.BreadthFirst(newRoot);
            Console.ReadLine();
        }
Пример #3
0
        public static void BinarySearchTraversal()
        {
            //instantiating a new object of the BinarySearchTree class
            BinaryTree binaryTree = new BinaryTree();
            //setting the values, so we can get a visualization
            Node nodeA = new Node(1);
            Node nodeB = new Node(2);
            Node nodeC = new Node(3);
            Node nodeD = new Node(4);
            Node nodeE = new Node(5);
            Node nodeF = new Node(6);

            //setting the values of left and right child of each node
            nodeA.LeftChild  = nodeB;
            nodeA.RightChild = nodeC;
            nodeB.LeftChild  = nodeD;
            nodeB.RightChild = nodeE;
            nodeC.LeftChild  = nodeF;

            Console.WriteLine("PreOrder: ");
            //PreOrder, output values should be in the following order:
            //A, B, D, E, C, F
            //1, 2, 4, 5, 3, 6
            binaryTree.PreOrder(nodeA);

            Console.WriteLine("InOrder: ");
            //InOrder, output values should be in the following order:
            //D, B, E, A, F, C
            //4, 2, 5, 1, 6, 3
            binaryTree.InOrder(nodeA);

            Console.WriteLine("PostOrder: ");
            //PostOrder, output values should be in the following order:
            //D, E, B, F, C, A
            //4, 5, 2, 6, 3, 1
            binaryTree.PostOrder(nodeA);

            Console.WriteLine("BreadthFirst: ");
            //Breadth, output values should be in the following order:
            //A, B, C, D, E, F
            //1, 2, 3, 4, 5, 6
            binaryTree.BreadthFirst(nodeA);

            //implementing the search functionality for the Binary Tree search
            Console.WriteLine($"\nSearching a Binary Tree for a value: {nodeA.Value}");
            binaryTree.Search(nodeA, 1);
        }
Пример #4
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);
        }
Пример #5
0
        public static void TestBinaryTree()
        {
            BinaryTree newTree = new BinaryTree();

            Node nodeA = new Node(1);
            Node nodeB = new Node(2);
            Node nodeC = new Node(3);
            Node nodeD = new Node(4);
            Node nodeE = new Node(5);
            Node nodeF = new Node(6);

            nodeA.LeftChild  = nodeB;
            nodeA.RightChild = nodeC;
            nodeB.LeftChild  = nodeD;
            nodeB.RightChild = nodeE;
            nodeC.LeftChild  = nodeF;

            Console.WriteLine("Preordered Binary Tree");
            newTree.PreOrder(nodeA);

            Console.WriteLine("Postordered Binary Tree");
            newTree.PostOrder(nodeA);

            Console.WriteLine("In-order Binary Tree");
            newTree.InOrder(nodeA);

            Console.WriteLine("Breadth First binary tree");
            Console.WriteLine("");
            newTree.BreadthFirst(nodeA);

            Console.WriteLine("");
            Console.WriteLine("Search Binary Tree");
            newTree.Search(nodeA, 6);
            newTree.Search(nodeA, 3);
            newTree.Search(nodeA, 10);
        }
Пример #6
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();
            }
        }
Пример #7
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");
        }