public static List <Object> TreeIntersection(BinaryTree treeA, BinaryTree treeB) { // build structures List <object> list = new List <object>(); Queue queue = new Queue(); Hashmap map = new Hashmap(1024); Object temp = null; // traverse treeA queue.Enqueue(treeA.Root); while (queue.Front != null) { if (queue.Front.Left != null) { queue.Enqueue(queue.Front.Left); } if (queue.Front.Right != null) { queue.Enqueue(queue.Front.Right); } temp = queue.Front.Value.ToString(); if (!map.Contains(temp, temp)) { map.Add(temp, temp); } queue.Dequeue(); } // traverse treeB queue.Enqueue(treeB.Root); while (queue.Front != null) { if (queue.Front.Left != null) { queue.Enqueue(queue.Front.Left); } if (queue.Front.Right != null) { queue.Enqueue(queue.Front.Right); } temp = queue.Front.Value.ToString(); if (map.Contains(temp, temp)) { //map.Remove(temp, temp); list.Add(temp); } queue.Dequeue(); } return(list); }
/// <summary> /// adds each word to hashmap if not already present; returns first duplicate /// </summary> /// <param name="line"> string to search for duplicate </param> /// <returns> first duplicate found, or 'no repeats found' if none </returns> public static string RepeatedWord(string line) { string[] words = line.Split(new char[] { ' ', ',', '.', ':', ';', '?', '!', '*', '<', '>', '(', ')', '{', '}', '[', ']' }); int idx = 0; Hashmap map = new Hashmap(1024); foreach (var word in words) { if (word != "") { string wordLower = word.ToLower(); idx = map.Hash(wordLower); if (map.Contains(wordLower, 0)) { return(wordLower); } else { map.Add(wordLower, 0); } } } return("no repeats found"); }