コード例 #1
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);
        }