public void RemoveIntTest() { var bst = new DSWBalancedBinarySearchTree<int>(); Assert.IsFalse(bst.Remove(0)); bst.Insert(11); Assert.IsTrue(bst.Remove(11)); Assert.IsFalse(bst.Remove(11)); for (var i = 0; i < 7; i++) { bst.Insert(i); } var balanced = "{\"Value\":3,\"LeftChild\":{\"Value\":1," + "\"LeftChild\":{\"Value\":0,\"LeftChild\":null,\"RightChild\":null}," + "\"RightChild\":{\"Value\":2,\"LeftChild\":null,\"RightChild\":null}}," + "\"RightChild\":{\"Value\":5," + "\"LeftChild\":{\"Value\":4,\"LeftChild\":null,\"RightChild\":null}," + "\"RightChild\":{\"Value\":6,\"LeftChild\":null,\"RightChild\":null}}}"; Assert.AreEqual(balanced,bst.GetJson()); Assert.IsTrue(bst.Remove(1)); Assert.IsFalse(bst.Remove(1)); }
public void RemoveBigTreeDoubleTest() { var bst = new DSWBalancedBinarySearchTree<double>(); for (var i = 0; i < 50; i++) { bst.Insert(i / 2.0); } Assert.IsTrue(bst.Remove(20.0)); Assert.IsTrue(bst.Remove(5.0)); Assert.IsTrue(bst.Remove(24.0)); Assert.IsFalse(bst.Remove(50.5)); Assert.IsTrue(bst.RemoveSubtree(23.0)); Assert.IsFalse(bst.RemoveSubtree(-1.9)); }
public void RemoveBigTreeIntTest() { var bst = new DSWBalancedBinarySearchTree<int>(); for (var i = 0; i < 50; i++) { bst.Insert(i); } Assert.IsTrue(bst.Remove(20)); Assert.IsTrue(bst.Remove(5)); Assert.IsTrue(bst.Remove(45)); Assert.IsFalse(bst.Remove(50)); Assert.IsTrue(bst.RemoveSubtree(37)); Assert.IsFalse(bst.RemoveSubtree(-1)); }
public static void TimingTest <T>(int amount) where T : IComparable { var path = GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\\TimingTest.csv"; var csv = new StringBuilder(); csv.AppendLine("generic_list;bst_insert;balanced_insert;balanced_search;remove_balanced;"); for (var i = 20; i < amount; i++) { var bstBalanced = new DSWBalancedBinarySearchTree <T>(); var bst = new BinarySearchTree <T>(); var genericList = new T[i]; var watch = Stopwatch.StartNew(); for (var j = 0; j < i; j++) { genericList[j] = (T)Convert.ChangeType(j, typeof(T)); } watch.Stop(); var listTime = watch.Elapsed.TotalMilliseconds; watch = Stopwatch.StartNew(); for (var j = 0; j < i; j++) { bst.Insert((T)Convert.ChangeType(j, typeof(T))); } watch.Stop(); var bstTime = watch.Elapsed.TotalMilliseconds; watch = Stopwatch.StartNew(); for (var j = 0; j < i; j++) { bstBalanced.Insert((T)Convert.ChangeType(j, typeof(T))); } watch.Stop(); var balancedTime = watch.Elapsed.TotalMilliseconds; watch = Stopwatch.StartNew(); bstBalanced.Search((T)Convert.ChangeType(0, typeof(T))); var search = watch.Elapsed.TotalMilliseconds; watch = Stopwatch.StartNew(); bstBalanced.Remove((T)Convert.ChangeType(0, typeof(T))); var remove = watch.Elapsed.TotalMilliseconds; var newLine = $"{listTime};{bstTime};{balancedTime},{search},{remove}"; csv.AppendLine(newLine); } File.WriteAllText(path, csv.ToString()); }
public void RemoveStringTest() { var bst = new DSWBalancedBinarySearchTree<string>(); Assert.IsFalse(bst.Remove("A")); bst.Insert("A"); Assert.IsTrue(bst.Remove("A")); Assert.IsFalse(bst.Remove("A")); bst.Insert("A"); bst.Insert("B"); bst.Insert("C"); var balanced = "{\"Value\":\"B\",\"LeftChild\":{\"Value\":\"A\",\"LeftChild\":null,\"RightChild\":null}," + "\"RightChild\":{\"Value\":\"C\",\"LeftChild\":null,\"RightChild\":null}}"; Assert.AreEqual(balanced, bst.GetJson()); Assert.IsTrue(bst.Remove("C")); Assert.IsFalse(bst.Remove("C")); }
public void RemoveDoubleTest() { var bst = new DSWBalancedBinarySearchTree<double>(); Assert.IsFalse(bst.Remove(0.4)); bst.Insert(11.3); Assert.IsTrue(bst.Remove(11.3)); Assert.IsFalse(bst.Remove(11.3)); bst.Insert(1.2); bst.Insert(2.8); bst.Insert(6.9); var balanced = "{\"Value\":2.8,\"LeftChild\":{\"Value\":1.2,\"LeftChild\":null,\"RightChild\":null}," + "\"RightChild\":{\"Value\":6.9,\"LeftChild\":null,\"RightChild\":null}}"; Assert.AreEqual(balanced, bst.GetJson()); Assert.IsTrue(bst.Remove(6.9)); Assert.IsFalse(bst.Remove(6.9)); }