-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary_Search_Trees.cpp
271 lines (248 loc) · 6.27 KB
/
Binary_Search_Trees.cpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <stdio.h>
#include <stdlib.h>
struct Node{
struct Node *lchild;
int data;
struct Node *rchild;
};
struct Queue{
int size;
int front;
int rear;
struct Node **A;
};
// Simple Queue for storing Tree Node addresses. (Required for tree implementation)
struct Queue* CreateQueue(int size);
void enqueue(struct Queue *q, struct Node *s);
struct Node* dequeue(struct Queue *q);
int isEmpty(struct Queue *q);
// Binary Trees implementation and required Functions.
struct Node* createTree();
void PreOrder(struct Node *r);
void InOrder(struct Node *r);
void PostOrder(struct Node *r);
int Height(struct Node *r);
int Count(struct Node *r);
void DisplayTree(struct Node *r);
// Specific Functions for Binary Search Trees (BST):-
struct Node* Search_in_BST(struct Node *r, int key);
void Insert_in_BST(struct Node *r, int x);
struct Node* InOrder_Predecessor(struct Node *p);
struct Node* InOrder_Successor(struct Node *p);
struct Node* Delete_from_BST(struct Node **r, int x);
// NOTE: While inputting values into BST, always follow the BST principle and don't add duplicates.
int main(){
struct Node *root = createTree();
struct Node *x;
x = Search_in_BST(root, 3);
x!=NULL?printf("Key Found!\n"):printf("Key Not Found!\n");
Insert_in_BST(root, 2);
Insert_in_BST(root, 60);
Delete_from_BST(&root, 70);
//Delete_from_BST(&root, 50);
//Check Later .. Might be a problem while deleting Root value.
DisplayTree(root);
return 0;
}
// Queue Functions:-
struct Queue* CreateQueue(int size){
struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = -1;
q->rear = -1;
q->size = size;
q->A = (struct Node**)malloc(q->size*sizeof(struct Node*));
return q;
}
void enqueue(struct Queue *q, struct Node *s){
if(q->rear == q->size-1){
printf("OverFlow\n");
}
else{
q->rear++;
q->A[q->rear] = s;
}
}
struct Node* dequeue(struct Queue *q){
struct Node *t = NULL;
if(q->front == q->rear+1){
printf("UnderFlow\n");
return t;
}
else{
q->front++;
t = q->A[q->front];
return t;
}
}
int isEmpty(struct Queue *q){
if(q->front >= q->rear || (q->front == -1 && q->rear == -1)){
return 1;
}
return 0;
}
// Binary Tree Functions:-
struct Node* createTree(){
struct Queue *q = CreateQueue(100);
struct Node *r = (struct Node*)malloc(sizeof(struct Node));
int x;
printf("Enter the Root Data: ");
scanf("%d", &x);
r->lchild = r->rchild = NULL;
r->data = x;
enqueue(q, r);
while(!isEmpty(q)){
struct Node *p = dequeue(q);
printf("Enter left child of %d: ", p->data);
scanf("%d", &x);
if(x != -1){
struct Node *t = (struct Node*)malloc(sizeof(struct Node));
t->lchild = t->rchild = NULL;
t->data = x;
p->lchild = t;
enqueue(q, t);
}
printf("Enter right child of %d: ", p->data);
scanf("%d", &x);
if(x != -1){
struct Node *t = (struct Node*)malloc(sizeof(struct Node));
t->lchild = t->rchild = NULL;
t->data = x;
p->rchild = t;
enqueue(q, t);
}
}
return r;
}
void PreOrder(struct Node *r){
if(r){
printf("%d ", r->data);
PreOrder(r->lchild);
PreOrder(r->rchild);
}
}
void InOrder(struct Node *r){
if(r){
InOrder(r->lchild);
printf("%d ", r->data);
InOrder(r->rchild);
}
}
void PostOrder(struct Node *r){
if(r){
PostOrder(r->lchild);
PostOrder(r->rchild);
printf("%d ", r->data);
}
}
int Height(struct Node *r){
int x = 0, y = 0;
if(r == NULL){
return 0;
}
x = Height(r->lchild);
y = Height(r->rchild);
return x>y?x+1:y+1;
}
int Count(struct Node *r){
int x, y;
if(r){
x = Count(r->lchild);
y = Count(r->rchild);
return x+y+1;
}
else{
return 0;
}
}
void DisplayTree(struct Node *r){
printf("PreOrder Traversal: ");PreOrder(r);printf("\n");
printf("InOrder Traversal: ");InOrder(r);printf("\n");
printf("PostOrder Traversal: ");PostOrder(r);printf("\n");
printf("Count of Nodes in the Tree is: %d\n", Count(r));
printf("Height of the Tree is: %d\n", Height(r));
}
// BST Specific Functions:-
struct Node* Search_in_BST(struct Node *r, int key){
while(r){
if(r->data == key){
return r;
}
else if(key < r->data){
r = r->lchild;
}
else{
r = r->rchild;
}
}
return NULL;
}
void Insert_in_BST(struct Node *r, int x){
struct Node *p = r;
struct Node *q = NULL;
while(p != NULL){
q = p;
if(p->data == x){
printf("No Duplicates in BST..");
return;
}
else if(x < p->data){
p = p->lchild;
}
else{
p = p->rchild;
}
}
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = x;
temp->lchild = temp->rchild = NULL;
if(temp->data < q->data){
q->lchild = temp;
}
else{
q->rchild = temp;
}
}
struct Node *InOrder_Predecessor(struct Node *p){
while(p && p->rchild != NULL){
p = p->rchild;
}
return p;
}
struct Node *InOrder_Successor(struct Node *p){
while(p && p->lchild != NULL){
p = p->lchild;
}
return p;
}
struct Node* Delete_from_BST(struct Node **r, int x){
struct Node *p = (*r);
if(p == NULL){
return NULL;
}
if(p->lchild == NULL && p->rchild == NULL){
if(p == (*r)){
(*r) = NULL;
}
free(p);
return NULL;
}
if(x < p->data){
p = Delete_from_BST(&p->lchild, x);
}
else if(x > p->data){
p = Delete_from_BST(&p->rchild, x);
}
else{
if(Height(p->lchild) > Height(p->rchild)){
struct Node *q = InOrder_Predecessor(p->lchild);
p->data = q->data;
p->lchild = Delete_from_BST(&p->lchild, q->data);
}
else{
struct Node *q = InOrder_Successor(p->rchild);
p->data = q->data;
p->lchild = Delete_from_BST(&p->rchild, q->data);
}
}
return p;
}