remove


Description:

public bool remove (K key)

Removes a key/value pair from a Tree.

If the Tree was created using Tree.full, the key and value are freed using the supplied destroy functions, otherwise you have to make sure that any dynamically allocated values are freed yourself. If the key does not exist in the Tree, the function does nothing.

The cost of maintaining a balanced tree while removing a key/value result in a O(n log(n)) operation where most of the other operations are O( log(n)).

Example: Remove a key/value pair:

public static int main (string[] args) {
Tree<string, string> tree = new Tree<string, string>.full ((a, b) => { return strcmp (a, b); }, free, free);
tree.insert ("key1", "val1");
tree.insert ("key2", "val2");
tree.insert ("key3", "val3");
tree.insert ("key4", "val4");

bool tmp = tree.remove ("key4");
// Output: ``removed: true``
print ("removed: %s\n", tmp.to_string ());

// Output:
// ``key=key1, value=val1``
// ``key=key2, value=val2``
// ``key=key3, value=val3``
tree.@foreach ((key, val) => {
print ("key=%s, value=%s\n", (string) key, (string) val);
return false;
});

return 0;
}

valac --pkg glib-2.0 GLib.Tree.remove.vala

Parameters:

this

a Tree

key

the key to remove

Returns:

true if the key was found (prior to 2.8, this function returned nothing)