/// <summary>
        /// Finds the intersection of two binary trees
        /// </summary>
        /// <param name="t1">Binary tree 1</param>
        /// <param name="t2">Binary tree 2</param>
        /// <returns>A list of values representing the intersection of the two trees</returns>
        public static List <int> TreeIntersection(BinaryTree t1, BinaryTree t2)
        {
            //Create containers
            List <int> intersection = new List <int>();
            HashTable  tree1Values  = new HashTable(10);

            //Put values from tree 1 into HashTable
            Tree.Classes.Node currentNode      = t1.Root;
            List <int>        valuesInPreorder = new List <int>();

            t1.PreOrder(t1.Root, valuesInPreorder);
            foreach (int num in valuesInPreorder)
            {
                tree1Values.Add(num.ToString(), true);
            }

            //Compare values in tree 2
            valuesInPreorder.Clear();
            t2.PreOrder(t2.Root, valuesInPreorder);
            foreach (int num in valuesInPreorder)
            {
                if (tree1Values.Contains(num.ToString()))
                {
                    intersection.Add(num);
                }
            }
            return(intersection);
        }
        public void NullTest()
        {
            Tree.Classes.Node TreeRoot = null;

            int value = FindMaxValue(TreeRoot);

            Assert.Equal(0, value);
        }
Exemple #3
0
        /// <summary>
        /// Find common node values using a stack
        /// </summary>
        /// <param name="rootA"></param>
        /// <param name="rootB"></param>
        /// <returns></returns>
        public static List <int> Intersection(Tree.Classes.Node rootA, Tree.Classes.Node rootB)
        {
            List <int> list = new List <int>();

            Stack <Tree.Classes.Node> stack1 = new Stack <Tree.Classes.Node>();
            Stack <Tree.Classes.Node> stack2 = new Stack <Tree.Classes.Node>();

            while (true)
            {
                if (rootA != null)
                {
                    stack1.Push(rootA);
                    rootA = rootA.Left;
                }
                if (rootB != null)
                {
                    stack2.Push(rootB);
                    rootB = rootB.Right;
                }

                else if (stack1.Count == 0 && stack2.Count == 0)
                {
                    rootA = stack1.Peek();
                    rootB = stack2.Peek();

                    if (rootA.Value == rootB.Value)
                    {
                        list.Add(rootA.Value);
                        stack1.Pop();
                        stack2.Pop();

                        rootA = rootA.Right;
                        rootB = rootB.Right;
                    }

                    else if (rootA.Value < rootB.Value)
                    {
                        stack1.Pop();
                        rootA = rootA.Right;

                        rootB = null;
                    }

                    else if (rootA.Value > rootB.Value)
                    {
                        stack2.Pop();
                        rootB = rootB.Right;
                        rootA = null;
                    }
                }
                return(list);
            }
        }
Exemple #4
0
        public void BreadthTraversalOrderRightTest(int value, int indices)
        {
            Tree.Classes.Node TreeRoot = new Tree.Classes.Node(2);
            TreeRoot.Right                               = new Tree.Classes.Node(3);
            TreeRoot.Right.Right                         = new Tree.Classes.Node(4);
            TreeRoot.Right.Right.Right                   = new Tree.Classes.Node(5);
            TreeRoot.Right.Right.Right.Right             = new Tree.Classes.Node(6);
            TreeRoot.Right.Right.Right.Right.Right       = new Tree.Classes.Node(7);
            TreeRoot.Right.Right.Right.Right.Right.Right = new Tree.Classes.Node(8);

            List <int> OutputList = BreadthFirst(TreeRoot);

            Assert.Equal(value, OutputList[indices]);
        }
        public void MaxNegativeValueTest()
        {
            Tree.Classes.Node TreeRoot = new Tree.Classes.Node(-55);
            TreeRoot.Left        = new Tree.Classes.Node(-56);
            TreeRoot.Right       = new Tree.Classes.Node(-57);
            TreeRoot.Left.Left   = new Tree.Classes.Node(-48);
            TreeRoot.Left.Right  = new Tree.Classes.Node(-59);
            TreeRoot.Right.Left  = new Tree.Classes.Node(-60);
            TreeRoot.Right.Right = new Tree.Classes.Node(-61);

            int value = FindMaxValue(TreeRoot);

            Assert.Equal(-48, value);
        }
        public void MaxValueTest()
        {
            Tree.Classes.Node TreeRoot = new Tree.Classes.Node(55);
            TreeRoot.Left        = new Tree.Classes.Node(56);
            TreeRoot.Right       = new Tree.Classes.Node(57);
            TreeRoot.Left.Left   = new Tree.Classes.Node(58);
            TreeRoot.Left.Right  = new Tree.Classes.Node(59);
            TreeRoot.Right.Left  = new Tree.Classes.Node(60);
            TreeRoot.Right.Right = new Tree.Classes.Node(61);

            int value = FindMaxValue(TreeRoot);

            Assert.Equal(61, value);
        }
Exemple #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Tree.Classes.Node TreeRoot = new Tree.Classes.Node(55);
            TreeRoot.Left        = new Tree.Classes.Node(56);
            TreeRoot.Right       = new Tree.Classes.Node(57);
            TreeRoot.Left.Left   = new Tree.Classes.Node(58);
            TreeRoot.Left.Right  = new Tree.Classes.Node(59);
            TreeRoot.Right.Left  = new Tree.Classes.Node(60);
            TreeRoot.Right.Right = new Tree.Classes.Node(61);

            int value = FindMaxValue(TreeRoot);

            Console.WriteLine(value);

            Console.ReadKey();
        }
        /// <summary>
        /// implements the first breadth traversal method using a custom defined queue
        /// </summary>
        /// <param name="Root">root of tree</param>
        /// <param name="Output">lists</param>
        /// <returns>list of breadth first values</returns>
        public static List <int> BreadthFirst(Tree.Classes.Node Root, List <int> Output)
        {
            Queue BreadthPrint = new Queue(new StackAndQueue.Classes.Node(Root));

            while (BreadthPrint.Front != null)
            {
                Tree.Classes.Node Current = (Tree.Classes.Node)BreadthPrint.Dequeue().Data;
                Console.Write($"{Current.Data} ");
                Output.Add(Current.Data);
                if (Current.Left != null)
                {
                    BreadthPrint.Enqueue(new StackAndQueue.Classes.Node(Current.Left));
                }
                if (Current.Right != null)
                {
                    BreadthPrint.Enqueue(new StackAndQueue.Classes.Node(Current.Right));
                }
            }
            return(Output);
        }
Exemple #9
0
        /// <summary>
        /// Output using a list
        /// </summary>
        /// <param name="tree1"></param>
        /// <param name="tree2"></param>
        /// <returns></returns>
        public static List <int> ListIntersectionBinaryTrees(BinaryTree tree1, BinaryTree tree2)
        {
            Tree.Classes.Node curentNode = tree1.root;
            //create a list
            List <int> preOrderVals1 = new List <int>();
            //create a hashtable
            HashTable treeHashTable = new HashTable(50);

            //preorder traversal
            tree1.PreOrder(tree1.root, preOrderVals1);
            //traverse through the list
            foreach (int num in preOrderVals1)
            {
                treeHashTable.Add(num.ToString(), true);
            }
            //create another list

            List <int> preOrderVals2 = new List <int>();

            tree2.PreOrder(tree2.root, preOrderVals2);
            //create a list that holds common values
            List <int> commonValues = new List <int>();

            //push the values of second list to hash table
            //check for common words
            foreach (int num in preOrderVals2)
            {
                if (treeHashTable.Contains(num.ToString()))
                {
                    commonValues.Add(num);
                }
                else
                {
                    //add the common values to a new list
                    treeHashTable.Add(num.ToString(), true);
                }
            }

            return(commonValues);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            Tree.Classes.Node TreeRoot = new Tree.Classes.Node(2);
            TreeRoot.Left        = new Tree.Classes.Node(3);
            TreeRoot.Right       = new Tree.Classes.Node(4);
            TreeRoot.Left.Left   = new Tree.Classes.Node(5);
            TreeRoot.Left.Right  = new Tree.Classes.Node(6);
            TreeRoot.Right.Left  = new Tree.Classes.Node(7);
            TreeRoot.Right.Right = new Tree.Classes.Node(8);

            List <int> OutputList = BreadthFirst(TreeRoot);

            Console.WriteLine();
            foreach (var item in OutputList)
            {
                Console.Write($"{item} ");
            }

            Console.ReadKey();
        }
        /// <summary>
        /// method made to create a list to store values in for testing
        /// </summary>
        /// <param name="Root">root of tree</param>
        /// <returns>list of breadth first traversal</returns>
        public static List <int> BreadthFirst(Tree.Classes.Node Root)
        {
            List <int> Output = new List <int>();

            return(BreadthFirst(Root, Output));
        }