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()
Your node class has no member for storing a value:
You have no
Nodeconstructor taking an element type:And, by definition one’s siblings are the children of one’s parent that are not yourself:
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
Nodecan 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:
Then you can make a simple tree as follows: