cprintf("First, \"%s\" will be read and words found inserted into the tree.\r\n",s);
pause();
/* build tree of words */
while (fscanf(fp,"%s",s) != EOF)
if (tavl_insert(tree,s,NO_REPLACE) == NULL) {
cprintf("Out of memory!\r\n");
exit(0);
}
/* check that all identifiers inserted can be found */
cprintf("Tree is built. Next, word file will be re-read to verify that\r\nall words are in the tree and can be found.\r\n");
pause();
fseek(fp,0,SEEK_SET);
cprintf("Testing 'Find'\r\n");
while (fscanf(fp,"%s",s) != EOF) {
cprintf("%-10.8s",s);
assert(tavl_find(tree,s) != NULL);
}
cprintf("\r\n\nFind OK.\r\n");
cprintf("Next, data items inserted into the tree will be listed in ascending order.\r\n");
pause();
fclose(fp);
cprintf("In order:\r\n");
p = tavl_reset(tree);
n = 0;
while ((p = tavl_succ(p)) != NULL) {
cprintf("%-10.8s",p->dataptr);
n++;
}
cprintf("\r\n\nNodes = %u\r\n",n);
k = n;
p = tavl_reset(tree);
if ((name = malloc(n * sizeof(char *))) == NULL) {
cprintf("\r\nOut of memory, can't do random deletions test.\r\n");
cprintf("Testing count in reverse direction...\r\n");
while (p = tavl_pred(p)) n--;
assert(n==0);
cprintf("Reverse direction tested & OK.\r\n");
exit(0);
}
cprintf("\r\nNext: An array is filled for random deletions while passing through TAVLtree\r\nin reverse direction. Words will not be written to output.\r\n");
pause();
while ((p = tavl_pred(p)) != NULL)
*(name + --k) = p->dataptr;
assert(k==0);
cprintf("\r\nReverse direction tested & OK.\r\n");
cprintf("\r\nDelete test: words to delete will be randomly selected from the array just\r\nfilled, written to screen, and then deleted from the TAVLtree.\r\n");
pause();
randomize();
cprintf("\r\nDelete test:\r\n");
while (n) {
i = random(n--); /* select random identifier to delete */
cprintf("%-10.8s",*(name + i));
tavl_delete(tree,*(name + i)); /* delete it */
for (; i < n; i++) /* shrink the names list */
*(name + i) = *(name + i + 1);
}
p = tavl_reset(tree); /* make sure the tree is empty */
if (tavl_succ(p) != NULL) {
cprintf("Can't happen: tree still conatains nodes - successor.\r\n");
exit(0);
}
p = tavl_reset(tree); /* make sure the tree is empty */
if (tavl_pred(p) != NULL) {
cprintf("Can't happen: tree still conatains nodes - predecessor.\r\n");