예제 #1
0
        public void Add_KeyAlreadyInTree_ThrowsException()
        {
            var tree = new RedBlackTree <int>();

            tree.AddRange(new[] { 1, 2, 3, 4, 5 });
            Assert.Throws <ArgumentException>(() => tree.Add(1));
        }
예제 #2
0
        public void AddRange()
        {
            var array = new[] { 13, 8, 17, 1, 11, 9, 15, 25, 6, 22, 27 };
            var tree  = new RedBlackTree <int>();

            tree.AddRange(array);

            Assert.Equal(array.OrderBy(x => x).ToArray(), tree.InorderTraversal().ToArray());

            var root = tree.RootNode;

            // Left part of the tree
            this.AssertNode(8, true, false, root.Left);
            this.AssertNode(1, false, false, root.Left.Left);
            this.AssertNode(6, true, true, root.Left.Left.Right);
            this.AssertNode(11, false, false, root.Left.Right);
            this.AssertNode(9, true, true, root.Left.Right.Left);

            this.AssertNode(13, false, false, root);

            // Right part of the tree
            this.AssertNode(17, true, false, root.Right);
            this.AssertNode(15, false, true, root.Right.Left);
            this.AssertNode(25, false, false, root.Right.Right);
            this.AssertNode(22, true, true, root.Right.Right.Left);
            this.AssertNode(27, true, true, root.Right.Right.Right);
        }
예제 #3
0
        public void Remove_KeyNotInTree_ThrowsException()
        {
            var tree = new RedBlackTree <int>();

            tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
            Assert.Throws <KeyNotFoundException>(() => tree.Remove(24));
        }
예제 #4
0
        public void Remove_Case3_TreeStillValid()
        {
            // Move to case 6
            var tree = new RedBlackTree <int>();

            tree.AddRange(new[] { 7, 3, 18, 1, 10, 22, 8, 11, 26 });
            tree.Remove(1);
            tree.Remove(3);
            tree.Count.Should().Be(7);
            tree.Contains(3).Should().BeFalse();
            tree.GetKeysInOrder().SequenceEqual(new[] { 7, 8, 10, 11, 18, 22, 26 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 18, 10, 7, 8, 11, 22, 26 }).Should().BeTrue();

            // Move to case 5
            tree = new RedBlackTree <int>();
            tree.AddRange(new[] { 8, 3, 2, 0, 9, 4, 7, 6, 1, 5 });
            tree.Remove(8);
            tree.Remove(6);
            tree.Remove(9);
            tree.Count.Should().Be(7);
            tree.GetKeysInOrder().SequenceEqual(new[] { 0, 1, 2, 3, 4, 5, 7 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 3, 1, 0, 2, 5, 4, 7 }).Should().BeTrue();

            // Move to case 4
            tree = new RedBlackTree <int>();
            tree.AddRange(new[] { 7, 5, 8, 4, 6, 3, 2, 9, 0, 1 });
            tree.Remove(9);
            tree.Remove(6);
            tree.Remove(5);
            tree.Remove(8);
            tree.Count.Should().Be(6);
            tree.GetKeysInOrder().SequenceEqual(new[] { 0, 1, 2, 3, 4, 7 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 3, 1, 0, 2, 7, 4 }).Should().BeTrue();
        }
예제 #5
0
        public void GetKeysPostOrder_CorrectReturn()
        {
            var tree = new RedBlackTree <int>();

            tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
            tree.GetKeysPostOrder().SequenceEqual(new[] { 1, 3, 2, 5, 7, 10, 9, 8, 6, 4 }).Should().BeTrue();
        }
예제 #6
0
        public void GetMax_CorrectReturn()
        {
            var tree = new RedBlackTree <int>();

            tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
            tree.GetMax().Should().Be(10);
        }
예제 #7
0
        public void Add_Case1_FormsCorrectTree()
        {
            var tree = new RedBlackTree <int>();

            tree.AddRange(new[] { 5, 4, 6, 3, 7 });
            tree.GetKeysPreOrder().SequenceEqual(new[] { 5, 4, 3, 6, 7 }).Should().BeTrue();
        }
예제 #8
0
        public void Remove_Case5_TreeStillValid()
        {
            var tree = new RedBlackTree <int>();

            tree.AddRange(new[] { 13, 8, 17, 1, 11, 15, 25, 6, 22, 27 });
            tree.Remove(8);
            tree.Count.Should().Be(9);
            tree.Contains(8).Should().BeFalse();
            tree.GetKeysInOrder().SequenceEqual(new[] { 1, 6, 11, 13, 15, 17, 22, 25, 27 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 13, 6, 1, 11, 17, 15, 25, 22, 27 }).Should().BeTrue();

            tree = new RedBlackTree <int>();
            tree.AddRange(new[] { 13, 8, 17, 1, 11, 15, 25, 0, 6, 22 });
            tree.Remove(13);
            tree.Count.Should().Be(9);
            tree.Contains(13).Should().BeFalse();
            tree.GetKeysInOrder().SequenceEqual(new[] { 0, 1, 6, 8, 11, 15, 17, 22, 25 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 15, 8, 1, 0, 6, 11, 22, 17, 25 }).Should().BeTrue();

            tree = new RedBlackTree <int>();
            tree.AddRange(new[] { 7, 0, 1, 4, 8, 2, 3, 6, 5, 9 });
            tree.Remove(7);
            tree.Remove(0);
            tree.Remove(1);
            tree.Remove(4);
            tree.Remove(8);
            tree.GetKeysInOrder().SequenceEqual(new[] { 2, 3, 5, 6, 9 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 3, 2, 6, 5, 9 }).Should().BeTrue();
        }
예제 #9
0
        public void AddRange2()
        {
            var tree = new RedBlackTree <int>();

            tree.AddRange(Generator.Generate <int>(1000, x => ++ x));

            Assert.Equal(1000, tree.Count);
        }
예제 #10
0
        public void LevelOrder()
        {
            var array = new[] { 13, 8, 17, 1, 11, 9, 15, 25, 6, 22, 27 };
            var tree  = new RedBlackTree <int>();

            tree.AddRange(array);
            Assert.Equal(new[] { 13, 8, 17, 1, 11, 15, 25, 6, 9, 22, 27 }, tree.LevelOrderTraversal().ToArray());
        }
예제 #11
0
        public void PostOrder()
        {
            var array = new[] { 13, 8, 17, 1, 11, 9, 15, 25, 6, 22, 27 };
            var tree  = new RedBlackTree <int>();

            tree.AddRange(array);

            Assert.Equal(array.OrderByDescending(x => x).ToArray(), tree.PostorderTraversal().ToArray());
        }
예제 #12
0
        public void MinValue()
        {
            var array = new[] { 13, 8, 17, 1, 11, 9, 15, 25, 6, 22, 27 };
            var tree  = new RedBlackTree <int>();

            tree.AddRange(array);

            Assert.Equal(1, tree.MinValue);
        }
예제 #13
0
        public void AddRange_MultipleKeys_FormsCorrectTree()
        {
            var tree = new RedBlackTree <int>();

            tree.AddRange(new[] { 9, 0, 1, 6, 7, 5, 2, 8, 4, 3 });
            tree.Count.Should().Be(10);
            tree.GetKeysInOrder().SequenceEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 5, 1, 0, 3, 2, 4, 7, 6, 9, 8 }).Should().BeTrue();
        }
예제 #14
0
        public void Count()
        {
            var array = new[] { 13, 8, 17, 1, 11, 9, 15, 25, 6, 22, 27 };
            var tree  = new RedBlackTree <int>();

            tree.AddRange(array);

            Assert.Equal(array.Length, tree.Count);
        }
예제 #15
0
        public void AddRange3()
        {
            var array = new[] { 100, 50, 25, 200, 300, 250, 299, 75, 98, 68, 236, 358, 402, 506, 89, 874, 258, 321, 12, 123, 1236, 987, 45, 852, 147, 369, 951, 159, 23, 58, 69 };
            var tree  = new RedBlackTree <int>();

            tree.AddRange(array);

            Assert.Equal(array.OrderBy(x => x).ToArray(), tree.InorderTraversal().ToArray());
        }
예제 #16
0
        public void Constructor_UseCustomComparer_FormsCorrect_Tree()
        {
            var tree = new RedBlackTree <int>(Comparer <int> .Create((x, y) => y.CompareTo(x)));

            tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
            tree.GetMin().Should().Be(10);
            tree.GetMax().Should().Be(1);
            tree.GetKeysInOrder().SequenceEqual(new[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }).Should().BeTrue();
        }
예제 #17
0
        public void Contains_CorrectReturn()
        {
            var tree = new RedBlackTree <int>();

            tree.AddRange(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
            tree.Contains(3).Should().BeTrue();
            tree.Contains(7).Should().BeTrue();
            tree.Contains(24).Should().BeFalse();
            tree.Contains(-1).Should().BeFalse();
        }
예제 #18
0
        public void Remove_Case1_TreeStillValid()
        {
            var tree = new RedBlackTree <int>();

            tree.AddRange(new[] { 5, 2, 8, 1 });
            tree.Remove(1);
            tree.Remove(2);
            tree.Contains(2).Should().BeFalse();
            tree.GetKeysInOrder().SequenceEqual(new[] { 5, 8 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 5, 8 }).Should().BeTrue();
        }
예제 #19
0
        public void Clear()
        {
            var array = new[] { 13, 8, 17, 1, 11, 9, 15, 25, 6, 22, 27 };
            var tree  = new RedBlackTree <int>();

            tree.AddRange(array);
            tree.Clear();

            Assert.Equal(0, tree.Count);
            Assert.Null(tree.RootNode);
        }
예제 #20
0
        public void Add_Case5_FormsCorrectTree()
        {
            // Left-right rotation
            var tree = new RedBlackTree <int>();

            tree.AddRange(new[] { 5, 4, 6, 2, 3 });
            tree.GetKeysPreOrder().SequenceEqual(new[] { 5, 3, 2, 4, 6 }).Should().BeTrue();

            // Right-left rotation
            tree = new RedBlackTree <int>();
            tree.AddRange(new[] { 5, 4, 6, 8, 7 });
            tree.GetKeysPreOrder().SequenceEqual(new[] { 5, 4, 7, 6, 8 }).Should().BeTrue();
        }
예제 #21
0
        public void CopyTo()
        {
            var array = new[] { 13, 8, 17, 1, 11, 9, 15, 25, 6, 22, 27 };
            var tree  = new RedBlackTree <int>();

            tree.AddRange(array);

            var output = new int[array.Length];

            tree.CopyTo(output, 0);

            Assert.Equal(array.OrderBy(x => x).ToArray(), output);
        }
예제 #22
0
        public void Remove_Case4_TreeStillValid()
        {
            var tree = new RedBlackTree <int>();

            tree.AddRange(new[] { 5, 2, 8, 1, 4, 7, 9, 0, 3 });
            tree.Remove(0);
            tree.Remove(3);
            tree.Remove(2);
            tree.Count.Should().Be(6);
            tree.Contains(2).Should().BeFalse();
            tree.GetKeysInOrder().SequenceEqual(new[] { 1, 4, 5, 7, 8, 9 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 5, 4, 1, 8, 7, 9 }).Should().BeTrue();
        }
예제 #23
0
        public void Remove_SimpleCases_TreeStillValid()
        {
            var tree = new RedBlackTree <int>();

            tree.AddRange(new[] { 13, 8, 17, 1, 11, 15, 25, 6, 22, 27 });
            tree.Remove(6);
            tree.Count.Should().Be(9);
            tree.Contains(6).Should().BeFalse();
            tree.GetKeysInOrder().SequenceEqual(new[] { 1, 8, 11, 13, 15, 17, 22, 25, 27 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 13, 8, 1, 11, 17, 15, 25, 22, 27 }).Should().BeTrue();

            tree = new RedBlackTree <int>();
            tree.AddRange(new[] { 13, 8, 17, 1, 11, 15, 25, 6, 22, 27 });
            tree.Remove(1);
            tree.Count.Should().Be(9);
            tree.Contains(1).Should().BeFalse();
            tree.GetKeysInOrder().SequenceEqual(new[] { 6, 8, 11, 13, 15, 17, 22, 25, 27 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 13, 8, 6, 11, 17, 15, 25, 22, 27 }).Should().BeTrue();

            tree = new RedBlackTree <int>();
            tree.AddRange(new[] { 13, 8, 17, 1, 11, 15, 25, 6, 22, 27 });
            tree.Remove(17);
            tree.Count.Should().Be(9);
            tree.Contains(17).Should().BeFalse();
            tree.GetKeysInOrder().SequenceEqual(new[] { 1, 6, 8, 11, 13, 15, 22, 25, 27 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 13, 8, 1, 6, 11, 22, 15, 25, 27 }).Should().BeTrue();

            tree = new RedBlackTree <int>();
            tree.AddRange(new[] { 13, 8, 17, 1, 11, 15, 25, 6, 22, 27 });
            tree.Remove(25);
            tree.Count.Should().Be(9);
            tree.Contains(25).Should().BeFalse();
            tree.GetKeysInOrder().SequenceEqual(new[] { 1, 6, 8, 11, 13, 15, 17, 22, 27 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 13, 8, 1, 6, 11, 17, 15, 27, 22 }).Should().BeTrue();

            tree = new RedBlackTree <int>();
            tree.AddRange(new[] { 7, 3, 18, 10, 22, 8, 11, 26 });
            tree.Remove(18);
            tree.Count.Should().Be(7);
            tree.Contains(18).Should().BeFalse();
            tree.GetKeysInOrder().SequenceEqual(new[] { 3, 7, 8, 10, 11, 22, 26 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 7, 3, 22, 10, 8, 11, 26 }).Should().BeTrue();

            tree = new RedBlackTree <int>();
            tree.Add(1);
            tree.Add(2);
            tree.Remove(1);
            tree.Count.Should().Be(1);
            tree.GetKeysInOrder().SequenceEqual(new[] { 2 }).Should().BeTrue();
            tree.GetKeysPreOrder().SequenceEqual(new[] { 2 }).Should().BeTrue();
        }
예제 #24
0
        public void Remove()
        {
            foreach (var itemToRemove in new[] { 6, 22, 27, 1, 11, 9, 15, 25, 8, 17, 13 })
            {
                var array = new[] { 13, 8, 17, 1, 11, 9, 15, 25, 6, 22, 27 };
                var tree  = new RedBlackTree <int>();
                tree.AddRange(array);
                var removed = tree.Remove(itemToRemove);

                Assert.True(removed);
                Assert.Equal(array.Where(x => x != itemToRemove).OrderBy(x => x).ToArray(), tree.InorderTraversal().ToArray());

                switch (itemToRemove)
                {
                case 6:
                {
                    var root = tree.RootNode;

                    // Left part of the tree
                    this.AssertNode(8, true, false, root.Left);
                    this.AssertNode(1, false, true, root.Left.Left);
                    this.AssertNode(11, false, false, root.Left.Right);
                    this.AssertNode(9, true, true, root.Left.Right.Left);

                    this.AssertNode(13, false, false, root);

                    // Right part of the tree
                    this.AssertNode(17, true, false, root.Right);
                    this.AssertNode(15, false, true, root.Right.Left);
                    this.AssertNode(25, false, false, root.Right.Right);
                    this.AssertNode(22, true, true, root.Right.Right.Left);
                    this.AssertNode(27, true, true, root.Right.Right.Right);
                }

                break;

                case 22:
                {
                    var root = tree.RootNode;

                    // Left part of the tree
                    this.AssertNode(8, true, false, root.Left);
                    this.AssertNode(1, false, false, root.Left.Left);
                    this.AssertNode(6, true, true, root.Left.Left.Right);
                    this.AssertNode(11, false, false, root.Left.Right);
                    this.AssertNode(9, true, true, root.Left.Right.Left);

                    this.AssertNode(13, false, false, root);

                    // Right part of the tree
                    this.AssertNode(17, true, false, root.Right);
                    this.AssertNode(15, false, true, root.Right.Left);
                    this.AssertNode(25, false, false, root.Right.Right);
                    this.AssertNode(27, true, true, root.Right.Right.Right);
                }

                break;

                case 27:
                {
                    var root = tree.RootNode;

                    // Left part of the tree
                    this.AssertNode(8, true, false, root.Left);
                    this.AssertNode(1, false, false, root.Left.Left);
                    this.AssertNode(6, true, true, root.Left.Left.Right);
                    this.AssertNode(11, false, false, root.Left.Right);
                    this.AssertNode(9, true, true, root.Left.Right.Left);

                    this.AssertNode(13, false, false, root);

                    // Right part of the tree
                    this.AssertNode(17, true, false, root.Right);
                    this.AssertNode(15, false, true, root.Right.Left);
                    this.AssertNode(25, false, false, root.Right.Right);
                    this.AssertNode(22, true, true, root.Right.Right.Left);
                }

                break;

                case 1:
                {
                    var root = tree.RootNode;

                    // Left part of the tree
                    this.AssertNode(8, true, false, root.Left);
                    this.AssertNode(6, false, true, root.Left.Left);
                    this.AssertNode(11, false, false, root.Left.Right);
                    this.AssertNode(9, true, true, root.Left.Right.Left);

                    this.AssertNode(13, false, false, root);

                    // Right part of the tree
                    this.AssertNode(17, true, false, root.Right);
                    this.AssertNode(15, false, true, root.Right.Left);
                    this.AssertNode(25, false, false, root.Right.Right);
                    this.AssertNode(22, true, true, root.Right.Right.Left);
                    this.AssertNode(27, true, true, root.Right.Right.Right);
                }

                break;

                case 11:
                {
                    var root = tree.RootNode;

                    // Left part of the tree
                    this.AssertNode(8, true, false, root.Left);
                    this.AssertNode(1, false, false, root.Left.Left);
                    this.AssertNode(6, true, true, root.Left.Left.Right);
                    this.AssertNode(9, false, true, root.Left.Right);

                    this.AssertNode(13, false, false, root);

                    // Right part of the tree
                    this.AssertNode(17, true, false, root.Right);
                    this.AssertNode(15, false, true, root.Right.Left);
                    this.AssertNode(25, false, false, root.Right.Right);
                    this.AssertNode(22, true, true, root.Right.Right.Left);
                    this.AssertNode(27, true, true, root.Right.Right.Right);
                }

                break;

                case 9:
                {
                    var root = tree.RootNode;

                    // Left part of the tree
                    this.AssertNode(8, true, false, root.Left);
                    this.AssertNode(1, false, false, root.Left.Left);
                    this.AssertNode(6, true, true, root.Left.Left.Right);
                    this.AssertNode(11, false, true, root.Left.Right);

                    this.AssertNode(13, false, false, root);

                    // Right part of the tree
                    this.AssertNode(17, true, false, root.Right);
                    this.AssertNode(15, false, true, root.Right.Left);
                    this.AssertNode(25, false, false, root.Right.Right);
                    this.AssertNode(22, true, true, root.Right.Right.Left);
                    this.AssertNode(27, true, true, root.Right.Right.Right);
                }

                break;

                case 15:
                {
                    var root = tree.RootNode;

                    // Left part of the tree
                    this.AssertNode(8, true, false, root.Left);
                    this.AssertNode(1, false, false, root.Left.Left);
                    this.AssertNode(6, true, true, root.Left.Left.Right);
                    this.AssertNode(11, false, false, root.Left.Right);
                    this.AssertNode(9, true, true, root.Left.Right.Left);

                    this.AssertNode(13, false, false, root);

                    // Right part of the tree
                    this.AssertNode(25, true, false, root.Right);
                    this.AssertNode(17, false, false, root.Right.Left);
                    this.AssertNode(22, true, true, root.Right.Left.Right);
                    this.AssertNode(27, false, true, root.Right.Right);
                }

                break;

                case 25:
                {
                    var root = tree.RootNode;

                    // Left part of the tree
                    this.AssertNode(8, true, false, root.Left);
                    this.AssertNode(1, false, false, root.Left.Left);
                    this.AssertNode(6, true, true, root.Left.Left.Right);
                    this.AssertNode(11, false, false, root.Left.Right);
                    this.AssertNode(9, true, true, root.Left.Right.Left);

                    this.AssertNode(13, false, false, root);

                    // Right part of the tree
                    this.AssertNode(17, true, false, root.Right);
                    this.AssertNode(15, false, true, root.Right.Left);
                    this.AssertNode(22, true, true, root.Right.Right.Left);
                    this.AssertNode(27, false, false, root.Right.Right);
                }

                break;

                case 8:
                {
                    var root = tree.RootNode;

                    // Left part of the tree
                    this.AssertNode(1, false, false, root.Left.Left);
                    this.AssertNode(6, true, true, root.Left.Left.Right);
                    this.AssertNode(11, false, true, root.Left.Right);
                    this.AssertNode(9, true, false, root.Left);

                    this.AssertNode(13, false, false, root);

                    // Right part of the tree
                    this.AssertNode(17, true, false, root.Right);
                    this.AssertNode(15, false, true, root.Right.Left);
                    this.AssertNode(25, false, false, root.Right.Right);
                    this.AssertNode(22, true, true, root.Right.Right.Left);
                    this.AssertNode(27, true, true, root.Right.Right.Right);
                }

                break;

                case 17:
                {
                    var root = tree.RootNode;

                    // Left part of the tree
                    this.AssertNode(8, true, false, root.Left);
                    this.AssertNode(1, false, false, root.Left.Left);
                    this.AssertNode(6, true, true, root.Left.Left.Right);
                    this.AssertNode(11, false, false, root.Left.Right);
                    this.AssertNode(9, true, true, root.Left.Right.Left);

                    this.AssertNode(13, false, false, root);

                    // Right part of the tree
                    this.AssertNode(15, false, true, root.Right.Left);
                    this.AssertNode(25, false, false, root.Right.Right);
                    this.AssertNode(22, true, false, root.Right);
                    this.AssertNode(27, true, true, root.Right.Right.Right);
                }

                break;

                case 13:
                {
                    var root = tree.RootNode;

                    // Left part of the tree
                    this.AssertNode(6, true, false, root.Left);
                    this.AssertNode(1, false, true, root.Left.Left);
                    this.AssertNode(8, false, false, root.Left.Right);
                    this.AssertNode(9, true, true, root.Left.Right.Right);

                    this.AssertNode(11, false, false, root);

                    // Right part of the tree
                    this.AssertNode(17, true, false, root.Right);
                    this.AssertNode(15, false, true, root.Right.Left);
                    this.AssertNode(25, false, false, root.Right.Right);
                    this.AssertNode(22, true, true, root.Right.Right.Left);
                    this.AssertNode(27, true, true, root.Right.Right.Right);
                }

                break;

                default:
                    Assert.True(false);
                    break;
                }
            }
        }