static void SearchExample() { var tree = new AATree<int>(); tree.Add(4); tree.Add(10); tree.Add(2); tree.Add(6); tree.Add(12); tree.Add(3); tree.Add(1); tree.Add(8); tree.Add(13); tree.Add(11); tree.Add(5); tree.Add(9); tree.Add(7); Console.WriteLine("All results should be true"); Console.WriteLine(tree.Find(4)); Console.WriteLine(!tree.Find(-5)); Console.WriteLine(!tree.Find(100)); Console.WriteLine(!tree.Find(15)); Console.WriteLine(tree.Find(13)); Console.WriteLine(tree.Find(7)); }
public void TestByAddingRandomValues() { Random rnd = new Random(); AATree <int> aatree = new AATree <int>(); int size = 100; List <int> list = new List <int>(size); while (list.Count < size) { int val = rnd.Next(); if (aatree.Add(val)) { list.Add(val); } } list.Sort(); Assert.AreEqual(size, aatree.Count); IComparer <int> comparer = Comparer <int> .Default; Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); Assert.IsTrue(aatree.Contains(list[50])); }
public void AddRemoveSingleElementShouldWorkCorrectly() { AATree<int> tree = new AATree<int>(); tree.Add(3); var result = tree.Remove(3); Assert.AreEqual(null, result); }
static void Test(int[] values) { AATree <int, int> tree = new AATree <int, int>(); for (int i = 0; i < values.Length; i++) { if (!tree.Add(values[i], (i + 1))) { Console.WriteLine("Failed to insert {0}", values[i]); } } for (int i = 0; i < values.Length; i++) { for (int j = 0; j < i; j++) { if (tree[values[j]] != 0) { Console.WriteLine("Found deleted key {0}", values[j]); } } for (int j = i; j < values.Length; j++) { if (tree[values[j]] != (j + 1)) { Console.WriteLine("Could not find key {0}", values[j]); } } if (!tree.Remove(values[i])) { Console.WriteLine("Failed to delete {0}", values[i]); } } }
public void GetKeysInOrder_NonEmptyTree_ReturnsCorrectAnswer() { var tree = new AATree <int>(); tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); tree.GetKeysInOrder().SequenceEqual(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).Should().BeTrue(); }
public void Add_KeyAlreadyInTree_ThrowsException() { var tree = new AATree <int>(); tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); Assert.Throws <ArgumentException>(() => tree.Add(1)); }
static void Main(string[] args) { AATree<int> tree = new AATree<int>(); tree.Add(3); var result = tree.Remove(3); Console.WriteLine(result); }
static void Main(string[] args) { var tree = new AATree <string>(); tree.Add("January"); tree.Add("February"); tree.Add("March"); tree.Add("April"); tree.Add("May"); tree.Add("June"); tree.Add("July"); tree.Add("August"); tree.Add("September"); tree.Add("October"); tree.Add("November"); tree.Add("December"); //for (int i = 1; i <= 1303; i++) //{ // tree.Add(i.ToString()); //} Console.WriteLine(); Console.WriteLine($"Tree has {tree.Count} nodes, depth {tree.Depth}"); Console.WriteLine($"Tree contains \"August\": {tree.Contains("August")}"); Console.WriteLine($"Tree contains \"Borktember\": {tree.Contains("Borktember")}"); Console.WriteLine("Ordered tree traversal:"); tree.TraverseTree(); }
public static void Test(int[] values) { AATree<int, int> tree = new AATree<int, int>(); for (int i = 0; i < values.Length; i++) { if (!tree.Add(values[i], i + 1)) { Console.WriteLine("Failed to insert {0}", values[i]); } } for (int i = 0; i < values.Length; i++) { for (int j = 0; j < i; j++) { if (tree[values[j]] != 0) { Console.WriteLine("Found deleted key {0}", values[j]); } } for (int j = i; j < values.Length; j++) { if (tree[values[j]] != (j + 1)) { Console.WriteLine("Could not find key {0}", values[j]); } } if (!tree.Remove(values[i])) { Console.WriteLine("Failed to delete {0}", values[i]); } } }
public static void Main() { var testTree = new AATree<int>(10); testTree.Add(1); testTree.Add(7); testTree.Add(11); testTree.Add(-12); foreach (var item in testTree) { Console.WriteLine(item); } Console.WriteLine(); testTree.Remove(7); foreach (var item in testTree) { Console.WriteLine(item); } Console.WriteLine(); testTree.Remove(11); foreach (var item in testTree) { Console.WriteLine(item); } testTree.Remove(122); }
public void GetMin_NonEmptyTree_ReturnsCorrectAnswer() { var tree = new AATree <int>(); tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); tree.GetMin().Should().Be(1); }
public void TestRemoveMethodWithRandomValues() { Random rnd = new Random(); IComparer <int> comparer = Comparer <int> .Default; AATree <int> aatree = new AATree <int>(); int size = 1000; List <int> list = new List <int>(size); while (list.Count < size) { int val = rnd.Next(); if (aatree.Add(val)) { list.Add(val); } } list.Sort(); Assert.AreEqual(size, aatree.Count); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); int index = 10; Assert.IsTrue(aatree.Remove(list[index])); list.RemoveAt(index); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); index = 20; Assert.IsTrue(aatree.Remove(list[index])); list.RemoveAt(index); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); index = 30; Assert.IsTrue(aatree.Remove(list[index])); list.RemoveAt(index); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); index = 40; Assert.IsTrue(aatree.Remove(list[index])); list.RemoveAt(index); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); index = 50; Assert.IsTrue(aatree.Remove(list[index])); list.RemoveAt(index); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); index = list.Count - 1; Assert.IsTrue(aatree.Remove(list[index])); list.RemoveAt(index); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); index = 0; Assert.IsTrue(aatree.Remove(list[index])); list.RemoveAt(index); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); Assert.AreEqual(list.Count, aatree.Count); }
public void TestRemoveMethodWithValuesSortByDescOrder() { IComparer <int> comparer = Comparer <int> .Default; AATree <int> aatree = new AATree <int>(); int size = 10000; List <int> list = new List <int>(size); for (int i = size - 1; i >= 0; i--) { int val = i; if (aatree.Add(val)) { list.Add(val); } } list.Sort(); Assert.AreEqual(size, aatree.Count); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); int index = 10; Assert.IsTrue(aatree.Remove(list[index])); list.RemoveAt(index); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); index = 20; Assert.IsTrue(aatree.Remove(list[index])); list.RemoveAt(index); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); index = 30; Assert.IsTrue(aatree.Remove(list[index])); list.RemoveAt(index); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); index = 40; Assert.IsTrue(aatree.Remove(list[index])); list.RemoveAt(index); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); index = 50; Assert.IsTrue(aatree.Remove(list[index])); list.RemoveAt(index); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); index = list.Count - 1; Assert.IsTrue(aatree.Remove(list[index])); list.RemoveAt(index); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); index = 0; Assert.IsTrue(aatree.Remove(list[index])); list.RemoveAt(index); Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); Assert.AreEqual(list.Count, aatree.Count); }
public static void AddNumber(AATree <int, string> tree, int key, string value) { tree.Add(key, value); Console.WriteLine("Added " + key); DisplayTree(tree.Root, string.Empty); Console.WriteLine("----------------------"); }
public void Remove_EmptyTree_Throws() { var tree = new AATree <int>(); Action act = () => tree.Remove(999); act.Should().Throw <InvalidOperationException>(); }
public void Contains_NonEmptyTree_ReturnsCorrectAnswer() { var tree = new AATree <int>(); tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); tree.Contains(6).Should().BeTrue(); tree.Contains(999).Should().BeFalse(); }
public static void AddNumber(AATree<int, string> tree, int key, string value) { tree.Add(key, value); Console.WriteLine("Added " + key); DisplayTree(tree.Root, string.Empty); Console.WriteLine("----------------------"); }
public void GetMin_NonEmptyTree_ReturnsCorrectAnswer() { var tree = new AATree <int>(); tree.AddRange(new [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); Assert.AreEqual(1, tree.GetMin()); }
public void RemoveSingleElementFromMultipleAdded() { AATree<int> tree = new AATree<int>(); tree.Add(3); tree.Add(20); var newRoot = tree.Remove(20); Assert.AreEqual(3, newRoot.Value); }
public void Contains_NonEmptyTree_ReturnsCorrectAnswer() { var tree = new AATree <int>(); tree.AddRange(new [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); Assert.IsTrue(tree.Contains(6)); Assert.IsFalse(tree.Contains(999)); }
public void Setup() { this.AATree = new AATree <int>(); foreach (int integer in input) { this.AATree.Insert(integer); } }
public static void Main() { var tree = new AATree<int, string>(); Console.WriteLine("The AA tree created."); var nums = new[] { -5, 20, 14, 11, 8, -3, 111, 7, 100, -55 }; for (int i = 0; i < nums.Length; i++) { AddNumber(tree, nums[i], "value " + nums[i]); } }
public void Remove_KeyNotInTree_Throws() { var tree = new AATree <int>(); tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); Action act = () => tree.Remove(999); act.Should().Throw <InvalidOperationException>(); }
public void Constructor_UseCustomComparer_FormsCorrectTree() { var tree = new AATree <int>(Comparer <int> .Create((x, y) => y.CompareTo(x))); tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); tree.GetMax().Should().Be(1); tree.GetMin().Should().Be(10); tree.GetKeysInOrder().SequenceEqual(new[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }).Should().BeTrue(); Validate(tree.Root); }
public void AddRange_MultipleKeys_FormsCorrectTree() { var tree = new AATree <int>(); tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); tree.Count.Should().Be(10); tree.GetKeysInOrder().SequenceEqual(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).Should().BeTrue(); tree.GetKeysPostOrder().SequenceEqual(new[] { 1, 3, 2, 5, 7, 10, 9, 8, 6, 4 }).Should().BeTrue(); Validate(tree.Root); }
public static void AddNumber(AATree<int> tree, int value) { if (tree.Root.IsSentinel()) tree.Root = new AATree<int>.AANode(value); tree.Root = tree.Add(value, tree.Root); Console.WriteLine("Added " + value); DisplayTree(tree.Root, string.Empty); Console.WriteLine("----------------------"); }
public void GetKeysPostOrder_NonEmptyTree_ReturnsCorrectAnswer() { var tree = new AATree <int>(); tree.AddRange(new [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); var expected = new [] { 1, 3, 2, 5, 7, 10, 9, 8, 6, 4 }; var actual = tree.GetKeysPostOrder(); Assert.IsTrue(Enumerable.SequenceEqual(expected, actual)); }
private static void DisplayTree(AATree<int, string>.Node node, string intend) { Console.WriteLine(intend + node.key + " (level:" + node.level + ")"); if (node.left.level != 0) { DisplayTree(node.left, intend + " "); } if (node.right.level != 0) { DisplayTree(node.right, intend + " "); } }
private static void DisplayTree(AATree<int>.AANode node, string intend) { Console.WriteLine(intend + node.value + " (level:" + node.level + ")"); if (node.leftChild.level != 0) { DisplayTree(node.leftChild, intend + " "); } if (node.rightChild.level != 0) { DisplayTree(node.rightChild, intend + " "); } }
public static void Main() { var tree = new AATree <int, string>(); Console.WriteLine("The AA tree created."); var nums = new[] { -5, 20, 14, 11, 8, -3, 111, 7, 100, -55 }; for (int i = 0; i < nums.Length; i++) { AddNumber(tree, nums[i], "value " + nums[i]); } }
private static void DisplayTree(AATree <int, string> .Node node, string intend) { Console.WriteLine(intend + node.key + " (level:" + node.level + ")"); if (node.left.level != 0) { DisplayTree(node.left, intend + " "); } if (node.right.level != 0) { DisplayTree(node.right, intend + " "); } }
public static void Main() { var tree = new AATree<int>(); Console.WriteLine("The AA tree created."); var nums = new[] { -5, 20, 14, 11, 8, -3, 111, 7, 100, -55 }; for (int i = 0; i < nums.Length; i++) { AddNumber(tree, nums[i]); } Console.WriteLine("\n\nTree after we remove element:\n"); tree.Delete(14, tree.Root); DisplayTree(tree.Root, string.Empty); }
public static void Main() { var tree = new AATree<int>(); Console.WriteLine("The AA tree created."); var nums = new[] { -5, 20, 14, 11, 8, -3, 111, 7, 100, -55 }; for (int i = 0; i < nums.Length; i++) { AddNumber(tree, nums[i]); } Console.WriteLine(tree.Contains(-5)); Console.WriteLine(tree.Contains(8)); Console.WriteLine(tree.Contains(1000)); }
public void Add_MultipleKeys_FormsCorrectTree() { var tree = new AATree <int>(); foreach (var elem in new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }) { tree.Add(elem); tree.Count.Should().Be(elem); tree.Contains(elem).Should().BeTrue(); } tree.GetKeysInOrder().SequenceEqual(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).Should().BeTrue(); tree.GetKeysPostOrder().SequenceEqual(new[] { 1, 3, 2, 5, 7, 10, 9, 8, 6, 4 }).Should().BeTrue(); Validate(tree.Root); }
public void Constructor_UseCustomComparer_FormsCorrectTree() { var tree = new AATree <int>(Comparer <int> .Create((x, y) => y.CompareTo(x))); tree.AddRange(new [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); Assert.AreEqual(1, tree.GetMax()); Assert.AreEqual(10, tree.GetMin()); var expected = new [] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; var actual = tree.GetKeysInOrder(); Assert.IsTrue(Enumerable.SequenceEqual(expected, actual)); Validate(tree.Root); }
// Test program; should print min and max and nothing else public static void Main(string[] args) { AATree <int> t = new AATree <int>(-9999); const int NUMS = 40000; const int GAP = 307; Console.WriteLine("Checking... (no bad output means success)"); t.Insert(NUMS * 2); t.Insert(NUMS * 3); for (int i = GAP; i != 0; i = (i + GAP) % NUMS) { t.Insert(i); } Console.WriteLine("Inserts complete"); t.Remove(t.FindMax( )); for (int i = 1; i < NUMS; i += 2) { t.Remove(i); } t.Remove(t.FindMax( )); Console.WriteLine("Removes complete"); if (t.FindMin( ) != 2 || t.FindMax( ) != NUMS - 2) { Console.WriteLine("FindMin or FindMax error!"); } for (int i = 2; i < NUMS; i += 2) { if (t.Find(i) != i) { Console.WriteLine("Error: find fails for " + i); } } for (int i = 1; i < NUMS; i += 2) { if (t.Contains(i)) { Console.WriteLine("Error: Found deleted item " + i); } } }
public void MainTest() { AATree t = new AATree(); int NUMS = 40000; int GAP = 307; System.Console.Out.WriteLine("Checking... (no bad output means success)"); for (int i = GAP; i != 0; i = (i + GAP) % NUMS) { t.insert(new MyInteger(i)); } System.Console.Out.WriteLine("Inserts complete"); for (int i = 1; i < NUMS; i += 2) { t.remove(new MyInteger(i)); } System.Console.Out.WriteLine("Removes complete"); if (NUMS < 40) { t.printTree(); } if (((MyInteger)(t.findMin())).intValue() != 2 || ((MyInteger)(t.findMax())).intValue() != NUMS - 2) { System.Console.Out.WriteLine("FindMin or FindMax error!"); } for (int i = 2; i < NUMS; i += 2) { if (((MyInteger)t.find(new MyInteger(i))).intValue() != i) { System.Console.Out.WriteLine("Error: find fails for " + i); } } for (int i = 1; i < NUMS; i += 2) { if (t.find(new MyInteger(i)) != null) { System.Console.Out.WriteLine("Error: Found deleted item " + i); } } }
public void TestByAddingValuesSortedByAscOrder() { AATree <int> aatree = new AATree <int>(); int size = 100; List <int> list = new List <int>(size); for (int i = 0; i < size; i++) { aatree.Add(i); list.Add(i); } Assert.AreEqual(size, aatree.Count); IComparer <int> comparer = Comparer <int> .Default; Assert.IsTrue(ValidateInOrder(aatree, list, comparer)); Assert.IsTrue(aatree.Contains(50)); }
static void Main(string[] args) { var tree = new AATree<int, string>(); Console.WriteLine("The AA tree created."); var nums = new[] { 3, 1, 5, 0, 2, 4, 6 }; //{ 5, 6, 25 }; //, 11, 8, -3, 111, 7, 100, -55 }; for (int i = 0; i < nums.Length; i++) { AddNumber(tree, nums[i], "value " + nums[i]); } tree.Remove(0); Console.WriteLine("Removed 0"); DisplayTree(tree.Root, string.Empty); Console.WriteLine("----------------------"); tree.Remove(3); Console.WriteLine("Removed 3"); DisplayTree(tree.Root, string.Empty); Console.WriteLine("----------------------"); }
public void AddRange_MultipleKeys_FormsCorrectTree() { var tree = new AATree <int>(); tree.AddRange(new [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); Assert.AreEqual(10, tree.Count); var expected = new [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var actual = tree.GetKeysInOrder(); Assert.IsTrue(Enumerable.SequenceEqual(expected, actual)); expected = new [] { 1, 3, 2, 5, 7, 10, 9, 8, 6, 4 }; actual = tree.GetKeysPostOrder(); Assert.IsTrue(Enumerable.SequenceEqual(expected, actual)); Validate(tree.Root); }
private static bool ValidateInOrder <T>(AATree <T> aaTree, List <T> sortedList, IComparer <T> comparer) { bool result = true; int index = 0; foreach (T val in aaTree.InOrderTraversal()) { if (comparer.Compare(val, sortedList[index++]) != 0) { result = false; break; } } if (index != sortedList.Count) { result = false; } return(result); }
public void Remove_MultipleKeys_TreeStillValid() { var tree = new AATree <int>(); tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); Remove(4).Should().NotThrow(); tree.Contains(4).Should().BeFalse(); tree.Count.Should().Be(9); Remove(8).Should().NotThrow(); tree.Contains(8).Should().BeFalse(); tree.Count.Should().Be(8); Remove(1).Should().NotThrow(); tree.Contains(1).Should().BeFalse(); tree.Count.Should().Be(7); Validate(tree.Root); Action Remove(int x) => () => tree.Remove(x); }
public void Remove_MultipleKeys_TreeStillValid() { var tree = new AATree <int>(); tree.AddRange(new [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); Remove(4).Should().NotThrow(); Assert.IsFalse(tree.Contains(4)); Assert.AreEqual(9, tree.Count); Remove(8).Should().NotThrow(); Assert.IsFalse(tree.Contains(8)); Assert.AreEqual(8, tree.Count); Remove(1).Should().NotThrow(); Assert.IsFalse(tree.Contains(1)); Assert.AreEqual(7, tree.Count); Validate(tree.Root); Action Remove(int x) => () => tree.Remove(x); }
static void InsertDeleteExample() { var tree = new AATree<int>(); tree.Add(4); tree.Add(10); tree.Add(2); tree.Add(6); tree.Add(12); tree.Add(3); tree.Add(1); tree.Add(8); tree.Add(13); tree.Add(11); tree.Add(5); tree.Add(9); tree.Add(7); tree.Remove(1); tree.Print(); tree.Remove(5); Console.WriteLine(new string('-', 40)); tree.Print(); }