Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8763345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:45:39+00:00 2026-06-13T15:45:39+00:00

Please someone help!!i m a newbie in Java. i want to make a tree

  • 0

Please someone help!!i m a newbie in Java. i want to make a tree structure from an Array List. My input is

1.1
1.2
1.3
1.3.1.1
1.3.1.2
1.4
1.4.1.1
1.4.2.1

and my aim is to get a Tree like

               1.1
               1.2
               1.3
       1.4           1.3.1.1
 1.4.1.1   1.5          1.3.1.2    
 1.4.1.2

and so on.

Please find below my classes for the same. I get a nullPoniter at test.tree.Node.addChild(Node.java:28) and I know it is because the ‘children’ is null but I dont know how to set the children for the first time. Please Help… 🙁

public class Tree {
private Node root;

public Tree(String rootData)
{
    root=new Node();
    root.data=rootData;
    root.children=new ArrayList<Node>();
}
public Tree() {
    super();
}
 public Node getRoot(){
    return this.root;
    }
 public void setRoot(Node rootElement) {
    this.root = rootElement;
}
}

and Node class

class Node { 
String data;
Node parent;
List<Node> children;

public Node() {
    super();
}
public Node(String name)
{
    super();
    this.data=name;
}
public void addChild(String name) {
    this.addChild(new Node(name));
}
public void addChild(Node child) {
    this.children.add(child);
}
public void removeChild(Node child) {
    this.children.remove(child);
}
public void removeChild(String name) {
    this.removeChild(this.getChild(name));
}
public Node getChild(int childIndex) {
    return this.children.get(childIndex);
}
public Node getChild(String childName) {
    for (Node child : this.children) {
        if (child.data.equals(childName)) { return child; }
    }
    return null;
}
public List<Node> getChildren() {
    if (this.children == null) {
        return new ArrayList<Node>();
    }
    return this.children;
}
 public void setChildren(List<Node> children) {
    this.children = children;
}
public Node getParentNode() {
    return this.parent;
}
}

and the Test class is

public class TreeTest {
public static void main(String[] args) {
    TreeTest tt = new TreeTest();
    ArrayList<String> newArr= new ArrayList<String>();
    newArr.add("1.1");
    newArr.add("1.2");
    newArr.add("1.3");
    newArr.add("1.3.1.1");
    newArr.add("1.3.1.2");
    newArr.add("1.4");
    newArr.add("1.4.1.1");
    newArr.add("1.4.2.1");
    int lCount=0;
    int maxCount= newArr.size();
    Tree tr= new Tree();
    Node rootNode = new Node();
    String parent_name=null;
    Node currentNode= new Node();
    for(String line: newArr){
        if(lCount==0){
        rootNode = tt.getTree(line);
        tr.setRoot(rootNode);
        currentNode= rootNode;
        }
        else{
            List<Integer> cur =  new ArrayList<Integer>();
            List<Integer> pre =  new ArrayList<Integer>();
            cur= tokenize(line);
            pre= tokenize(newArr.get(lCount-1));
            if(cur.size()==pre.size()){

                currentNode.addChild(tt.getTree(line));
                currentNode= tt.getTree(line);
                }
            else if (cur.size()>pre.size()){
                currentNode.addChild(tt.getTree(line));
                parent_name= newArr.get(lCount-1);
                currentNode= tt.getTree(line);
                }
            else if(cur.size()< pre.size()){
                currentNode= tt.getTree(parent_name);
                currentNode.addChild(tt.getTree(line));
                currentNode= tt.getTree(line);
            }
        }

        lCount++;
        }
        }
private Node getTree(String string) {
    // TODO Auto-generated method stub
        Node rootNode = new Node(string);
        return rootNode;
    }
private static List<Integer> tokenize(String line) {
    // TODO Auto-generated method stub
    List<Integer> line_Arr =  new ArrayList<Integer>();

    String[] tokens = line.split("\\.");
    int i=0;
    for(String atr: tokens)
        line_Arr.add(Integer.parseInt(atr));

    return line_Arr;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:45:40+00:00Added an answer on June 13, 2026 at 3:45 pm

    In both constructors of your Node class, add this statement after the super call: –

    children = new ArrayList<Node>();
    

    This will instantiate your List children.

    public Node() {
        super();
        this.children = new ArrayList<Node>();
    }
    
    public Node(String name)
    {
        super();
        this.children = new ArrayList<Node>();
        this.data=name;
    }
    

    Also, you can change your 1-arg and 0-arg Tree constructor from: –

    public Tree(String rootData)
    {
        root=new Node();
        root.data=rootData;
        root.children=new ArrayList<Node>();
    }
    
    public Tree() {
        super();
    }
    

    to the below one, that will instantiate the Node using the parameterized constructor: –

    public Tree(String rootData) {
        root=new Node(rootData);
    }
    
    public Tree() {
        root = new Node();
    }
    

    P.S: –

    You don’t need to add super() call explicitly, if you only want to invoke the super class 0-arg constructor. Compiler adds this call by default.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am still newbie to android, please could someone help. I want to use
I'm a newbie, could someone please help me what type is the Parts, I
Im a beginner to css, please can someone help me get this element to
can someone please help me out? I'm trying to create an input dynamically with
Someone please help rather a newbie in Excel-Vba. How do i implement a minimize
Please can someone help? I have the following code which uploads a file to
Please could someone help me understand why the div.fl element shown in Developer Tools
I am new to HTML and Javascript. Please someone can help me with the
Someone please help. I have an interesting issue. I am trying to implement an
Can someone please help to clarify? Also, please mention if there are other representation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.