-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstandard-bst.c
executable file
·320 lines (271 loc) · 10 KB
/
standard-bst.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
/*******************************************************************************
* // Begin statement *
* *
* Author: Dr. Nikolas Askitis *
* Email: [email protected] *
* Github.com: https://github.com/naskitis *
* *
* Copyright @ 2016. All rights reserved. *
* *
* Permission to use my software is granted provided that this statement *
* is retained. *
* *
* My software is for non-commercial use only. *
* *
* If you want to share my software with others, please do so by *
* sharing a link to my repository on github.com. *
* *
* If you would like to use any part of my software in a commercial or public *
* environment/product/service, please contact me first so that I may *
* give you written permission. *
* *
* This program is distributed without any warranty; without even the *
* implied warranty of merchantability or fitness for a particular purpose. *
* *
* // End statement *
******************************************************************************/
#include "include/common.h"
uint64_t memory_used=0;
/* the structure of a standard node */
typedef struct node
{
struct node *left;
struct node *right;
char *str;
}node;
/* a pointer to the root node */
node *root_node=NULL;
/* initialize the standard-chain binary search tree by ensuring that
* the root node pointer is null
*/
int init()
{
memory_used=0;
root_node=NULL;
return 0;
}
void in_order(node *node1);
/* perform an in order traversal of the BST to count the memory used and to the memory allocated */
void in_order(node *node1)
{
if(node1==NULL) return;
/* the total memory allocated is as follows:
* the size of the node plus eight bytes for allocating the node,
* the memory required by the string + one extra byte for its null
* character, and then a further eight bytes of allocation overhead
*/
memory_used += sizeof(struct node) + 16 + slen(node1->str)+1+16;
in_order(node1->left);
in_order(node1->right);
free(node1->str);
free(node1);
}
/* free the memory used by the BST and calculate the amount of space allocated */
void destroy()
{
in_order(root_node);
}
/* search for a string in the BST and Return 1 on success, 0 otherwise */
int search(char *word)
{
char *key;
node *current_node=root_node;
int32_t result;
if(root_node==NULL)
{
return 0;
}
/* traverse the BST to find a string that matches or until a null pointer is
* encountered, on which, the search fails */
while (1)
{
key = current_node->str;
result=scmp(word, key);
if(result==0) /* if the query matches the string in the current node */
{
return 1;
}
/* if the query is smaller than the string in the current node, then follow the left child-node pointer */
else if(result < 0)
{
current_node=current_node->left;
if(current_node ==NULL)
{
return 0;
}
}
/* if the query is larger than the string in the current node, then follow the right child-node pointer */
else
{
current_node = current_node->right;
if(current_node == NULL)
{
return 0;
}
}
}
}
/* search for the string to insert in the BST.
* if the string is found, then the insertion fails. Otherwise, when a null
* pointer is encountered, encapsulate the string in a standard
* node and attach the node to the null-pointer encountered to complete the insertion
*/
int insert(char *word)
{
node *previous_node=NULL;
node *current_node=root_node;
char *key;
int len=0;
int32_t result;
/* if there is no BST, then create it */
if(root_node==NULL)
{
/* allocate space for the node */
if ((root_node=malloc(sizeof(node))) == NULL) fatal (MEMORY_EXHAUSTED);
len = slen(word)+1;
/* allocate space for the string */
if ((root_node->str=calloc(len, sizeof(char))) == NULL) fatal (MEMORY_EXHAUSTED);
/* make sure the left and right child node pointers are null */
root_node->left = NULL;
root_node->right= NULL;
key=root_node->str;
/* copy the string into the node to complete the insertion */
for(; *word != '\0'; word++, key++)
{
*key=*word;
}
*key='\0';
return 1;
}
/* if the BST exists, then search for the string. If a null pointer is
* encountered, then encapsulate the string into a standard node and attach
* the node to the null pointer encountered to complete the insertion
*/
while (1)
{
key = current_node->str;
previous_node=current_node;
result=scmp(word, key);
if(result==0)
{
return 0;
}
else if(result < 0)
{
current_node = current_node->left;
/* if a null pointer is encountered, then allocate a new
* standard node to encapsulate the string to insert, then assign
* the node to the pointer to complete the insertion
*/
if(current_node ==NULL)
{
/* create and assign the node */
if ((previous_node->left=malloc(sizeof(node))) == NULL) fatal (MEMORY_EXHAUSTED);
len = slen(word)+1;
/* allocate space for the string */
if ((previous_node->left->str = calloc(len, sizeof(char))) == NULL) fatal (MEMORY_EXHAUSTED);
current_node = previous_node->left;
key = current_node->str;
/* make sure the new node has its child pointer nulled */
current_node->left=current_node->right=NULL;
/* copy the string into the node to complete the insertion */
for(; *word != '\0'; word++, key++)
{
*key=*word;
}
*key='\0';
return 1;
}
}
else
{
current_node= current_node->right;
/* if a null pointer is encountered, then allocate a new
* standard node to encapsulate the string, then assign
* it to the null pointer to complete the insertion
*/
if(current_node == NULL)
{
/* allocate space for the node */
if ((previous_node->right=malloc(sizeof(node))) == NULL ) fatal (MEMORY_EXHAUSTED);
len = slen(word)+1;
/* allocate space for its string */
if ((previous_node->right->str = calloc(len, sizeof(char))) == NULL) fatal (MEMORY_EXHAUSTED);
current_node = previous_node->right;
key = current_node->str;
/* make sure the new node has its child pointer nulled */
current_node->left=current_node->right=NULL;
/* copy the string into the node to complete the insertion */
for(; *word != '\0'; word++, key++)
{
*key=*word;
}
*key='\0';
return 1;
}
}
}
return 0;
}
int main(int argc, char **argv)
{
char *to_insert=NULL, *to_search=NULL;
double mem=0,insert_real_time=0.0, search_real_time=0.0;
int num_files=0,i=0,j=0;
/* get the number of files to insert */
num_files = atoi(argv[1]);
init();
/* insert each file in sequence and accumulate the time required */
for(i=0, j=2; i<num_files; i++, j++)
{
to_insert=argv[j];
insert_real_time+=perform_insertion(to_insert);
}
uint64_t vsize=0;
{
pid_t mypid;
FILE * statf;
char fname[1024];
uint64_t ret;
uint64_t pid;
char commbuf[1024];
char state;
uint64_t ppid, pgrp, session, ttyd, tpgid;
uint64_t flags, minflt, cminflt, majflt, cmajflt;
uint64_t utime, stime, cutime, cstime, counter, priority;
uint64_t timeout, itrealvalue;
uint64_t starttime;
uint64_t rss, rlim, startcode, endcode, startstack, kstkesp, ksteip;
uint64_t signal, blocked, sigignore, sigcatch;
uint64_t wchan;
uint64_t size, resident, share, trs, drs, lrs, dt;
mypid = getpid();
snprintf(fname, 1024, "/proc/%u/stat", mypid);
statf = fopen(fname, "r");
ret = fscanf(statf, "%lu %s %c %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu "
"%lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
&pid, commbuf, &state, &ppid, &pgrp, &session, &ttyd, &tpgid,
&flags, &minflt, &cminflt, &majflt, &cmajflt, &utime, &stime,
&cutime, &cstime, &counter, &priority, &timeout, &itrealvalue,
&starttime, &vsize, &rss, &rlim, &startcode, &endcode, &startstack,
&kstkesp, &ksteip, &signal, &blocked, &sigignore, &sigcatch,
&wchan);
if (ret != 35) {
fprintf(stderr, "Failed to read all 35 fields, only %d decoded\n",
ret);
}
fclose(statf);
}
/* get the number of files to search */
num_files = atoi(argv[j++]);
/* search each file in sequence and accumulate the time required */
for(i=0; i<num_files; i++, j++)
{
to_search=argv[j];
search_real_time+=perform_search(to_search);
}
destroy();
printf("Standard-BST %.2f %.2f %.2f %.2f %d %d --- Dr. Nikolas Askitis, Copyright @ 2016, [email protected]\n", vsize/(double)TO_MB, memory_used/(double)TO_MB, insert_real_time,
search_real_time, get_inserted(), get_found());
return 0;
}