コード例 #1
0
ファイル: tr_tree.c プロジェクト: ashoka-versa/libdict
const void*
tr_tree_min(const tr_tree* tree)
{
    ASSERT(tree != NULL);

    return tree_min(tree);
}
コード例 #2
0
/**
 * Removes a node from the RB tree.
 *
 * @param[in] table the table on which this operation is performed
 * @param[in] z the node that is being removed
 */
static void remove_node(TreeTable *table, RBNode *z)
{
    RBNode *x;
    RBNode *y = z;

    int y_color = y->color;

    if (z->left == table->sentinel) {
        x = z->right;
        transplant(table, z, z->right);
    } else if (z->right == table->sentinel) {
        x = z->left;
        transplant(table, z, z->left);
    } else {
        y = tree_min(table, z->right);
        y_color = y->color;
        x = y->right;
        if (y->parent == z) {
            x->parent = y;
        } else {
            transplant(table, y, y->right);
            y->right = z->right;
            y->right->parent = y;
        }
        transplant(table, z, y);
        y->left = z->left;
        y->left->parent = y;
        y->color = z->color;
    }
    if (y_color == RB_BLACK)
        rebalance_after_delete(table, x);

    table->mem_free(z);
    table->size--;
}
コード例 #3
0
ファイル: tree.cpp プロジェクト: jarfield/SolutionToCLR2nd
Node* tree_min(const Node *T)
{
	Node *p = const_cast<Node*>(T);
	if (NIL == p)
		return p;
	
	return (NIL != LEFT(p))? tree_min(LEFT(p)): p;
}
コード例 #4
0
/**
 * Applies the function fn to each value of the TreeTable.
 *
 * @param[in] table the table on which this operation is being performed
 * @param[in] fn the operation function that is invoked on each value of the
 *               table
 */
void treetable_foreach_value(TreeTable *table, void (*fn) (void *k))
{
    RBNode *n = tree_min(table, table->root);

    while (n != table->sentinel) {
        fn(n->value);
        n = get_successor_node(table, n);
    }
}
コード例 #5
0
/**
 * Returns the first (lowest) 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_first_key(TreeTable const * const table, void **out)
{
    RBNode *node = tree_min(table, table->root);

    if (node != table->sentinel) {
        *out = node->key;
        return CC_OK;
    }
    return CC_ERR_KEY_NOT_FOUND;
}
コード例 #6
0
/**
 * Checks whether or not the TreeTable contains the specified value.
 *
 * @param[in] table the table into which the lookup is performed
 * @param[in] value the value that is being looked up
 *
 * @return number of occurrences of the specified value.
 */
size_t treetable_contains_value(TreeTable const * const table, const void *value)
{
    RBNode *node = tree_min(table, table->root);

    size_t o = 0;
    while (node != table->sentinel) {
        if (node->value == value)
            o++;
        node = get_successor_node(table, node);
    }
    return o;
}
コード例 #7
0
ファイル: tree.cpp プロジェクト: jarfield/SolutionToCLR2nd
Node* tree_successor(const Node *n)
{
	Node *p = const_cast<Node*>(n);
	if (NIL == p)
		return p;

	if (NIL != RIGHT(p))
		return tree_min(RIGHT(p));
	
	while (IS_RIGHT(p) && (p = PARENT(p)));	
	return PARENT(p);
}
コード例 #8
0
/**
 * Removes the first (lowest) key from the specified table and sets the out
 * parameter to value.
 *
 * @param[in] table the table from which the first 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_first(TreeTable *table, void **out)
{
    if (table->size == 0)
        return CC_ERR_KEY_NOT_FOUND;

    RBNode *node = tree_min(table, table->root);

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

    remove_node(table, node);
    return CC_OK;
}
コード例 #9
0
/**
 * Returns a successor node of the node <code>x</code>
 *
 * @param[in] table the table on which this operation is performed
 * @param[in] x the node whose successor is being returned
 *
 * @return successor node of x
 */
static RBNode *get_successor_node(TreeTable const * const table, RBNode *x)
{
    if (x == NULL)
        return NULL;

    if (x->right != table->sentinel)
        return tree_min(table, x->right);

    RBNode *y = x->parent;

    while (y != table->sentinel && x == y->right) {
        x = y;
        y = y->parent;
    }
    return y;
}
コード例 #10
0
ファイル: treev2.c プロジェクト: tsiangleo/clrs
/* find the one after node n*/
tnode_t * tree_successor(tnode_t *x)
{
	if(x->rchild != NULL)
		return tree_min(x->rchild);
	
	/* x->rchild == NULL */

	tnode_t *y = x->parent;
		
	/* exit when (1) x is the left child of his parent. (2) x's parent is NULL. */
	while(y != NULL && x == y->rchild)
	{
		x = y;
		y = y->parent;
	}
	return y;
}
コード例 #11
0
ファイル: binary_tree.c プロジェクト: sirithink/ITA
treenode_t * tree_successor(treenode_t * root){
    if (root == gNil) {
        printf("Tree is null.\n");
        return gNil;
    }
        
    treenode_t * ynode = gNil;

    if (root->right_child != gNil) {
        return tree_min(root->right_child);
    }

    ynode = root->parent;
    while (ynode && ynode->right_child == root) {
        root = ynode;
        ynode = ynode->parent;
    }
    return ynode;
}
コード例 #12
0
ファイル: treev2.c プロジェクト: tsiangleo/clrs
/* since this operation may change the root of this tree, the parameter is a pointer to the structure searchTree, rather than a pointer of the root node.
 * insert an node into tree t, return 0 if SUCCESS, -1 on error. */
void tree_delete(searchTree_t *t, tnode_t *node)
{
	tnode_t *y;

	if(node->lchild == NULL)
		transplant(t,node,node->rchild);
	else if(node->rchild == NULL)
		transplant(t,node,node->lchild);
	else
	{
		y = tree_min(node->rchild);
		if(y->parent != node)
		{
			transplant(t,y,y->rchild);
			y->rchild = node->rchild;
			y->rchild->parent = y;
		}
		transplant(t,node,y);
		y->lchild = node->lchild;
		y->lchild->parent = y;
	}
}
コード例 #13
0
ファイル: main.c プロジェクト: herbertdai/ITA
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);
}
コード例 #14
0
ファイル: treev2.c プロジェクト: tsiangleo/clrs
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);
}
コード例 #15
0
/**
 * Initializes the TreeTableIter structure.
 *
 * @param[in] iter the iterator that is being initialized
 * @param[in] table the table over whose entries the iterator is going to iterate
 */
void treetable_iter_init(TreeTableIter *iter, TreeTable *table)
{
    iter->table   = table;
    iter->current = table->sentinel;
    iter->next    = tree_min(table, table->root);
}
コード例 #16
0
ファイル: binary_tree.c プロジェクト: nit99kumar/algo_lab
int delete_item(node *root)
{
  int exchange;
  node *min_par, *min;
  node *temp1, *temp2;	
  temp1=root;
  int val;
  printf("please enter the value to be deleted: ");
  scanf("%d",&val);
  printf("\n");
  
  if(temp1==NULL)
    {
      printf("tree not created.\n");
      return 0;
    }
  temp2=temp1;
  while(val!=temp1->data)
    {
      temp2=temp1;
      if(val > temp1->data)
	temp1=temp1->rchild;
      else
	temp1=temp1->lchild;
    }
  
  if(temp1->lchild==NULL && temp1->rchild==NULL)
    {
      if(temp1->data > temp2->data)
        {
	  temp2->rchild=NULL;
	  free(temp1);
	  return val;
        }
      else
        {
	  temp2->lchild=NULL;
	  free(temp1);                                     
	  return val;
        }
    }
  
  else if(temp1->lchild==NULL && temp1->rchild!=NULL)
    {
      if(temp1->data > temp2->data)
        {
	  temp2->rchild=temp1->rchild;
	  free(temp1);
	  return val;
        }
      else
        {
	  temp2->lchild=temp1->rchild;
	  free(temp1);                                     
	  return val;
        }                   
    }
  
  else if(temp1->lchild!=NULL && temp1->rchild==NULL)
    {
      if(temp1->data > temp2->data)
        {
	  temp2->rchild=temp1->lchild;
	  free(temp1);
	  return val;
        }
      else
        {
	  temp2->lchild=temp1->lchild;
	  free(temp1);                                     
	  return val;
        }                   
    }
  
  else if(temp1->lchild!=NULL && temp1->rchild!=NULL)
    {
      min_par=tree_min(temp1->rchild);
      min=min_par->lchild;
      exchange=min->data;
      min->data=temp1->data;
      temp1->data=exchange;
      
      if(min->lchild==NULL && min->rchild==NULL)
        {
	  min_par->lchild=NULL;
	  free(min);                 
	  return val;
        }
      
      else if(min->rchild!=NULL)
        {
	  min_par->lchild=min->rchild;
	  free(min);
	  return val;
        }
    }
}
コード例 #17
0
ファイル: tree.c プロジェクト: tsiangleo/clrs
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);
}