【发布时间】:2012-04-24 01:36:05
【问题描述】:
不知道我是否可以根据网站的规则这样做......但我会抓住机会......请多多包涵,我只是一个学生...... :-)
我有一个大学作业...我很难理解课程应该做什么...我已经在三个不同的场合去找过我的老师,但我从他那里得到的答案根本没有帮助。无论如何,分配细节如下......
创建一个名为Tree 的类,它充当节点的容器。树类应该支持以下方法。
public void add(Node parent, Node child){} -- 向父节点添加一个新的子节点
public void removeChild(Nodeparent, Node child){} -- 从父节点移除子节点。
public Node getRootNode(){} -- 返回树的根
public void setRoot(Node root){} -- 设置树的根节点
public boolean contains(T data){} -- 在树中搜索给定类型
public void dfs(Node child){} -- 执行树的深度优先搜索并输出每个节点(缩进)
public void bfs(Node child){} -- 执行树的广度优先搜索并输出每个节点(缩进)
- 应该对树类进行参数化以处理通用类型 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()]);
}
}
}
【问题讨论】:
-
“裸”与你?我一直在穿衣服——这是“忍受我”。
-
我认为您提供的第一个链接看起来非常接近我们所讲授的内容......