btree_t * btree_delete_node(btree_t *root, int num) { btree_t *temp = NULL; if (root == NULL) { return NULL; } else if(num < root->key) root->left = btree_delete_node(root->left, num); else if(num > root->key) root->right= btree_delete_node(root->right, num); else { if(root->left!= NULL && root->right!=NULL) { temp = root->left->key < num ? root->left: root->right; root->key = temp->key; root->right= btree_delete_node(root->right, root->key); } else if(root->left == NULL) { temp = root; root = root->right; } else if(root->right == NULL) { temp = root; root = root->left; } free(temp); } //root->size--; return root; }
int main() { struct b_tree_node_t *bst_root = DF_NODE(10, DF_NODE(6, DF_NODE(3, DF_LEAF(1), DF_LEAF(4)), DF_LEAF(7)), DF_NODE(19, DF_NODE(12, DF_LEAF(11), DF_NODE(17, DF_LEAF(15), DF_LEAF(18))), DF_LEAF(21))); printf("-- initial status:\n"); btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor); printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value); printf("-- adding 17:\n"); btree_insert_node(&bst_root, 17); btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor); printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value); printf("-- adding 30:\n"); btree_insert_node(&bst_root, 30); btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor); printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value); printf("-- adding 1:\n"); btree_insert_node(&bst_root, 1); btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor); printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value); printf("-- adding 4:\n"); btree_insert_node(&bst_root, 4); btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor); printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value); printf("-- remove 11:\n"); btree_delete_node(&bst_root, 11); btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor); printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value); printf("-- remove 10:\n"); btree_delete_node(&bst_root, 10); btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor); printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value); printf("-- remove 3:\n"); btree_delete_node(&bst_root, 3); btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor); printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value); printf("-- remove 4:\n"); btree_delete_node(&bst_root, 4); btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor); printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value); printf("-- remove 15:\n"); btree_delete_node(&bst_root, 15); btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor); printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value); }
int btree_remove(btree_t *tree, uint32_t key, void **data) { btree_node_t *x; btree_validate(tree); x = btree_search(tree->root, key); if (x == NULL) { debug_msg("Item not on tree - key %ul\n", key); *data = NULL; return FALSE; } /* Note value that gets freed is not necessarily the the same * as node that gets removed from tree since there is an * optimization to avoid pointer updates in tree which means * sometimes we just copy key and data from one node to * another. */ *data = x->data; x = btree_delete_node(tree, x); xfree(x); return TRUE; }
void *acl_btree_remove(ACL_BTREE *tree, unsigned int key) { const char *myname = "acl_btree_remove"; BTREE_NODE *x; void *data; btree_validate(tree); x = btree_search(tree->root, key); if (x == NULL) { acl_msg_error("%s(%d): Item not on tree - key %u\n", myname, __LINE__, key); return (NULL); } /* Note value that gets freed is not necessarily the the same * as node that gets removed from tree since there is an * optimization to avoid pointer updates in tree which means * sometimes we just copy key and data from one node to another. */ data = x->data; x = btree_delete_node(tree, x); acl_slice_free2(tree->slice, x); return (data); }