static void binomial_tree_node_destroy(binomial_tree_node_t *node, int rindex) { int i; binomial_tree_node_t *new_parent = NULL; if (node->parent) { new_parent = node->parent; int node_index = -1; for (i = 0; i < new_parent->num_children; i++) { if (new_parent->children[i] == node) { node_index = i; break; } } if (new_parent->num_children && node_index >= 0) { int to_copy = new_parent->num_children - (node_index + 1); if (to_copy) { memmove(&new_parent->children[node_index], &new_parent->children[node_index+1], sizeof(binomial_tree_node_t *) * to_copy); } else { // TODO - Error messages // (something is badly corrupted if we are here) } new_parent->num_children--; } } else { // the node is a root node, let's first remove it from the list of trees int item_num = 0; if (rindex) { (void)list_fetch_value(node->bh->trees, rindex); item_num = rindex + 1; } else { item_num = list_foreach_value(node->bh->trees, binheap_remove_root_node, node); } if (node->num_children) { int child_index = node->bh->mode == BINHEAP_MODE_MAX ? binomial_tree_node_find_max_child(node) : binomial_tree_node_find_min_child(node); if (child_index >= 0) { new_parent = node->children[child_index]; if (child_index < node->num_children - 1) { memmove(&node->children[child_index], &node->children[child_index + 1], sizeof(binomial_tree_node_t *) * (node->num_children - (child_index + 1))); } node->num_children--; new_parent->parent = NULL; if (item_num > 0) // and finally add one of its child back to the list of trees list_insert_value(node->bh->trees, new_parent, item_num - 1); } } } for (i = 0; i < node->num_children; i++) { if (new_parent) binomial_tree_node_add(new_parent, node->children[i]); else node->children[i]->parent = NULL; } if (node == node->bh->head) UPDATE_HEAD(node->bh); free(node->key); node->bh->count--; free(node); }
tagged_value_t * list_fetch_tagged_value(linked_list_t *list, size_t pos) { return (tagged_value_t *)list_fetch_value(list, pos); }