不知道我是否被允许按照网站的规则做到这一点...但我会抓住我的机会...请忍受我,我只是一个学生.. 。:-)节点树<T>解释
我有一份大学作业......我很难理解班上应该做什么......我已经在三次不同的场合去过我的老师,我从他那里得到的答案没有帮助。无论如何,分配细节如下...
创建一个名为Tree
的类,充当节点的容器。树类应该支持以下方法。
公共无效添加(节点父,子节点){} - 增加了一个新的子节点的父节点
公共无效removeChild之(Nodeparent,子节点){} - 从父项中移除一个子节点。
公共节点getRootNode(){} - 返回树
公共无效setRoot(节点根){}的根 - 设置树的根节点
公共布尔包含(T数据){} - 检索树给定类型
公共无效DFS(子节点){} - 执行深度优先搜索的TRE e和输出每个节点(缩进)
公共无效BFS(节点子){} - 执行一个广度优先搜索树的并输出的每个节点(缩进)
- 树类应该被参数化以处理泛型类型T,允许创建字符串,文件等的树,例如
Tree<String> tree = new Tree<String>()
- 树类应使用邻接表实现的树状结构和通过以下方式确定:
Map<Node<T>, List<Node<T>>> tree = new HashMap<Node<T>, List<Node<T>>();
节点类也应进行参数化处理泛型类型T和暴露的几种方法..
现在我写了我的Node类,它工作正常......并且说实话,我确信我写了一个创建树的Node类。但在阅读Tree类描述后,我感到困惑。我应该在树地图中存储什么。我很难想象整个事情。
也许有人可以解释一下老师想要什么,并让我走向正确的方向。我是不是寻找代码本身...只是想明白我想要做什么。
我的节点类
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 T child;
private List<Node<T>> children = new ArrayList<Node<T>>(); // a T type list to store the children of the list
// default constructor
public Node(T child)
{
setParent(null);
setRoot(null);
setItem(child);
}
// 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);
}
/**
* This method doesn't return anything and takes a parameter of
* the object type you are trying to store in the node
*
* @param Obj an object
* @param
**/
public void addChild(Node<T> child)
{
child.root = null;
child.setParent((Node<T>)this);
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()
{
// check to see if the root is null if yes then return true else return false
return this.root != null;
}
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 T getItem() {
return child;
}
public void setItem(T child) {
this.child = child;
}
public boolean hasChildren()
{
return this.children.size()>0;
}
@SuppressWarnings("unchecked")
public Node<T>[] children()
{
return (Node<T>[]) children.toArray(new Node[children.size()]);
}
@SuppressWarnings({ "unchecked"})
public Node<T>[] getSiblings()
{
if(this.isRoot()!=false && parent==null)
{
System.out.println("this is root or there are no siblings");
return null;
}
else{
List<Node<T>> siblings = new ArrayList<Node<T>>((Collection<? extends Node<T>>) Arrays.asList(new Node[this.parent.children.size()]));
Collections.copy(siblings, this.parent.children);
siblings.remove(this);
return siblings.toArray(new Node[siblings.size()]);
}
}
}
“裸”与你?我保持着我的衣服 - 这是“忍受着我”。 – duffymo 2012-04-24 01:38:03
:-)不错的一个...... – 2012-04-24 01:40:15
一个有用的区别是:你在处理[this](http://en.wikipedia.org/wiki/Tree_%28data_structure%29)类树,或[this ](http://en.wikipedia.org/wiki/Tree_%28graph_theory%29)那种树?我猜想前者,但使用邻接列表的要求暗示了后者。 – Taymon 2012-04-24 01:47:43