C++ Binary Search Tree - Deleting a Node / Return Type

I am working with Binary Search Trees and have been checking out a variety of implementations online, as well as in some books. When deleting from a binary search tree, the common return type seems to be a node* and I'm having trouble grasping why that's the common practice.

I guess its part of a broader question on deciding return types in general for most operations.

In class, we had traditionally used bool/int return types to indicate some kind of success status back to the user, but every implementation I try of an int return type (when deleting from a BST) has some sort of problem.

So I figured I'd start with asking why its common to see node* return types for BST operations (deletion in particular) and then ask how people generally decide return types.
I see no reason why it would be.
delete one node, seems like that would be

void delete(sometypetype data_to_delete) //or bool if you want to know if you did anything or int for a code

personally, when I delete from a classic bst tree I mark the node as deleted rather than actually do anything, and clean up later. Just a thought.

many libraries do return success or error codes for most actions. Its not a bad design. Many others just do what seems best for the action at hand.

so what went wrong with your delete when you tried to make it int?
To delete an item I'd return bool:
1
2
// Attempt to remove data from the tree. Returns false if data isn't in the tree, otherwise true
bool remove(const DataType &data);


Internally, I usually find it's helpful to write this routine:
1
2
3
4
// Find data in the tree. This returns a reference to a pointer that points to the node
// where data is, or where it should go if it were inserted.  In other words, it returns
// a reference to root, or the right or left pointer of some node.
Node * &find(const DataType &data);


This can be used for higher level insert/update/remove logic.
When deleting from a binary search tree, the common return type seems to be a node*

To me it depends on what you're passing to the delete() function.

1) If you're passing a pointer to a node to be deleted from the tree, then returning a bool indicating success or failure makes sense. Often when you've found the node you want to delete from the tree, you then want to then delete the node itself. If you've passed a pointer to the delete() function, you already have a pointer to pass to delete.

2) On the other hand, if your delete function is based on passing data to it, returning a pointer to the node is handy in case you want to delete the node object. You can of course return nullptr to in the event the node was not found.

Both of these approaches assume that your delete () function does not delete the node object. If that's the case, returning a pointer to it makes no sense since the node object no longer exists.


Thank you all for the assistance. I intuitively went with a combination of the find first before doing operations, since it seemed to make more sense, rather than doing comparative operations in a bunch of different functions.
c
Last edited on
can someone help me on my topic?

Hijacking someone else's thread isn't a good way to get help.

sorry
Last edited on
Topic archived. No new replies allowed.