-1

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;
}
dead programmer
  • 3,727
  • 8
  • 38
  • 65
  • 10
    Just do `root = null`. Let the GC do its work. – Luiggi Mendoza Jul 30 '14 at 16:07
  • 1
    @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 Jul 30 '14 at 16:09
  • 2
    @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 Jul 30 '14 at 16:10
  • @LuiggiMendoza I tried doing root = null in a Java method( that accepted root as an argument). The value of root is not carried back to the Main method(calling code) though. I want main method to retain the null value of root that was changed in the delete method. – user2441441 Oct 12 '15 at 20:00
  • @user2441441 that's because [Java is pass by value](http://stackoverflow.com/q/40480/1065197). If you want/need to wipe out all the nodes in a tree, set its root (not the misleading `root` parameter) to `null`. – Luiggi Mendoza Oct 12 '15 at 20:03

5 Answers5

2
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.

cdblades
  • 101
  • 2
1

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".

Joop Eggen
  • 96,344
  • 7
  • 73
  • 121
  • 1
    (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 Jul 30 '14 at 17:09
  • @njzk2 Corrected it; this was an (unrealistic) example of changing a tree. So I renamed and corrected the return. – Joop Eggen Jul 31 '14 at 09:36
0

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.

njzk2
  • 37,030
  • 6
  • 63
  • 102
0

Just do root = null and the garbage collector will delete the whole tree during the garbage collection as no reference to the tree root is available in the program.

Sawan Patodia
  • 446
  • 2
  • 10
0
public class BinaryTree {
Node root;

public BinaryTree() {
    root = null;
}    

void deleteTree(Node node){
    if(node==null)
        return;
    deleteTree(node.left);
    deleteTree(node.right);
    System.out.println("The deleted node is " + node.key);
    node = null;
}

void deleteTreeRef()
{
    deleteTree(root);
    root=null;
}
}

And client will call like following: tree1.deleteTreeRef();

mitesh
  • 39
  • 6
  • The constructor does nothing the implied constructor wouldn't do. `deleteTree(node)` does nothing but print. (What do you think `node = null;` does? See [this comment to the question](http://stackoverflow.com/questions/25041675/java-code-to-delete-whole-binary-tree/42267296#comment38948683_25041675).) A client would be well advised to just assign `tree1 = null` to get rid of `tree1`. (Can't probably call `deleteTreeRef()`, anyway: not `public`.) – greybeard Feb 16 '17 at 10:10