コード例 #1
0
ファイル: t_rbtree.c プロジェクト: xuzhixi/ky
// 获取黑色路径的长度, 及测试各路径黑色节点数目是否一致
int black_count(ky_rbtree_s *root)
{
	if ( root == ky_rbtree_nil )
	{
		return 1;
	}
	else
	{
		int left_black_count;
		int right_black_count;

		left_black_count = black_count(root->left);
		right_black_count = black_count(root->right);
		if ( root->color == KY_RBTREE_RED )
		{
			if ( left_black_count == right_black_count )
			{
				return left_black_count;
			}	
			else
			{
				printf("某路径黑色节点数目不一致!!!key: %d\n", *(int *)(root->key));
				return -1;
			}
		}
		else
		{
			if ( left_black_count == right_black_count )
			{
				return left_black_count + 1;
			}	
			else
			{
				printf("某路径黑色节点数目不一致!!!key: %d\n", *(int *)(root->key));
				return -1;
			}
		}
	}
}
コード例 #2
0
ファイル: rb-tree-main.c プロジェクト: 7sujit/libds
int main(void)
{

    struct Node *root = NULL;
    root_ptr = NULL;
    long int loop;
    long int value;
    int ch;long int x;

    scanf("%ld",&loop );
    x = loop;
    while(loop --)
    {
        // ch = 1;
        scanf("%d %ld",&ch, &value );
        // scanf("%ld", &value);
        switch (ch) {
            case 0 :
                // (search(root, value)) ? printf("%s\n", "VALUE FOUND") : printf("%s\n", "VALUE NOT FOUND");
                break;
            case 1:
                rb_tr_insert( value);
                root = root_ptr;
                inorder(root);
                break;
            case 2:
                root = root_ptr;
                if(!root)
                {
                    printf("%s\n", "ROOT IS NULL");
                    break;
                }
                rb_tr_delete(value);
                root = root_ptr;
                inorder(root);
                break;
            case 3:
                root = root_ptr;
                // printf("rank of element %ld : %ld\n",value, rank(root, value));
                break;
            case 4:
                // root = root_ptr;
                // if(! root)
                //     break;
                // if(value > 1+root->lcount+root->rcount)
                // {
                //     printf("%s\n","EXCEEDS NUMBER OF ELEMENTS PRESENT IN TREE" );
                //     break;
                // }
                // printf("no at rank %ld : %ld\n",value, find_rank(root, value));
                break;
            default:
                break;
        }
    }
    printf("red nodes : %d\n", red_count(root_ptr));
    printf("root color : %s\n",(root_ptr->col)? "RED" : "BLACK" );
    printf("black nodes : %d\n", black_count(root_ptr));
    printf("lcount : %d, rcount = %d\n",root_ptr->lcount, root_ptr->rcount );

    // check_double_red(root_ptr);
    // black_path(root_ptr,0);
    // inorder(root_ptr);



    return EXIT_SUCCESS;
}
コード例 #3
0
ファイル: t_rbtree.c プロジェクト: xuzhixi/ky
int main()
{
	ky_rbtree_t *rbTree;
	int isQuit = 0;
	int key;
	int value;
	uint8 c;
	int i;

	rbTree = ky_rbtree_new(sizeof(int), sizeof(int), ky_cmp_int);

	srand( time(NULL) );
	for (i=0; i<50; i++)		// 随机插入
	{
		key = rand() % 100;
		value = rand() % 1000;
		ky_rbtree_add( rbTree, &key, &value );
		test_two_red( rbTree->tree );	// 每次插入都进行测试
		black_count( rbTree->tree );
	}
	test_two_red( rbTree->tree );
	printf("黑色路径长度: %d\n", black_count( rbTree->tree ) );
	//printf("################################################\n");
	//preorder_traversal( rbTree->tree );
	//printf("################################################\n\n");

	for (i=0; i<200; i++)		// 随机删除
	{
		key = rand() % 100;
		ky_rbtree_del( rbTree, &key );
		test_two_red( rbTree->tree );	// 每次删除都进行测试
		black_count( rbTree->tree );
	}
	test_two_red( rbTree->tree );
	printf("黑色路径长度: %d\n", black_count( rbTree->tree ) );
	printf("################################################\n");
	preorder_traversal( rbTree->tree );
	printf("################################################\n");

	/*
	while (1)
	{
		printf("\n\n1. add\n2. mod\n3. del\n4. show data\n5. quit\nplease input your choise: ");
		scanf("%d", &c);
		switch ( c )
		{
			case 1:
				printf("please input yout key and value: ");
				scanf("%d %d", &key, &value);	
				ky_rbtree_add( rbTree, &key, &value );
				break;
			case 2:
				printf("please input your key and value: ");
				scanf("%d %d", &key, &value);	
				ky_rbtree_mod( rbTree, &key, &value );
				break;
			case 3:
				printf("please input yout key: ");
				scanf("%d", &key);	
				ky_rbtree_del( rbTree, &key );
				break;
			case 4:
				printf("################################################\n");
				preorder_traversal( rbTree->tree );
				printf("################################################\n");
				break;
			case 5:
				isQuit = 1;
				break;
			default:
				break;
		}

		if ( isQuit == 1 )
		{
			ky_rbtree_release( rbTree );
			break;
		}
	}
	*/

	return 0;
}