【问题标题】:JAVA Generic Tree IssueJAVA通用树问题
【发布时间】:2012-05-03 15:12:53
【问题描述】:

我正在尝试理解泛型和树结构,并坚持以下问题...

我创建了 3 个类 1) 节点 2) 人 3) 节点测试

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() 的帮助

【问题讨论】:

    标签: java tree


    【解决方案1】:

    您将 Person 传递给需要 Node&lt;Person&gt; 的构造函数

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

    public Node(Node<T> parent,T value)
    

    【讨论】:

    • 我最终正在尝试构建一棵树...我现在正在学习 Node 课程...这是大学作业,老师从未教过我们泛型...现在我完全理解了你的回复......但只是无法弄清楚我如何初始化这个我试过做 Node> root = new Node>(new Node(rootPerson));但这也没有用...
    • Node> root = new Node>(new Node(rootPerson));
    【解决方案2】:

    您的节点类没有用于存储值的成员:

    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

    编辑:回应 cmets。

    要初始化类,你应该首先进行我上面提到的更改 - 创建一个成员,以便每个 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
    

    【讨论】:

    • 感谢您的回复...我确实了解 getSiblings 方法的逻辑...但整个问题在于 parent.children();父级没有公开子级数组列表或我创建的方法 children()
    • 请注意,private 仅表示对其他类私有:类(对象)的实例可以访问同一类的其他实例的私有成员:在这种情况下,一个Node 可以访问另一个 children 列表,即使它被标记为私有。当然,它也可以使用children() 方法——使用列表比使用数组更容易。另外,请注意,我解决的不仅仅是 getSiblings() 问题。
    • 你的帖子对我帮助很大......我现在已经解决了我的问题:-)但是返回的兄弟姐妹.toArray(new Node[]{}) 引发了不存在的错误能够创建一个泛型类型的数组,因此删除 部分现在解决了这个问题......但在运行时它给出了空指针异常......
    • 这就是我所说的“它没有经过测试”:我不确定匿名数组(new Node&lt;T&gt;[]{})。至于空指针异常,编译器说是在哪里发生的?
    • if(this.parent.children.size()>0) 但我想我知道原因,因为当我将类初始化为 Node root = new Node(rootPerson); 在构造函数 Public Node(T child){this.child = child} 我也需要设置它的父节点...我是对的...并且父母是将其添加为孩子的父母...否则现在将其设置为空...?
    猜你喜欢
    • 2011-08-23
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多