public void CopyToShouldWalkTheTreeForward() { int seed = 12345; int RandomNext() { seed = (seed * 69069) + 12345; return((seed >> 16) & 0x7FFF); } WeightedRedBlackTree <int, int> tree = new WeightedRedBlackTree <int, int>(); { List <WeightedRedBlackTreeNode <int, int> > actualNodes = tree.ToList(); int[] actualKeys = actualNodes.Select(n => n.Key).ToArray(); CollectionAssert.AreEqual(actualKeys, new int[0]); } List <int> keys = new List <int>(); for (int i = 0; i < 500; i++) { int key = RandomNext(); keys.Add(key); tree.Add(key, i); if (IsFibonacci(i)) { List <WeightedRedBlackTreeNode <int, int> > actualNodes = tree.ToList(); int[] actualKeys = actualNodes.Select(n => n.Key).ToArray(); int[] expectedKeys = keys.OrderBy(k => k).ToArray(); CollectionAssert.AreEqual(actualKeys, expectedKeys); } } }
public void CanCopyToSpecialCaseTrees() { { WeightedRedBlackTree <int, int> tree = new WeightedRedBlackTree <int, int>(); Assert.That(tree.Root, Is.Null); // CopyTo should safely bail if the root is null. WeightedRedBlackTreeNode <int, int>[] actualNodes = new WeightedRedBlackTreeNode <int, int> [0]; tree.CopyTo(actualNodes, 0); int[] actualKeys = actualNodes.Select(n => n.Key).ToArray(); CollectionAssert.AreEqual(actualKeys, new int[0]); } { WeightedRedBlackTree <int, int> tree = new WeightedRedBlackTree <int, int> { { 1, 10 }, }; // The above is a valid red-black tree with only one node. Assert.That(tree.Root.Key, Is.EqualTo(1)); // CopyTo performs an inorder walk using Next(), starting at // Root's minimum subnode. List <WeightedRedBlackTreeNode <int, int> > actualNodes = tree.ToList(); int[] actualKeys = actualNodes.Select(n => n.Key).ToArray(); CollectionAssert.AreEqual(actualKeys, new[] { 1 }); } { WeightedRedBlackTree <int, int> tree = new WeightedRedBlackTree <int, int> { { 2, 20 }, { 1, 10 }, }; // The above is a valid red-black tree in which the root is 2 // and it has a left child of 1. Let's check that to be sure... Assert.That(tree.Root.Key, Is.EqualTo(2)); Assert.That(tree.Root.Left.Key, Is.EqualTo(1)); // CopyTo performs an inorder walk using Next(), starting at // Root's minimum subnode. In this case, we ensure that // Root is non-null and has a minimum subnode to start at. List <WeightedRedBlackTreeNode <int, int> > actualNodes = tree.ToList(); int[] actualKeys = actualNodes.Select(n => n.Key).ToArray(); CollectionAssert.AreEqual(actualKeys, new[] { 1, 2 }); } { WeightedRedBlackTree <int, int> tree = new WeightedRedBlackTree <int, int> { { 1, 10 }, { 2, 20 }, }; // The above is a valid red-black tree in which the root is 1 // and it has a right child of 2. Let's check that to be sure... Assert.That(tree.Root.Key, Is.EqualTo(1)); Assert.That(tree.Root.Right.Key, Is.EqualTo(2)); // CopyTo performs an inorder walk using Next(), starting at // Root's minimum subnode. In this case, we ensure that // Root is non-null but has no minimum subnode to start at. List <WeightedRedBlackTreeNode <int, int> > actualNodes = tree.ToList(); int[] actualKeys = actualNodes.Select(n => n.Key).ToArray(); CollectionAssert.AreEqual(actualKeys, new[] { 1, 2 }); } }