Ejemplo n.º 1
0
static void print_rbtree(struct rbtree *root, int zero)
{
	static char str[10][100];
	static int n = 0;
	int i=0;
	
	if(zero > n)	n = zero;
	if(zero == 0)	memset(str, 0, sizeof(str));

	if(root == NULL)
	{
		strcat(str[zero], "N ");
	}
	else
	{
		strcat(str[zero], (root->color) ? "B " : "R ");
	}
	
	if(root == NULL)
		return;
	
	print_rbtree(root->left, zero+1);
	print_rbtree(root->right, zero+1);

	if(zero == 0)
	{
		printf("------ \n");
		for(i=0; i<=n; i++)
		{
			printf("%s \n", str[i]);
		}
	}
}
Ejemplo n.º 2
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;
}