int main (int argc, const char *argv[]) { int n, t, p, i, j; int counters[3], extra[4]; int *tmp; if (argc >= 2 && !strcmp (argv[1], "-e")) { Root.extra_words = 1; } scanf ("%d", &n); for (i = 0; i < n; i++) { scanf ("%d", &t); switch (t) { case 1: scanf ("%d", &p); if (!tree23_lookup (&Root, p)) { extra[0] = p+3; tree23_insert (&Root, p, extra); } break; case 2: scanf ("%d", &p); puts (tree23_lookup (&Root, p)?"YES":"NO"); break; case 3: scanf ("%d", &p); tmp = sort (&Root, arr); p = tmp - arr; for (j = 0; j < p; j++) printf ("%d%c", arr[j], '\n'); break; case 4: scanf ("%d", &p); if (tree23_lookup (&Root, p)) tree23_delete (&Root, p); break; } //dump (T); if (check (&Root) < 0) printf ("BAD TREE\n"); } printf ("%d\n", check (&Root)); dump (&Root); tmp = sort (&Root, arr); p = tmp - arr; for (j = 0; j < p; j++) printf ("%d%c", arr[j], '\n'); count (&Root, counters); printf ("leaves allocated %d\nnodes allocated %d\nleaves freed %d\nnodes freed %d\nleaves current %d\nnodes current %d\n", lp, np, ld, nd, lp - ld, np - nd); printf ("leaves in tree %d\nnodes in tree %d\nnumbers in tree %d\n", counters[1], counters[2], counters[0]); return 0; }
int main(void) { tree23_t *t; test_item_t item_array[N_ITEMS], *i, *i2, duplicate, lookup_item; test_item_t *result; int j; exit_flag = 0; /* For debugging. */ /* Create a 2-3 tree. */ t = tree23_alloc(item_cmp); /* Add some items to it. */ printf("Inserting items:\n"); for(j = 0; j < N_ITEMS; j++) { i = &item_array[j]; i->data1 = rand() % RND_MAX; i->data2 = rand() % RND_MAX; if((i2 = tree23_insert(t, i))) { printf(" duplicate %d detected - trying again\n", i2->data1); j--; } else { /* printf(" %d", i->data1); */ printf("Inserted key %d:\n", i->data1); tree23_dump_r(t->root, 0); } } /* Test duplicate detection. */ printf("\nAttempting insert to give duplicate key..."); i2 = &item_array[N_ITEMS/2]; duplicate.data1 = i2->data1; duplicate.data2 = rand() % RND_MAX; if(tree23_insert(t, &duplicate)) { printf("duplicate detected.\n"); } else { printf("failed - duplicate undetected.\n"); exit(1); } /* Test tree23_find() */ printf("\nTesting tree23_find() to find an item..."); lookup_item.data1 = item_array[N_ITEMS/2].data1; /* key to lookup. */ result = tree23_find(t, &lookup_item); if(result) { printf("%s\n", result->data1 == lookup_item.data1 ? "successful." : "failed."); } else { printf("failed.\n"); } /* Test tree23_find_min() */ printf("Testing tree23_find_min()..."); result = tree23_find_min(t); if(result) printf("%d.\n", result->data1); else printf("failed.\n"); /* Test tree23_delete(). */ printf("\nTesting tree23_delete()..."); result = tree23_delete(t, &lookup_item); if(result) { printf("%s\n", result->data1 == lookup_item.data1 ? "successful." : "failed."); } else { printf("failed.\n"); } /* Test tree23_delete_min() */ printf("Testing tree23_delete_min()..."); result = tree23_delete_min(t); if(result) printf("%d.\n", result->data1); else printf("failed.\n"); /* Test tree23_free(). */ printf("Testing tree23_free()..."); tree23_free(t); printf("successful.\n"); /* Create a new tree then repeat tree23_insert()/tree23_delete(). */ t = tree23_alloc(item_cmp); printf("\nRepeating tree23_insert()/tree23_delete()..."); for(j = 0; j < N_ITEMS; j++) { i = &item_array[j]; /* Uniqueness of keys was enforced earlier. */ printf("Inserting key %d:\n", i->data1); tree23_insert(t, i); tree23_dump_r(t->root, 0); } for(j = N_ITEMS/2; j < N_ITEMS; j++) { i = &item_array[j]; printf("Deleting key %d:\n", i->data1); if(!tree23_delete(t, i)) { printf("failed.\n"); exit(1); } tree23_dump_r(t->root, 0); } for(j = N_ITEMS/2; j < N_ITEMS; j++) { i = &item_array[j]; printf("Inserting key %d:\n", i->data1); tree23_insert(t, i); tree23_dump_r(t->root, 0); } for(j = 0; j < N_ITEMS/2; j++) { i = &item_array[j]; printf("Deleting key %d:\n", i->data1); if(!tree23_delete(t, i)) { printf("failed.\n"); exit(1); } tree23_dump_r(t->root, 0); } for(j = N_ITEMS/2; j < N_ITEMS; j++) { i = &item_array[j]; printf("Deleting key %d:\n", i->data1); if(!tree23_delete(t, i)) { printf("failed.\n"); exit(1); } if(t->root) tree23_dump_r(t->root, 0); } printf("successful.\n"); /* Test delete_min. */ for(j = 0; j < N_ITEMS; j++) { i = &item_array[j]; /* Uniqueness of keys was enforced earlier. */ printf("Inserting key %d:\n", i->data1); tree23_insert(t, i); tree23_dump_r(t->root, 0); } for(j = 0; j < N_ITEMS; j++) { if(!(i = tree23_delete_min(t))) { printf("failed.\n"); exit(1); } printf("Deleted minimum key %d:\n", i->data1); if(t->root) tree23_dump_r(t->root, 0); } tree23_free(t); return 0; }
void *_tree23_insert(void *t, void *item) { return tree23_insert((tree23_t *)t, item); }