Ejemplo n.º 1
0
        //O(log(n)) worst O(n) for unbalanced tree
        internal static int GetHeight <T>(this BSTNodeBase <T> node) where T : IComparable
        {
            if (node == null)
            {
                return(-1);
            }

            return(Math.Max(GetHeight(node.Left), GetHeight(node.Right)) + 1);
        }
Ejemplo n.º 2
0
        public static int VerifyCount <T>(this BSTNodeBase <T> node) where T : IComparable
        {
            if (node == null)
            {
                return(0);
            }

            var count = VerifyCount(node.Left) + VerifyCount(node.Right) + 1;

            Assert.AreEqual(count, node.Count);

            return(count);
        }
Ejemplo n.º 3
0
        public static bool VerifyIsBinarySearchTree(BSTNodeBase <T> node, T lowerBound, T upperBound)
        {
            if (node == null)
            {
                return(true);
            }

            if (node.Value.CompareTo(upperBound) >= 0 || node.Value.CompareTo(lowerBound) <= 0)
            {
                return(false);
            }

            return(VerifyIsBinarySearchTree(node.Left, lowerBound, node.Value) &&
                   VerifyIsBinarySearchTree(node.Right, node.Value, upperBound));
        }
Ejemplo n.º 4
0
        internal static bool IsBinarySearchTree <T>(this BSTNodeBase <T> node, T lowerBound, T upperBound) where T : IComparable
        {
            if (node == null)
            {
                return(true);
            }

            if (node.Value.CompareTo(upperBound) >= 0 || node.Value.CompareTo(lowerBound) <= 0)
            {
                return(false);
            }

            return(IsBinarySearchTree(node.Left, lowerBound, node.Value) &&
                   IsBinarySearchTree(node.Right, node.Value, upperBound));
        }