Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I read a postorder traversal application to delete a binary tree and a code which is written in c++. I am wondering how to write this code for java. I think we have to set the node as null in postorder way.

void delete(Node root)
{
    if (root!=null){
        delete(root.left);
        delete(root.right);
    }
    root=null;
}
share|improve this question
7  
Just do root = null. Let the GC do its work. –  Luiggi Mendoza 2 days ago
    
@LuiggiMendoza: no. root is a local pointer to the object. Setting it to anything does not affect the calling method. In this case you need to do root.left = null and root.right = null to detach the nodes from the root –  njzk2 2 days ago
1  
@njzk2 that's a variable name problem. The only thing you should do is this.root = null. And do not use misleading names for your local variables. –  Luiggi Mendoza 2 days ago

3 Answers 3

root = null

C++ isn't garbage collected, but Java is. In Java, once an object no longer has any references to it, it will be automatically removed from memory. All of the referenced objects of the garbage collected object will also be removed if they have no other references to them. That bit addresses your question: if the nodes under root don't have any external references, they will be automatically removed after root.

share|improve this answer

Trivially root = null;. However there is a difference with C++ as Java cannot change a passed variable:

delete(root);

will never set root to something else. This was a design principle to make Java more qualitative in software engineering sense (less error prone) than C++ (Node*&).

You need to use return values, as there are no output values. A modified example:

Node deleteSubtree(Node tree, Object value)  {
    if (tree == null) {
        return null;
    }
    if (value.equals(tree.value)) {
        return null; // Deleting a subtree
    }
    tree.left = delete(tree.left, value);
    tree.right = delete(tree.right, value);
    return root;
}

NOde root = ...
root = delete(root, "war");

This would delete the subtree rooted at "war".

share|improve this answer
    
(does not compile, you should return null at the end. but apparently you would always be returning null anyway, so why return anything at all?) –  njzk2 2 days ago
    
@njzk2 Corrected it; this was an (unrealistic) example of changing a tree. So I renamed and corrected the return. –  Joop Eggen yesterday

You simply need to unlink the left and right nodes from the root:

void delete(Node root)
{
    if (root!=null){
        delete(root.left);
        root.left = null;
        delete(root.right);
        root.right = null;
    }
}

Setting root to null is useless, because it is a local variable that will be unlinked when the method returns anyway. Also it does not affect the value in the calling method, so it has no effect at all.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.