void test_bt_1(CuTest* tc) { int val; int i; bt* tree; tree = bt_new(sizeof(int), 4); printf("Empty:\n"); bt_print(tree, print_int); printf("\n"); { int vals[] = { 10, 5, 100, 10, 50, 50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 200,200,200,200,200,250,150 }; for (i=0; i<sizeof(vals)/sizeof(int); i++) { val = vals[i]; printf("Insert %i:\n", val); bt_insert(tree, &val, 0, compare_ints); //bt_print(tree, print_int); printf("\n"); } } printf("Values: "); for (i=0; i<tree->N; i++) { int val = *(int*)bt_access(tree, i); printf("%i ", val); // these tests depend on the values in the "vals" array above. if (i < 11) { CuAssertIntEquals(tc, 1, val); } else if (i < 12) { CuAssertIntEquals(tc, 5, val); } else if (i < 14) { CuAssertIntEquals(tc, 10, val); } else if (i < 16) { CuAssertIntEquals(tc, 50, val); } else if (i < 17) { CuAssertIntEquals(tc, 100, val); } else if (i < 18) { CuAssertIntEquals(tc, 150, val); } else if (i < 23) { CuAssertIntEquals(tc, 200, val); } else { CuAssertIntEquals(tc, 250, val); } } printf("\n"); { int vals[] = { 0, 1, 2, 9, 10, 11, 49, 50, 51, 99, 100, 101, 149, 150, 151, 199, 200, 201, 249, 250, 251 }; int doesit[]={ 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0 }; for (i=0; i<sizeof(vals)/sizeof(int); i++) { int youthink; val = vals[i]; youthink = bt_contains(tree, &val, compare_ints); printf("Contains %i: %s\n", val, (youthink ? "yes" : "no")); CuAssertIntEquals(tc, doesit[i], youthink); } } bt_free(tree); }
void bt_print (BTree* a, int indent) { int i; if (isleaf(a)) { for (i = a->n - 1; i >= 0; --i) { INDENT(indent); printf( "%d \n" , a->k[i]); } } else { bt_print(a->p[a->n],indent+2); for (i = a->n - 1; i>=0; --i) { INDENT(indent); printf("%d \n" , a->k[i] ) ; bt_print(a->p[i],indent+2); } } }
static void bt_fun(void *arg, const char *module, uintptr_t addr, const char *function, uintptr_t offset) { t_bt_fun *const t = (t_bt_fun*) arg; JNIEnv*const env = t->env; jstring declaringClass = (*env)->NewStringUTF(env, bt_module(module)); jstring methodName = (*env)->NewStringUTF(env, bt_addr(addr)); jstring fileName = (*env)->NewStringUTF(env, bt_print(function, offset)); const int lineNumber = function != NULL ? 0 : -2; /* "-2" is "inside JNI code" */ jobject trace = (*env)->NewObject(env, t->cls_ste, t->cons_ste, declaringClass, methodName, fileName, lineNumber); if (t->index < t->size) { (*t->env)->SetObjectArrayElement(t->env, t->elements, t->index++, trace); } }
int main(int argc, char** argv) { if (argc < (INPUT_ARG + 1)) { printf("***Missing input argument\n"); return EXIT_FAILURE; } FILE* file = fopen(argv[INPUT_ARG], FILE_READ_ONLY); if (!file) { printf("***File \"%s\" could not be found\n", argv[INPUT_ARG]); fclose(file); return EXIT_FAILURE; } linked_list* in_order_list = build_list_from_file(file, false); linked_list* post_order_list = build_list_from_file(file, true); if (in_order_list->size != post_order_list->size || in_order_list->size == 0 || post_order_list->size == 0) { ll_delete(in_order_list); ll_delete(post_order_list); fclose(file); printf("***the length of the inorder and post order traversal" " in the input must be nonzero and equal."); return EXIT_FAILURE; } printf("inorder: "); ll_print(in_order_list); printf("postorder: "); ll_print(post_order_list); binary_tree* btree = new_binary_tree(in_order_list, post_order_list); bt_print(btree); fclose(file); ll_delete(in_order_list); free(in_order_list); ll_delete(post_order_list); free(post_order_list); bt_delete(btree); free(btree); return EXIT_SUCCESS; }