public void AddItem() { var set = new MyHashTable <string, int>(); set.Add("Ricardo", 30); set.Add("Hadassa", 29); Assert.IsTrue(set.Contains("Ricardo")); Assert.IsTrue(set.Contains("Hadassa")); Assert.IsFalse(set.Contains("Pedro")); Assert.AreEqual(30, set["Ricardo"]); Assert.AreEqual(29, set["Hadassa"]); }
/// <summary> /// Takes in two Hash Tables, and performs a Left Join on them. All values from the first table are added to a list, followed by any values in the second table with a matching key. If table two does not have a matching key, null is added after the first value. The key and values are added in the following order: [key, value1, value2]. /// </summary> /// <param name="tableOne">First Hash Table to join.</param> /// <param name="tableTwo">Second Hash Table to join.</param> /// <returns>List of keys and values.</returns> public static List <string> LeftJoin(MyHashTable tableOne, MyHashTable tableTwo) { List <string> resultList = new List <string>(); if (tableOne == null) { return(null); } foreach (var item in tableOne.Map) { if (item != null) { resultList.Add(item.First.Value.Key); resultList.Add(item.First.Value.Value); if (tableTwo.Contains(item.First.Value.Key)) { MyNode node = tableTwo.Get(item.First.Value.Key); resultList.Add(node.Value); } else { resultList.Add(null); } } } return(resultList); }
/// <summary> /// Takes in two Binary Search Trees and uses a HashTable to find any values that are shared between the trees. Returns a List of all common values. If no values are shared returns null. /// </summary> /// <param name="treeOne">First Tree to compare against.</param> /// <param name="treeTwo">Second Tree to compare against.</param> /// <returns>List of shared values, or null if no values shared.</returns> public static List <int> TreeIntersection(MyBinarySearchTree treeOne, MyBinarySearchTree treeTwo) { if (treeOne.Root == null || treeTwo.Root == null) { return(null); } MyHashTable hashTable = new MyHashTable(); List <int> returnList = new List <int>(); Queue <Node <int> > queue = new Queue <Node <int> >(); queue.Enqueue(treeOne.Root); while (queue.Count != 0) { Node <int> node = queue.Dequeue(); if (node.LChild != null) { queue.Enqueue(node.LChild); } if (node.RChild != null) { queue.Enqueue(node.RChild); } hashTable.Add(node.Value.ToString(), ""); } queue.Enqueue(treeTwo.Root); while (queue.Count != 0) { Node <int> node = queue.Dequeue(); if (node.LChild != null) { queue.Enqueue(node.LChild); } if (node.RChild != null) { queue.Enqueue(node.RChild); } if (hashTable.Contains(node.Value.ToString())) { returnList.Add(node.Value); hashTable.Remove(node.Value.ToString(), ""); } } if (returnList.Count > 0) { return(returnList); } return(null); }
public void CanRemoveValueFromHashTable() { MyHashTable hashTable = new MyHashTable(); hashTable.Add("test", "test"); hashTable.Remove("test", "test"); Assert.False(hashTable.Contains("test")); }
public void CanContainValue() { MyHashTable hash = new MyHashTable(); hash.Add(5, "naming"); hash.Add(7, "conventions"); hash.Add(8, "always"); hash.Add(9, "destroy"); hash.Add(21, "time"); Assert.True(hash.Contains("always")); }
public void Contains_Returns_True_Test() { //arrange MyHashTable <int> testHT = new MyHashTable <int>(1024); testHT.Add("Diana", 678); //act bool actual = testHT.Contains("Diana"); //assert Assert.True(actual); }
public static string FindRepeatedWord(string input) { MyHashTable <string> wordSearch = new MyHashTable <string>(1024); var words = input.Split(' '); foreach (var word in words) { bool toggle = wordSearch.Contains(word.ToLower()); if (toggle) { return(word); } else { wordSearch.Add(word.ToLower(), word); } } return(null); }
/// <summary> /// Takes in a long string and uses a HashTable to determine the first repeated word in the string. If no repeated words are found returns null. /// </summary> /// <param name="longString">String to parse.</param> /// <returns>First repeated word in longString, or null if none found.</returns> public static string RepeatedWord(string longString) { string[] words = longString.Split(" "); if (words.Length > 1) { MyHashTable hashTable = new MyHashTable(); for (int i = 0; i < words.Length; i++) { if (hashTable.Contains(words[i].ToLower())) { return(words[i]); } hashTable.Add(words[i].ToLower(), ""); } } return(null); }
public static HashSet <string[]> LeftJoin(MyHashTable <string> left, MyHashTable <string> right) { HashSet <string[]> leftHash = new HashSet <string[]>(); HashSet <string[]> result = new HashSet <string[]>(); foreach (var word in left) { leftHash.Add(word.Split(',')); } foreach (var entry in leftHash) { if (right.Contains(entry[0])) { result.Add(new string[] { entry[0], entry[1], right.Get(entry[0]) }); } else { result.Add(new string[] { entry[0], entry[1], "NULL" }); } } return(result); }
public IEnumerable <Vertex <T> > DepthFirst() { MyStack <Vertex <T> > graphStack = new MyStack <Vertex <T> >(); MyHashTable <string> visited = new MyHashTable <string>(1024); List <Vertex <T> > results = new List <Vertex <T> >(); graphStack.push(Vertices[0]); while (!graphStack.isEmpty()) { var current = graphStack.pop(); visited.Add(current.Value.ToString(), "visited"); foreach (var edge in current.Neighbors) { if (!visited.Contains(edge.Neighbor.Value.ToString())) { graphStack.push(edge.Neighbor); } //results.Add(current); } yield return(current); } //return results; }
static void Main() { MyHashTable <string, string> hashtable = new MyHashTable <string, string>(); //Add functions //Contains and ContainsKey functions used for asserting hashtable.Add("Mark", "Football player"); hashtable.Add(new KeyValuePair <string, string>("Jerry", "Engineer")); hashtable.Add("Tina", "Ballerina"); hashtable.Add(new KeyValuePair <string, string>("Susan", "Architect")); Debug.Assert(hashtable.Count == 4); Debug.Assert(hashtable.Contains(new KeyValuePair <string, string>("Jerry", "Engineer")) == true); Debug.Assert(hashtable.ContainsKey("Tina") == true); //Prints hashtable Console.WriteLine("Table: "); foreach (KeyValuePair <string, string> pair in hashtable) { Console.WriteLine(pair.Key + " - " + pair.Value); } Console.WriteLine(); Console.WriteLine("Keys only: "); foreach (string key in hashtable.Keys) { Console.WriteLine(key); } Console.WriteLine(); Console.WriteLine("Value only: "); foreach (string value in hashtable.Values) { Console.WriteLine(value); } //Remove functions hashtable.Remove("Mark"); hashtable.Remove(new KeyValuePair <string, string> ("Tina", "Ballerina")); Debug.Assert(hashtable.Count == 2); Debug.Assert(hashtable.ContainsKey("Tina") == false); Debug.Assert(hashtable.Contains(new KeyValuePair <string, string>("Mark", "Football player")) == false); Console.WriteLine(); Console.WriteLine("Table with Mark and Tina using both Remove methods: "); foreach (KeyValuePair <string, string> pair in hashtable) { Console.WriteLine(pair.Key + " - " + pair.Value); } //Try Get Value function Console.WriteLine(); string gotValue = ""; if (hashtable.TryGetValue("Jerry", out gotValue)) { Console.WriteLine("Value out of key 'Jerry' using TryGetValue: "); Console.WriteLine(gotValue); } Debug.Assert(gotValue == "Engineer"); //Clear function Console.WriteLine(); Console.WriteLine("Table cleared: "); hashtable.Clear(); foreach (KeyValuePair <string, string> pair in hashtable) { Console.WriteLine(pair.Key + " - " + pair.Value); } Debug.Assert(hashtable.Count == 0); Console.WriteLine(); }