-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathES 2 ( 07.02.2023 )
146 lines (115 loc) · 2.89 KB
/
ES 2 ( 07.02.2023 )
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
// Define structure for list node
typedef struct list_t{
char *name;
struct list_t *next;
}list_t;
// Define structure for BST node
typedef struct bst_t{
int id;
struct bst_t *left;
struct bst_t *right;
struct list_t *head;
}bst_t;
// Function Prototypes
bst_t *insertBST(bst_t *root, int id, char *name);
bst_t *createBSTNode(int id, char *name);
list_t *createListNode(char *name);
void inOrderTraversal(bst_t *root);
bst_t *insert(char *fileName);
void freeBST(bst_t *root);
// Main Function
int main(void){
bst_t *root = NULL;
root = insert("../input.txt");
inOrderTraversal(root);
freeBST(root);
return EXIT_SUCCESS;
}
// Function Declarations
bst_t *insert(char *fileName){
FILE *fp;
bst_t *root = NULL;
char name[MAX];
int id;
fp = fopen(fileName,"r");
if(fp == NULL){
fprintf(stderr,"Error opening file\n");
exit(EXIT_FAILURE);
}
while (fscanf(fp,"%d %s", &id, name) != EOF){
root = insertBST(root,id,name);
}
fclose(fp);
return root;
}
bst_t *insertBST(bst_t *root, int id, char *name){
if(root == NULL){
return createBSTNode(id, name);
}
if(id < root->id){
root->left = insertBST(root->left, id, name);
}else if(id > root->id){
root->right = insertBST(root->right, id, name);
}else{
// Create node list
list_t *current = createListNode(name);
current->next = root->head;
root->head = current;
}
return root;
}
bst_t *createBSTNode(int id, char *name){
bst_t *newNode = (struct bst_t *) malloc (sizeof (bst_t));
if(newNode == NULL){
fprintf(stderr,"Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->id = id;
newNode->left = NULL;
newNode->right = NULL;
newNode->head = createListNode(name);
return newNode;
}
list_t *createListNode(char *name){
list_t *newNode = (struct list_t *) malloc (sizeof (list_t));
if(newNode == NULL){
fprintf(stderr,"Memory allocation failed\n");
exit(EXIT_FAILURE);
}
newNode->name = strdup(name);
newNode->next = NULL;
return newNode;
}
void inOrderTraversal(bst_t *root){
if(root == NULL){
return;
}
inOrderTraversal(root->left);
fprintf(stdout,"- %d: ", root->id);
list_t *current = root->head;
while (current != NULL){
fprintf(stdout,"%s ", current->name);
current = current->next;
}
fprintf(stdout,"\n");
inOrderTraversal(root->right);
}
void freeBST(bst_t *root){
if(root == NULL){
return;
}
freeBST(root->left);
freeBST(root->right);
list_t *current = root->head;
while (current != NULL){
list_t *tmp = current;
current = current->next;
free(tmp->name);
free(tmp);
}
free(root);
}