JAVA Generic Tree Issue
我试图理解泛型和树结构,并坚持以下问题…
我已经创建了3个类1)节点2)人员3)节点测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 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; } } |
我的问题是如何初始化节点类Person类…
我试过了
1 2 3 | Person rootPerson = new Person("root", 80,"Alive"); Node<Person> root = new Node<Person>(rootPerson); |
号
但这对我不起作用…
还需要GetSibilings()的帮助
您将一个人传递给需要
如果这是一个树,那么既需要父级变量,也需要树包含的对象。
1 | public Node(Node<T> parent,T value) |
您的节点类没有用于存储值的成员:
1 2 3 4 5 6 | class Node<T> { ... private T value; ... } |
号
您没有采用元素类型的
1 2 3 4 5 6 | ... public node (T value) { this.value = value; } ... |
而且,根据定义,一个人的兄弟姐妹是不是你自己的父母的子女:
1 2 3 4 5 6 7 8 9 10 11 | 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>[]{}); } |
。
警告:以上代码均未测试。
另外,看起来你在模拟一个家族树?如果是这样的话,请注意,您所遵循的严格的层次模型实际上并没有很好地模拟现实,这是著名的历史记录。
编辑:回应评论。
要初始化类,您应该首先进行我上面提到的更改-创建一个成员,这样每个
在这方面,@spinning_plate是正确的:正如我展示的那样,你需要一个值和一个父值。其构造函数的完整实现可能如下所示:
1 2 3 4 5 6 7 8 | 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); } |
然后您可以制作一个简单的树,如下所示:
1 2 3 4 5 | 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 |
。