Ejemplo n.º 1
0
        public void TestGetPredecessor()
        {
            var tree = new BinarySearchTree<int>();
            var size = 10;
            Node<int> node = null;
            for (int i = 0; i < size; i++)
            {
                node = tree.Add(i);
            }

            for (int i = size - 1; i >= 0; i--)
            {
                tree.TryGetNode(i, out node);
                var prev = tree.Predecessor(node);
                if (i > 0)
                    Assert.AreEqual(node.Value - 1, prev.Value);
                else
                    Assert.IsNull(prev);
            }
        }
Ejemplo n.º 2
0
        public void TestTryGet()
        {
            var tree = new BinarySearchTree<int>();
            var size = 10;
            for (int i = 0; i < size; i++)
            {
                tree.Add(i);
            }

            Node<int> node;
            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(tree.TryGetNode(i, out node));
            }

            Assert.IsFalse(tree.TryGetNode(1000, out node));
        }
Ejemplo n.º 3
0
        public void TestGetSuccessor()
        {
            var tree = new BinarySearchTree<int>();
            var size = 10;
            Node<int> node = null;
            for (int i = 0; i < size; i++)
            {
                node = tree.Add(i);
            }

            for (int i = 0; i < size; i++)
            {
                tree.TryGetNode(i, out node);
                var next = tree.Successor(node);
                if (i < size - 1)
                    Assert.AreEqual(node.Value + 1, next.Value);
                else
                    Assert.IsNull(next);
            }
        }
Ejemplo n.º 4
0
        public void TestDelete()
        {
            var tree = new BinarySearchTree<int>();
            var size = 10;
            for (int i = 0; i < size; i++)
            {
                tree.Add(i);
            }

            Assert.AreEqual(tree.Count, size);

            Node<int> node;
            for (int i = 0; i < size; i++)
            {
                tree.TryGetNode(i, out node);
                tree.Remove(node);
            }

            Assert.AreEqual(tree.Count, 0);
            Assert.IsTrue(tree.IsEmpty);
        }