0

我试图了解泛型和树结构并坚持以下问题......

我创建了 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() 的帮助

4

2 回答 2

1

您将 Person 传递给需要Node<Person>

如果这是一棵树,您既需要父变量,也需要树包含的对象。

public Node(Node<T> parent,T value)
于 2012-04-23T00:08:23.960 回答
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>[]{});
}

警告:以上代码均未经过测试。

此外,您似乎正在为家谱建​​模?如果是这样,请注意,您所遵循的严格的层次模型实际上并不能很好地模拟现实,正如著名的这里记载的那样。

编辑:回应评论。

要初始化类,您应该首先进行我上面提到的更改 - 创建一个成员,以便每个成员都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
于 2012-04-23T00:08:42.007 回答