public static void Main() { var myhashtable = new MyHashTable<int, string>(); myhashtable.Add(3, "ar"); myhashtable[2] = "asd"; var indexCheck = myhashtable[2]; Console.WriteLine("toString:"); Console.WriteLine(myhashtable); Console.WriteLine("indexer:"); Console.WriteLine(myhashtable[2]); Console.WriteLine(indexCheck); Console.WriteLine("keys:"); var keysChecker = myhashtable.Keys; foreach (var key in keysChecker) { Console.WriteLine(key); } Console.WriteLine("count:"); Console.WriteLine(myhashtable.Count); Console.WriteLine("remove:"); myhashtable.Remove(4); Console.WriteLine(myhashtable[2]); myhashtable.Remove(2); Console.WriteLine(myhashtable[2]); Console.WriteLine("count:"); Console.WriteLine(myhashtable.Count); string res; var findChecker = myhashtable.Find(3, out res); Console.WriteLine("Find value by key 3:"); Console.WriteLine(res); Console.WriteLine(findChecker); Console.WriteLine(myhashtable); Console.WriteLine("clear"); myhashtable.Clear(); Console.WriteLine(myhashtable); Console.WriteLine("----"); Console.WriteLine("resize"); for (int i = 0; i < 100; i++) { myhashtable.Add(i, i.ToString()); } Console.WriteLine(myhashtable); }
public void TestRemove() { var hashTable = new MyHashTable<string, int> { { "five", 5 }, { "six", 6 } }; hashTable.Remove("five"); hashTable.Remove("six"); Assert.AreEqual(0, hashTable.Count); }
/// <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 CanRemoveValueFromHashTableWithCollisions() { MyHashTable hashTable = new MyHashTable(); hashTable.Add("test", "testOne"); hashTable.Add("test", "testTwo"); hashTable.Remove("test", "testOne"); Assert.False(hashTable.ContainsUnique("test", "testOne")); }
public void RemoveSmallTest() { MyHashTable <string, int> ht = new MyHashTable <string, int>(5); string key = "ABC"; ht[key] = 22; bool expected = false; ht.Remove(key); bool actual = ht.Include(key); Assert.AreEqual(expected, actual); }
public void RemoveBigTest() { MyHashTable <string, int> ht = new MyHashTable <string, int>(2); bool expected = false; for (int i = 0; i < 20; i++) { ht[i.ToString()] = i; } ht.Remove("2"); bool actual = ht.Include("2"); Assert.AreEqual(expected, actual); }
private static void Main() { var hashTable = new MyHashTable<string, int>(); hashTable.Add("five", 5); hashTable.Add("six", 6); Console.WriteLine(hashTable.Find("five")); // throws exception //Console.WriteLine(hashTable.Find("seven")); //test auto grow for (int i = 0; i < 16; i++) { hashTable.Add(i.ToString(), i); Console.WriteLine(hashTable.Count); } Console.WriteLine(hashTable.Find("five")); Console.WriteLine(hashTable.Find("9")); Console.WriteLine(hashTable.Count); hashTable.Remove("9"); Console.WriteLine(hashTable.Count); Console.WriteLine("test indexator"); Console.WriteLine(hashTable["10"]); hashTable["10"]++; Console.WriteLine(hashTable["10"]); // throws exception //Console.WriteLine(hashTable.Find("9")); Console.WriteLine("Test HashTable.Keys enumerator:"); foreach (var key in hashTable.Keys) { Console.WriteLine(key); } Console.WriteLine("Test HashTable enumerator:"); foreach (var pair in hashTable) { Console.WriteLine("{0} -> {1}", pair.Key, pair.Value); } }
public void TestRemove_Fail() { MyHashTable <string, int> hashTable = new MyHashTable <string, int>(); hashTable.Add("asd", 10); int count = 1000; for (int i = 0; i < count; i++) { hashTable.Add(i.ToString(), i); } Assert.AreEqual(1001, hashTable.Count); bool removed = hashTable.Remove("rty"); Assert.AreEqual(1001, hashTable.Count); Assert.AreEqual(true, hashTable.ContainsKey("asd")); }
public void Should_Check_Remove() { //arrange var key = "key"; var value = "value"; var map = new MyHashTable <string, string>(); map.Add(key, value); map.Add(key + "1", value + "1"); //act map.Remove(key); var keyResult = map.ContainsKey(key); var valueResult = map.ContainsValue(value); var count = map.Count; //assert keyResult.ShouldBeEquivalentTo(false); valueResult.ShouldBeEquivalentTo(false); count.ShouldBeEquivalentTo(1); }
public static void Main() { var table = new MyHashTable<string, string>(); table.Add("first", "Pesho"); table.Add("second", "Gosho"); table.Add("third", "Tosho"); table.Add("fourth", "Ivan"); Console.WriteLine(table.Count); foreach (var pair in table) { Console.WriteLine("{0} -> {1}", pair.Key, pair.Value); } string first; table.Find("first", out first); Console.WriteLine(first); table.Remove("second"); Console.WriteLine(table.Count); foreach (var pair in table) { Console.WriteLine("{0} -> {1}", pair.Key, pair.Value); } Console.WriteLine(table["fourth"]); string[] keys = table.Keys; foreach (var key in keys) { Console.WriteLine(key); } table.Clear(); Console.WriteLine(table.Count); }
public void TestRemoveShouldThrowException() { var hashTable = new MyHashTable<string, int> { { "five", 5 }, { "six", 6 } }; hashTable.Remove("five"); hashTable.Remove("six"); hashTable.Remove("missing"); }
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(); }
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Green; Console.Title = "Хэш таблицы.html"; //создается массив элементов размерности N = 56 int NumberElements = 56; var array = new int[NumberElements]; var rnd = new Random(); //генерируются ключи размерности m = 5 Console.WriteLine("Сгенерированные ключи:"); for (int i = 0; i < array.Length; i++) { array[i] = rnd.Next(10000, 100000); } OutArray(array, 1); int sizeTable = (int)Math.Round(NumberElements * 1.5); var hashTable = new MyHashTable(sizeTable, true); //var hashTable = new MyHashTable(sizeTable); Console.WriteLine(); Console.WriteLine(); //добавляем ключи в хэш таблицу for (int i = 0; i < array.Length; i++) { hashTable.Add(array[i]); } Console.WriteLine(); Console.WriteLine("Коэффициент заполнения таблицы = {0:0.000}", hashTable.CoefOccupancy); Console.WriteLine("Среднее число проб = {0:0.000}", hashTable.AverageCountAttempt); Console.WriteLine(); while (true) { Console.WriteLine("Хэш-таблица:"); hashTable.PrintTable(); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("Интерактивный режим"); Console.WriteLine("1.Добавить ключ"); Console.WriteLine("2.Найти индекс по ключу"); Console.WriteLine("3.Удалить ключ"); Console.WriteLine("4.Заменить один ключ другим"); Console.WriteLine("5.Вывести параметры"); Console.WriteLine("0.Выход"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(); Console.Write("Выбор режима: "); var mode = Console.ReadLine(); int key1, key2; string str; switch (mode[0]) { case '1': Console.WriteLine("1.Свой ключ"); Console.WriteLine("2.Диапазон случайных"); Console.WriteLine(); Console.Write("Выбор режима: "); mode = Console.ReadLine(); switch (mode[0]) { case '1': Console.WriteLine(); Console.Write("Введите ключ = "); str = Console.ReadLine(); if (!int.TryParse(str, out key1)) { break; } if (hashTable.Add(key1)) { NumberElements++; } Console.WriteLine("Нажмите любую клавишу, чтобы продолжить"); Console.ReadKey(); break; case '2': Console.WriteLine(); Console.Write("Сколько ключей = "); str = Console.ReadLine(); if (!int.TryParse(str, out key1) || key1 < 0) { break; } var rand = new Random(); for (int i = 0; i < key1; i++) { if (hashTable.Add(rand.Next(10000, 99999))) { NumberElements++; } } Console.WriteLine("Нажмите любую клавишу, чтобы продолжить"); Console.ReadKey(); break; } break; case '2': Console.WriteLine(); Console.Write("Введите ключ = "); str = Console.ReadLine(); if (!int.TryParse(str, out key1)) { break; } hashTable.Find(key1); Console.WriteLine("Нажмите любую клавишу, чтобы продолжить"); Console.ReadKey(); break; case '3': Console.Write("Введите ключ = "); str = Console.ReadLine(); if (!int.TryParse(str, out key1)) { break; } if (hashTable.Remove(key1)) { NumberElements--; } Console.WriteLine("Нажмите любую клавишу, чтобы продолжить"); Console.ReadKey(); break; case '4': Console.WriteLine(); Console.Write("Введите ключ для замены = "); str = Console.ReadLine(); if (!int.TryParse(str, out key1)) { break; } Console.Write("Введите ключ чем заменить = "); str = Console.ReadLine(); if (!int.TryParse(str, out key2)) { break; } hashTable.Replace(key1, key2); Console.WriteLine("Нажмите любую клавишу, чтобы продолжить"); Console.ReadKey(); break; case '5': Console.WriteLine(); Console.WriteLine("Коэффициент заполнения таблицы = {0:0.000}", hashTable.CoefOccupancy); Console.WriteLine("Среднее число проб = {0:0.000}", hashTable.AverageCountAttempt); Console.WriteLine("Нажмите любую клавишу, чтобы продолжить"); Console.ReadKey(); break; case '0': return; default: break; } } }