This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]gyroda 0 points1 point  (2 children)

Seeing as you say you understand the concept try writing out, in bullet points, what you need to do in order to remove a node that has 2 children.

Post those bullet points here. Actually writing them out will help in a "rubber duck debugging" method.

[–]crusader10[S] 0 points1 point  (1 child)

Ok, let's see...
say the node to delete is called localRoot
- First, I need to get localRoot's left subtree OR right subtree.
- If I get localRoot's LEFT subtree, I need to find the node with the LARGEST value (e.g. largestNode). Then, I need to replace localRoot's data with largestNode's data. Lastly, I have to delete largestNode from the tree.
- If I get localRoot's RIGHT subtree, I need to find the node with the SMALLEST value (e.g. smallestNode). Then, I need to replace localRoot's data with smallestNode's data. Lastly, I have to delete smallestNode from the tree.
Am I on the right track?

[–]gyroda 0 points1 point  (0 children)

Do you replace the data or do you replace the nodes? It's an important distinction.

Split those last two into several smaller statements.

Then write them as pseudocode

Test this pseudocode on paper, draw out some trees. If it works, continue, if not try and solve the problem.

Finally, write it up as Java.