Ejemplo n.º 1
0
        /// <summary>
        /// Tranverses the tree depth first, pre-orderly and prints the value of each node in the tree; if the node is divisible by 3, 5, or 15 then the value is replaced by "Fizz", "Buzz", or "FizzBuzz" respectively
        /// </summary>
        /// <param name="tree">Binary tree to be traversed</param>
        /// <param name="top">The tree's root value</param>
        public static void FizzBuzzTree(BinaryTree tree, Node top)
        {
            tree.nodes = new List <Node>();
            List <Node> preOrder = tree.PreOrder(top);

            foreach (var item in preOrder)
            {
                Console.WriteLine(FizzBuzzPrint(item));
            }
        }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            BinaryTree <object> tree = new BinaryTree <object>();

            Node <object> node1  = new Node <object>(1);
            Node <object> node2  = new Node <object>(2);
            Node <object> node3  = new Node <object>(3);
            Node <object> node4  = new Node <object>(4);
            Node <object> node5  = new Node <object>(5);
            Node <object> node6  = new Node <object>(6);
            Node <object> node7  = new Node <object>(7);
            Node <object> node8  = new Node <object>(8);
            Node <object> node9  = new Node <object>(9);
            Node <object> node10 = new Node <object>(10);
            Node <object> node11 = new Node <object>(15);

            tree.Root                                 = node1;
            tree.Root.LeftChild                       = node2;
            tree.Root.RightChild                      = node3;
            tree.Root.LeftChild.LeftChild             = node4;
            tree.Root.LeftChild.RightChild            = node5;
            tree.Root.RightChild.LeftChild            = node6;
            tree.Root.RightChild.RightChild           = node7;
            tree.Root.LeftChild.LeftChild.LeftChild   = node8;
            tree.Root.LeftChild.LeftChild.RightChild  = node9;
            tree.Root.LeftChild.RightChild.LeftChild  = node10;
            tree.Root.LeftChild.RightChild.RightChild = node11;

            // Print tree nodes before FizzBuzz
            var fbTreeList = tree.PreOrder(tree.Root);

            foreach (var obj in tree.Nodes)
            {
                Console.Write($"{obj} ");
            }
            Console.WriteLine();
            tree.ClearNodeList();             // Clear tree node list before next tree traversal

            // Demo FizzBuzzTree() method
            fbTreeList = FizzBuzzTree(tree, tree.Root);

            // Print tree nodes after FizzBuzz
            foreach (var obj in tree.Nodes)
            {
                Console.Write($"{obj} ");
            }
            Console.WriteLine();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Node Root       = new Node(2);
            Node Fizz       = new Node(3);
            Node FizzSix    = new Node(6);
            Node BuzzTen    = new Node(10);
            Node FizzBuzz   = new Node(15);
            Node FizzBuzz30 = new Node(30);

            Node Four  = new Node(4);
            Node Eight = new Node(8);
            Node Seven = new Node(7);

            Root.Left  = Fizz;
            Root.Right = BuzzTen;

            Root.Left.Left  = FizzSix;
            Root.Left.Right = Four;

            Root.Left.Left.Left  = Eight;
            Root.Left.Left.Right = FizzBuzz;

            Root.Left.Right.Left  = Seven;
            Root.Left.Right.Right = FizzBuzz30;

            List <int> PreOrderTraversal = BinaryTree.PreOrder(Root);

            Console.WriteLine("The integer values by PreOrder Traversal stored in the List is: ");
            foreach (var item in PreOrderTraversal)
            {
                Console.Write($"{item} ");
            }

            Console.WriteLine();

            List <string> FizzBuzzOutput = FizzBuzzTree(Root);

            Console.WriteLine();
            Console.WriteLine("The FizzBuzz values by PreOrder Traversal stored in the List is: ");
            foreach (var item in FizzBuzzOutput)
            {
                Console.Write($"{item} ");
            }
            Console.ReadKey();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            BinaryTree <int> bt = new BinaryTree <int>();

            bt.Add(3);
            bt.Add(5);
            bt.Add(15);
            bt.Add(4);
            bt.Add(82);
            bt.PreOrder(bt.Root);
            Console.WriteLine("Initial tree:");
            Array.ForEach(bt.ToArray(), elm => Console.Write($"{elm}, "));
            Console.WriteLine();
            Console.WriteLine("FizzBuzz parsed tree:");
            Console.WriteLine(FizzBuzzTree(bt));
            Console.ReadLine();
        }
        /// <summary>
        /// Main method gets executed upon start
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Console.WriteLine("Hey World");

            BinaryTree BT = new BinaryTree(new Node("5"));

            BT.Root.LeftChild             = new Node("9");
            BT.Root.RightChild            = new Node("4");
            BT.Root.LeftChild.LeftChild   = new Node("30");
            BT.Root.RightChild.LeftChild  = new Node("10");
            BT.Root.RightChild.RightChild = new Node("19");
            BT.PreOrder(BT.Root);
            Console.WriteLine("Now with fizzbuzz");
            FizzBuzzTree(BT.Root);
            //BT.PreOrder(BT.Root);
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            BinaryTree <object> tree = new BinaryTree <object>();

            BTNode <object> node1 = new BTNode <object>(10);
            BTNode <object> node2 = new BTNode <object>(2);
            BTNode <object> node3 = new BTNode <object>(3);
            BTNode <object> node4 = new BTNode <object>(4);
            BTNode <object> node5 = new BTNode <object>(5);
            BTNode <object> node6 = new BTNode <object>(6);
            BTNode <object> node7 = new BTNode <object>(15);

            tree.Root   = node1;
            node1.Left  = node2;
            node1.Right = node3;
            node2.Left  = node4;
            node2.Right = node5;
            node3.Left  = node6;
            node3.Right = node7;

            Console.WriteLine("Initial tree values in preorder:");
            object[] output = tree.PreOrder();
            foreach (var item in output)
            {
                Console.Write(item + " ");
            }

            Console.WriteLine();
            Console.WriteLine("-----------------------");

            BinaryTree <object> treeMutated = FizzBuzzTreeFunction(tree);

            Console.WriteLine("Mutated tree values in preorder:");
            object[] postOutput = treeMutated.PreOrder();
            foreach (var item in postOutput)
            {
                Console.Write(item + " ");
            }

            Console.WriteLine();
            Console.WriteLine();
        }
Ejemplo n.º 7
0
        public static void Main(string[] args)
        {
            // Declare placeholder variables - used later by various methods
            string menuSelection = "", newNodeInput = "";

            // Instantiate BinaryTree object, add initial values
            BinaryTree binTree = ResetBinaryTree();

            // 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();

                    string[] nodeInputArr = newNodeInput.Split(" ");

                    foreach (string n in nodeInputArr)
                    {
                        binTree.Add(binTree.Root, new Node(n));
                    }

                    Console.WriteLine("Success!");

                    PromptToReturnToMainMenu();
                    break;

                case "2":     // Converts Node Values to "FizzBuzz" values
                    Console.Write("Before: ");
                    binTree.PreOrder(binTree.Root);
                    Console.WriteLine();

                    binTree.ConvertToFizzBuzz(binTree.Root);

                    Console.Write("After: ");
                    binTree.PreOrder(binTree.Root);
                    Console.WriteLine();

                    PromptToReturnToMainMenu();
                    break;

                case "3":     // Resets Binary Tree to original Node Values
                    binTree = ResetBinaryTree();
                    Console.WriteLine("Your Binary Tree has been reset.");

                    PromptToReturnToMainMenu();
                    break;

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

                    PromptToReturnToMainMenu();
                    break;

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

                    PromptToReturnToMainMenu();
                    break;

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

                    PromptToReturnToMainMenu();
                    break;

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

                    PromptToReturnToMainMenu();
                    break;

                case "8":     // 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 != "8");
        }