Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a beginner in java and learning Data Structures and algorithms and I am currently learning trees. However, I cannot understand how a tree has been implemented using ArrayLists. I cannot understand the meaning of:
ArrayList<TreeNode> children;


Also, could the person who answers check whether the comments give the correct explanation of each step?

The code is given below:

import java.util.*;//Imported package

public class TreeNode{//Created class TreeNode for nodes of trees that contain two fields data(the data to be stored in each node) and children (as every node except root node has a child)//

  String data;//Data tobe stored is of type string
  ArrayList<TreeNode> children;//What does this mean and do?

  public TreeNode(String data){//Constructor for TreeNode class
    this.data=data;
    this.children=new ArrayList<TreeNode>();
  }

  public void addChild(TreeNode node){//Method to add child
    this.children.add(node);
  } 

  public String print(int level){//Print tree according to the level
    String ret;
    ret=" ".repeat(level)+data+"\n";
    for(TreeNode node:this.children){
      ret +=node.print(level+1);
    }
    return ret;
  }
}


What I have tried:

I have searched the internet however there are no proper explanations on trees using ArrayLists. My code is from the Udemy course.
Posted
Updated 16-Jan-22 11:22am

1 solution

(1)

Java
ArrayList<TreeNode> children;


declares an object called "children" with type "ArrayList<treenode>" where ArrayList is a particular collection of objects, in this case all those objects must be instances of type TreeNode. So children will be a bunch of TreeNode instances.

That by itself does not make a tree. However if every TreeNode in the collection has sufficient information about either its parent or its children, then you can discover the tree structure in that collection of TreeNodes.

(2)

I don't see much value in your comments; The "this is a table, this is a chair" approach doesn't add much. IMHO there mainly are two kinds of comment really worthwhile:

[A] at the start of a class or a method, describe what it is going to do, in functional terms, with input requirements, results to be expected, assumptions made, limitations, etc. That is akin to a contract.
aim: you or anyone else can at any point in time start using the class/method without having to study the implementation.

[B] inside a method, explain what algorithm is used, provide a reference, explain why you are doing things the way you are doing them; but only when such is not obvious.
aim: you or anyone else, when interested in the internals (e.g. when functionality needs to be added, or performance needs improved), gets a kick start.

Warning: maintain your comments meticulously; there is nothing worse than wrong comments. People are inclined to read and believe comments and get blinded by them when the code does not agree with the comment. It is better to have no comment than wrong or ambiguous comments.

:)
 
Share this answer
 
Comments
Sourabh Chavan 16-Jan-22 19:14pm    
So what you are saying is that the statement creates an ArrayList and stores instances of TreeNode class. Here, children is an object since ArrayList is a class. Right?
Luc Pattyn 16-Jan-22 19:29pm    
Where ever that declaration applies (in it's "scope") children will represent an ArrayList<treenode> unless it is null (null is not considered an instance of any class). Here children can't represent a string, a collection of pixels, or anything else.

However that line does not create anything at all. It tells the compiler to reserve some memory so a reference to an ArrayList<treenode> can be stored. The instance of ArrayList<treenode> has not been created here, that will take a statement with a new keyword, which would call a constructor etc.

Are you sure you understood the previous lessons of the course you are taking? I would suggest you reread some of the previous chapters...
Sourabh Chavan 16-Jan-22 19:41pm    
Thank you. I will try to go back and make my concepts clearer.
Luc Pattyn 16-Jan-22 19:47pm    
You're welcome.
Enjoy your study!
Sourabh Chavan 16-Jan-22 19:49pm    
Wait before you go just one last thing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900