-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
is_tree_a_bst.cc
52 lines (43 loc) · 1.57 KB
/
is_tree_a_bst.cc
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
#include <limits>
#include <memory>
#include "binary_tree_node.h"
#include "test_framework/generic_test.h"
using std::numeric_limits;
using std::unique_ptr;
bool AreKeysInRange(const unique_ptr<BinaryTreeNode<int>>&, int, int);
bool IsBinaryTreeBST(const unique_ptr<BinaryTreeNode<int>>& tree) {
return AreKeysInRange(tree, numeric_limits<int>::min(),
numeric_limits<int>::max());
}
bool AreKeysInRange(const unique_ptr<BinaryTreeNode<int>>& tree, int low_range,
int high_range) {
if (tree == nullptr) {
return true;
} else if (tree->data < low_range || tree->data > high_range) {
return false;
}
return AreKeysInRange(tree->left, low_range, tree->data) &&
AreKeysInRange(tree->right, tree->data, high_range);
}
bool InorderTraversal(const unique_ptr<BinaryTreeNode<int>>& tree,
BinaryTreeNode<int>** prev) {
if (!tree) {
return true;
} else if (!InorderTraversal(tree->left, prev)) {
return false;
} else if (*prev && (*prev)->data > tree->data) {
return false;
}
*prev = tree.get();
return InorderTraversal(tree->right, prev);
}
bool IsBinaryTreeBSTAlternative(const unique_ptr<BinaryTreeNode<int>>& tree) {
BinaryTreeNode<int>* prev = nullptr;
return InorderTraversal(tree, &prev);
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"tree"};
return GenericTestMain(args, "is_tree_a_bst.cc", "is_tree_a_bst.tsv",
&IsBinaryTreeBST, DefaultComparator{}, param_names);
}