The manpage for the POSIX binary tree functions includes the following statements:
tdelete()
returns a pointer to the parent of the item deleted, orNULL
if the item was not found.
tdelete()
frees the memory required for the node in the tree. The user is responsible for freeing the memory for the corresponding data.
This means that there is no way to get access to a node's data for a given key from a tdelete()
call. One would be required to call tfind()
(rather than tsearch()
so as not to add the given key), perform destruction of the node's data, and then call tdelete()
with the same key to remove the node from the binary tree.
Have I interpreted this correctly? Is there some way around what I perceive to be limitations with this approach?
- If the key is heap-allocated, it can't be freed (or made useless to the comparison function in use) before the node is deleted. This requires calling
tfind()
to obtain a pointer to the data,tdelete()
to remove the node, and then destroying the data retrieved from thetfind()
call. - Two lookups are required to delete a node and destroy it's enclosed data.