java 通用树_JAVA通用树问题

我试图理解泛型和树结构,并坚持以下问题…

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

import java.util.*;

public class Node

{

private Node root; // a T type variable to store the root of the list

private Node parent; // a T type variable to store the parent of the list

private List> children = new ArrayList>(); // a T type list to store the children of the list

// default constructor

public Node(){ }

// constructor overloading to set the parent

public Node(Node parent)

{

this.setParent(parent);

//this.addChild(parent);

}

// constructor overloading to set the parent of the list

public Node(Node parent, Node child)

{

this(parent);

this.children.add(child);

}

public void addChild(Node child)

{

this.children.add(child); // add this child to the list

}

public void removeChild(Node child)

{

this.children.remove(child); // remove this child from the list

}

public Node 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 root) {

this.root = root;

}

public Node getParent() {

return parent;

}

public void setParent(Node parent) {

this.parent = parent;

}

public boolean hasChildren()

{

return this.children.size()>0;

}

public Node[] children()

{

return (Node[]) children.toArray(new Node[children.size()]);

}

public Node[] getSiblings()

{

if(this.isRoot()==false)

{

System.out.println("this is not root");

}

List> tempSiblingList = new ArrayList>();

//this.parent.children() isn't working for me

//hence i tried to get around it next two lines

Node parent =  this.parent;

Node[] children =  parent.children();

for(int i=0; i

{

if(this!=children[i])

{

tempSiblingList.add(children[i]);

}

}

return (Node[]) 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;

}

}

我的问题是如何初始化节点类Person类…

我试过了

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

Node root = new Node(rootPerson);

但这对我不起作用…

还需要GetSibilings()的帮助

您将一个人传递给需要Node的构造函数

如果这是一个树,那么既需要父级变量,也需要树包含的对象。

public Node(Node parent,T value)

我最终还是想造一棵树…我现在正在处理节点类…这是一个大学作业,老师从未教过我们仿制药…现在我完全理解你的回答…但是我不知道如何初始化这个,我尝试过做node>root=new node>(new node(rootperson));但是这也不起作用…

node>root=new node>(new node(rootperson));

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

class Node

{

...

private T value;

...

}

您没有采用元素类型的Node构造函数:

...

public node (T value)

{

this.value = value;

}

...

而且,根据定义,一个人的兄弟姐妹是不是你自己的父母的子女:

public Node[] getSiblings ( )

{

if (parent == null)

return null;

List> siblings = new ArrayList>( );

Collections.copy(siblings, parent.children);

siblings.remove(this);

return siblings.toArray(new Node[]{});

}

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

另外,看起来你在模拟一个家族树?如果是这样的话,请注意,您所遵循的严格的层次模型实际上并没有很好地模拟现实,这是著名的历史记录。

编辑:回应评论。

要初始化类,您应该首先进行我上面提到的更改-创建一个成员,这样每个Node都可以存储一个值,并生成可以获取值的构造函数。

在这方面,@spinning_plate是正确的:正如我展示的那样,你需要一个值和一个父值。其构造函数的完整实现可能如下所示:

public Node (Node 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 rootNode = new Node(rootPerson); // This uses my constructor

Person son = new Person("son", 50,"alive");

Node sonNode = new Node(rootPerson, son); // This uses spinning_plate's constructor

谢谢你的回复…我确实理解了"获取兄弟姐妹"方法的后勤…但整个问题都在parent.children()上;父级没有公开子数组列表或我创建子级()的方法

注意,private只表示对其他类是私有的:类(对象)的实例可以访问同一类的其他实例的私有成员:在这种情况下,一个Node可以访问另一个实例的children列表,即使它被标记为私有。当然,它也可以使用children()方法——使用列表比使用数组简单。另外,请注意,我所讨论的不仅仅是getSiblings()问题。

你的帖子帮了我很多…现在我已经解决了我的问题:但是返回兄弟姐妹。ToArray(new node[])会产生无法生成泛型类型数组的错误,因此移除部分现在解决了问题…但在运行时它会给出nullpointer异常…

这就是我所说的"它没有被测试"的意思:我不确定匿名数组(new Node[]{})。至于空指针异常,编译器说它发生在哪里?

关于if(this.parent.children.size()>0),但我认为我知道原因,因为当我将类初始化为noderoot=new node(rootperson);在constructor public node(t child)this.child=child中,我还需要设置它的父级…我说得对吗…父级是将此添加为子级的父级…否则现在就将其设置为空…?

这似乎是正确的-根没有父节点,因此根节点的parent将为空。这就是为什么我要为if (parent == null)签入上面的代码,以避免出现这种问题。

好吧,这样的话……同样,我应该如何初始化类….

假设我有一个"noderoot=new node(rootperson);"和一个"nodeson=new node(sonperson);"root.addchild(sonperson);"

我想我现在还行……非常感谢你的帮助…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值