-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path72_binary_search_tree.c
More file actions
71 lines (61 loc) · 1.59 KB
/
Copy path72_binary_search_tree.c
File metadata and controls
71 lines (61 loc) · 1.59 KB
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
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* createNode(int data) {
struct node *n;
n = (struct node *)malloc(sizeof(struct node));
n->data = data;
n->left = NULL;
n->right = NULL;
return n;
}
void inorder(struct node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
int isBSTUtil(struct node* root, struct node** prev) {
if (root != NULL) {
if (!isBSTUtil(root->left, prev)) {
return 0;
}
// Check if the current node's data is less than or equal to the previous node's data
if (*prev != NULL && root->data <= (*prev)->data) {
return 0;
}
// Update the previous node to the current node
*prev = root;
return isBSTUtil(root->right, prev);
}
return 1;
}
int isBST(struct node* root) {
struct node* prev = NULL;
return isBSTUtil(root, &prev);
}
int main() {
struct node *p = createNode(5);
struct node *p1 = createNode(2);
struct node *p2 = createNode(10);
struct node *p3 = createNode(1);
struct node *p4 = createNode(4);
// Linking the root node with left and right children
p->left = p1;
p->right = p2;
p1->left = p3;
p1->right = p4;
printf("Inorder traversal of the tree: ");
inorder(p);
if (isBST(p)) {
printf("\nIt is a Binary Search Tree (BST).\n");
} else {
printf("\nIt is not a Binary Search Tree (BST).\n");
}
return 0;
}