Program To Check Whether A Binary Tree Is Binary search tree

A Binary Search Tree is a Binary tree in which all the nodes has following properties.

  • Left subtree of a node contains all the nodes having values lesser than the node.
  • Right subtree of a node contains all the nodes having values higher than the node.
  • Both the left and right subtree is also a Binary Search Tree.

As the name suggests, this data structure is mainly used for faster searching. In order to do that, restrictions are applied while inserting/deleting an element into the tree. As a result of these restrictions, worst case search time complexity remains at O(log n). Before going ahead have a look into Binary Search Tree basics, Binary Search Tree Insert, Binary Search Tree Delete, Binary Search Tree Search operations.

Let’s have a look into basic C++ class definition for Binary Search Tree.

template <typename T>
class BinarySearchTree
{
    public:
        T m_data;
        BinarySearchTree* m_left;
        BinarySearchTree* m_right;

        BinarySearchTree (T data);
        ~BinarySearchTree ();
};

/* Below are some useful functions which we are going to use it in our examples */
template <typename T>
void print_binary_tree_inorder (BinarySearchTree<T>* root)
{
    if (root -> m_left)
        print_binary_tree_inorder (root -> m_left);
    cout << " " << root -> m_data;
    if (root -> m_right)
        print_binary_tree_inorder (root -> m_right);
}

template <typename T>
void print_tree (BinarySearchTree<T>* root)
{
    cout << "Binary tree contents: ";
    print_binary_tree_inorder (root);
    cout << endl;
}

/* Simply used this function to delete all nodes and free memory */
template <typename T>
void delete_all_nodes (BinarySearchTree<T>* root)
{
    if (!root)
        return;
    if (root -> m_left)
    {
        delete_all_nodes (root -> m_left);
    }
    if (root -> m_right)
    {
        delete_all_nodes (root -> m_right);
    }
    delete root;
}

Checking a Binary Tree is Binary Search Tree or not

In Binary Search Tree data is stored in a particular manner due to which inorder traversal of Binary Search Tree will provide a sorted list. Based on the property of Binary Search Tree in which left node is lower than root node which is lower than right node, we can find out whether any Binary tree is Binary search tree or not.

Algorithm

  • Start traversing the tree from root.
  • If root data is outside the range (start with INT_MIN,INT_MAX) then return false.
  • Traverse to left child of root and check with range (received_min, root->data-1)
  • Traverse to right child of root and check with range (root->data+1, received_max)
  • If all nodes are traversed and above condition holds true for each of the node then Binary tree is BST.

Let’s take an example to understand it better.

In the above diagram, each node is higher than it’s left child and lower than it’s right child. Hence, above Binary Tree is a Binary Search Tree.

Let’s look into the sample code for this.

template <typename T>
bool isBstUtil (BinarySearchTree<T>* root, int min, int max)
{
    if (!root)
        return true;

    if (root -> m_data < min && root -> m_data > max)
        return false;

    return (isBstUtil (root->m_left, min, root -> m_data - 1) &&
            isBstUtil (root->m_right, root -> m_data + 1, max));
}

template <typename T>
bool isBstCorrect (BinarySearchTree<T>* root)
{
    return isBstUtil (root, INT_MIN, INT_MAX);
}

Few of the functions defined below are explained in Binary Search Tree Insert Operation Explanation and Binary Search Tree Delete Operation Explanation. Let’s look into the sample main function which utilizes Binary Search Tree class definition and functions defined above.

int main ()
{
    BinarySearchTree<int>* root = new BinarySearchTree<int> (33);
    insert_element_in_bst_recursive (20, root);
    insert_element_in_bst_recursive (10, root);
    insert_element_in_bst_recursive (30, root);
    insert_element_in_bst_recursive (40, root);
    insert_element_in_bst_recursive (50, root);
    insert_element_in_bst_recursive (43, root);
    insert_element_in_bst_recursive (77, root);
    insert_element_in_bst_recursive (69, root);
    insert_element_in_bst_recursive (25, root);
    insert_element_in_bst_recursive (11, root);
    insert_element_in_bst_recursive (10, root);
    insert_element_in_bst_recursive (5, root);
    insert_element_in_bst_recursive (18, root);
    insert_element_in_bst_recursive (67, root);
    insert_element_in_bst_recursive (88, root);
    insert_element_in_bst_recursive (99, root);
    insert_element_in_bst_recursive (65, root);
    insert_element_in_bst_recursive (58, root);
    insert_element_in_bst_recursive (51, root);
    insert_element_in_bst_recursive (77, root);
    print_tree (root);

    cout << "is above tree Binary Search Tree ? Answer -- " << isBst (root) << endl;

    BinarySearchTree<int>* parent = NULL;
    root = delete_element_in_bst_recursive (20, root, parent);
    print_tree (root);
    root = delete_element_in_bst_recursive (33, root, parent);
    print_tree (root);
    root = delete_element_in_bst_recursive (10, root, parent);
    print_tree (root);
    root = delete_element_in_bst_recursive (77, root, parent);
    print_tree (root);
    root = delete_element_in_bst_recursive (135, root, parent);
    print_tree (root);
    delete_all_nodes (root);
}

Let’s analyze the output of this main function.

Binary tree contents:  5 10 10 11 18 20 25 30 33 40 43 50 51 58 65 67 69 77 77 88 99
is above tree Binary Search Tree ? Answer -- 0
Data: 20 found in BST, node will be deleted now !!!
Binary tree contents:  5 10 10 11 18 25 30 33 40 43 50 51 58 65 67 69 77 77 88 99
Data: 33 found in BST, node will be deleted now !!!
Binary tree contents:  5 10 10 11 18 25 30 40 43 50 51 58 65 67 69 77 77 88 99
Data: 10 found in BST, node will be deleted now !!!
Binary tree contents:  5 10 11 18 25 30 40 43 50 51 58 65 67 69 77 77 88 99
Data: 77 found in BST, node will be deleted now !!!
Binary tree contents:  5 10 11 18 25 30 40 43 50 51 58 65 67 69 77 88 99
Data: 135 not found in BST !!!
Binary tree contents:  5 10 11 18 25 30 40 43 50 51 58 65 67 69 77 88 99

Leave a Reply

Your email address will not be published. Required fields are marked *