struct nod_arbore* tree_delete(struct nod_arbore *radacina, int data) { if (radacina == NULL) { return NULL; } else if (data < radacina->key) { radacina->left = tree_delete(radacina->left, data); } else if (data > radacina->key) { radacina->right = tree_delete(radacina->right, data); } else { //we found the item to delete //case 1: No child a.k.a leaf node if (radacina->left == NULL && radacina->right == NULL) { free(radacina); radacina = NULL; //case 2: No left child } else if (radacina->left == NULL) { struct nod_arbore *temp = radacina; radacina = radacina->right; free(temp); //case 2: No right child } else if (radacina->right == NULL) { struct nod_arbore *temp = radacina; radacina = radacina->left; free(temp); } else { //case 3 : 2 children struct nod_arbore *temp = MIN_TREE(radacina->right); radacina->key = temp->key; radacina->right = tree_delete(radacina->right,temp->key); } } return radacina; }
void mncl_release_raw(MNCL_RAW *raw) { struct resmap_node seek, *found = NULL, *found2 = NULL; if (!raw) { return; } seek.resource = raw; found = (struct resmap_node *)tree_find(&reverse_map, (TREE_NODE *)&seek, ptrcmp); if (!found) { return; } printf("Mapped to %s...", found->resname); found2 = (struct resmap_node *)tree_find(&locked_resources, (TREE_NODE *)found, rescmp); if (found2) { --found2->refcount; if (!found2->refcount) { printf("freeing.\n"); tree_delete(&reverse_map, (TREE_NODE *)found); tree_delete(&locked_resources, (TREE_NODE *)found2); if (found2->resource->data) { free(found2->resource->data); free(found2->resource); free((void *)found2->resname); free((void *)found->resname); free(found); free(found2); } } else { printf("new refcount %d\n", found2->refcount); } } }
bool calc_sum_zero(tree* my_tree) { assert(my_tree); int ret = 0; if ( (my_tree -> type == TR_SIGN && *(int*)(my_tree -> value) == '+' && ( is_tree_value_num(my_tree -> left, 0) || is_tree_value_num(my_tree -> right, 0))) || (my_tree -> type == TR_SIGN && *(int*)(my_tree -> value) == '-' && ( is_tree_value_num(my_tree -> right, 0))) ) { if (is_tree_value_num(my_tree -> left, 0)) { ret = tree_delete(my_tree, RIGHT); assert(ret == TREE_OK); } else { ret = tree_delete(my_tree, LEFT); assert(ret == TREE_OK); } return true; } return false; }
SearchTree tree_delete(SearchTree tree, int data) { if (tree == NULL ) { return NULL; } if (data < tree->data) { tree->left = tree_delete(tree->left, data); } else if (data == tree->data) { if (tree->left != NULL && tree->right != NULL) { int tmp_data = tree_node_data(tree_find_min(tree->right)); tree->data = tmp_data; tree->right = tree_delete(tree->right, tmp_data); } else if (tree->left != NULL) { TreeNode tmp_node = tree; tree = tree->left; free(tmp_node); } else { TreeNode tmp_node = tree; tree = tree->right; free(tmp_node); } } else { tree->right = tree_delete(tree->right, data); } return tree; }
END_TEST START_TEST(test_tree_basics) { int seven = 7, one = 1, three = 3; ck_assert_int_eq(tree_size(tree), 0); ck_assert_int_eq(*(int *)tree_insert(tree, &seven, &seven), seven); ck_assert_int_eq(tree_size(tree), 1); ck_assert_int_eq(*(int *)tree_insert(tree, &one, &one), one); ck_assert_int_eq(tree_size(tree), 2); ck_assert_int_eq(*(int *)tree_insert(tree, &three, &three), three); ck_assert_int_eq(tree_size(tree), 3); ck_assert_int_eq(*(int *)tree_find(tree, &seven), seven); ck_assert_int_eq(*(int *)tree_find(tree, &one), one); ck_assert_int_eq(*(int *)tree_find(tree, &three), three); ck_assert_int_eq(*(int *)tree_delete(tree, &seven), seven); ck_assert_int_eq(tree_size(tree), 2); ck_assert_int_eq(*(int *)tree_delete(tree, &one), one); ck_assert_int_eq(tree_size(tree), 1); ck_assert_int_eq(*(int *)tree_delete(tree, &three), three); ck_assert_int_eq(tree_size(tree), 0); }
bool calc_operand_one(tree* my_tree) { assert(my_tree); int ret = 0; if ( (my_tree -> type == TR_SIGN && *(int*)(my_tree -> value) == '*' && ( is_tree_value_num(my_tree -> left, 1) || is_tree_value_num(my_tree -> right, 1))) || (my_tree -> type == TR_SIGN && *(int*)(my_tree -> value) == '/' && ( is_tree_value_num(my_tree -> right, 1))) ) { if (is_tree_value_num(my_tree -> left, 1)) { ret = tree_delete(my_tree, RIGHT); assert(ret == TREE_OK); } else { ret = tree_delete(my_tree, LEFT); assert(ret == TREE_OK); } return true; } return false; }
void tree_delete(tree xs) { // {{{ if (!tree_empty(xs->left)) { tree_delete(xs->left); } if (!tree_empty(xs->right)) { tree_delete(xs->right); } free(xs); } // }}}
int main(int argc, char *argv[]) { int sizeOfElement = sizeof(int); int *ins = calloc(1,sizeof(int)); Tree tree = new_tree (sizeOfElement, navigateItem, NULL, NULL, NULL); printf("*******TREE VISUALISATION**********\n\n"); printf("TREE STARTS EMPTY\n\n"); printf("INSERTING VALUES 1-10\n\n"); for (int i = 1; i <= 10; i++) { tree_insert(&tree, &i, NULL); printTest(&tree, printInt); printf("\n"); } printf("DELETING VALUE 10\n\n"); *ins = 10; tree_delete(&tree, ins, NULL); printTest(&tree, printInt); printf("\n"); printf("DELETING VALUE 8\n\n"); *ins = 8; tree_delete(&tree, ins, NULL); printTest(&tree, printInt); printf("\n"); if (tree != NULL) { destroy_tree(&tree); } if (ins != NULL) { free(ins); } return 0; }
void c_test_tree_delete(void) { int items = 2; int sizeOfElement = sizeof(struct complexThing); Tree tree = new_tree(sizeOfElement, navigateComplex, copyComplex, destroyComplex, NULL); for (int i = 1; i < items; i++) { Complex *c = malloc(sizeof(struct complexThing)); c->int_regular = i; c->int_pointer = malloc(sizeof(int)); *c->int_pointer = i+i; tree_insert(&tree, c, NULL); if (c != NULL) { if (c->int_pointer != NULL) { free(c->int_pointer); } free(c); } } for (int d = items - 1; d >= 0; d--) { tree_delete(&tree, &d, navigateSearchComplex); } CU_ASSERT_EQUAL(get_tree_height(tree), 0); if (tree != NULL) { destroy_tree(&tree); } }
int sess_delete(session *sess) { rb_node *node = tree_delete(&sess_head, sess->fd); sess_free(sess); free(node); return 0; }
int main(int argc, char* argv[]) { node_t* root = NULL; root = tree_insert(root, 12); root = tree_insert(root, 10); root = tree_insert(root, 16); root = tree_insert(root, 6); root = tree_insert(root, 18); root = tree_insert(root, 15); node_t* n = tree_search(root, 10); if (n) { printf("find 10\n"); node_t* next = tree_succesor(n); if (next) { printf("next of 10 is %d\n", next->key); } } root = tree_delete(root, 16); printf("inorder_tree_walk\n"); inorder_tree_walk(root); printf("\n"); return 0; }
int main(void) { bt root; root.root = NULL; int i; int key[] = {1, 4, 5, 10, 16, 17, 21}; int num = sizeof(key) / sizeof(int); printf("num:%d\n", num); bn *tmp; srand((unsigned int) 6120); for (i = 0; i < 6120; i++) { tmp = malloc(sizeof(bn)); tmp->key = rand() % 6120; tmp->p = tmp->left = tmp->right = NULL; tree_insert_rec(&root, tmp); } tmp = malloc(sizeof(bn)); tmp->key = 1991; tmp->p = tmp->left = tmp->right = NULL; tree_insert_rec(&root, tmp); tmp = tree_search(root.root, 1991); printf("%d\n", tree_successor(tmp)->key); /*inorder_tree_walk(root.root);*/ printf("\n"); tree_delete(&root, tmp); /*inorder_tree_walk(root.root);*/ /*inorder_tree_walk_nore(root.root);*/ printf("\n"); return 0; }
/* * tree_delete */ void tree_delete(tree_node_t *root, void (*fp) (void *)) { tree_node_t *right; restart: if (!root) { return; } if (root->data) { if (fp) { fp(root->data); } else { free(root->data); } } if (root->key) { free(root->key); } tree_delete(root->left, fp); right = root->right; free(root); /* Avoid tail-recursion: tree_delete (right, fp); */ root = right; goto restart; }
void nsgtk_treeview_destroy(struct nsgtk_treeview *tv) { tree_delete(tv->tree); g_object_unref(tv->input_method); gtk_widget_destroy(GTK_WIDGET(tv->window)); free(tv); }
static void astar_explored_remove(struct astar_node *path) { struct astar_node *ptr; /* Purge the 'found' tree of all nodes in the path */ for (ptr = path; ptr; ptr = ptr->next) tree_delete(&found, &ptr->order); }
void ro_treeview_destroy(ro_treeview *tv) { ro_gui_wimp_event_finalise(tv->w); tree_delete(tv->tree); free(tv); }
void tree_delete(Node *tree, int key) { if (*tree) { if (key == (*tree)->key) { node_delete(tree); } else if (key < (*tree)->key) { tree_delete(&(*tree)->left, key); } else if (key > (*tree)->key) { tree_delete(&(*tree)->right, key); } } return; }
void tree_clear(bool (*func)(tree), tree xs) { // {{{ if (!tree_empty(xs->left)) { if (func(xs->left)) { tree_delete(xs->left); xs->left = NULL; } else { tree_clear(func, xs->left); } } if (!tree_empty(xs->right)) { if (func(xs->right)) { tree_delete(xs->right); xs->right = NULL; } else { tree_clear(func, xs->right); } } } // }}}
void nsgtk_treeview_destroy(struct nsgtk_treeview *tv) { if (tv->tree_flags == TREE_SSLCERT) { ssl_current_session = tv->ssl_data; } tree_delete(tv->tree); g_object_unref(tv->input_method); gtk_widget_destroy(GTK_WIDGET(tv->window)); free(tv); }
struct cat* cat_delete(struct cat* cat) { if (cat) { free(cat->pts); free(cat->cos_radius); cat->hpix = hpix_delete(cat->hpix); cat->tree = tree_delete(cat->tree); free(cat); } return NULL; }
void tree_delete(ltree T, ltree z) { ltree x; int y; if (z->lchild == NULL && z->rchild == NULL)//z has no child { x = z->parent; if (x == NULL)//z is rooot T = NULL; else{ if (x->lchild == z) x->lchild = NULL; if (x->rchild == z) x->rchild = NULL; } } if ((z->lchild == NULL && z->rchild != NULL) || (z->lchild != NULL && z->rchild == NULL))//z has only one child { if (z->lchild != NULL){ z->lchild->parent = z->parent; if (z->parent != NULL){//z is not root if (z->parent->lchild == z){//z is the left child of its parent z->parent->lchild = z->lchild; }else{ z->parent->rchild = z->lchild; } }else{ T = z->lchild; } }else{ z->rchild->parent = z->parent; if (z->parent != NULL){ if (z->parent->lchild == z){ z->parent->lchild = z->rchild; }else{ z->parent->rchild = z->rchild; } }else{ T = z->rchild; } } } if (z->lchild != NULL && z->rchild != NULL)//z has tow children { x = tree_successor(z);//find z's successor and delete it y = x->data;//get data tree_delete(T, x);//delete the successor z->data = y; } }
int main (void) { TreeNode *tree_root = NULL; TreeNode *found = NULL; char *orders[4] = { "no-order", "pre-order", "in-order", "post-order" }; char buf[KEYSIZE] = ""; int record_num = 0; int ch, count = 0; prompt ("Insert"); while (*gets(buf)) { StrLcpy (rec.key, buf, KEYSIZE); rec.id = ++record_num; if (!tree_insert (&tree_root, &rec, sizeof(rec), (CmpFunc)rec_cmp)) { printf ("\n\tTree Insertion Failure!\n"); return (1); } prompt ("Insert"); } prompt ("Delete"); gets (buf); rec.key[0] = '\0'; StrLcpy (rec.key, buf, KEYSIZE); while ((found = tree_find (tree_root, &rec, (CmpFunc)rec_cmp)) != NULL) { tree_print (found->info); tree_root = tree_delete (tree_root, found); count++; } printf ("\n\t%d String(s) Deleted\n", count); printf ("\n\tSelect Tree Traversal Type\n"); printf ("\n\t\t1) pre-order\n\t\t2) in-order\n\t\t3) post-order\n\n\t>"); ch = getch(); ch -= '0'; if (ch < PRE_ORDER || ch > POST_ORDER) ch = NO_ORDER; printf ("\n\t... Walking Tree %s ...\n\n", orders[ch]); t_order = ch; tree_trace (tree_root); tree_free (tree_root); return (0); }
// This method should be locked void_p map_remove(map m, void_p key) { pthread_mutex_lock(m->mutex); data * finder = (data*) malloc(sizeof(struct data)); finder->key = key; c_index = m->comparer_index; data * d = (data *)tree_delete(m->t, finder); free(finder); void_p ret = d->value; if (m->clon_function) { free(d->key); } free(d); pthread_mutex_unlock(m->mutex); return ret; }
void tree_delete(struct node ** root, int x) { struct node * temp_ptr = NULL; if (*root == NULL) { return; } else if (x < (*root)->data) { tree_delete(&((*root)->left), x); } else if (x > (*root)->data) { tree_delete(&((*root)->right), x); } else if ((*root)->left != NULL && (*root)->right != NULL) { (*root)->data = (tree_minimum_node(*root))->data; tree_delete(&((*root)->right), (*root)->data); } else { temp_ptr = *root; if ((*root)->left != NULL) { *root = (*root)->left; } else { *root = (*root)->right; } free(temp_ptr); } }
int main (int argc, char** argv) { tree_t t = tree_new(); list_t l = list_new(); for (int i=0; i<argc-1; i++) list_add_element (l, argv[i+1]); int j=0; for (element_t e = list_get_first_element (l); e != NULL; e = element_get_next (e)) { if (includeElement()) tree_insert_node (t, e); } printf ("Before deleting list:\n"); tree_ordered_suffix_print (t); list_delete (l); printf ("After deleting list:\n"); tree_ordered_suffix_print (t); tree_delete (t); }
/*struct node* tree_minimum(struct node*x) { while(x->left!=NULL) x=x->left; return x; } */ int main() { struct node *root=NULL;//=(struct node*)malloc(sizeof(struct node*)); //root->left = NULL; //root->right=NULL; insert(&root,20); insert(&root,22); insert(&root,6); insert(&root,3); insert(&root,15); insert(&root,13); insert(&root,11); insert(&root,12); insert(&root,14); insert(&root,18); insert(&root,17); insert(&root,19); display_elem(root); tree_delete(&root,6); display_elem(root); return 0; }
void update_popularity_tree(struct bff_user **users, struct tree_node **p_root, int userid, int increment) { struct bff_user user; struct tree_node *node = NULL; if (users[userid]) { user = *users[userid]; printf("Before temporary deletion:\n"); tree_print(*p_root); node = tree_delete(p_root, &user.key); /* Useful debug code: */ #if 1 printf("Found node %p with bff_count %d and userid %d:\n", node, user.key.bff_count, user.key.userid); if (node) { printf("node->key.bff_count = %d node->key.userid = %d\n", node->data.key.bff_count, node->data.key.userid); } printf("After temporary deletion.\n"); tree_print(*p_root); printf(".\n"); #endif free(node); user.key.bff_count += increment; users[userid] = tree_insert(p_root, user); printf("After re-insertion:\n"); tree_print(*p_root); printf(".\n"); if (tree_validate(*p_root) < 0) { abort(); } } }
void s_test_tree_insert_delete(void) { int sizeOfElement = sizeof(int); Tree tree = new_tree(sizeOfElement, navigateItem, NULL, NULL, NULL); int ins = 10; tree_insert(&tree, &ins, NULL); void *p = tree_search(&tree, cb, &ins, NULL); CU_ASSERT_PTR_NOT_NULL(p); tree_delete(&tree, &ins, NULL); p = tree_search(&tree, cb, &ins, NULL); CU_ASSERT_PTR_NULL(p); if (tree != NULL) { destroy_tree(&tree); } }
void s_test_tree_delete(void) { int items = 500; int sizeOfElement = sizeof(int); Tree tree = new_tree(sizeOfElement, navigateItem, NULL, NULL, NULL); //tree_insert(&tree, &find, NULL); for (int i = 1; i < items; i++) { int o = i; tree_insert(&tree, &o, NULL); } for (int d = items - 1; d >= 0; d--) { tree_delete(&tree, &d, NULL); } CU_ASSERT_EQUAL(get_tree_height(tree), 0); if (tree != NULL) { destroy_tree(&tree); } }
static ERTS_INLINE void bf_unlink_free_block(Allctr_t *allctr, Block_t *block) { BFAllctr_t *bfallctr = (BFAllctr_t *) allctr; RBTree_t **root = &bfallctr->mbc_root; RBTree_t *x = (RBTree_t *) block; if (IS_LIST_ELEM(x)) { /* Remove from list */ ASSERT(LIST_PREV(x)); LIST_NEXT(LIST_PREV(x)) = LIST_NEXT(x); if (LIST_NEXT(x)) LIST_PREV(LIST_NEXT(x)) = LIST_PREV(x); } else if (LIST_NEXT(x)) { /* Replace tree node by next element in list... */ ASSERT(BF_BLK_SZ(LIST_NEXT(x)) == BF_BLK_SZ(x)); ASSERT(IS_TREE_NODE(x)); ASSERT(IS_LIST_ELEM(LIST_NEXT(x))); #ifdef HARD_DEBUG check_tree(root, 0, 0); #endif replace(root, x, LIST_NEXT(x)); #ifdef HARD_DEBUG check_tree(bfallctr, 0); #endif } else { /* Remove from tree */ tree_delete(allctr, block); } DESTROY_LIST_ELEM(x); }