📜  门| GATE-CS-2014-(Set-1) |问题 6(1)

📅  最后修改于: 2023-12-03 15:12:42.083000             🧑  作者: Mango

GATE-CS-2014-(Set-1) | Question 6

This problem is related to binary search trees (BSTs) and their properties. Given a BST, you need to write a function to check whether it is balanced or not.

Definition of a balanced binary tree

A binary tree is called balanced if the height of its two subtrees differ by at most one, and both of its subtrees are also balanced. In other words, a tree is balanced only if the difference between the left and right subtree is not more than 1 and the left and right subtrees are also a balanced binary tree.

Approach to checking whether a given BST is balanced or not

To check whether a given BST is balanced or not, we can use a recursive algorithm. We start at the root node of the tree, and for each node we check whether its left and right subtrees are balanced or not. We also compute the height of the node's left and right subtrees, and check whether the difference between them is not more than 1. If all these conditions hold for the root node, then we proceed to check the same properties for its left and right subtrees. If any of the conditions fail for a node and its subtrees, then the tree is not a balanced BST.

Here is the pseudocode for the recursive algorithm:

# is_balanced_bst: Returns True if the input BST is balanced, False otherwise
# Input: A root node of a BST
def is_balanced_bst(root_node):
    # Base case: An empty tree is always balanced
    if root_node == None:
        return True
    
    # Check whether the left subtree is balanced or not
    left_subtree_balanced = is_balanced_bst(root_node.left_child)
    if not left_subtree_balanced:
        return False
    
    # Check whether the right subtree is balanced or not
    right_subtree_balanced = is_balanced_bst(root_node.right_child)
    if not right_subtree_balanced:
        return False
    
    # Check whether the difference between the heights of the left and right subtrees is not more than 1
    left_subtree_height = get_height(root_node.left_child)
    right_subtree_height = get_height(root_node.right_child)
    if abs(left_subtree_height - right_subtree_height) > 1:
        return False
    
    # If all conditions hold, then the BST is balanced
    return True
  
# get_height: Returns the height of a given node in a BST
# Input: A node of a BST
def get_height(node):
    if node == None:
        return 0
    
    left_subtree_height = get_height(node.left_child)
    right_subtree_height = get_height(node.right_child)
    
    return max(left_subtree_height, right_subtree_height) + 1
Complexity analysis

The time complexity of the is_balanced_bst function is O(n), where n is the number of nodes in the input BST. This is because we visit each node in the BST exactly once, and each visit takes O(1) time. The space complexity of the function is also O(n), because in the worst case the call stack can grow as large as the height of the tree, which can be n in the worst case.