public void BinaryTreeUnbalancedDelete12() { List <int> input = new List <int>() { 1, 18, 17, 13, 16, 10, 7, 15, 9, 6, 3, 2, 24, 25, 8, 12, 11, 4, 14, 21, 23, 5, 20, 19, 22 }; UnbalancedBinaryTree <int, int> tree = new UnbalancedBinaryTree <int, int>(); foreach (int i in input) { tree.Add(i, i); } List <int> deletes = new List <int>() { 1, 6, 18, 25, 2 }; int count = input.Count; foreach (int i in deletes) { Console.WriteLine("Removing Key " + i); Assert.IsTrue(tree.Remove(i), "Failed to remove Key " + i); input.Remove(i); count--; this.TestOrderStructs <int>(input.OrderBy(k => k, Comparer <int> .Default).ToList(), tree.Keys.ToList()); Assert.AreEqual(count, tree.Nodes.Count(), "Removal of Key " + i + " did not reduce node count as expected"); } }
public void BinaryTreeUnbalancedIndexAccess1() { UnbalancedBinaryTree <int, int> tree = new UnbalancedBinaryTree <int, int>(); List <int> inputs = Enumerable.Range(1, 10).ToList(); this.TestOrderPreservationOnInsertStructs <IBinaryTreeNode <int, int>, int>(inputs, tree); for (int i = 0; i < 10; i++) { Assert.AreEqual(i + 1, tree.GetValueAt(i)); } // Swap the values using index access for (int i = 0, j = 9; i < j; i++, j--) { int temp = tree.GetValueAt(i); tree.SetValueAt(i, tree.GetValueAt(j)); tree.SetValueAt(j, temp); } TestTools.PrintEnumerableStruct(tree.Values, ","); for (int i = 0; i < 10; i++) { Assert.AreEqual(10 - i, tree.GetValueAt(i)); } }
public void BinaryTreeUnbalancedDelete7() { for (int i = 0; i < 10; i++) { UnbalancedBinaryTree <int, int> tree = new UnbalancedBinaryTree <int, int>(); this.TestOrderPreservationOnDeleteStructs <IBinaryTreeNode <int, int>, int>(Enumerable.Range(1, 100), tree); } }
private UnbalancedBinaryTree <int, int> GetTreeForDelete() { UnbalancedBinaryTree <int, int> tree = new UnbalancedBinaryTree <int, int>(); tree.Root = new BinaryTreeNode <int, int>(null, 2, 2); tree.Root.LeftChild = new BinaryTreeNode <int, int>(tree.Root, 1, 1); tree.Root.RightChild = new BinaryTreeNode <int, int>(tree.Root, 3, 3); return(tree); }
public void BinaryTreeUnbalancedDelete5() { UnbalancedBinaryTree <int, int> tree = new UnbalancedBinaryTree <int, int>(); List <int> inputs = Enumerable.Range(1, 100).ToList(); this.TestOrderPreservationOnInsertStructs <IBinaryTreeNode <int, int>, int>(inputs, tree); Assert.IsTrue(tree.Remove(inputs[50])); inputs.RemoveAt(50); this.TestOrderStructs <int>(inputs, tree.Keys.ToList()); }
public void BinaryTreeUnbalancedDelete3() { UnbalancedBinaryTree <int, int> tree = this.GetTreeForDelete(); BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree); tree.Remove(2); BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree); this.TestOrderStructs <int>(new List <int> { 1, 3 }, tree.Keys.ToList()); }
public void BinaryTreeUnbalancedDelete2() { UnbalancedBinaryTree <int, int> tree = this.GetTreeForDelete(); BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree); tree.Remove(3); BinaryTreeTools.PrintBinaryTreeStructs <IBinaryTreeNode <int, int>, int>(tree); Assert.IsNull(tree.Root.RightChild, "Right Child should now be null"); this.TestOrderStructs <int>(new List <int> { 1, 2 }, tree.Keys.ToList()); }
public void BinaryTreeUnbalancedInsert4() { UnbalancedBinaryTree <int, int> tree = new UnbalancedBinaryTree <int, int>(); //Randomize the input order List <int> pool = Enumerable.Range(1, 1000).ToList(); List <int> input = new List <int>(); while (pool.Count > 0) { int r = this._rnd.Next(pool.Count); input.Add(pool[r]); pool.RemoveAt(r); } this.TestOrderPreservationOnInsertStructs <IBinaryTreeNode <int, int>, int>(input, tree); }
public void BinaryTreeUnbalancedDelete8() { for (int i = 0; i < 10; i++) { //Randomize the input order List <int> pool = Enumerable.Range(1, 25).ToList(); List <int> input = new List <int>(); while (pool.Count > 0) { int r = this._rnd.Next(pool.Count); input.Add(pool[r]); pool.RemoveAt(r); } UnbalancedBinaryTree <int, int> tree = new UnbalancedBinaryTree <int, int>(); this.TestOrderPreservationOnDeleteStructs <IBinaryTreeNode <int, int>, int>(input, tree); } }
public void BinaryTreeUnbalancedIndexAccess2() { UnbalancedBinaryTree <int, int> tree = new UnbalancedBinaryTree <int, int>(); List <int> inputs = Enumerable.Range(1, 10).ToList(); this.TestOrderPreservationOnInsertStructs <IBinaryTreeNode <int, int>, int>(inputs, tree); for (int i = 0; i < 10; i++) { Assert.AreEqual(i + 1, tree.GetValueAt(i)); } // Remove values in random order while (tree.Root != null) { int index = this._rnd.Next(tree.Root.Size); tree.RemoveAt(index); inputs.RemoveAt(index); this.TestOrderStructs(inputs, tree.Keys.ToList()); } }
public void BinaryTreeUnbalancedInsert2() { UnbalancedBinaryTree <int, int> tree = new UnbalancedBinaryTree <int, int>(); this.TestOrderPreservationOnInsertStructs <IBinaryTreeNode <int, int>, int>(Enumerable.Range(1, 100), tree); }
public void BinaryTreeUnbalancedDeleteValidation6c() { UnbalancedBinaryTree <int, int> tree = new UnbalancedBinaryTree <int, int>(); this.TestTree6c(tree); }