我正在尝试理解泛型和树结构,并坚持以下问题......
我创建了3个班级 1)节点 2)人 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;
}
}
我的问题是如何初始化Node类Person类...
我试过了
Person rootPerson = new Person("root", 80, "Alive");
Node<Person> root = new Node<Person>(rootPerson);
但它不适合我...
还需要getSibilings()
的帮助答案 0 :(得分:1)
您将Person传递给需要Node<Person>
如果这是一棵树,则既需要父树的变量,也需要树包含的对象。
public Node(Node<T> parent,T value)
答案 1 :(得分:0)
您的节点类没有用于存储值的成员:
class Node<T>
{
...
private T value;
...
}
你没有Node
构造函数采用元素类型:
...
public node (T value)
{
this.value = value;
}
...
而且,根据定义,一个人的兄弟姐妹是一个父母的孩子,而不是你自己:
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>[]{});
}
警告:以上代码均未经过测试。
此外,您似乎正在为家谱树建模?如果是这样,请注意,您所遵循的严格的分层模型实际上并不能很好地模拟现实,正如着名的here所记载。
编辑:以回应评论。
要初始化类,首先应该进行上面提到的更改 - 创建一个成员,以便每个Node
可以存储一个值,并使构造函数可以获取值。
在这方面,@ spinning_plate是正确的:除了采用我所展示的值的那个,你还需要一个取值和父亲。其构造函数的完整实现可能如下所示:
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);
}
然后你可以按如下方式制作一个简单的树:
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