예제 #1
0
        //Verify the Height Property of each node AVLTree contains correct value.
        private static int TreeHeightVerification(AvlNode <int> node)
        {
            int h;

            if (node.IsLeafNode())
            {
                Assert.AreEqual(node.Height, 1);
                return(1);
            }

            if (node.HasOneChild())
            {
                if (node.Left != null)
                {
                    h = TreeHeightVerification(node.Left) + 1;
                }
                else
                {
                    h = TreeHeightVerification(node.Right) + 1;
                }
                Assert.AreEqual(node.Height, h);
                return(h);
            }

            h = Math.Max(TreeHeightVerification(node.Left), TreeHeightVerification(node.Right)) + 1;
            Assert.AreEqual(node.Height, h);
            return(h);
        }
예제 #2
0
        //Verify that every subtree at given node has balancing factor with range of -2(exclusive) to 2(exclusive)
        //This test relies on the Height property of AVL Tree, so it shall only be performed after the Height property
        //has been verified by TreeHeightVerification method to ensure the correctness of this test.
        private static void BalancingFactorVerification(AvlNode <int> node)
        {
            int  bf;
            bool res;

            if (node == null)
            {
                return;
            }
            if (node.IsLeafNode())
            {
                return;
            }
            if (node.HasOneChild())
            {
                if (node.Left != null)
                {
                    bf  = node.Left.Height - 0;
                    res = bf > -2 && bf < 2;
                    Assert.IsTrue(res);
                    BalancingFactorVerification(node.Left);
                    return;
                }

                bf  = 0 - node.Right.Height;
                res = bf > -2 && bf < 2;
                Assert.IsTrue(res);
                BalancingFactorVerification(node.Right);
                return;
            }

            bf  = node.Left.Height - node.Right.Height;
            res = bf > -2 && bf < 2;
            Assert.IsTrue(res);
            BalancingFactorVerification(node.Left);
            BalancingFactorVerification(node.Right);
        }