-
Notifications
You must be signed in to change notification settings - Fork 8
/
is_bst.c
71 lines (65 loc) · 1.61 KB
/
is_bst.c
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
/* ----------------------------------------------------------------------- *
*
* Author: Leandro Augusto Lacerda Campos <llacerdacampos@gmail.com>
*
* Data Structures and Algorithms Specialization,
* by University of California, San Diego,
* and National Research University Higher School of Economics
*
* Course 2: Data Structures
*
* Solution for Is It a Binary Search Tree? Problem
*
* ----------------------------------------------------------------------- */
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#define MAXLEN 100000
#define MAXVAL INT_MAX
#define MINVAL INT_MIN
int key[MAXLEN] = {0};
int left[MAXLEN] = {-1};
int right[MAXLEN] = {-1};
int sm[MAXLEN] = {MAXVAL};
int lg[MAXLEN] = {MINVAL};
/*
* is_bst: tests whether a binary search tree data structure was implemented
* correctly.
*/
bool is_bst(int idx)
{
if (idx == -1)
return true;
if (left[idx] == -1) {
sm[idx] = key[idx];
} else {
if (!is_bst(left[idx]))
return false;
if (key[idx] <= lg[left[idx]])
return false;
sm[idx] = sm[left[idx]];
}
if (right[idx] == -1) {
lg[idx] = key[idx];
} else {
if (!is_bst(right[idx]))
return false;
if (key[idx] >= sm[right[idx]])
return false;
lg[idx] = lg[right[idx]];
}
return true;
}
int main()
{
int i, n;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d %d %d", &key[i], &left[i], &right[i]);
}
if (is_bst(0))
printf("CORRECT\n");
else
printf("INCORRECT\n");
return 0;
}