コード例 #1
0
        public void Initialize()
        {
            var A = new MockBinaryTreeNode <int, string>(40, "str3");
            var B = new MockBinaryTreeNode <int, string>(20, "str1");
            var C = new MockBinaryTreeNode <int, string>(70, "str6");
            var D = new MockBinaryTreeNode <int, string>(50, "str4");
            var E = new MockBinaryTreeNode <int, string>(80, "str7");
            var F = new MockBinaryTreeNode <int, string>(30, "str2");
            var G = new MockBinaryTreeNode <int, string>(60, "str5");

            A.Parent     = null;
            A.LeftChild  = B;
            A.RightChild = C;

            B.Parent     = A;
            B.LeftChild  = null;
            B.RightChild = F;

            C.Parent     = A;
            C.LeftChild  = D;
            C.RightChild = E;

            D.Parent     = C;
            D.LeftChild  = null;
            D.RightChild = G;

            E.Parent     = C;
            E.LeftChild  = null;
            E.RightChild = null;

            F.Parent     = B;
            F.LeftChild  = null;
            F.RightChild = null;

            G.Parent     = D;
            G.LeftChild  = null;
            G.RightChild = null;

            _tree = new MockBinarySearchTreeBase <int, string>();
            _root = A;
        }
コード例 #2
0
        public void InsertBST_WithoutBalancing()
        {
            var keyVals = new Dictionary <int, string>
            {
                [40] = "str3",
                [20] = "str1",
                [70] = "str6",
                [50] = "str4",
                [80] = "str7",
                [30] = "str2",
                [60] = "str5",
            };

            var tree = new MockBinarySearchTreeBase <int, string>();
            MockBinaryTreeNode <int, string> root = null;

            root = tree.Insert_BST(root, new MockBinaryTreeNode <int, string>(40, "str3"));
            root = tree.Insert_BST(root, new MockBinaryTreeNode <int, string>(20, "str1"));
            root = tree.Insert_BST(root, new MockBinaryTreeNode <int, string>(70, "str6"));
            root = tree.Insert_BST(root, new MockBinaryTreeNode <int, string>(50, "str4"));
            root = tree.Insert_BST(root, new MockBinaryTreeNode <int, string>(80, "str7"));
            root = tree.Insert_BST(root, new MockBinaryTreeNode <int, string>(30, "str2"));
            root = tree.Insert_BST(root, new MockBinaryTreeNode <int, string>(60, "str5"));

            Assert.IsTrue(HasBinarySearchTreeOrderProperty <MockBinaryTreeNode <int, string>, int, string>(root));

            var nodes = new List <MockBinaryTreeNode <int, string> >();

            _tree.InOrderTraversal(root, nodes);
            Assert.AreEqual(7, nodes.Count);
            for (int i = 0; i < nodes.Count - 1; i++)
            {
                Assert.IsTrue(nodes[i].Key < nodes[i + 1].Key);
            }

            Assert.AreEqual(40, root.Key);
            Assert.AreEqual("str3", root.Value, ignoreCase: false);
        }
コード例 #3
0
        public void RotateLeft()
        {
            var A = new MockBinaryTreeNode <int, string>(50, "A");
            var B = new MockBinaryTreeNode <int, string>(30, "B");
            var C = new MockBinaryTreeNode <int, string>(20, "C");
            var D = new MockBinaryTreeNode <int, string>(40, "D");
            var E = new MockBinaryTreeNode <int, string>(35, "E");
            var F = new MockBinaryTreeNode <int, string>(45, "F");
            var G = new MockBinaryTreeNode <int, string>(47, "G");

            A.Parent     = null;
            A.LeftChild  = B;
            A.RightChild = null;

            B.Parent     = A;
            B.LeftChild  = C;
            B.RightChild = D;

            C.Parent     = B;
            C.LeftChild  = null;
            C.RightChild = null;

            D.Parent     = B;
            D.LeftChild  = E;
            D.RightChild = F;

            E.Parent     = D;
            E.LeftChild  = null;
            E.RightChild = null;

            F.Parent     = D;
            F.LeftChild  = null;
            F.RightChild = G;

            G.Parent     = F;
            G.LeftChild  = null;
            G.RightChild = null;

            Assert.IsTrue(HasBinarySearchTreeOrderProperty <MockBinaryTreeNode <int, string>, int, string>(A));
            var tree = new MockBinarySearchTreeBase <int, string>();

            tree.RotateLeft(B);
            Assert.IsTrue(HasBinarySearchTreeOrderProperty <MockBinaryTreeNode <int, string>, int, string>(A));

            Assert.IsTrue(A.Parent == null);
            Assert.IsTrue(A.LeftChild.Equals(D));
            Assert.IsTrue(A.RightChild == null);
            Assert.IsTrue(B.Parent.Equals(D));
            Assert.IsTrue(B.LeftChild.Equals(C));
            Assert.IsTrue(B.RightChild.Equals(E));
            Assert.IsTrue(C.Parent.Equals(B));
            Assert.IsTrue(C.LeftChild == null);
            Assert.IsTrue(C.RightChild == null);
            Assert.IsTrue(D.Parent.Equals(A));
            Assert.IsTrue(D.LeftChild.Equals(B));
            Assert.IsTrue(D.RightChild.Equals(F));
            Assert.IsTrue(E.Parent.Equals(B));
            Assert.IsTrue(E.LeftChild == null);
            Assert.IsTrue(E.RightChild == null);
            Assert.IsTrue(F.Parent.Equals(D));
            Assert.IsTrue(F.LeftChild == null);
            Assert.IsTrue(F.RightChild.Equals(G));
            Assert.IsTrue(G.Parent.Equals(F));
            Assert.IsTrue(G.LeftChild == null);
            Assert.IsTrue(G.RightChild == null);
        }