Пример #1
0
int main(int argc,char *argv[])
{
	char word[20];
	init_rbtree();
	HEADER=NIL;
	while(getword(word,20)!= EOF)
	{
		insert_node(word);
	}
	show_rbtree(HEADER);
	/*rbnode *p=insert_node("wang");
	rbnode *s=insert_node("zhu");
	left_rotate(HEADER,HEADER);
	printf("%s\n",HEADER->word);
	show_rbtree(HEADER);
	right_rotate(HEADER,HEADER);
	show_rbtree(HEADER);
	printf("%s\n",HEADER->word);
	*/
	rbnode *p = find_node(HEADER,"a");
	if(p == NIL)
	{
		printf("can't find\n");
		return -1;
	}
	del_node(HEADER,p);
	show_rbtree(HEADER);

}
Пример #2
0
/*
 * Initialize set for bitsize n
 * - n must be positive
 */
void init_rb_bvset(rb_bvset_t *set, uint32_t n) {
  assert(n > 0);

  init_rbtree(&set->tree, 0);
  set->max_val = UINT32_MAX;
  if (n < 32) {
    set->max_val = (((uint32_t) 1) << n) - 1; // 2^n - 1
  }
  set->ptr = 0;
}
Пример #3
0
CuSuite* reg_cutest_rbtree(void)
{
	CuSuite* suite = CuSuiteNew();
	
	if (init_rbtree() != 0) {
		exit(1);
	}
	
	SUITE_ADD_TEST(suite, rbtree_1);
	SUITE_ADD_TEST(suite, rbtree_2);
	SUITE_ADD_TEST(suite, rbtree_3);
	SUITE_ADD_TEST(suite, rbtree_4);
	SUITE_ADD_TEST(suite, rbtree_5);
	SUITE_ADD_TEST(suite, rbtree_6);
	SUITE_ADD_TEST(suite, rbtree_7);
	SUITE_ADD_TEST(suite, rbtree_8);
	SUITE_ADD_TEST(suite, rbtree_9);
	SUITE_ADD_TEST(suite, rbtree_10);
        
	return suite;
}
Пример #4
0
int main(int argc, char * argv[])
{
	const char * tree_str = 0;
	NODE * t = 0;
	int limit = -1;
	opterr = 0;
	______________________________  = print_nothing;
	______________________________I = print_nothing;
	______________________________D = print_nothing;

	while (1) {
		int c;
		static struct option long_options[] = {
			{"help",           no_argument,       0, 'h'},
			{"algorithm",      required_argument, 0, 'a'},
			{"debug-level",    required_argument, 0, 'd'},
			{"tree",           required_argument, 0, 't'},
			{"limit",          required_argument, 0, 'l'},
			{0, 0, 0, 0}
		};
		/* getopt_long stores the option index here. */
		int option_index = 0;
     
		c = getopt_long(argc, argv, "ha:t:d:l:",
				long_options, &option_index);
     
		/* Detect the end of the options. */
		if (c == -1)
			break;
     
		switch (c) {
		case 0:
			/* If this option set a flag, do nothing else now. */
			if(long_options[option_index].flag != 0)
				break;
			printf("option %s", long_options[option_index].name);
			if(optarg)
				printf(" with arg %s", optarg);
			printf("\n");
			break;

		case 'h':
			print_usage(argv[0]);
			break;
     
		case 'a':
			if (strcmp(optarg, "CLRS") == 0) {
				insert_val = insert_CLRS;
				delete_val = delete_CLRS;
			} else if (strcmp(optarg, "Sedgewick") == 0) {
				insert_val = insert_Sedgewick;
				delete_val = delete_Sedgewick;
			} else {
				abort();
			}
			break;
     
		case 'd':
			dbg_level = atoi_or_abort(optarg);
			if (dbg_level == 1) {
				______________________________  = print_rbtree;
			} else if (dbg_level == 2) {
				______________________________  = print_rbtree;
				______________________________I = print_rbtree;
			} else if (dbg_level == 3) {
				______________________________  = print_rbtree;
				______________________________D = print_rbtree;
			} else if (dbg_level == 4) {
				______________________________  = print_rbtree;
				______________________________I = print_rbtree;
				______________________________D = print_rbtree;
			} else {
				; /* use default */
			}
			break;
     
		case 't':
			tree_str = optarg;
			break;
          
		case 'l':
			limit = atoi_or_abort(optarg);
			break;
          
		case '?':
			/* getopt_long already printed an error message. */
			printf("%s%s%s\n", RED_B, argv[optind-1], NOCOLOR);
			break;
     
		default:
			abort();
		}
	}

	printf("%sDebug Level: %s%d%s\n", YELLOW, CYAN, dbg_level, NOCOLOR);

	static const char * default_tree[] = {/* p.275 */ "(26(17+(14(10+(7(3+)())(12))(16(15+)()))(21(19()(20+))(23)))(41(30+(28)(38(35+)(39+)))(47)))",
					      /* p.282 */ "(11(2+(1)(7(5+)(8+)))(14()(15+)))",
					      ""};
	if (!tree_str) {
		tree_str = default_tree[0];
		printf("%sUsing default tree: %s%s%s\n", YELLOW, CYAN, tree_str, NOCOLOR);
	}
	t = init_rbtree(tree_str);
	assert(t->L->P == nil);

        ______________________________("./fig/", t, t, "init");
	printf("Node count: %d\n", node_cnt);

	/* default algorithms */
	if (insert_val == 0)
		insert_val = insert_Sedgewick;
	if (delete_val == 0)
		delete_val = delete_Sedgewick;

	/* printf("optind: %d, argc: %d\n", optind, argc); */
	/* for (int j = 0; j < argc; j++) */
	/* 	printf("        %s\n", argv[j]); */
	if (optind < argc)
	{
		while (optind < argc) {
			char * a = argv[optind++];
			if (*a == '~')
				delete_val(t, atoi_or_abort(a+1));
			else
				insert_val(t, atoi_or_abort(a));
			verify_rbtree(t->L, 0);
		}
	} else {
		srand(time(NULL));
		if (limit == -1)
			limit = 100;
		printf("%sWill run %s%d%s tests.%s\n", YELLOW, CYAN, limit, YELLOW, NOCOLOR);
		while (t->L != nil && limit--) {
			int k = rand() % 50;
			int r = rand();
			if (dbg_level != 0)
				printf("random: %d\n", r);
			if (r % 2 != 0)
				insert_val(t, k);
			else
				if (rb_search(t, k) != 0)
					delete_val(t, k);
		}
		verify_rbtree(t->L, 0);
	}

	fprintf(stderr, "\n");
	print_rbtree("./fig/", t, t, "END");
	printf("Node count: %d\n", node_cnt);
	return 0;
}