C Program to Check whether Binary Tree is Binary Search Tree or not

By | 05.05.2017

Check Binary Tree is Binary Search Tree or not


Write a C Program to Check whether a Binary Tree is Binary Search Tree or not. Here’s simple Program to Check if a Binary Tree is Binary Search Tree(BST) or not in C Programming Language.


What is Tree ?


In linear data structure, data is organized in sequential order and in non-linear data structure, data is organized in random order. Tree is a very popular data structure used in wide range of applications.

A tree data structure can be defined as follows…

  • Tree is a non-linear data structure which organizes data in hierarchical structure and this is a recursive definition.

A tree data structure can also be defined as follows…

  • Tree data structure is a collection of data (Node) which is organized in hierarchical structure and this is a recursive definition.

Every individual element is called as Node. Node in a tree data structure, stores the actual data of that particular element and link to next element in hierarchical structure.


Below is the source code for C Program to Check whether Binary Tree is Binary Search Tree or not which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* C Program to Check whether Binary Tree is Binary Search Tree or not */

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>

struct node
{
        struct node *lchild;
        int info;
        struct node *rchild;
};

void display(struct node *ptr,int level);
void inorder(struct node *ptr);
struct node *Node(int item);
int IsBST(struct node *ptr, int MIN, int MAX);

int main( )
{
        struct node *root1, *root2;

        root1 = Node(32);
        root1->lchild = Node(23);
        root1->rchild = Node(36);
        root1->lchild->rchild = Node(25);
        root1->rchild->lchild = Node(33);

        root2 = Node(42);
        root2->lchild = Node(60);
        root2->rchild = Node(19);
        root2->lchild->rchild = Node(36);
        root2->rchild->lchild = Node(41);

        display(root1,1);
        printf("\n\n");
        inorder(root1);
        printf("\n\n");

        if( IsBST(root1,INT_MIN,INT_MAX) )
                printf("\nTree 1 is a BST.............................\n");
        else
                printf("\nTree 1 is not a BST.............................\n");

        display(root2,1);
        printf("\n\n");
        inorder(root2);
        printf("\n\n");

        if( IsBST(root2,INT_MIN,INT_MAX) )
                printf("\nTree 2 is a BST.............................\n");
        else
                printf("\nTree 2 is not a BST.............................\n");
}/*End of main( )*/


int IsBST(struct node *ptr, int MIN, int MAX)
{
        if(ptr == NULL)
                return 1;
        if(ptr->info < MIN  ||  ptr->info > MAX)
                return 0;
        return ( IsBST(ptr->lchild,MIN,ptr->info) && IsBST(ptr->rchild,ptr->info,MAX));
}

struct node *Node(int item)
{
        struct node* tmp = (struct node *)malloc(sizeof(struct node));
        tmp->info = item;
        tmp->lchild = tmp->rchild = NULL;
        return tmp;
}/*End of Node()*/

void inorder(struct node *ptr)
{
        if(ptr == NULL )/*Base Case*/
                return;
        inorder(ptr->lchild);
        printf("%d  ",ptr->info);
        inorder(ptr->rchild);
}/*End of inorder( )*/

void display(struct node *ptr,int level)
{
        int i;
        if ( ptr!=NULL )
        {
                display(ptr->rchild, level+1);
                printf("\n");
                for (i = 0; i < level; i++)
                        printf("    ");
                printf("%d", ptr->info);
                display(ptr->lchild, level+1);
        }
}/*End of display()*/

OUTPUT : :


/* C Program to Check whether Binary Tree is Binary Search Tree or not */

        36
            33
    32
            25
        23

23  25  32  33  36


Tree 1 is a BST.............................

        19
            41
    42
            36
        60

60  36  42  41  19


Tree 2 is not a BST.............................

Process returned 0

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post…

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments