/// <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);
        }
        /// <summary>
        /// Searches through both given trees to check for common values
        /// </summary>
        /// <param name="BST1">binary search tree</param>
        /// <param name="nodeA">node</param>
        /// <param name="BST2">binary search tree</param>
        /// <param name="nodeG">node</param>
        /// <returns>hashtable with common values</returns>
        public HashTable TreeIntersection(BinarySearchTree BST1, Node nodeA, BinarySearchTree BST2, Node nodeG)
        {
            //put both trees into hashtables
            HashTable ht = new HashTable();

            ht = BST1.BreadthFirst(nodeA);

            HashTable ht2 = new HashTable();

            ht2 = BST2.BreadthFirst(nodeG);

            HashTable ht3 = new HashTable();

            for (int i = 0; i < ht.HashArray.Length; i++)
            {
                if (ht.HashArray[i] != null)
                {
                    if (ht2.Contains(ht.HashArray[i].Key))
                    {
                        ht3.Add(ht.HashArray[i].Key, 1);
                        Console.WriteLine($"found a match: {ht.HashArray[i].Key}");
                    }
                }
            }
            return(ht3);
        }
        public static List <int> TreeIntersection(BinaryTree <int> tree1, BinaryTree <int> tree2)
        {
            HashTable  table      = new HashTable(50);
            List <int> listTree1  = new List <int>();
            List <int> listTree2  = new List <int>();
            List <int> returnList = new List <int>();

            listTree1 = tree1.PreOrder(tree1.Root, listTree1);
            foreach (int val in listTree1)
            {
                string stringifiedVal = val.ToString();
                table.Add(stringifiedVal, stringifiedVal);
            }

            listTree2 = tree2.PreOrder(tree1.Root, listTree2);
            foreach (int val in listTree2)
            {
                string stringifiedVal = val.ToString();
                if (table.Contains(stringifiedVal))
                {
                    returnList.Add(val);
                }
            }
            return(returnList);
        }
示例#4
0
        /// <summary>
        /// Trees intersection method.
        /// </summary>
        /// <param name="BT1">The first Binary Tree.</param>
        /// <param name="BT2">The second Binary Tree.</param>
        /// <returns>Returns a list of shared values.</returns>
        public static List <string> TreeIntersectionMethod(BinaryTree BT1, BinaryTree BT2)
        {
            BT1.nodes = new List <Node>();
            BT2.nodes = new List <Node>();
            BT1.nodes = BT1.InOrder(BT1.Root);
            BT2.nodes = BT2.InOrder(BT2.Root);
            HashTable HT = new HashTable();

            foreach (Node node in BT1.nodes)
            {
                HT.Add(node.Value.ToString(), null);
            }
            List <string> matches = new List <string>();

            foreach (Node node in BT2.nodes)
            {
                if (HT.Contains(node.Value.ToString()))
                {
                    matches.Add(node.Value.ToString());
                }
            }
            return(matches);
        }
示例#5
0
        public static List <string> IntersectedTrees(BinaryTree binaryTree, BinaryTree binaryTree2)
        {
            List <string> commonValues = new List <string>();
            HashTable     hashTable    = new HashTable();

            List <int> tree1 = binaryTree.PreOrder(binaryTree.Root);
            List <int> tree2 = binaryTree2.PreOrder(binaryTree2.Root);

            foreach (int value in tree1)
            {
                Node node = new Node(value);
                hashTable.Add(node.Value.ToString(), null);
            }

            foreach (int value in tree2)
            {
                if (hashTable.Contains(value.ToString()))
                {
                    commonValues.Add(value.ToString());
                }
            }
            return(commonValues);
        }