-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.c
374 lines (342 loc) · 9.51 KB
/
sensor.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "declrns.h"
extern Boolean_type taller_sensor;
sensor_node *RotateLeft(sensor_node *p)
{
sensor_node *temp = p;
if (p == NULL)
printf("Cannot rotate an empty tree");
else if (p->right == NULL)
printf("Cannot rotate left") ;
else
{
temp = p->right;
p->right = temp->left;
temp->left = p;
}
return temp;
}
sensor_node *RotateRight(sensor_node *p)
{
sensor_node *temp = p;
if (p == NULL)
printf("Cannot rotate an empty tree");
else if (p->left == NULL)
printf("Cannot rotate right") ;
else
{
temp = p->left;
p->left = temp->right;
temp->right = p;
}
return temp;
}
sensor_node *RightBalance (sensor_node *root, Boolean_type *taller)
{
sensor_node *rs = root->right;
sensor_node * ls;
switch (rs->bf)
{
case RH:
root->bf = rs->bf = EH ;
root = RotateLeft(root);
* taller = FALSE;
break;
case EH:
printf( "Tree is already balanced" );
break;
case LH:
ls = rs ->left;
switch ( ls->bf)
{
case RH:
root->bf = LH ;
rs->bf = EH;
break;
case EH:
root ->bf = rs->bf = EH;
break;
case LH:
root->bf = EH;
rs->bf = RH;
break;
}
ls->bf = EH;
root ->right = RotateRight(rs) ;
root = RotateLeft (root) ;
* taller = FALSE;
break;
}
return root;
}
sensor_node *LeftBalance (sensor_node *root, Boolean_type *taller)
{
sensor_node *rs ;
sensor_node * ls= root->left;
switch (ls->bf)
{
case RH:
root->bf = ls->bf = EH ;
root = RotateLeft(root);
* taller = FALSE;
break;
case EH:
printf( "Tree is already balanced" );
break;
case LH:
rs = ls ->left;
switch ( rs->bf)
{
case RH:
root->bf = LH ;
ls->bf = EH;
break;
case EH:
root ->bf = ls->bf = EH;
break;
case LH:
root->bf = EH;
ls->bf = RH;
break;
}
rs->bf = EH;
root ->left = RotateLeft(ls) ;
root = RotateRight (root) ;
* taller = FALSE;
break;
}
return root;
}
sensor_node *Insert(sensor_node *root, sensor_node *newnode, Boolean_type *taller) //Insert new node starting at root
{
if(!root)
{
root = newnode;
root->bf = EH;
root->left = root->right = NULL;
*taller = TRUE;
}
else if(newnode->ID == root->ID)
printf("Duplicate ID in binary tree");
else if (newnode->ID < root->ID) //"Key" is ID, assumed station is also input so
{
root->left = Insert (root->left, newnode, taller);
if ( *taller)
switch (root->bf)
{
case LH:
root = LeftBalance ( root, taller);
break;
case EH:
root->bf = LH;
break;
case RH:
root->bf = EH;
* taller = FALSE;
break;
}
}else
{
root->right = Insert (root->right, newnode, taller);
if ( *taller)
switch (root->bf)
{
case LH:
root->bf = EH;
*taller = FALSE;
break;
case EH:
root->bf = RH;
break;
case RH:
root = RightBalance ( root, taller) ;
break;
}
}
return root;
}
sensor_node *GetNode() //get a new node with data for BT
{
sensor_node *p = NULL;
p =(sensor_node*)malloc(sizeof(sensor_node));
printf("\n ID (Any natural number): "); scanf("%d", &p->ID); getchar();
printf("\n Type (temperature, sound, humidity, wind, etc.): "); gets(p->sensor_type);
printf("\n Station/Location: (Single character: A, B, etc.) "); scanf("%c", &p->sensor_station);
printf("\n Time Interval: ");
p->interval=ip_time(p->interval);
p->left = p->right = NULL;
return p;
}
sensor_node* create_sensor_node(sensor_node *root, Boolean_type *taller) //makes a BST and returns the root
{
sensor_node *p;
p = GetNode();
if(p!= NULL) //if malloc doesn't fail
root = Insert(root, p, taller);
return root;
}
int TreeSearch_stn(sensor_node *x, char stn, char* type) //Search - assume that station, like ID is stored in order.
{
sensor_node* p = x;
while (p && (stn != p->sensor_station)) //finds first record of req station
if (stn < p->sensor_station)
p = p->left;
else
p = p->right;
int fnd =0;
while(!fnd && p!=NULL && stn==p->sensor_station)
if(p->sensor_type==type)
fnd = 1;
else
p=p->right; //same stations can only be to the right
if(!fnd)
printf("Sensor type %s isn't at sensor station %c.", type, stn);
return fnd;
}
sensor_node* Install_new_Sensor(sensor_node *root, Boolean_type *taller) //makes a BST and returns the root
{
sensor_node *p;
p = GetNode();
if(p!= NULL) //if malloc doesn't fail
if(!TreeSearch_stn(p, p->sensor_station, p->sensor_type))
{
root = Insert(root, p, taller);
printf("New sensor installed.");
}
else
printf("The station already has this type.");
else
printf("All stations are in full capacity.");
return root;
}
void print_snsr(sensor_node* p)
{
printf("\n\nSensor ID no. %d info: ", p->ID);
printf("\n Type : %s", p->sensor_type);
printf("\n Station/Location: %c", p->sensor_station);
printf("\n Time Interval: ");
op_time(p->interval);
}
void Inorder(sensor_node *root, int l_ID, int u_ID) //Traversal
{
if (root)
{
Inorder ( root->left, l_ID, u_ID) ;
if(root->ID>=l_ID && root->ID<=u_ID)
print_snsr(root);
Inorder (root->right, l_ID, u_ID);
}
}
void sensors_in_between(sensor_node* root)
{
printf("\nEnter lower limit ID: ");
int l_ID, u_ID;
scanf("%d", &l_ID);
printf("\nEnter upper limit ID: ");
scanf("%d", &u_ID);
Inorder(root, l_ID, u_ID);
}
int repo_Inorder(record* repo_root, int ID, int curr_mnt) //Traversal
{
int fnd=0;
if(repo_root)
{
repo_Inorder (repo_root->left, ID, curr_mnt);
if(repo_root->ID == ID)
if(repo_root->dt.mnt< curr_mnt-2)
fnd=1;
else
fnd = 0;
repo_Inorder (repo_root->right, ID, curr_mnt);
}
return fnd;
}
void Delete(sensor_node **p, sensor_node *root, Boolean_type *taller) //call xBalance when x side is taller.
{
sensor_node * q;
if ( *p == NULL)
printf("Cannot delete empty node" );
else if ((*p)->right == NULL)
{
q = *p;
*p = (*p)->left;
free(q);
*taller = TRUE;
root->bf = RH;
if ( *taller)
switch (root->bf)
{
case LH:
root->bf = EH;
*taller = FALSE;
break;
case EH:
root->bf = RH;
break;
case RH:
root = RightBalance ( root, taller) ;
break;
}
}
else if ((*p)->left == NULL)
{
q = *p;
*p = ( *p) ->right;
free(q);
*taller = TRUE;
root->bf = LH;
if ( *taller)
switch (root->bf)
{
case LH:
root = LeftBalance ( root, taller);
break;
case EH:
root->bf = LH;
break;
case RH:
root->bf = EH;
* taller = FALSE;
break;
}
}
else
{
for (q = (*p)->right; q->left; q = q->left)
q->left = (*p) ->left;
q = *p;
*p = (*p) ->right;
free(q);
*taller=TRUE;
root = LeftBalance(root, taller);
root = RightBalance(root, taller);
}
}
int sn_Inorder(sensor_node* sn_root, sensor_node* sn_node, record* repo_root, int curr_mnt, Boolean_type* taller) //Traversal
{
int dlt=0;
if(sn_node)
{
sn_Inorder(sn_root, sn_node->left, repo_root, curr_mnt, taller) ;
if(repo_Inorder(repo_root, sn_node->ID, curr_mnt)==1)
{
printf("\nDeleting sensor of ID %d ", sn_node->ID);
Delete(&sn_node, sn_root, taller);
dlt=1;
}
sn_Inorder(sn_root, sn_node->right, repo_root, curr_mnt, taller);
}
return dlt;
}
sensor_node* idle_delete(sensor_node* sn_root, record* repo_root, Boolean_type* taller)
{
int curr_mnt;
printf("\nEnter current month (no.): ");
scanf("%d", &curr_mnt);
int dlt = sn_Inorder(sn_root, sn_root, repo_root, curr_mnt, taller);
if(!dlt)
printf("\nNo sensor is idle.");
return sn_root;
}