예제 #1
0
        public void Successor()
        {
            var n = new TreeNode(4)
            {
                Left = new TreeNode(2)
                {
                    Left  = new TreeNode(1),
                    Right = new TreeNode(3)
                },
                Right = new TreeNode(6)
                {
                    Left  = new TreeNode(5),
                    Right = new TreeNode(7)
                }
            };

            var result = TreesAndGraphs.Successor(n);

            Assert.AreEqual(result.Value, 5);

            result = TreesAndGraphs.Successor(n.Left.Right);
            Assert.AreEqual(result.Value, 4);

            result = TreesAndGraphs.Successor(n.Right.Right);
            Assert.AreEqual(result, null);

            result = TreesAndGraphs.Successor(n.Right);
            Assert.AreEqual(result.Value, 7);

            result = TreesAndGraphs.Successor(n.Left.Left);
            Assert.AreEqual(result.Value, 2);
        }
예제 #2
0
        public void TestSuccessor()
        {
            // Arrange

            // null node
            BSTPNode nullNode = null;

            // single node
            BSTPNode singleNode = new BSTPNode(1, null);

            // common case
            BSTPNode commonCaseRoot          = new BSTPNode(10, null);
            BSTPNode commonCaseLeft          = new BSTPNode(7, commonCaseRoot);
            BSTPNode commonCaseLeftRight     = new BSTPNode(9, commonCaseLeft);
            BSTPNode commonCaseLeftRightLeft = new BSTPNode(8, commonCaseLeftRight);
            BSTPNode commonCaseRight         = new BSTPNode(11, commonCaseRoot);

            commonCaseRoot.Left  = commonCaseLeft;
            commonCaseRoot.Right = commonCaseRight;

            commonCaseLeft.Right     = commonCaseLeftRight;
            commonCaseLeftRight.Left = commonCaseLeftRightLeft;

            // Act
            BSTPNode nullNodeResult    = TreesAndGraphs.Successor(nullNode);
            BSTPNode singleNodeResult  = TreesAndGraphs.Successor(singleNode);
            BSTPNode commonCaseResult1 = TreesAndGraphs.Successor(commonCaseLeft);
            BSTPNode commonCaseResult2 = TreesAndGraphs.Successor(commonCaseLeftRight);
            BSTPNode commonCaseResult3 = TreesAndGraphs.Successor(commonCaseRight);

            // Assert
            Assert.IsNull(nullNodeResult);
            Assert.IsNull(singleNodeResult);
            Assert.AreEqual(commonCaseLeftRightLeft, commonCaseResult1);
            Assert.AreEqual(commonCaseRoot, commonCaseResult2);
            Assert.IsNull(commonCaseResult3);
        }