forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindBinaryTreeBst.c
167 lines (138 loc) · 3.16 KB
/
FindBinaryTreeBst.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
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
/* C Program to find whether a Binary Tree is a Binary Search Tree.
This problem is solved using In-order traversal.We start with root node,
and during the traversal , we compare the previous element with the current
element and during that if we found current is greater than previous ,we
conclude that it is not a Binary Search Tree*/
#include <malloc.h>
#include <stdio.h>
// structure of the Node
struct Node {
int data;
struct Node *left;
struct Node *right;
};
// structure of the queue
struct Queue {
struct Node *data;
struct Queue *next;
};
// check whether Queue is empty or not
int isEmpty(struct Queue *q) {
if (q == NULL) {
return 1;
}
return 0;
}
// enqueue the element at the rear end
int enQueue(struct Queue **f, struct Queue **r, struct Node *data) {
struct Queue *q;
/*create new node*/
q = (struct Queue *)malloc(sizeof(struct Queue));
q->data = data;
q->next = NULL;
// If queue is empty
if (*f == NULL) {
*f = q;
} else {
(*r)->next = q;
}
*r = q;
return 0;
}
// dequeue the element from the front end
struct Node *deQueue(struct Queue **f) {
struct Node *temp;
if (isEmpty(*f)) {
printf("\n Queue is empty");
} else {
temp = (*f)->data;
(*f) = (*f)->next;
}
return temp;
}
// insertion in Binary Tree
int insertBinaryTree(struct Node **root, int data) {
struct Node *temp, *cur;
struct Queue *f = NULL, *r = NULL;
// new node creation based on data
temp = (struct Node *)malloc(sizeof(struct Node));
if (temp == NULL) {
return -1;
} else {
temp->data = data;
temp->left = NULL;
temp->right = NULL;
}
if (*root == NULL) {
*root = temp;
return;
}
// queue used to insert data to the binary tree
enQueue(&f, &r, *root);
while (!isEmpty(f)) {
cur = deQueue(&f);
// move left
if (cur->left == NULL) {
cur->left = temp;
break;
} else {
enQueue(&f, &r, cur->left);
}
if (cur->right == NULL) {
cur->right = temp;
break;
} else {
enQueue(&f, &r, cur->right);
}
}
return 0;
}
// check whether Binary Tree is Binary Search Tree
int curElement = 0, flag = 1;
int checkBST(struct Node *root) {
if (root == NULL) {
return;
}
checkBST(root->left);
if (curElement != 0 && (root->data) <= curElement) {
flag = 0;
}
// store the previous element
curElement = root->data;
checkBST(root->right);
return flag;
}
int main() {
int n, i, value, status;
struct Node *root = NULL;
printf("Enter the no of elements in the Binary Tree:");
scanf("%d", &n);
printf("\nEnter the values of the Binary Tree:\n");
for (i = 0; i < n; i++) {
scanf("%d", &value);
insertBinaryTree(&root, value);
}
status = checkBST(root);
if (status) {
printf("\nThe Entered Binary Tree is a Binary Search Tree:");
} else {
printf("\nThe Entered Binary Tree is Not a Binary Search Tree");
}
return 0;
}
/*
Sample input/output:
Enter the no of elements in the Binary Tree:8
Enter the values of the Binary Tree:
204
198
207
99
199
205
251
4
The Entered Binary Tree is a Binary Search Tree
Time complexity: O(n)
Space complexity: O(n)
*/