public void EmptyTest() { var map = new RedBlackTreeDictionary <int, string>(); bool thrown; thrown = false; try { var c = map[1]; } catch (KeyNotFoundException) { thrown = true; } Assert.IsTrue(thrown); thrown = false; try { var min = map.Min(); } catch (InvalidOperationException) { thrown = true; } Assert.IsTrue(thrown); thrown = false; try { var max = map.Max(); } catch (InvalidOperationException) { thrown = true; } Assert.IsTrue(thrown); thrown = false; try { var next = map.GetNext(0); } catch (KeyNotFoundException) { thrown = true; } Assert.IsTrue(thrown); thrown = false; try { var prev = map.GetPrevious(0); } catch (KeyNotFoundException) { thrown = true; } Assert.IsTrue(thrown); }
public void EmptyTest() { var map = new RedBlackTreeDictionary<int, string>(); bool thrown; thrown = false; try { var c = map[1]; } catch (KeyNotFoundException) { thrown = true; } Assert.IsTrue(thrown); thrown = false; try { var min = map.Min();} catch (InvalidOperationException) { thrown = true; } Assert.IsTrue(thrown); thrown = false; try { var max = map.Max();} catch (InvalidOperationException) { thrown = true; } Assert.IsTrue(thrown); thrown = false; try { var next = map.GetNext(0);} catch (KeyNotFoundException) { thrown = true; } Assert.IsTrue(thrown); thrown = false; try { var prev = map.GetPrevious(0); } catch (KeyNotFoundException) { thrown = true; } Assert.IsTrue(thrown); }
public void DuplicateTest() { var map = new RedBlackTreeDictionary <int, string>(); map[1] = "1"; Assert.AreEqual("1", map[1]); map[2] = "2"; Assert.AreEqual("2", map[2]); map[1] = "3"; Assert.AreEqual("3", map[1]); Assert.AreEqual(2, map.Count); }
public void DuplicateTest() { var map = new RedBlackTreeDictionary<int, string>(); map[1] = "1"; Assert.AreEqual("1", map[1]); map[2] = "2"; Assert.AreEqual("2", map[2]); map[1] = "3"; Assert.AreEqual("3", map[1]); Assert.AreEqual(2, map.Count); }
public void NextPrevTest() { bool thrown = false; var map = new RedBlackTreeDictionary <int, string>(); map[0] = "0"; map[1] = "1"; map[2] = "2"; { var a = map.GetNext(0); Assert.AreEqual(1, a.Key); Assert.IsTrue(map.TryGetNext(0, out a)); Assert.AreEqual(1, a.Key); var b = map.GetNext(1); Assert.AreEqual(2, b.Key); Assert.IsTrue(map.TryGetNext(1, out b)); Assert.AreEqual(2, b.Key); thrown = false; try { map.GetNext(2); } catch (KeyNotFoundException) { thrown = true; } Assert.IsTrue(thrown); Assert.IsFalse(map.TryGetNext(2, out b)); thrown = false; try { map.GetNext(-1); } catch (KeyNotFoundException) { thrown = true; } Assert.IsTrue(thrown); Assert.IsFalse(map.TryGetNext(-1, out b)); } { KeyValuePair <int, string> a; thrown = false; try { a = map.GetPrevious(0); } catch (KeyNotFoundException) { thrown = true; } Assert.IsTrue(thrown); Assert.IsFalse(map.TryGetPrevious(0, out a)); Assert.AreEqual(0, a.Key); var b = map.GetPrevious(1); Assert.AreEqual(0, b.Key); Assert.IsTrue(map.TryGetPrevious(1, out b)); Assert.AreEqual(0, b.Key); var c = map.GetPrevious(2); Assert.AreEqual(1, c.Key); Assert.IsTrue(map.TryGetPrevious(2, out b)); Assert.AreEqual(1, b.Key); thrown = false; try { map.GetNext(3); } catch (KeyNotFoundException) { thrown = true; } Assert.IsTrue(thrown); } }
public void NextPrevTest() { bool thrown = false; var map = new RedBlackTreeDictionary<int, string>(); map[0] = "0"; map[1] = "1"; map[2] = "2"; { var a = map.GetNext(0); Assert.AreEqual(1, a.Key); Assert.IsTrue(map.TryGetNext(0, out a)); Assert.AreEqual(1, a.Key); var b = map.GetNext(1); Assert.AreEqual(2, b.Key); Assert.IsTrue(map.TryGetNext(1, out b)); Assert.AreEqual(2, b.Key); thrown = false; try { map.GetNext(2); } catch (KeyNotFoundException) { thrown = true; } Assert.IsTrue(thrown); Assert.IsFalse(map.TryGetNext(2, out b)); thrown = false; try { map.GetNext(-1); } catch (KeyNotFoundException) { thrown = true; } Assert.IsTrue(thrown); Assert.IsFalse(map.TryGetNext(-1, out b)); } { KeyValuePair<int, string> a; thrown = false; try { a = map.GetPrevious(0); } catch (KeyNotFoundException) { thrown = true; } Assert.IsTrue(thrown); Assert.IsFalse(map.TryGetPrevious(0, out a)); Assert.AreEqual(0, a.Key); var b = map.GetPrevious(1); Assert.AreEqual(0, b.Key); Assert.IsTrue(map.TryGetPrevious(1, out b)); Assert.AreEqual(0, b.Key); var c = map.GetPrevious(2); Assert.AreEqual(1, c.Key); Assert.IsTrue(map.TryGetPrevious(2, out b)); Assert.AreEqual(1, b.Key); thrown = false; try { map.GetNext(3); } catch (KeyNotFoundException) { thrown = true; } Assert.IsTrue(thrown); } }
static void Main(string[] args) { Dictionary <int, int> d1 = new Dictionary <int, int>(); RedBlackTreeDictionary <int, int> rb1 = new RedBlackTreeDictionary <int, int>(); AVLDictionary <int, int> avl1 = new AVLTree.AVLDictionary <int, int>(); var random = new Random(); var InsertionRBT1 = new Stopwatch(); // calculating the running time Console.WriteLine("------------- Case of 320 elements---------------"); InsertionRBT1.Start(); for (int i = 0; i < 320; i++) { rb1.Add(i, random.Next(1000, 2000)); } InsertionRBT1.Stop(); Console.WriteLine("Taken time for inserting in miliseconds for Red-Black tree is {0} ", InsertionRBT1.ElapsedMilliseconds); var InsertionAVL1 = new Stopwatch(); // calculating the running time InsertionAVL1.Start(); for (int j = 0; j < 320; j++) { avl1.Add(j, random.Next(2000, 3000)); } InsertionAVL1.Stop(); Console.WriteLine("Taken time for inserting in milliseconds for AVL tree is {0} ", InsertionAVL1.ElapsedMilliseconds); var InsertionDictionary1 = new Stopwatch(); InsertionDictionary1.Start(); for (int k = 0; k < 320; k++) { d1.Add(k, random.Next(6000, 7000)); } InsertionDictionary1.Stop(); Console.WriteLine("Taken time for inserting in milliseconds for hash-table Dictionary is {0}", InsertionDictionary1.ElapsedMilliseconds); var deletionRBT1 = new Stopwatch(); deletionRBT1.Start(); for (int i = 0; i < 320; i++) { rb1.Remove(i); } deletionRBT1.Stop(); Console.WriteLine("Deletion in red black tree in milliseconds is {0} ", deletionRBT1.ElapsedMilliseconds); var deletionAVL1 = new Stopwatch(); deletionAVL1.Start(); for (int j = 0; j < 320; j++) { avl1.Remove(j); } deletionAVL1.Stop(); Console.WriteLine("Deletion in AVL tree in milliseconds is {0} ", deletionAVL1.ElapsedMilliseconds); var deletionDictionary1 = new Stopwatch(); deletionDictionary1.Start(); for (int k = 0; k < 320; k++) { d1.Remove(k); } deletionDictionary1.Stop(); Console.WriteLine("Deletion in hash-table based dictionary in milliseconds is {0} ", deletionDictionary1.ElapsedMilliseconds); Console.WriteLine("------------ Case of 640 elements------------------"); RedBlackTreeDictionary <int, int> rb2 = new RedBlackTreeDictionary <int, int>(); AVLDictionary <int, int> avl2 = new AVLDictionary <int, int>(); Dictionary <int, int> d2 = new Dictionary <int, int>(); var InsertionRBT2 = new Stopwatch(); InsertionRBT2.Start(); for (int i = 0; i < 640; i++) { rb2.Add(i, random.Next(3000, 5000)); } InsertionRBT2.Stop(); Console.WriteLine("Taken time in Red-Black tree for insertion in milliseconds is {0} ", InsertionRBT2.ElapsedMilliseconds); var InsertionAVL2 = new Stopwatch(); InsertionAVL2.Start(); for (int j = 0; j < 640; j++) { avl2.Add(j, random.Next(5000, 7000)); } InsertionAVL2.Stop(); Console.WriteLine("Taken time in AVL tree for insertion in milliseconds is {0} ", InsertionAVL2.ElapsedMilliseconds); var InsertionDictionary2 = new Stopwatch(); InsertionDictionary2.Start(); for (int k = 0; k < 640; k++) { d2.Add(k, random.Next(4000, 5000)); } InsertionDictionary2.Stop(); Console.WriteLine("Taken time in hash-based dictionary for insertion in millisecons is {0} ", InsertionDictionary2.ElapsedMilliseconds); var DeletionRBT2 = new Stopwatch(); DeletionRBT2.Start(); for (int i = 0; i < 640; i++) { rb2.Remove(i); } DeletionRBT2.Stop(); Console.WriteLine("Taken time in Red-Black tree for deletion in milliseconds is {0} ", DeletionRBT2.ElapsedMilliseconds); var DeletionAVL2 = new Stopwatch(); DeletionAVL2.Start(); for (int j = 0; j < 640; j++) { avl2.Remove(j); } DeletionAVL2.Stop(); Console.WriteLine("Taken time in AVL tree for deletion in milliseconds is {0} ", DeletionAVL2.ElapsedMilliseconds); var DeletionDictionary2 = new Stopwatch(); DeletionDictionary2.Start(); for (int k = 0; k < 640; k++) { d2.Remove(k); } DeletionDictionary2.Stop(); Console.WriteLine("Taken time in hash-table based dictionary for deletion in milliseconds is {0} ", DeletionDictionary2.ElapsedMilliseconds); Console.WriteLine("------------ Case of 1280 elements------------------"); // I know that I could clear my tree instead of creating a new instance, but it is already done :) RedBlackTreeDictionary <int, int> rb3 = new RedBlackTreeDictionary <int, int>(); AVLDictionary <int, int> avl3 = new AVLDictionary <int, int>(); Dictionary <int, int> d3 = new Dictionary <int, int>(); var InsertionRBT3 = new Stopwatch(); InsertionRBT3.Start(); for (int i = 0; i < 1280; i++) { rb3.Add(i, random.Next(3000, 5000)); } InsertionRBT3.Stop(); Console.WriteLine("Taken time in Red-Black tree for insertion in milliseconds is {0} ", InsertionRBT3.ElapsedMilliseconds); var InsertionAVL3 = new Stopwatch(); InsertionAVL3.Start(); for (int j = 0; j < 1280; j++) { avl3.Add(j, random.Next(5000, 7000)); } InsertionAVL3.Stop(); Console.WriteLine("Taken time in AVL tree for insertion in milliseconds is {0} ", InsertionAVL3.ElapsedMilliseconds); var insertionDictionary3 = new Stopwatch(); insertionDictionary3.Start(); for (int k = 0; k < 1280; k++) { d3.Add(k, random.Next(7000, 9000)); } insertionDictionary3.Stop(); Console.WriteLine("Taken time in hash-table based dictionary in milliseconds is {0} ", insertionDictionary3.ElapsedMilliseconds); var DeletionRBT3 = new Stopwatch(); DeletionRBT3.Start(); for (int i = 0; i < 1280; i++) { rb3.Remove(i); } DeletionRBT3.Stop(); Console.WriteLine("Taken time in Red-Black tree for deletion in milliseconds is {0} ", DeletionRBT3.ElapsedMilliseconds); var DeletionAVL3 = new Stopwatch(); DeletionAVL3.Start(); for (int j = 0; j < 1280; j++) { avl3.Remove(j); } DeletionAVL3.Stop(); Console.WriteLine("Taken time in AVL tree for deletion in milliseconds is {0} ", DeletionAVL3.ElapsedMilliseconds); var deletionDictionary3 = new Stopwatch(); deletionDictionary3.Start(); for (int k = 0; k < 1280; k++) { d3.Remove(k); } deletionDictionary3.Stop(); Console.WriteLine("Taken time in hash-table based dictionary for deletion in milliseconds is {0} ", deletionDictionary3.ElapsedMilliseconds); //same for retrieve }