Ejemplo n.º 1
0
const void*
tr_tree_max(const tr_tree* tree)
{
    ASSERT(tree != NULL);

    return tree_max(tree);
}
Ejemplo n.º 2
0
Node* tree_max(const Node *p)
{
	Node *p = const_cast<Node*>(T);
	if (NIL == p)
		return p;

	return (NIL != RIGHT(p))? tree_max(RIGHT(p)): p;
}
Ejemplo n.º 3
0
/**
 * Returns the last (highest) key in the table and sets the out parameter
 * to it.
 *
 * @param[in] table the table in which the lookup is performed
 * @param[out] out  Pointer to where the returned key is stored
 *
 * @return CC_OK if the key was found, or CC_ERR_KEY_NOT_FOUND if not.
 */
enum cc_stat treetable_get_last_key(TreeTable const * const table, void **out)
{
    RBNode *node = tree_max(table, table->root);

    if (node != table->sentinel) {
        *out = node->key;
        return CC_OK;
    }
    return CC_ERR_KEY_NOT_FOUND;
}
Ejemplo n.º 4
0
Node* tree_predecessor(const Node *n)
{
	Node *p = const_cast<Node*>(n);
	if (NIL == p)
		return p;
	
	if (NIL != LEFT(p))
		return tree_max(LEFT(p));

	while(IS_LEFT(p) && (p = PARENT(p)));
	return PARENT(p);
}
Ejemplo n.º 5
0
/**
 * Removes the last (highest) key from the specified table and sets the out
 * parameter to value.
 *
 * @param[in] table the table from which the last entry is being removed
 * @param[out] out Pointer to where the removed value is stored, or NULL
 *                 if it is to be ignored
 *
 * @return CC_OK if the mapping was successfully removed, or CC_ERR_KEY_NOT_FOUND
 * if the key was not found.
 */
enum cc_stat treetable_remove_last(TreeTable *table, void **out)
{
    RBNode *node = tree_max(table, table->root);

    if (!node)
        return CC_ERR_KEY_NOT_FOUND;

    if (out)
        *out = node->value;

    remove_node(table, node);
    return CC_OK;
}
Ejemplo n.º 6
0
/**
 * Returns a predecessor node of the node <code>x</code>
 *
 * @param[in] table the table on which this operation is performed
 * @param[in] x the node whose predecessor is being returned
 *
 * @return predecessor node of x
 */
static RBNode *get_predecessor_node(TreeTable const * const table, RBNode *x)
{
    if (x == NULL)
        return NULL;

    if (x->left != table->sentinel)
        return tree_max(table, x->left);

    RBNode *y = x->parent;

    while (y != table->sentinel && x == y->left) {
        x = y;
        y = y->parent;
    }
    return y;
}
Ejemplo n.º 7
0
/* find the one before node n*/
tnode_t * tree_predecessor(tnode_t *x)
{
	if(x->lchild != NULL)
		return tree_max(x->lchild);

	/* x->lchild == NULL */

	tnode_t *y = x->parent;

	/* exit when (1) x is the right child of his parent. (2) x's parent is NULL. */
	while(y != NULL && x == y->lchild)
	{
		x = y;
		y = y->parent;
	}
	return y;
}
Ejemplo n.º 8
0
treenode_t * tree_predecessor(treenode_t * root){
    if (root == gNil) {
        printf("Tree is null.\n");
        return gNil;
    }
        
    treenode_t * ynode = gNil;
    
    if (root->left_child != gNil) {
        return tree_max(root->left_child);
    }
    
    ynode = root->parent;
    while (ynode && ynode->left_child == root) {
        root = ynode;
        ynode = ynode->parent;
    }

    return ynode;
}
Ejemplo n.º 9
0
int main() {
	int n; scanf("%d", &n);

	for (int i = 0; i < n; i++)
		scanf("%d", &ts[i].x);
	for (int i = 0; i < n; i++) 
		scanf("%d", &ts[i].y);
	for (int i = 0; i < n; i++)
		scanf("%d", &ts[i].z);

	sort(0, n-1);

	for (int i = 0; i < n; i++)
		fprintf(stderr, "%d %d %d\n", ts[i].x, ts[i].y, ts[i].z);

	for (int i = 0; i < n; i++)
		ys[i] = ts[i].y;

	sort_ys(0, n-1);
	ys_n = 1;
	for (int i = 1; i < n; i++) 
		if (ys[i] != ys[i-1])
			ys[ys_n++] = ys[i];

	tree_init(ys_n+1);
	int c = 0;

	for (int i = n-1; i >= 0; i--) {
		int j = ys_n-ys_find(ts[i].y);
		fprintf(stderr, "j = %d\n", j);
		int v = tree_max(j-1);
		fprintf(stderr, "v = %d\n", v);
		if (v > ts[i].z)
			c++;
		tree_update(j, ts[i].z);
	}

	printf("%d\n", c);

	return 0;
}
Ejemplo n.º 10
0
void testBSTree() {

    printf("\nNow is Create Binary tree with node size %d >>>>>>>>>>\n", ARRAY_SIZE);
    randomize_in_place(A, ARRAY_SIZE);
    //    randomize_maxnum(A, ARRAY_SIZE, ARRAY_SIZE);
    print_array(A, ARRAY_SIZE);

    startProfileTime();
    tree_t * tree = tree_create(A, ARRAY_SIZE);
    endProfileTime("Create Binary search tree ");

#if 0
    printf("\nPre order traverse:\n");
    tree_preorder_traverse(tree->root, my_treenode_key_traverse);

    printf("\nPost order traverse:\n");
    tree_postorder_traverse(tree->root, my_treenode_key_traverse);

    printf("\nIn order traverse:\n");
    tree_inorder_traverse(tree->root, my_treenode_key_traverse);
#endif

    int key = 50;
    startProfileTime();

    treenode_t * search_result = tree_search(tree->root, key);
    endProfileTime("Binary tree search");
    
    if (search_result != get_nil_node()) {
        printf("Found key:%d\n", key);
    } else {
        printf(" Not found key:%d\n", key);
    }
    
    tree_left_rotate(tree, search_result);
    tree_right_rotate(tree, search_result);

    traverse_no_recurise(tree->root, my_treenode_key_traverse);

    treenode_t * max, * min;
    max = tree_max(tree->root);
    min = tree_min(tree->root);
    printf("\nmax = %ld\n min = %ld\n", max->key, min->key);

    treenode_t * bigger = tree_successor(search_result);
    printf("successor = %ld\n", (bigger!=NULL) ? bigger->key : -1);
    treenode_t * smaller = tree_predecessor(search_result);
    printf("perdecessor = %ld\n", (smaller!=NULL) ? smaller->key : -1);
    
    //Test delete:
        treenode_t * deleted_node = RBTree_delete(tree, search_result);
        //    treenode_t * deleted_node = tree_delete(tree, search_result);
    if (deleted_node)
        printf("del %p, key=%ld from tree.\n", deleted_node, deleted_node->key);
    tree_inorder_traverse(tree->root, my_treenode_key_traverse);
    //    traverse_no_recurise(tree->root, my_treenode_key_traverse);
    
    int height = get_tree_height(tree->root);
    printf("\nget tree h = %d\n", height);
 
    tree_destroy(tree, NULL);
}
Ejemplo n.º 11
0
int main()
{
	tnode_t *n;
	searchTree_t root = NULL;;
	
	int loops = 80000000;
	int i,rd;
	int max = 99999999;


	srand(time(NULL));
	for(i = 1;i<= loops;i++)
	{
		rd = rand() % max + 1;
//		printf("rd:%d\n",rd);
	
		n = malloc(sizeof(tnode_t));
		n->data = rd;

		root = tree_insert(root,n);
		if(root == NULL)
			printf("insert error\n");
//		inorder_walk_tree(root,print_tnode); printf("\n");
	}
		
	inorder_walk_tree(root,print_tnode); printf("\n");

	struct timeval start,end;

	gettimeofday(&start,NULL);
	tnode_t *r =  tree_search_v1(root , 9999998);
	gettimeofday(&end,NULL);

	if(r == NULL)
		printf("not found\n");
	else
		printf("found %d at 0x%x\n",r->data,(unsigned int)r);

	printf("it takes %ld seconds,%ld micorseconds in v1\n",(long)(end.tv_sec - start.tv_sec),(long)(end.tv_usec - start.tv_usec));


	r = NULL;
	gettimeofday(&start,NULL);
	r =  tree_search_v2(root , 9999998);
	gettimeofday(&end,NULL);

	if(r == NULL)
		printf("not found\n");
	else
		printf("found %d at 0x%x\n",r->data,(unsigned int)r);

	printf("it takes %ld seconds,%ld micorseconds in v2\n",(long)(end.tv_sec - start.tv_sec),(long)(end.tv_usec - start.tv_usec));


	tnode_t * min = tree_min(root);
	if(min != NULL)
		printf("min is %d\n",min->data);

	tnode_t * mx = tree_max(root);
	if(mx != NULL)
		printf("max is %d\n",mx->data);

	tree_destroy(root);
}
Ejemplo n.º 12
0
void transplant( nodeptr *root , nodeptr *u , nodeptr *v )
{
	if( (*u)->parent == NULL ) // root node
		*root = *v;
	else if ( (*u)->parent->left == *u )
		(*u)->parent->left = *v;
	else if ( (*u)->parent->right == *u ) 
		(*u)->parent->right = *v;

	if( *v != NULL )
		(*v)->parent = (*u)->parent;
}
nodeptr delete(nodeptr *root )
{
	nodeptr temp_root = *root;
	nodeptr z = tree_max(temp_root);  
	if( z->left == NULL )
		transplant(root,&z,&(z->right));
	else if ( z->right == NULL )
		transplant(root,&z,&(z->left));
	
	z->left = z->right = NULL;
	return z;
}
int isempty(nodeptr root)
{
	return ( root == NULL );  
}
void inorder(nodeptr root)
{
	if(root == NULL )
Ejemplo n.º 13
0
int main()
{
	tnode_t *n;
	searchTree_t tree;
	tree.root = NULL;
	tree.size = 0;

	int loops = 10;
	int i,rd;
	int max = 9;
	int status = -2;


	srand(time(NULL));
	for(i = 1;i<= loops;i++)
	{
		rd = rand() % max + 1;
		printf("rd:%d\n",rd);
	
		n = malloc(sizeof(tnode_t));
		n->data = rd;

		status = tree_insert(&tree,n);
		if(status == -1)
			printf("insert error\n");
//		inorder_walk_tree(tree.root,print_tnode); printf("\n");
	}
		
	inorder_walk_tree(tree.root,print_tnode); printf("\n");

	printf("deep walk\n");
	deep_walk(tree.root);
	
	struct timeval start,end;

	gettimeofday(&start,NULL);
	tnode_t *r =  tree_search_v1(tree.root , max - 1);
	gettimeofday(&end,NULL);

	if(r == NULL)
		printf("not found\n");
	else
		printf("found %d at 0x%x\n",r->data,(unsigned int)r);

	printf("it takes %ld seconds,%ld micorseconds in v1\n",(long)(end.tv_sec - start.tv_sec),(long)(end.tv_usec - start.tv_usec));


	r = NULL;
	gettimeofday(&start,NULL);
	r =  tree_search_v2(tree.root , max - 1);
	gettimeofday(&end,NULL);

	if(r == NULL)
		printf("not found\n");
	else
		printf("found %d at 0x%x\n",r->data,(unsigned int)r);

	printf("it takes %ld seconds,%ld micorseconds in v2\n",(long)(end.tv_sec - start.tv_sec),(long)(end.tv_usec - start.tv_usec));


	tnode_t * min = tree_min(tree.root);
	if(min != NULL)
		printf("min is %d\n",min->data);

	tnode_t * mx = tree_max(tree.root);
	if(mx != NULL)
		printf("max is %d\n",mx->data);
	

	tnode_t *s = tree_successor(min);
	tnode_t *pre = tree_predecessor(s);
	printf("successor of min is %d\n",s->data);
	printf("predecessor of s is %d\n",pre->data);


	tnode_t *pre2 = tree_predecessor(mx);
	tnode_t *s2 = tree_successor(pre2);
	printf("predecessor of max is %d\n",pre2->data);
	printf("successor of pre2 is %d\n",s2->data);

	tree_delete(&tree, min);
	printf("delete\n");
	inorder_walk_tree(tree.root,print_tnode); printf("\n");

	printf("deep walk\n");
	deep_walk(tree.root);
	tree_destroy(tree.root);
}