-
Notifications
You must be signed in to change notification settings - Fork 124
/
pettree.c
243 lines (221 loc) · 4.82 KB
/
pettree.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
// pettree.c -- implementation for binary search tree linked list data type
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "pettree.h"
static bool AddItemToList(Pet * ppet, List * plist);
static int ListCount(List * plist);
static bool InList(Pet * ppet, List * plist);
static void TraverseList(List * plist, void (*fn)(Pet));
static void DeleteList(List * plist);
static bool DeleteListItem(Pet * ppet, List * plist);
Tree * InitializeTree(Tree * ptree) {
*ptree = NULL;
return ptree;
}
bool TreeIsEmpty(const Tree * ptree) {
return (*ptree == NULL);
}
bool TreeIsFull(const Tree * ptree) {
Node * pnode = (Node *) malloc(sizeof(Node));
Pet * ppet = (Pet *) malloc(sizeof(Pet));
if (pnode == NULL || ppet == NULL) {
return true;
}
else {
free(pnode);
free(ppet);
return false;
}
}
bool AddPet(Pet * ppet, Tree * ptree) {
if (TreeIsFull(ptree)) {
return false;
}
int cmp;
while (*ptree != NULL) {
int cmp = strcmp(ppet->name, (*ptree)->name);
if (cmp == 0) {
// add new pet item to list in Node
return AddItemToList(ppet, &((*ptree)->head));
}
else if (cmp < 0) {
ptree = &((*ptree)->left);
}
else {
ptree = &((*ptree)->right);
}
}
// create a new node if one doesn't already exist
Node * pnode = (Node *) malloc(sizeof(Node));
if (pnode == NULL) {
fprintf(stderr, "Could not allocate memory.\n");
return false;
}
strncpy(pnode->name, ppet->name, STRLEN);
pnode->head = ppet;
pnode->left = NULL;
pnode->right = NULL;
*ptree = pnode;
return true;
}
bool DeletePet(Pet * ppet, Tree * ptree) {
while (*ptree != NULL) {
int cmp = strcmp(ppet->name, (*ptree)->name);
if (cmp == 0) {
List * plist = &((*ptree)->head);
bool deleted = DeleteListItem(ppet, plist);
// the list at this node is empty, delete the entire node
if ((*ptree)->head == NULL) {
Node * tmp = (*ptree);
if ((*ptree)->left == NULL) {
*ptree = (*ptree)->right;
}
else {
Node * right = (*ptree)->right;
*ptree = (*ptree)->left;
// reattach right subtree
if (right != NULL) {
while ((*ptree)->right != NULL) {
*ptree = (*ptree)->right;
}
(*ptree)->right = right;
}
}
free(tmp);
}
return deleted;
}
else if (cmp < 0) {
ptree = &((*ptree)->left);
}
else {
ptree = &((*ptree)->right);
}
}
return false;
}
bool InTree(Pet * ppet, const Tree * ptree) {
Node * pnode = *ptree;
while (pnode != NULL) {
int cmp = strcmp(ppet->name, pnode->name);
if (cmp == 0) {
return InList(ppet, &(pnode->head));
}
else if (cmp < 0) {
pnode = pnode->left;
}
else {
pnode = pnode->right;
}
}
return false;
}
int TreeItemCount(Tree tree) {
int count = 0;
if (tree == NULL) {
return count;
}
count += ListCount(&(tree->head));
count += TreeItemCount(tree->left);
count += TreeItemCount(tree->right);
return count;
}
void ApplyToNode(Pet * ppet, const Tree * ptree, void (*fn)(Pet)) {
Node * pnode = *ptree;
while (pnode != NULL) {
int cmp = strcmp(ppet->name, pnode->name);
if (cmp == 0) {
TraverseList(&(pnode->head), fn);
return;
}
else if (cmp < 0) {
pnode = pnode->left;
}
else {
pnode = pnode->right;
}
}
return;
}
void TraverseTree(const Tree * ptree, void (*fn)(Pet)) {
Node * pnode = *ptree;
if (pnode == NULL) {
return;
}
TraverseTree(&(pnode->left), fn);
TraverseList(&(pnode->head), fn);
TraverseTree(&(pnode->right), fn);
}
void DeleteAll(Tree * ptree) {
if (*ptree == NULL) {
return;
}
Node * pnode = *ptree;
DeleteAll(&(pnode->left));
DeleteAll(&(pnode->right));
DeleteList(&(pnode->head));
free(pnode);
}
static bool AddItemToList(Pet * ppet, List * plist) {
if (*plist == NULL) {
*plist = ppet;
}
else {
Pet * current = *plist;
while(current->next != NULL) {
current = current->next;
}
current->next = ppet;
}
ppet->next = NULL;
return true;
}
static int ListCount(List * plist) {
int count = 0;
Pet * ppet = *plist;
while (ppet != NULL) {
count++;
ppet = ppet->next;
}
return count;
}
static bool InList(Pet * ppet, List * plist) {
while (*plist != NULL) {
if (strcmp(ppet->name, (*plist)->name) == 0)
if (strcmp(ppet->type, (*plist)->type) == 0)
return true;
}
return false;
}
static void TraverseList(List * plist, void (*fn)(Pet)) {
Pet * ppet = *plist;
while (ppet != NULL) {
fn(*ppet);
ppet = ppet->next;
}
}
static void DeleteList(List * plist) {
Pet * save, * ppet = *plist;
while (ppet != NULL) {
save = ppet;
ppet = ppet->next;
free(save);
}
*plist = NULL;
}
static bool DeleteListItem(Pet * ppet, List * plist) {
Pet * save;
while (*plist != NULL) {
if (strcmp((*plist)->type, ppet->type) == 0 &&
strcmp((*plist)->name, ppet->name) == 0 ) {
save = *plist;
*plist = (*plist)->next;
free(save);
return true;
}
plist = &((*plist)->next);
}
return false;
}