Thursday, 23 November 2017

C++ Program To Check Whether a Given Tree is Binary Search Tree


Code:

#include    stdio.h
#include    stdlib.h
#include    limits.h
#include    iostream
using namespace std;
struct node
{
    int data;
    struct node* left;
    struct node* right;
}*node1 = NULL;
int isBSTUtil(struct node* node, int min, int max) 

    if (node==NULL) 
        return 1;      
    if (node->data < min || node->data > max) 
        return 0; 
    return isBSTUtil(node->left, min, node->data-1) && isBSTUtil(node->right, node->data+1, max); 

int isBST(struct node* node) 

    return(isBSTUtil(node, INT_MIN, INT_MAX)); 

struct node* newNode(int data)
{
    node1 = new node;
    node1->data = data;
    node1->left = NULL;
    node1->right = NULL;
    return(node);
}
int main()
{
    struct node *root = newNode(4);
    root->left = newNode(2);
    root->right = newNode(5);
    root->left->left = newNode(1);
    root->left->right = newNode(3); 

    if(isBST(root))
        cout<<"Is BST";
    else
        cout<<"Not a BST";   
    getch();
}


Output:

Is BST



More C++ Programs:
















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...