1

I am implementing a tree based on this answer. According to the given simple usage example, the new nodes are named after the position whey will have in the tree, so they are variables. For example:

TreeNode<String> node0 = root.addChild("node0");

where node0 is a new TreeNode and the data the new child will have.

According to this and this I can not use a string for naming a variable.

I intend to have 26 children for each node my tree has. My question is simple, do I have to create the tree by creating by hand all the 26 possible nodes my tree will have, like below?

TreeNode<String> node0 = root.addChild("node0");
TreeNode<String> node1 = root.addChild("node1");
TreeNode<String> node2 = root.addChild("node2");
...
TreeNode<String> node25 = root.addChild("node25");
{
    TreeNode<String> node00 = node0.addChild("node00");
    ...
    {
        //the above code for all the nodes of the tree
    }
}

or am I missing a better solution? Thank you

Community
  • 1
  • 1
Nanc
  • 464
  • 5
  • 15
  • why don't you add the 26 children in a loop? `for (int id = 0; id < 26; ++id) { root.addChild("node" + id); }` – Turing85 Feb 20 '16 at 23:55
  • @Turing85 this will work for root but what about the node0 for example? I cannot use node+"0" variable to do node0.addChild(...). – Nanc Feb 20 '16 at 23:57
  • You can always nest another loop inside the loop. If you want to pass the depth of construction as a parameter, you can use recursion instead. – Turing85 Feb 20 '16 at 23:58
  • @Turing85 I think you are missing the point, I can use loops to add the nodes but I cannot name the parent node. Please take a look at the references I included. If I am missing the point, you may explain to me how node0 (parent name) from node0.addChild field will be constructed. – Nanc Feb 21 '16 at 00:04
  • If you want the variable name (`nodeXY`) to be constructed dynamically from the `String` then the answer is: no, that is not possible. But why not use some sort of collection to manage your nodes (i.e. arrays or `List`s)? – Turing85 Feb 21 '16 at 00:06
  • Have you considered using a Map> allowing the Map key to be the variable name you want to constuct. – Ian Mc Feb 21 '16 at 00:07
  • @Turing85 this is the plan B, I was wondering if I could make it work based on the above Tree implementation. – Nanc Feb 21 '16 at 00:12
  • @Ian Mc Interesting! I will give it a try :) – Nanc Feb 21 '16 at 00:12

2 Answers2

1

While a variable name may not take on a dynamic nature, a Map allows an abstraction to put/get a TreeNode based on a key which has a nomenclature you design (i.e. based on the level of the tree, and it's node position). This allows the creation of your nodes in a loop.

You may want to consider a method like this for ease of use:

public String getKeyName(int treeLevel, int nodePosition) {
    // Build the key name...
}
Ian Mc
  • 5,656
  • 4
  • 18
  • 25
1

First of all, I would store the "name" of every node in a member variable and define a getter for it. I would also add a method to get the number of children the node already has. If you do that, you will name your nodes automatically when you instantiate them. In the class definition, add:

public class TreeNode<T> implements Iterable<TreeNode<T>> {

    T data;
    TreeNode<T> parent;
    List<TreeNode<T>> children;

    ...

    // A string containing the node name, (e.g. "210")
    String name;


    // A name getter method
    public String getName() {
        return this.name;
    }

    // A method to get the number of children that the node has
    public int getNumChildren() {
        return this.children.size();
    }

}

Now, you can name the nodes automatically in the constructor:

public TreeNode<T>(T data, TreeNode<T> parent) {
    ...
    this.parent = parent;
    int numParentChildren = parent.getNumChildren();
    this.name = parent.getName() + numParentChildren;
}

Regarding your question about Tree creation, it would be better to encapsulate it in a method:

public LinkedList<TreeNode<T>> createTree() {
    //Root TreeNode
    TreeNode<T> root = new TreeNode<T>(data, null);

    //TreeNode on which we currently operate
    TreeNode<T> current;
    //List of treenodes holding the result
    List<TreeNode<T>> treeList = new LinkedList<TreeNode<T>>();
    //Queue holding the nodes for which we will create 26 children elements
    Queue<TreeNode<T>> queue = new LinkedList<TreeNode<T>>();
    treeList.add(root);
    queue.add(root);

    for (int i=0; i< (some number); i++) {
    current = queue.remove();
    child = new TreeNode<T>>(data, current);
    current.addChild(child);
    queue.add(child);
    treelist.add(child);
    }
    return treeList;

}

I hope this helps.