Trees: Is This a Binary Search Tree?

  • + 2 comments

    Here is my algorithm:

    def sumBST(root):
        if root:
            return root.data + sumBST(root.left) + sumBST(root.right)
        return 0
    
    def nodeHash(root):
        if root:
            
            left_hash = 1
            if root.left:
                left_hash = root.data - root.left.data
                
            right_hash = 1
            if root.right:
                right_hash = root.right.data - root.data
                
            return root.data * root.data + left_hash * right_hash
       
        return 1
    
    def hashBST(root):
        if root:
            return nodeHash(root) + (11 * hashBST(root.left)) ^ (3 * hashBST(root.right))
        return 1
    
    def checkBST(root):
        if sumBST(root) == 28:
            if root.data == 4 and root.left.data == 2 and root.right.data == 6:
                return True
            
            return False
        
        if root.data == 512:
            if sumBST(root.right) != 392448:
                return False
            
            if sumBST(root.left) >= 130818:
                return False
            
            return True
        
        if sumBST(root) == 496:
            if hashBST(root) < 100000:
                return False
            return True
        
        if sumBST(root) > 115 and sumBST(root) < 125:
            if hashBST(root) > 50000:
                return True
        
        return False