Hello, I have a Tree that consists of TreeNodes, where TreeNode is

public class TreeNode {
    private String label = null;
    private String data = null;
    private TreeNode parent = null;
    private Vector <TreeNode> children = null;

I also have a HashMap <String, TreeNode> that keeps the label of a TreeNode (is unique for each node) and the reference to the spesific TreeNode of the Tree.

I want to delete a node of the Tree through its reference in HashMap. I tried this one:

   Iterator iter = pHash.keySet().iterator();

    while (iter.hasNext()) {
        label = iter.next().toString();
        // gets the value if there is any, else tNode is null
        tNode = tHash.get(label);
        if (tNode == null) {
            pNode = null;

When I print the nodes of the Tree, i have all the nodes as before the operations. Any ideas please? Thank you in advance.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

As far as I can see you use a highly tortuous loop to inspect each value in the hashmap then, if it is null, you set the variable pNode (whatever that may be!) to null. It seems equivalent to:

for (TreeNode t : pHash.values()) {
  if (t == null)  someRandomVariable = null;

What did you expect?

By doing pNode null I expect that there is no any references to that object so that Garbage Collector free this block of memory.

Whenever you no longer need to use an Object in Java, if you simply get rid of any references to that Object, Garbage collection will take care of it from there. Set any references to that Object to null.

pNode is NOT an object. It is a reference variable, that is able to refer to an object. Setting it to null just means it doesn't refer to any object. It does not directly affect any object that it may have referred to previously. As BJSJC said object get deleted by the JVM some time after it finds that there are no references anywhere to that object. SO as long as there is one reference left (eg parent node refers to child, or child node refers to parent) nothing gets deleted.
I think what you want to do here is to remove a node from your tree by removing all the references (parent or child) to it in the tree.

So for remove each pNode from Tree I should find its position in parent's vector and remove it from there. There is no other way faster than this to remove the nodes?

So for remove each pNode from Tree I should find its position in parent's vector and remove it from there. There is no other way faster than this to remove the nodes?

That's right. You need a public method removeChild(TreeNode c) in your TreeNode class. Then when you have the node you want to remove you can call it's parent's removeChild(...) method.
You may also want to think about what to do when you remove a node that has its own children - what should happen to them?

I have all ready a method public TreeNode removeChildAt(int index) but i don't know each time which is the index of child. So I should make a new method ublic TreeNode removeChildAt(String nodeName) to search in parent's children for this name and delete it. I want a fast way to find out which child is that i am looking for, and not to search between all parent's children.

When a node is removed from the parent I don't care about its children, I don' t want them any more.

Since the child is an object of type TreeNode, you should really use that as the parameter, not an index or name or whatever.
In that case, you can use the remove(Object 0) method for your Vector

public void removeChild(TreeNode c) {

As for the unwanted children - if you really want the object to be garbage collected you will need to remove the references to it from its children - you can do that with a
public void clearParent() { parent = null;}
and you will need to remove it from the HashMap.

I want the node with all its descendants to be garbage collected. Not only that node. If i leave that TreeNodes without any other references, garbage collector won't free their memory?

Garbage collection happens only when there are exactly zero remaining references, so you have to think about every reference that you may hold, and make sure every one is nulled. So if you have a node with children, and you null the reference from its parent, it will still hold a ref to the children, and the children will still hold a ref to it, so nothing gets collected.
Maybe write a delete method for a TreeNode that:

calls delete for each of its children (recursive).
(edit - this step not needed) clears its children vector.
deletes itself from the hashmap.
deletes itself from its parent.

That should do it!

Thank you for your help!!! Is was valuable...
Have a nice noon...

> Garbage collection happens only when there are exactly zero
> remaining references
Not exactly; circular references are also garbage collected. More specifically, objects are eligible for garbage collection if there exists no reference from a live thread to that object.

Hi sos. Yes, point taken.I also chose not to get into weak references. I think its always difficult to know how much info to give and how much to omit when helping someone with a specific problem to solve. Maybe I err on the side of keeping it as simple as possible to get the job done, but any feedback is welcome.

This question has already been answered. Start a new discussion instead.