示例#1
0
        public void TestBinaryTree()
        {
            int MAX = 1000;

            for (int cases = 0; cases < 100; cases++)
            {
                BinaryTree <int> tree = CreateTree(MAX);

                // Test tree traversal, node by node
                BinaryTree <int> current = tree.Find(1);
                for (int i = 1; i <= MAX; i++)
                {
                    Assert.AreEqual(i, current.Value);
                    Assert.AreEqual(i - 1, current.Index());
                    current = current.Next();
                }
                Assert.IsTrue(current.IsRoot());
                Assert.AreEqual(MAX, current.Index());

                // Test tree traversal, random jumps
                for (int i = 0; i < MAX; i++)
                {
                    int start  = _random.Next(1, MAX + 2);
                    int jumpTo = _random.Next(1, MAX + 2);

                    BinaryTree <int> x = start == MAX + 1 ? tree : tree.Find(start);
                    BinaryTree <int> y = x.Step(jumpTo - start);
                    if (jumpTo == MAX + 1)
                    {
                        Assert.IsTrue(y.IsRoot());
                    }
                    else
                    {
                        Assert.AreEqual(jumpTo, y.Value);
                    }
                }

                // Find indexes
                for (int i = 0; i < MAX; i++)
                {
                    BinaryTree <int> bt = tree.FindIndex(i);
                    Assert.AreEqual(i + 1, bt.Value);
                }
                BinaryTree <int> findRoot = tree.FindIndex(MAX);
                Assert.IsTrue(findRoot.IsRoot());

                if (cases % 2 == 0)
                {
                    // remove in order
                    current = tree.Find(1);
                    for (int i = 1; i <= MAX; i++)
                    {
                        Assert.AreEqual(i, current.Value);
                        current = current.Remove();
                        tree.Validate();
                    }
                    Assert.IsTrue(current.IsRoot());
                }
                else
                {
                    // remove in random order
                    for (int i = 0; i < MAX; i++)
                    {
                        int pos = _random.Next(0, MAX - i);
                        current = tree.FindIndex(pos);
                        Assert.AreEqual(pos, current.Index());
                        int oldValue = current.Value;
                        current = current.Remove();
                        Assert.AreEqual(pos, current.Index());
                        if (!current.IsRoot())
                        {
                            int newValue = current.Value;
                            Assert.IsTrue(oldValue < newValue);
                        }
                        tree.Validate();
                    }
                    Assert.IsTrue(current.IsRoot());
                }
            }
        }