This C++ program checks whether the nodes entered form a binary search tree, a tree consisting of only two child nodes.

Here is the source code of the C++ program to display whether the nodes given as input give a binary search tree as output or not. This C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is given below.

`/*`

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

`*/`

`#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

**Sanfoundry Global Education & Learning Series – 1000 C++ Programs.**

If you wish to look at all C++ Programming examples, go to C++ Programs.