Nov 26, 2020 at 6:26am UTC
> cout << "here4" << endl;
When you get to here, also print
- min
- max
- t->data
Does it look like you're following a tree traversal?
> return CheckIfBST(t -> right, t -> data, max) && CheckIfBST(t -> left, min, t -> data);
It might be worth doing left then right, if only for sanity in your debug log
return CheckIfBST(t -> left, min, t -> data) && CheckIfBST(t -> right, t -> data, max);
Nov 26, 2020 at 11:59am UTC
Couple of points
1) You should always initialize variables when defined
2) Use nullptr rather than NULL in C++ code
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
#include <iostream>
#include <string>
#include <limits>
#include <sstream>
#include <vector>
// C++ implementation to check if the given array
// can represent Level Order Traversal of Binary
// Search Tree
using namespace std;
#include <iostream>
using namespace std;
class BST {
struct node {
int data {};
node* left {};
node* right {};
};
node* root {};
node* makeEmpty(node* t)
{
if (t != nullptr ) {
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
return nullptr ;
}
node* insert(int x, node* t)
{
if (t == nullptr ) {
t = new node {};
t->data = x;
t->left = t->right = nullptr ;
} else
if (x < t->data)
t->left = insert(x, t->left);
else
if (x > t->data)
t->right = insert(x, t->right);
return t;
}
node* findMin(node* t) const
{
if (t == nullptr || t->left == nullptr )
return t;
return findMin(t->left);
}
node* findMax(node* t) const
{
if (t == nullptr || t->right == nullptr )
return t;
return findMax(t->right);
}
void inorder(node* t) const
{
if (t != nullptr ) {
inorder(t->left);
cout << t->data << " " ;
inorder(t->right);
}
}
bool CheckIfBST(node* t, int min, int max) const
{
// Base case. An empty tree is a BST.
if (t == nullptr ) {
cout << "here1" << endl;
return true ;
}
// Checking if a key is outside the permitted range.
if (t->data < min) {
cout << "here2" << endl;
return false ;
}
if (t->data > max) {
cout << "here3" << endl;
return false ;
}
cout << "here4" << endl;
// Sending in updates ranges to the right and left subtree
return CheckIfBST(t->right, t->data, max) && CheckIfBST(t->left, min, t->data);
}
public :
BST() {}
~BST()
{
root = makeEmpty(root);
}
void insert(int x)
{
root = insert(x, root);
}
void display() const
{
inorder(root);
cout << endl;
}
int getMin() const
{
return findMin(root)->data;
}
int getMax() const
{
return findMax(root)->data;
}
bool isBst() const
{
constexpr int min {std::numeric_limits<int >::min()};
constexpr int max {std::numeric_limits<int >::max()};
const bool results {CheckIfBST(root, min, max)};
cout << "This is results for CheckIfBST: " << results << endl;
if (results == true )
cout << "Is BST" ;
else
cout << "Not a BST" ;
return results;
}
};
int main()
{
BST t;
string mystr;
int tmp; // A string to store the word on each iteration.
cout << "What are the numbers" << endl;
getline(cin, mystr);
stringstream str_strm(mystr);
vector<int > myvect;
while (str_strm >> tmp) {
myvect.push_back(tmp);
}
for (int i = 0; i < myvect.size(); i++) {
t.insert(myvect[i]);
}
//t.isBst();
//t.display();
// cout << t.getMin() << endl;
// cout << t.getMax() << endl;
cout << t.isBst() << endl;
return 0;
}
Last edited on Nov 26, 2020 at 12:07pm UTC