|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Collections.Generic; |
| 4 | + |
| 5 | +using DataStructures.Common; |
| 6 | +using DataStructures.Trees; |
| 7 | + |
| 8 | +namespace C_Sharp_Algorithms.DataStructuresTests |
| 9 | +{ |
| 10 | + public static class BinarySearchTreeMapTests |
| 11 | + { |
| 12 | + public static void DoTest() |
| 13 | + { |
| 14 | + // Binary Search Tree Map collection |
| 15 | + var bstMap = new BinarySearchTreeMap<int, string>(allowDuplicates: false); |
| 16 | + |
| 17 | + // Testing data |
| 18 | + KeyValuePair<int, string>[] values = new KeyValuePair<int, string>[10]; |
| 19 | + |
| 20 | + // Prepare the values array |
| 21 | + for(int i = 1; i <= 10; ++i) |
| 22 | + { |
| 23 | + var keyValPair = new KeyValuePair<int, string>(i, String.Format("Integer: {0}", i)); |
| 24 | + values[i - 1] = keyValPair; |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + // |
| 29 | + // Test singular insert |
| 30 | + for (int i = 0; i < 10; ++i) |
| 31 | + bstMap.Insert(values[i].Key, values[i].Value); |
| 32 | + |
| 33 | + Debug.Assert(bstMap.Count == values.Length, "Expected the same number of items."); |
| 34 | + |
| 35 | + bstMap.Clear(); |
| 36 | + |
| 37 | + |
| 38 | + // |
| 39 | + // Test collection insert |
| 40 | + bstMap.Insert(values); |
| 41 | + |
| 42 | + |
| 43 | + // |
| 44 | + // Test enumeration of key-value pairs is still in oreder |
| 45 | + var enumerator = bstMap.GetInOrderEnumerator(); |
| 46 | + for (int i = 0; i < 10; ++i) |
| 47 | + { |
| 48 | + if (enumerator.MoveNext()) |
| 49 | + { |
| 50 | + var curr = enumerator.Current; |
| 51 | + if (curr.Key != values[i].Key || curr.Value != values[i].Value) |
| 52 | + throw new Exception(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + // |
| 58 | + // Test against re-shuffled insertions (not like above order) |
| 59 | + bstMap = new BinarySearchTreeMap<int, string>(allowDuplicates: false); |
| 60 | + |
| 61 | + bstMap.Insert(4, "int4"); |
| 62 | + bstMap.Insert(5, "int5"); |
| 63 | + bstMap.Insert(7, "int7"); |
| 64 | + bstMap.Insert(2, "int2"); |
| 65 | + bstMap.Insert(1, "int1"); |
| 66 | + bstMap.Insert(3, "int3"); |
| 67 | + bstMap.Insert(6, "int6"); |
| 68 | + bstMap.Insert(0, "int0"); |
| 69 | + bstMap.Insert(8, "int8"); |
| 70 | + bstMap.Insert(10, "int10"); |
| 71 | + bstMap.Insert(9, "int9"); |
| 72 | + |
| 73 | + Debug.Assert(bstMap.Count == values.Length, "Expected the same number of items."); |
| 74 | + |
| 75 | + |
| 76 | + // |
| 77 | + // ASSERT INSERTING DUPLICATES WOULD BREAK |
| 78 | + var insert_duplicate_passed = true; |
| 79 | + try |
| 80 | + { |
| 81 | + // 2 already exists in tree |
| 82 | + bstMap.Insert(2, "int2"); |
| 83 | + insert_duplicate_passed = true; |
| 84 | + } |
| 85 | + catch |
| 86 | + { |
| 87 | + insert_duplicate_passed = false; |
| 88 | + } |
| 89 | + |
| 90 | + Debug.Assert(insert_duplicate_passed == false, "Fail! The tree doesn't allow duplicates"); |
| 91 | + |
| 92 | + |
| 93 | + // |
| 94 | + // Test find |
| 95 | + Debug.Assert(bstMap.Find(5).Key == 5, "Wrong find result!"); |
| 96 | + Debug.Assert(bstMap.FindMin().Key == 0, "Wrong min!"); |
| 97 | + Debug.Assert(bstMap.FindMax().Key == 10, "Wrong max!"); |
| 98 | + |
| 99 | + |
| 100 | + // |
| 101 | + // Assert find raises exception on non-existing elements |
| 102 | + bool threwKeyNotFoundError = false; |
| 103 | + |
| 104 | + try |
| 105 | + { |
| 106 | + bstMap.Find(999999999); |
| 107 | + threwKeyNotFoundError = false; |
| 108 | + } |
| 109 | + catch(KeyNotFoundException) |
| 110 | + { |
| 111 | + threwKeyNotFoundError = true; |
| 112 | + } |
| 113 | + |
| 114 | + Debug.Assert(true == threwKeyNotFoundError, "Expected to catch KeyNotFoundException."); |
| 115 | + |
| 116 | + |
| 117 | + // |
| 118 | + // PRINT TREE |
| 119 | + Console.WriteLine("*****************************"); |
| 120 | + Console.WriteLine(" [*] BINARY SEARCH TREE TREE:\r\n"); |
| 121 | + Console.WriteLine("*****************************"); |
| 122 | + Console.WriteLine(bstMap.DrawTree()); |
| 123 | + Console.WriteLine("\r\n"); |
| 124 | + |
| 125 | + |
| 126 | + // |
| 127 | + // Assert count |
| 128 | + Debug.Assert(bstMap.Count == 11); |
| 129 | + |
| 130 | + |
| 131 | + // |
| 132 | + // Assert existence and nonexistence of some items |
| 133 | + Debug.Assert(bstMap.Contains(1) == true); |
| 134 | + Debug.Assert(bstMap.Contains(3) == true); |
| 135 | + Debug.Assert(bstMap.Contains(999) == false); |
| 136 | + |
| 137 | + |
| 138 | + // |
| 139 | + // Do some deletions |
| 140 | + bstMap.Remove(7); |
| 141 | + bstMap.Remove(1); |
| 142 | + bstMap.Remove(3); |
| 143 | + |
| 144 | + |
| 145 | + // |
| 146 | + // Assert count |
| 147 | + Debug.Assert(bstMap.Count == 8); |
| 148 | + |
| 149 | + |
| 150 | + // |
| 151 | + // Assert nonexistence of previously existing items |
| 152 | + Debug.Assert(bstMap.Contains(1) == false); |
| 153 | + Debug.Assert(bstMap.Contains(3) == false); |
| 154 | + |
| 155 | + |
| 156 | + // |
| 157 | + // Remove root key |
| 158 | + var oldRootKey = bstMap.Root.Key; |
| 159 | + bstMap.Remove(bstMap.Root.Key); |
| 160 | + |
| 161 | + |
| 162 | + // |
| 163 | + // Assert count |
| 164 | + Debug.Assert(bstMap.Count == 7); |
| 165 | + |
| 166 | + |
| 167 | + // |
| 168 | + // Assert nonexistence of old root's key |
| 169 | + Debug.Assert(bstMap.Contains(oldRootKey) == false); |
| 170 | + |
| 171 | + |
| 172 | + // |
| 173 | + // PRINT TREE |
| 174 | + Console.WriteLine("*****************************"); |
| 175 | + Console.WriteLine(" [*] BINARY SEARCH TREE TREE:\r\n"); |
| 176 | + Console.WriteLine("*****************************"); |
| 177 | + Console.WriteLine(bstMap.DrawTree(includeValues: true)); |
| 178 | + Console.WriteLine("\r\n"); |
| 179 | + |
| 180 | + Console.ReadLine(); |
| 181 | + }//end-do-test |
| 182 | + } |
| 183 | +} |
| 184 | + |
0 commit comments