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

  • Home
  • SEARCH
  • 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 7839205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:22:18+00:00 2026-06-02T15:22:18+00:00

I am trying to understand generics and the tree structure and stuck on the

  • 0

I am trying to understand generics and the tree structure and stuck on the following issue…

I have created 3 classes
1) Node
2) Person
3) NodeTest

import java.util.*;

public class Node<T>
{
    private Node<T> root; // a T type variable to store the root of the list
    private Node<T> parent; // a T type variable to store the parent of the list
    private List<Node<T>> children = new ArrayList<Node<T>>(); // a T type list to store the children of the list

    // default constructor
    public Node(){ }

    // constructor overloading to set the parent
    public Node(Node<T> parent)
    {
        this.setParent(parent);
        //this.addChild(parent);
    }

    // constructor overloading to set the parent of the list  
    public Node(Node<T> parent, Node<T> child)
    {
        this(parent);
        this.children.add(child);
    }


    public void addChild(Node<T> child)
    {
        this.children.add(child); // add this child to the list
    }

    public void removeChild(Node<T> child)
    {
        this.children.remove(child); // remove this child from the list
    }

    public Node<T> getRoot() {
        return root;
    }

    public boolean isRoot()
    {
        return this.root != null; // check to see if the root is null if yes then return true else return false
    }

    public void setRoot(Node<T> root) {
        this.root = root;
    }

    public Node<T> getParent() {
        return parent;
    }

    public void setParent(Node<T> parent) {
        this.parent = parent;
    }

    public boolean hasChildren()
    {
        return this.children.size()>0;
    }

    public Node<T>[] children()
    {
        return (Node<T>[]) children.toArray(new Node[children.size()]);
    }

    public Node<T>[] getSiblings()
    {

        if(this.isRoot()==false)
        {
            System.out.println("this is not root");
        }

        List<Node<T>> tempSiblingList = new ArrayList<Node<T>>();

        //this.parent.children() isn't working for me
        //hence i tried to get around it next two lines
        Node<T> parent =  this.parent;

        Node<T>[] children =  parent.children();

        for(int i=0; i<children.length; i++)
        {
            if(this!=children[i])
            {
                tempSiblingList.add(children[i]);
            }
        }
        return (Node<T>[]) tempSiblingList.toArray(new Node[children.length]);
    }
}






public class Person {

    private String name;
    private int age;
    private String status;

    public Person(String name, int age, String status)
    {
        this.setName(name);
        this.setAge(age);
        this.setStatus(status);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

My question is how i can go about initializing the Node class Person class…

i have tried

Person rootPerson = new Person("root", 80, "Alive");

Node<Person> root = new Node<Person>(rootPerson);

but it isn’t working for me…

also need help with the getSibilings()

  • 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-02T15:22:21+00:00Added an answer on June 2, 2026 at 3:22 pm

    Your node class has no member for storing a value:

    class Node<T>
    {
        ...
        private T value;
        ...
    }
    

    You have no Node constructor taking an element type:

    ...
    public node (T value)
    {
        this.value = value;
    }
    ...
    

    And, by definition one’s siblings are the children of one’s parent that are not yourself:

    public Node<T>[] getSiblings ( )
    {
        if (parent == null)
            return null;
    
        List<Node<T>> siblings = new ArrayList<Node<T>>( );
        Collections.copy(siblings, parent.children);
        siblings.remove(this);
    
        return siblings.toArray(new Node<T>[]{});
    }
    

    Caveat: none of the above code tested.

    Also, it appears that you’re modelling a family tree? If so, please be aware that the strict hierarchical model you’re following does not actually model reality very well, as famously chronicled here.

    EDIT: in response to comments.

    To initialise the class, you should first make the changes I mention above – make a member so that each Node can store a value, and make constructors that can take a value.

    In this regard, @spinning_plate has it right: as well as the one taking a value that I’ve shown, you’ll need one taking a value and a parent. A complete implementation of their constructor might look like the following:

    public Node<T> (Node<T> parent, T value)
    {
        this.parent = parent;
        this.value = value;
    
        // Don't forget: if you have a parent, you are their child.
        parent.addChild(this);
    }
    

    Then you can make a simple tree as follows:

    Person rootPerson = new Person("root", 80, "alive");
    Node<Person> rootNode = new Node<Person>(rootPerson); // This uses my constructor
    
    Person son = new Person("son", 50, "alive");
    Node<Person> sonNode = new Node<Person>(rootPerson, son); // This uses spinning_plate's constructor
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing some experiments, trying to understand the issues surrounding returning generics. The following
I am just trying to understand the extends keyword in Java Generics. List<? extends
I'm trying to understand type patterns and generic classes in Haskell but can't seem
Trying to understand something. I created a d:\svn\repository on my server. I committed folders
Just trying to understand that - I have never used it before. How is
Trying to understand why this doesn't work. I keep getting the following errors: left
It maybe a little naive question but I have been trying to understand how
I am going through a book trying to understand Generics with C# and I
I am trying to understand generics. Here's an example of one: public static bool
I have spent most of a day trying to understand why Java cannot compile

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.