Exemple #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Node n1 = new Node(1);
            Node n2 = new Node(2);
            Node n3 = new Node(3);
            Node n4 = new Node(4);
            Node n5 = new Node(5);
            Node n6 = new Node(6);

            BinaryTree.Classes.BinaryTree tree = new BinaryTree.Classes.BinaryTree(n1);
            tree.Root.LChild               = n2;
            tree.Root.RChild               = n3;
            tree.Root.LChild.LChild        = n4;
            tree.Root.RChild.RChild        = n5;
            tree.Root.RChild.RChild.LChild = n6;
            Console.WriteLine($"hereee {n2.LChild.Value}");
            List <Node> list = tree.PreOrder(n1);

            foreach (var item in list)
            {
                Console.WriteLine($"foreach {item.Value}");
            }
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine($"{list[i].Value}");
            }
        }
        static void Main(string[] args)
        {
            Node n1 = new Node(1);
            Node n2 = new Node(2);
            Node n3 = new Node(3);
            Node n4 = new Node(4);
            Node n5 = new Node(5);

            n1.LChild = n2;
            n1.RChild = n3;
            n2.LChild = n4;
            n3.RChild = n5;
            BinaryTree.Classes.BinaryTree t1 = new BinaryTree.Classes.BinaryTree(n1);

            Node n4_2 = new Node(4);
            Node n5_2 = new Node(5);
            Node n6   = new Node(6);
            Node n7   = new Node(7);
            Node n8   = new Node(8);

            n4_2.LChild = n5_2;
            n4_2.RChild = n6;
            n5_2.LChild = n7;
            n6.RChild   = n8;
            BinaryTree.Classes.BinaryTree t2 = new BinaryTree.Classes.BinaryTree(n4_2);

            List <int> commonNums = TreeIntersections(t1, t2);

            foreach (int num in commonNums)
            {
                Console.WriteLine($"{num}");
            }
        }
        public void PostOrderTest()
        {
            BinaryTree.Classes.BinaryTree tree = new BinaryTree.Classes.BinaryTree(n1);
            tree.Root.LChild               = n2;
            tree.Root.RChild               = n3;
            tree.Root.LChild.LChild        = n4;
            tree.Root.RChild.RChild        = n5;
            tree.Root.RChild.RChild.LChild = n6;

            List <Node> list = tree.PostOrder(n1);

            Assert.True(list[0].Value == 1);
        }
        public static int FindMaxValue(BinaryTree.Classes.BinaryTree tree)
        {
            int temp = 0;

            List <Node> list = tree.PreOrder(tree.Root);

            foreach (var item in list)
            {
                if (item.Value > temp)
                {
                    temp = item.Value;
                }
            }
            Console.WriteLine($"The max Value in this tree is {temp}");
            return(temp);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Node n1 = new Node(1);
            Node n2 = new Node(2);
            Node n3 = new Node(3);
            Node n4 = new Node(4);
            Node n5 = new Node(5);
            Node n6 = new Node(6);

            BinaryTree.Classes.BinaryTree tree = new BinaryTree.Classes.BinaryTree(n1);
            tree.Root.LChild               = n2;
            tree.Root.RChild               = n3;
            tree.Root.LChild.LChild        = n4;
            tree.Root.RChild.RChild        = n5;
            tree.Root.RChild.RChild.LChild = n6;

            FindMaxValue(tree);
        }
        public void CorrectMaxValIsReturnedTest(int highNode, int expected)
        {
            Node n1 = new Node(1);
            Node n2 = new Node(2);
            Node n3 = new Node(3);
            Node n4 = new Node(4);
            Node n5 = new Node(5);
            Node n6 = new Node(6);

            BinaryTree.Classes.BinaryTree tree = new BinaryTree.Classes.BinaryTree(n1);
            tree.Root.LChild               = n2;
            tree.Root.RChild               = n3;
            tree.Root.LChild.LChild        = n4;
            tree.Root.RChild.RChild        = n5;
            tree.Root.RChild.RChild.LChild = n6;
            tree.Root.RChild.RChild.RChild = new Node(highNode);
            List <Node> list = tree.PreOrder(tree.Root);

            Assert.Equal(FindMaxValue(tree), expected);
        }
        /// <summary>
        /// Returns list of common values in two Binary Trees
        /// </summary>
        /// <param name="uno">binary tree 1</param>
        /// <param name="dos">binary tree 2</param>
        /// <returns>list of common vals</returns>
        static public List <int> TreeIntersections(BinaryTree.Classes.BinaryTree uno, BinaryTree.Classes.BinaryTree dos)
        {
            List <int> commonVals = new List <int>();
            Hashtable  table      = new Hashtable();

            foreach (Node node in uno.PreOrder(uno.Root))
            {
                table.Add(node.Value.ToString(), node.Value);
            }

            foreach (Node node in dos.PreOrder(dos.Root))
            {
                if (table.Contains(node.Value.ToString()))
                {
                    commonVals.Add(node.Value);
                }
            }

            return(commonVals);
        }
        public static void FizzBuzzTree(BinaryTree.Classes.BinaryTree tree, Node node)
        {
            List <Node> list = tree.PreOrder(node);

            foreach (var item in list)
            {
                if (item.Value % 15 == 0)
                {
                    Console.WriteLine($"{item.Value}: Fizzbuzz");
                }
                else if (item.Value % 3 == 0)
                {
                    Console.WriteLine($"{item.Value}: Fizz");
                }
                else if (item.Value % 5 == 0)
                {
                    Console.WriteLine($"{item.Value}: Buzz");
                }
                else
                {
                    Console.WriteLine($"{item.Value}");
                }
            }
        }
Exemple #9
0
        public void CorrectDuplicateNumbersAreReturnedTest()
        {
            Node n1 = new Node(1);
            Node n2 = new Node(2);
            Node n3 = new Node(3);
            Node n4 = new Node(4);
            Node n5 = new Node(5);
            Node n6 = new Node(6);

            n1.LChild = n2;
            n1.RChild = n3;
            n2.LChild = n4;
            n3.RChild = n5;
            n5.LChild = n6;
            BinaryTree.Classes.BinaryTree t1 = new BinaryTree.Classes.BinaryTree(n1);

            Node n4_2 = new Node(4);
            Node n5_2 = new Node(5);
            Node n6_2 = new Node(6);
            Node n7   = new Node(7);
            Node n8   = new Node(8);
            Node n9   = new Node(9);

            n4_2.LChild = n5_2;
            n4_2.RChild = n6;
            n5_2.LChild = n7;
            n6.RChild   = n8;
            n8.LChild   = n9;
            BinaryTree.Classes.BinaryTree t2 = new BinaryTree.Classes.BinaryTree(n4_2);

            List <int> commonNums = TreeIntersections(t1, t2);

            Assert.True(TreeIntersections(t1, t2)[0] == 4);
            Assert.True(TreeIntersections(t1, t2)[1] == 5);
            Assert.True(TreeIntersections(t1, t2)[2] == 6);
        }