-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab13.c
63 lines (56 loc) · 1.5 KB
/
lab13.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
#include "lab13.h"
Employee* readRecord(FILE* fp){
Employee* record = (Employee*)malloc(sizeof(Employee));
fscanf(fp, "%d,%f,%d\n", &record->ID, &record->salary, &record->age); // scan file and get inputs
return record;
}
Node* createNode(void *data){
Node *mynode = malloc(sizeof(Node));
if (mynode == NULL){
printf("Allocation Failed");
return NULL;
}
mynode->data = data;
mynode->left = NULL;
mynode->right = NULL;
return mynode;
};
int comparison(void *a, void *b){
float alpha = ((Employee *)a)->salary;
float beta = ((Employee *)b)->salary;
if (alpha > beta) {
return 1;
} else if (alpha == beta) {
return 0;
} else {
return -1;
}
};
Node* insertBST(Node *head, void *newdata){
if (head == NULL){
Node *node1 = createNode(newdata);
return node1;
} else {
if (comparison(head->data, newdata) == 1){
head->left = insertBST(head->left, newdata);
} else {
head->right = insertBST(head->right, newdata);
}
}
return head;
};
void inOrderPrintAge(Node *root){
if (root == NULL){
return;
}
inOrderPrintAge(root->left);
printf(" [%d]\n", ((Employee*)root->data)->age);
inOrderPrintAge(root->right);
};
void deleteTree(Node *root){
if (root->left == NULL && root->right == NULL){
return free(root);
}
deleteTree(root->left);
deleteTree(root->right);
};