Write an isBST() function that returns true if a tree is a binary search tree
and false otherwise.
/*
* Is BST helper function
*/
int isBSTUtil(nodeptr node, int min, int max)
{
if(node != NULL)
return (node->data >= min && node->data <=max) &&
isBSTUtil(node->left, min, node->data) &&
isBSTUtil(node->right, node->data, max);
return 1;
}
/*
* determine given binary tree is BST
*/
int isBST(nodeptr node)
{
int min,max;
if (node != NULL) {
min = minValue(node);
max = maxValue(node);
return isBSTUtil(node, min, max);
}
return 1;
}
and false otherwise.
/*
* Is BST helper function
*/
int isBSTUtil(nodeptr node, int min, int max)
{
if(node != NULL)
return (node->data >= min && node->data <=max) &&
isBSTUtil(node->left, min, node->data) &&
isBSTUtil(node->right, node->data, max);
return 1;
}
/*
* determine given binary tree is BST
*/
int isBST(nodeptr node)
{
int min,max;
if (node != NULL) {
min = minValue(node);
max = maxValue(node);
return isBSTUtil(node, min, max);
}
return 1;
}
No comments:
Post a Comment