-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy path15_checkIf_BinarySearchTree.c
108 lines (94 loc) · 2.83 KB
/
15_checkIf_BinarySearchTree.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
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
// Check if a given binary search tree is a binary search tree or not
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
struct Node
{
int data;
struct Node *left;
struct Node *right;
};
// ===== Method 1 - using auxillary array =====
// This method is based on the idea that inorder travsersal of a BST will always give elements in sorted order
// So it is enough to check if the array generated on inorder traversal is sorted or not
// == Time complexity: O(n), Space complexity: O(n) ==
int inorderArray[100];
int idx = 0;
void generateInorderArray(struct Node *root)
{
if (root == NULL)
return;
generateInorderArray(root->left);
inorderArray[idx++] = root->data;
generateInorderArray(root->right);
}
bool isBST_inorder(struct Node *root)
{
generateInorderArray(root); // generate array
for (int i = 1; i < idx; i++) // check if it is sorted
if (inorderArray[i] < inorderArray[i - 1])
return false;
return true;
}
// The above method can be further optimized for space. Instead of storing the entire array,
// we can keep track of the previous node and compare currrent and previous node at each stage
// ==== Method 2 - Using range of each node ======
// Each node in a BST will have a specific range based on its ancestors
// So we check if all nodes fall within this range
// == Time complexity: O(n), Space complexity: O(1) ==
bool isBST(struct Node *root, int min, int max)
{
if (root == NULL)
return true;
if (root->data < min || root->data > max)
return false;
return isBST(root->left, min, root->data - 1) && isBST(root->right, root->data + 1, max);
}
struct Node *createNode(int);
struct Node *insertLeft(struct Node *, int);
struct Node *insertRight(struct Node *, int);
int main()
{
struct Node *root = NULL;
root = createNode(10);
insertLeft(root, 5);
insertRight(root, 15);
insertLeft(root->left, 2);
insertRight(root->left, 7);
insertLeft(root->right, 12);
insertRight(root->right, 18);
// For reference
// 10
// / \
// 5 15
// / \ / \
// 2 7 12 18
if (isBST(root, INT_MIN, INT_MAX))
printf("\nGiven tree is a BST");
else
printf("\nNope");
// if (isBST_inorder(root))
// printf("\nGiven tree is a BST");
// else
// printf("\nNope");
return 0;
}
struct Node *createNode(int data)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct Node *insertLeft(struct Node *root, int data)
{
root->left = createNode(data);
return root->left;
}
struct Node *insertRight(struct Node *root, int data)
{
root->right = createNode(data);
return root->right;
}