-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtlab.c
239 lines (219 loc) · 5.57 KB
/
tlab.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
// tlab.c ... Tree Lab main program
// Builds trees, collects stats
// Written by John Shepherd, March 2013
#include <stdlib.h>
#include <stdio.h>
#include "Tree.h"
// all values are in range 0000..9999
#define RANGE 10000
void usage();
void mkprefix(int *, int, int, int);
void mkuniq(int *, int);
int *makeValues(int, char, int);
Tree makeTree(int *, int, char);
void runTests(Tree, int *, int, char);
char *prog; // used by usage()
int ix = 0; // used by mkprefix()
int main(int argc, char *argv[])
{
int N = 0; // numer values/nodes in tree
int seed = 1234; // random number seed
char order = 'R'; // order in which values are inserted
char style = 'L'; // style of insertion
Tree mytree; // tree
int *values; // values inserted into tree
// collect command-line params
prog = argv[0]; N = 0; order = 'P'; seed = 1234;
switch (argc) {
case 2: N = atoi(argv[1]); order = 'P';
style = 'L'; break;
case 3: N = atoi(argv[1]); order = argv[2][0];
style = 'L'; break;
case 4: N = atoi(argv[1]); order = argv[2][0];
style = argv[3][0]; break;
case 5: N = atoi(argv[1]); order = argv[2][0];
style = argv[3][0]; seed = atoi(argv[4]); break;
default: usage(); break;
}
if (N < 0 || N >= RANGE) usage();
values = makeValues(N,order,seed);
mytree = makeTree(values,N,style);
if (TreeNumNodes(mytree) != N) {
fprintf(stderr,"Tree not built correctly\n");
exit(1);
}
printf("Tree:\n");showTree(mytree);
char line[20];
printf("\n> ");
while (fgets(line,20,stdin) != NULL) {
int i; int show = 1;
int value = atoi(&line[1]);
switch (line[0]) {
case 'i':
TreeInsert(mytree,value);
break;
case 'd':
TreeDelete(mytree,value);
break;
case 'f':
if (TreeFind(mytree, value))
printf("Found!\n");
else
printf("Not found\n");
break;
case 's':
// nothing to do ... it's displayed below
break;
case 'v':
for (i = 0; i < N; i++) {
if (i%15 == 0) printf("\n");
printf("%d ",values[i]);
}
printf("\n");
break;
case 't':
runTests(mytree, values, N, order);
break;
case '?':
printf("Commands:\n");
printf("i N ... insert N into tree\n");
printf("d N ... delete N from tree\n");
printf("f N ... search for N in tree\n");
printf("s ... display tree if not big\n");
printf("v ... print values in tree\n");
printf("t ... run tests on tree\n");
printf("q ... quit\n");
show = 0;
break;
case 'q':
return 0;
break;
}
if (show) {
printf("Tree:\n");
showTree(mytree);
}
printf("\n> ");
}
return 0;
}
// run search tests on tree
void runTests(Tree t, int *values, int N, char order)
{
int i, x, ok = 0, NN = 0; Key not[RANGE];
printf("Tree search test\n");
// search for values known to be in tree
srand(0); // for consistency
x = rand()%N; // in case the first is reuse
for (i = 0; i < N; i++) {
if (rand()%10 < 1) // on 1 in 10 loops
x = x+1-1; // reuse last search key
else
x = rand()%N; // random search key
// should be found
if (TreeFind(t,values[x])) ok++;
}
printf("Search for %d values known to be in tree\n",N);
printf("Found %d matches; ",ok);
printf("%s\n", (ok == N) ? "ok" : "not ok");
// set up array of values *not* in tree
int start, incr;
ok = 0;
not[NN++] = 0; not[NN++] = RANGE;
switch (order) {
case 'A': start = N; incr = 1; break;
case 'D': start = values[N-1]-1; incr = -1;
case 'P': start = N; incr = 1;
case 'R': start = 0; incr = 1; break;
}
for (x = start; NN < N/3; x += incr) {
for (i = 0; i < N; i++) {
if (x == values[i]) break;
}
if (i == N) { // x is not in tree
not[NN++] = x;
}
}
// search for values *not* in tree
for (i = 0; i < NN; i++) {
if (!TreeFind(t, not[i])) ok++;
}
printf("Search for %d values known to *not* be in tree\n",NN);
printf("Found %d matches; ",NN-ok);
printf("%s\n", (ok == NN) ? "ok" : "not ok");
}
// generate array of values to be inserted in tree
int *makeValues(int N, char order, int seed)
{
int i, *values;
values = malloc(N*sizeof(int));
switch (order) {
case 'A':
seed = 1;
for (i = 0; i < N; i++) values[i] = seed++;
break;
case 'D':
seed = N;
for (i = 0; i < N; i++) values[i] = seed--;
break;
case 'P':
ix = 0;
mkprefix(values, N, 1, N);
break;
case 'R':
srand(seed);
mkuniq(values, N);
break;
default:
usage(); break;
}
return values;
}
// create the tree and insert the values
Tree makeTree(int *values, int N, char style)
{
Tree t; Style ins = InsertAtLeaf; int i;
switch (style) {
case 'L': ins = InsertAtLeaf; break;
case 'A': ins = InsertAtRoot; break;
case 'R': ins = InsertRandom; break;
case 'B': ins = InsertRebalance; break;
case 'S': ins = InsertSplay; break;
case 'V': ins = InsertAVL; break;
default: usage(); break;
}
t = newTree(ins);
for (i = 0; i < N; i++) TreeInsert(t,values[i]);
return t;
}
void mkprefix(int *v, int N, int lo, int hi)
{
if (ix >= N || lo > hi) return;
int mid = (lo+hi)/2;
v[ix++] = mid;
mkprefix(v,N,lo,mid-1);
mkprefix(v,N,mid+1,hi);
}
void mkuniq(int *v, int N)
{
int i, x, already[RANGE];
for (i = 0; i < RANGE; i++)
already[i] = 0;
i = 0;
while (i < N) {
x = 1 + rand()%(RANGE-1);
if (already[x]) continue;
v[i++] = x;
already[x]++;
}
}
void usage()
{
fprintf(stderr, "Usage: %s N Order Insert Seed\n",prog);
fprintf(stderr, "0 <= N <= 9999, Seed = a random number\n");
fprintf(stderr, "Order = Ascending|Descending|Prefix|Random\n");
fprintf(stderr, "Insert = Leaf|At-root|reBalance|Random|Splay|aVl\n");
fprintf(stderr, "For Order and Insert, use just the upper-case letter\n");
fprintf(stderr, "e.g. for AVL, use V; for Rebalancing, use B\n");
exit(1);
}