contributed by < kevin55216
>
kevin550029
ternary search tree
c
/ | \
a u h
| | | \
t t e u
/ / | / |
s p e i s
a ac c hi u
s tu u e s
p t
e
copy from wikipedia/Ternary_search_tree
delete word
delete "cup"
c stack
/ | \ |__t__|
a u h |__u__|
| | | \ |__c__|
t t e u
/ / | / |
s p e i s
---------------------------------------------------------------------
node p is victim
node t is parrent
parent lokid = NULL, free victim, victim = NULL, return NULL
---------------------------------------------------------------------
c
/ | \
a u h
| | | \
t t e u
/ | / |
s e i s
---------------------------------------------------------------------
delete "us"
stack
|__u__|
|__h__|
|__c__|
---------------------------------------------------------------------
node s is victim, node u is parrent
parent eqkid = NULL, free victim, victim=parent, parent = node h (pop stack)
parent hikid (u) = victim lokid (i), free victim, victim = NULL
---------------------------------------------------------------------
c
/ | \
a u h
| | | \
t t e i
/ |
s e
insert
insert "cup"
c
/ | \
a u h
| | | \
t t e u
/ / | / |
s p e i s
---------------------------------------------------------------------
p node->refcnt ++
so refcnt is a count that count how many "cup" word in tree.
---------------------------------------------------------------------
insert "tea"
t > c
t > h
t < u
t > i
---------------------------------------------------------------------
c
/ | \
a u h
| | | \
t t e u
/ / | / |
s p e i s
\
t
|
e
|
a
---------------------------------------------------------------------
t e a node = 1
/*取得tst_search cycle*/
while ((rtn = fscanf(bfp, "%s", word)) != EOF) {
char *testbench = word;
rmcrlf(testbench);
get_cycles(&timec_high1, &timec_low1);
res = tst_search(root, testbench);
get_cycles_end(&timec_high2, &timec_low2);
result[j] = diff_in_cycles(timec_high1, timec_low1, timec_high2, timec_low2);
if (res)
fprintf(brfp, "%"PRIu64"\n", result[j]);
else
fprintf(brfp,"%s not found.\n", testbench);
j++;
}
/*計算平均、標準差*/
double resultmid = 0.0;
for(int i = 0; i < 1000; i++) {
resultmid += (double)result[i];
}
resultmid /= 1000.0;
double resultsd = 0.0;
for(int i = 0; i < 1000; i++) {
resultsd += (((double)result[i]-resultmid)*((double)result[i]-resultmid));
}
resultsd /= (1000.0-1.0);
resultsd = sqrt(resultsd);
printf("mid: %.9f\n", resultmid);
printf("SD : %.9f\n", resultsd);
並print出平均與標準差。
請列出程式碼
課程助教
單位為cycles, mid 為平均值, SD 為標準差
$ ./test_ref --bench
benching...
bench complete
mid: 889.605000000
SD : 772.879254034
$ ./test_cpy --bench
benching...
bench complete
mid: 1016.366000000
SD : 1758.118714267