Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            //instantiate binary search tree 1
            BinarySearchTree BST1 = new BinarySearchTree();
            //declare nodes to add to BST1
            Node nodeA = new Node(100);
            Node nodeB = new Node(50);
            Node nodeC = new Node(150);
            Node nodeD = new Node(75);
            Node nodeE = new Node(300);
            Node nodeF = new Node(25);

            BST1.Add(nodeA, nodeB);
            BST1.Add(nodeA, nodeC);
            BST1.Add(nodeA, nodeD);
            BST1.Add(nodeA, nodeE);
            BST1.Add(nodeA, nodeF);

            //instantiate binary search tree 2
            BinarySearchTree BST2 = new BinarySearchTree();
            //declare nodes to add to BST2
            Node nodeG = new Node(100);
            Node nodeH = new Node(50);
            Node nodeI = new Node(150);
            Node nodeJ = new Node(175);
            Node nodeK = new Node(400);
            Node nodeL = new Node(205);

            BST2.Add(nodeG, nodeH);
            BST2.Add(nodeG, nodeI);
            BST2.Add(nodeG, nodeJ);
            BST2.Add(nodeG, nodeK);
            BST2.Add(nodeG, nodeL);

            HashTable ht = new HashTable();

            //nodes need to be passed in because Breadth First Searches
            //need a node to start searching from
            ht = ht.TreeIntersection(BST1, nodeA, BST2, nodeG);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }