mockers
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Code to check if a tree is a BST

mockers :: DS

Go down

Code to check if a tree is a BST Empty Code to check if a tree is a BST

Post  ramthegreatcv Wed Aug 05, 2009 9:35 pm

Code:

#include<iostream>

using namespace std;

struct node{
   int data;
   node *left,*right;
};

bool isBSTrec(node* p,int min,int max){
   if (p==NULL) return true;
   if (p->data < min || p->data > max) return false ;
 
  return ( isBSTrec(p->left, min, p->data) && isBSTrec(p->right, p->data+1, max) );
}


bool isBST(node* p){
   return isBSTrec(p,0,1000);
}

int main(){
   node *root = new node;
   root->data = 100;   
   root->left = new node;
   root->left->data = 50;
   root->right = new node;
   root->right->data = 150;
   root->left->left = new node;
   root->left->left->data = 25;
   root->left->right = new node;
   root->left->right->data = 75;
   
   root->left->right->left = root->left->right->left = root->right->left = root->right->right = NULL;
   
   cout<<isBST(root);
}
ramthegreatcv
ramthegreatcv

Posts : 55
Join date : 2009-01-30
Age : 36

Back to top Go down

Back to top

- Similar topics

mockers :: DS

 
Permissions in this forum:
You cannot reply to topics in this forum