-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisItBST.cpp
79 lines (70 loc) · 1.53 KB
/
isItBST.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
#include<climits>
using namespace std;
class Node{
public:
int data;
Node * left;
Node * right;
Node(int val): data(val),left(NULL), right(NULL){};
Node (): data(0), left(NULL), right(NULL){};
};
class Tree{
public:
Node * root;
Tree(): root(NULL){};
};
void buildTree(int * arr, int size, Node * nodeArr){
for(int i=0; i<size; i++){
if(arr[i]!=0){
if(i==0){
nodeArr[0].data= arr[i];
}
else{
nodeArr[i].data = arr[i];
int parentPos= (i-1)/2;
//Node parent = nodeArr[parentPos];
if(i%2 == 1)
nodeArr[parentPos].left=&nodeArr[i];
else
nodeArr[parentPos].right=&nodeArr[i];
}
}
}
return ;
}
void InOrder( Node * node){
if(node== NULL) return;
InOrder(node->left);
cout << node->data << " ";
InOrder(node->right);
}
bool isBST(Node * node, int low, int high){
if(!node) return true;
if((node->data > low) && (node->data < high))
return (isBST(node->left, low, node->data) &&
isBST(node->right, node->data, high));
else
return false;
}
int main(){
int n;
int num;
cin>> n;
int inputArr[n];
for (int i =0; i<n; i++){
cin>> num;
inputArr[i]=num;
}
Node root[n];
buildTree(inputArr, n, root);
if(isBST(root, INT_MIN, INT_MAX))
cout << " Tree is BST \n";
else
cout << "Tree is NOT BST \n";
/********************************/
cout << "In Order traversal\n";
InOrder(root);
/*******************************/
return 0;
}