不知道我是否被允許按照網站的規則做到這一點...但我會抓住我的機會...請忍受我,我只是一個學生.. 。:-)節點樹<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