-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.c
422 lines (367 loc) · 12.9 KB
/
tree.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/********************************************************************
*
* tree.c
*
* This program implements the tree which will be used
* to partition and access the entire user base according
* to the mutually exclusive attributes. Any person will
* be associated with a single path through the tree.
* At the leaf nodes, there would be a list of all people
* who fall into this combination of categories.
*
* Created by Jiajia Bai (Jia) ID 63070503404
* Sonia Gautam (Sonia) ID 63070503410
* Tamonwan Boonpa (Nice) ID 63070503418
* Theeranont Ruchiyakorn (Peak) ID 63070503420
*
* 22 May 2021
*
********************************************************************
*/
#include "datingApp.h"
/* global variables */
NODE_T* pRoot = NULL; /* Root node of the tree */
int attributeCount = 4; /* number of exclusive attributes */
/* an array of char* holding names of exclusive attributes */
char* attributes[] = {"GENDER","AGE","ETHNICITY","RELIGION"};
/* private function */
/**************************************************************
*
* This function is responsible for recursively creating lower
* levels' nodes until the level is equal to the number of
* exclusive attributes.
*
* Arguments
* pParent - The parent node of the child nodes
* childCount - number of child nodes to be allocated
* level - level of the tree
*
*************************************************************/
void allocateChildren(NODE_T* pParent, int childCount, int level)
{
int i = 0; /* loop variable */
NODE_T* pNewNode = NULL; /* to create the new node */
level = level + 1;
if (level < attributeCount)
{
/* allocate child nodes */
for (i = 0; i < childCount; i++)
{
pNewNode = (NODE_T*) calloc(1,sizeof(NODE_T));
if (pNewNode == NULL)
{
printf("\n\tMemory allocation failure - exiting!\n");
exit(0);
}
strcpy(pNewNode->attribute,attributes[level]);
pParent->children[i] = pNewNode;
allocateChildren(pNewNode,6,level);
}
}
else
{
if (level == attributeCount)
{
/* allocate the leaf nodes */
for (i = 0; i < childCount; i++)
{
pNewNode = (NODE_T*) calloc(1,sizeof(NODE_T));
if (pNewNode == NULL)
{
printf("\n\tMemory allocation failure - exiting!\n");
exit(0);
}
strcpy(pNewNode->attribute,"LEAF");
pParent->children[i] = pNewNode;
pNewNode->head = NULL;
pNewNode->tail = NULL;
}
}
}
}
/* public function */
/**************************************************************
*
* This function is responsible for building the tree
* with empty leaf nodes.
*
* No Argument.
*
*************************************************************/
void createNewTree()
{
NODE_T* pNewNode = NULL; /* to create the new node */
NODE_T* pParent = NULL; /* points to the parent node */
int level = 0; /* level of the tree */
pNewNode = (NODE_T*) calloc(1,sizeof(NODE_T));
if (pNewNode == NULL)
{
printf("\n\tMemory allocation failure - exiting!\n");
exit(0);
}
strcpy(pNewNode->attribute,attributes[0]);
pRoot = pNewNode;
pParent = pRoot;
/* recursively create lower levels */
allocateChildren(pParent,2,level);
}
/* public function */
/**************************************************************
*
* This function is responsible for adding new person into
* the list at the leaf node according to the exclusive
* attributes of that person.
*
* pPerson - pointer to PERSON_T structure
* holding logged in user's data
*
*************************************************************/
void addNewPerson(PERSON_T* pPerson)
{
int i = 0; /* loop variable */
/* an array of integer to store values of exclusive attributes */
int attributeValue[4] = {0,0,0,0};
NODE_T* pCurrent = NULL; /* points to the current node */
PERSON_REF_T* pEntry = NULL; /* points to new user's reference */
/* check if information is filled or not */
if (checkGender(pPerson->gender) == 1)
{
/* get exclusive attributes' value */
if (strcmp(pPerson->gender,"M") == 0)
attributeValue[0] = 1;
else
attributeValue[0] = 2;
attributeValue[1] = pPerson->age;
attributeValue[2] = pPerson->ethnicity;
attributeValue[3] = pPerson->religion;
/* traverse the tree to find the right leaf node */
pCurrent = pRoot;
for (i = 0; i < attributeCount; i++)
{
pCurrent = pCurrent->children[attributeValue[i]-1];
}
pEntry = (PERSON_REF_T*) calloc(1,sizeof(PERSON_REF_T));
if (pEntry == NULL)
{
printf("\n\tMemory allocation failure - exiting!\n");
exit(0);
}
pEntry->pPerson = pPerson;
/* add new person to the list on that leaf node */
if (pCurrent->head == NULL)
{
pCurrent->head = pEntry;
pCurrent->tail = pEntry;
}
else
{
pCurrent->tail->pNext = pEntry;
pCurrent->tail = pEntry;
}
}
}
/* private function */
/**************************************************************
*
* This function is responsible for removing the user's
* from the list in order to change reference list
* corresponding to the updated exclusive attributes.
*
* Arguments
* pPerson - pointer to PERSON_T structure
* holding logged in user's data
* oldAttribute - array of integer holding values
* of exclusive attributes before
* modifying the data
*
*************************************************************/
void removePerson(PERSON_T* pPerson, int oldAttribute[4])
{
int i = 0; /* loop variable */
NODE_T* pCurrent = NULL; /* points to the current node */
PERSON_REF_T* currentPerson = NULL; /* points to the current person */
/* in the list */
PERSON_REF_T* previousPerson = NULL; /* points to the previous person */
/* in the list */
/* check if information is filled or not */
if (checkGender(pPerson->gender) == 1)
{
/* traverse the tree to find the right leaf node */
pCurrent = pRoot;
for (i = 0; i < attributeCount; i++)
{
pCurrent = pCurrent->children[oldAttribute[i] - 1];
}
previousPerson = NULL;
currentPerson = pCurrent->head;
/* find the person to be removed */
while (currentPerson != NULL)
{
if (currentPerson->pPerson == pPerson)
break;
previousPerson = currentPerson;
currentPerson = currentPerson->pNext;
}
/* remove the person from the list on that leaf node */
if (currentPerson == pCurrent->head)
{
pCurrent->head = currentPerson->pNext;
}
else if (currentPerson == pCurrent->tail)
{
pCurrent->tail = previousPerson;
}
else
{
previousPerson->pNext = currentPerson->pNext;
free(currentPerson);
}
}
}
/* public function */
/**************************************************************
*
* This function is responsible for updating the user's
* reference list after modifying one of the exclusive
* attributes.
*
* Arguments
* pPerson - pointer to PERSON_T structure
* holding logged in user's data
* oldAttribute - array of integer holding values
* of exclusive attributes before
* modifying the data
*
*************************************************************/
void transferPerson(PERSON_T* pPerson, int oldAttribute[4])
{
/* remove the person from the former list */
removePerson(pPerson,oldAttribute);
/* add the person to the new list */
addNewPerson(pPerson);
}
/* public function */
/**************************************************************
*
* This function is responsible for finding appropriate people
* list at the leaf node according to the selected exclusive
* attributes for compatible partners.
*
* pPartner - pointer to PERSON_T structure
* holding desire qualities to be
* matched with logged in user
*
* This function returns 'currentPerson' which is pointer
* to PERSON_REF_T structure holding reference for the head
* person of the list. (NULL if no one is in the list)
*
*************************************************************/
PERSON_REF_T* passList(PERSON_T* pPartner)
{
int i = 0; /* loop variable */
/* an array of integer to store values of exclusive attributes */
int attributeValue[4] = {0,0,0,0};
NODE_T* pCurrent = NULL; /* points to the current node */
PERSON_REF_T* currentPerson = NULL; /* points to the reference of */
/* current person in the list */
/* get exclusive attributes' value */
if (strcmp(pPartner->gender,"M") == 0)
attributeValue[0] = 1;
else
attributeValue[0] = 2;
attributeValue[1] = pPartner->age;
attributeValue[2] = pPartner->ethnicity;
attributeValue[3] = pPartner->religion;
/* traverse the tree to find the right leaf node */
pCurrent = pRoot;
for (i = 0; i < attributeCount; i++)
{
pCurrent = pCurrent->children[attributeValue[i]-1];
}
currentPerson = pCurrent->head;
return currentPerson;
}
/* private function */
/**************************************************************
*
* This function is responsible for freeing people in the
* the list at the leaf node of the tree.
*
* pLeaf - pointer to NODE_T structure
* holding leaf node of the tree
*
*************************************************************/
void freePeople(NODE_T* pLeaf)
{
PERSON_REF_T* pHead = NULL; /* to store reference to the list's head */
PERSON_REF_T* pCurrent = NULL; /* points to the current reference */
PERSON_REF_T* pDelete = NULL; /* points to the reference to be freed */
pHead = pLeaf->head;
pCurrent = pHead;
while (pCurrent != NULL)
{
pDelete = pCurrent;
pCurrent = pCurrent->pNext;
free(pDelete->pPerson);
free(pDelete);
}
}
/* private function */
/**************************************************************
*
* This function is responsible for recursively freeing lower
* levels' nodes and calls other function to free people
* in the list of each leaf node.
*
* Arguments
* pParent - The parent node of the child nodes
* childCount - number of child nodes to be freed
* level - level of the tree
*
*************************************************************/
void freeChildren(NODE_T* pParent, int childCount, int level)
{
NODE_T* currentNode = NULL; /* points to the current node */
int i = 0; /* loop variable */
level = level + 1;
if (level < attributeCount)
{
/* free child nodes */
for (i = 0; i < childCount; i++)
{
currentNode = pParent->children[i];
freeChildren(currentNode,6,level);
free(currentNode);
}
}
else
{
if (level == attributeCount)
{
/* free leaf nodes */
for (i = 0; i < childCount; i++)
{
currentNode = pParent->children[i];
freePeople(currentNode);
free(currentNode);
}
}
}
}
/* public function */
/**************************************************************
*
* This function is responsible for freeing items in the
* tree and linked lists at the leaf nodes.
*
* No Argument.
*
*************************************************************/
void freeTree()
{
int level = 0; /* level of the tree */
/* free nodes in the tree and lists at leaf nodes */
freeChildren(pRoot,2,level);
/* free root node of the tree */
free(pRoot);
}