-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
184 lines (142 loc) · 4.64 KB
/
main.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
/* Test file for the dictionary library */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include "lbst.h"
#include "lbst_private.h"
static char** random_strings(char *alphabet, int num_keys, int max_key_len);
static void print(char *key, void *val);
int main() {
int i, j;
char *val;
char **keys, **vals;
lbst_T root;
root = lbst_create();
srand(time(NULL));
keys = random_strings("abc", 10, 3);
vals = random_strings("abc", 10, 3);
/* insert random (key, val) */
printf("----------Insert-----------------------------------\n");
for (i = 0; i < 10; i++) {
printf("+ [%s :: %s]\n", keys[i], vals[i]);
lbst_insert(root, keys[i], vals[i]);
lbst_print(root, print);
}
/* we no longer need the keys but we need the vals */
for (i = 0; i < 10; i++) {
free(keys[i]);
}
free(keys);
printf("----------Print tree (preorder traversal)----------\n");
lbst_print_tree(root, print);
/* lookup random keys */
printf("----------Lookup-----------------------------------\n");
keys = random_strings("abc", 10, 3);
for (i = 0; i < 10; i++) {
j = rand() % 10;
printf("%s :: ", keys[j]);
val = lbst_lookup(root, keys[j]);
if (val) {
printf("%s\n", val);
}
else {
printf("Not found\n");
}
}
/* we no longer need the keys */
for (i = 0; i < 10; i++) {
free(keys[i]);
}
free(keys);
printf("----------Range Query [ba to ca]------------------------\n");
lbst_range_query(root, "ba", "ca", print);
/* delete random keys */
printf("----------Delete-----------------------------------\n");
keys = random_strings("abc", 10, 3);
for (i = 0; i < 10; i++) {
j = rand() % 10;
printf("- %s\n", keys[j]);
lbst_delete(root, keys[j]);
lbst_print(root, print);
}
/* we no longer need the keys */
for (i = 0; i < 10; i++) {
free(keys[i]);
}
free(keys);
printf("---------------------------------------------------\n");
printf("Clear dictionary\n");
lbst_clear(root);
printf("Check if dictionary is empty (should print 1): %d\n", lbst_is_empty(root));
printf("Print dictionary: ");
lbst_print(root, print);
printf("Insert ['nice', 'day']\n");
lbst_insert(root, "nice", "day");
printf("Print dictionary: ");
lbst_print(root, print);
printf("Delete 'nice'\n");
lbst_delete(root, "nice");
printf("Print dictionary: ");
lbst_print(root, print);
printf("Check if dictionary is empty (should print 1): %d\n", lbst_is_empty(root));
/* We cannot call any other functions after deleting the dictionary */
printf("Delete dictionary\n");
lbst_free(root);
/* clear whatever is left */
for (i = 0; i < 10; i++) {
free(vals[i]);
}
free(vals);
return 0;
}
/* Creates an array of character arrays (keys). Each key has
at most max_key_len characters and is created by selecting random characters
from the character array alphabet.
Runtime checks:
1) if alphabet is not NULL
2) if length of alphabet is not 0
3) if number of keys is >=0
4) if maximum key length is >0
5) if memory was allocated succesfully for keys
Parameters:
alphabet: pointer to an array of characters. Must be null-terminated.
num_keys: the number of keys in the array.
max_key_len: the maximum number of characters in each key.
Returns: a pointer to an array of non-empty null-terminated keys. */
static char** random_strings(char *alphabet, int num_keys, int max_key_len) {
char **keys;
int i, j, rand_int, alpha_length;
assert(alphabet);
assert(num_keys >= 0);
assert(max_key_len > 0);
alpha_length = strlen(alphabet);
assert(alpha_length);
keys = malloc(num_keys * sizeof(char *));
assert(keys);
for (i = 0; i < num_keys; i++) {
/* generate a random length for each key */
rand_int = rand() % max_key_len + 1;
keys[i] = malloc((rand_int + 1) * sizeof(char));
/* fill the key with random characters from alphabet */
for (j = 0; j < rand_int; j++) {
keys[i][j] = alphabet[rand() % alpha_length];
}
keys[i][j] = '\0';
}
return keys;
}
/* Prints the key and val of a dictionary entry.
Checks: if val is not NULL at runtime.
Parameters:
key: pointer to a character array. Must be null-terminated.
val: pointer to a void value (treated as char*).
Returns: void */
static void print(char *key, void *val) {
char *value;
assert(key);
value = val;
printf("[%s::%s]", key, value);
return;
}