Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The first void Remove searches and checks whether the tree contains a node with the passed value, then passes the found node to second Remove void.
Second remove void should be able to recursively remove the passed node and all it's sub nodes aka, childen.

If using the second void Remove method I attempt to remove the found nodes while cycling through them, I get NullReferenceExeption.

I've found countless of videos and tutorials using bool, node and other Remove methods, but none with void, so any help would be appreciated!

C#
public void Remove(int number)
{
    BNode currentnode = Root;

    if (currentnode == null)
    {
        Console.WriteLine("Tree is empty!");
    }

    if (currentnode.Number != number)
    {
        currentnode = FindNode(number, currentnode);
    }

    Remove(currentnode);
}

C#
public void Remove(BNode node)
{

    if (node == null)
    {
        return;
    }

    Remove(node.LeftNode);

    node = null;

    Remove(node.RightNode);

    // fails to execute
}

Thank you in advance!
Posted
Updated 10-May-15 1:30am
v3

1 solution

In the Remove method:



  1. You don't check if the given node is null.
  2. Where is the code that performs the actual remove of the node?

It seems like it should be something like:


C#
public void Remove(BNode node)
{
    if (node == null)
    {
        return;
    }

    Remove(node.LeftNode);
    Remove(node.RightNode);

    // Remove the current node (the parameter).
}
 
Share this answer
 
v2
Comments
[no name] 10-May-15 7:28am    
The idea was to include the actually remove code that deleted the node and all the sub nodes in the second void Remove.
Recursively find all nodes, and when found remove them by assigning null to them (assuming if this is the correct way to remove them).
I'll update the question with the way I intended to remove the nodes.
Shmuel Zang 10-May-15 8:22am    
Are you serious? You set node to null and, use it in the line after. It isn't surprising that you get a NullReferenceException (about node). By the way, setting node to null does nothing to the sent object because the parameter is passed by value. Try to pass the parameter by reference and set it at the last line (after the whole of its uses).
Sergey Alexandrovich Kryukov 10-May-15 11:45am    
Voted 4. To my taste, if (node == null) return would do better.
However, isn't it intentional coding-style feature? Also, there are purists who insist on only one return point, but I would not pay attention for such questionable claims... :-)
—SA
Shmuel Zang 11-May-15 3:20am    
Thanks. I also try to stick with the method of only one return point with an exception for validations checks (in order to not write the whole of the method's code in the right side). But, in this case, maybe it isn't only a validation since there are valid paths that call this method with a null value...
Sergey Alexandrovich Kryukov 11-May-15 3:30am    
I would not take this rule seriously at all. The rationale for it is very weak.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900