2012-04-24 28 views
5

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

我有一份大學作業......我很難理解班上應該做什麼......我已經在三次不同的場合去過我的老師,我從他那裏得到的答案沒有幫助。無論如何,分配細節如下...

創建一個名爲Tree的類,充當節點的容器。樹類應該支持以下方法。

公共無效添加(節點父,子節點){} - 增加了一個新的子節點的父節點

公共無效removeChild之(Nodeparent,子節點){} - 從父項中移除一個子節點。

公共節點getRootNode(){} - 返回樹

公共無效setRoot(節點根){}的根 - 設置樹的根節點

公共布爾包含(T數據){} - 檢索樹給定類型

公共無效DFS(子節點){} - 執行深度優先搜索的TRE e和輸出每個節點(縮進)

公共無效BFS(節點子){} - 執行一個廣度優先搜索樹的並輸出的每個節點(縮進)

  1. 樹類應該被參數化以處理泛型類型T,允許創建字符串,文件等的樹,例如Tree<String> tree = new Tree<String>()
  2. 樹類應使用鄰接表實現的樹狀結構和通過以下方式確定: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()]); 
     } 
    } 
} 
+8

「裸」與你?我保持着我的衣服 - 這是「忍受着我」。 – duffymo 2012-04-24 01:38:03

+0

:-)不錯的一個...... – 2012-04-24 01:40:15

+0

一個有用的區別是:你在處理[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

回答

8

您使用地圖以下的事情:

HashMap中的關鍵是給定的節點,HashMap中的值,對於給定的子節點節點。

public class Tree<T> { 
    private Node<T> rootNode; 
    private HashMap<Node<T>, List<Node<T>> tree; 

    //and then some kind of function to go through the tree. 
    public void expandNode(Node<T> node) { 
     if (tree.get(node) == null) { 
      System.out.println(node); 
      return; 
     } 
     for(Node<T> n : tree.get(node)) { 
      System.out.println(node); 
      expandNode(n); 
     } 
    } 
} 

我可以說清楚樹的工作原理嗎?

+0

hmmmm 在我的Node類中我做了一個方法,它返回一個數組中的孩子....所以我應該那麼,將節點作爲節點的鍵和子節點作爲數組... ...? – 2012-04-24 01:45:11

+1

想一想,哈希映射就是整棵樹,你可以爲自己保留對根節點的參照,然後它就像這樣工作,有一個給定的節點,你可以調用哈希映射的get mehtod來獲取子節點那麼你可以迭代子節點並使用相同的映射來繼續檢索節點的子節點,當get方法返回null時,則有葉節點。 – 2012-04-24 01:48:55

+0

另一個細節,爲了使它工作,你應該在您的Node類中實現equals和hashcode方法。 – 2012-04-24 01:49:38

0

看看列表中的2點,我會猜測1號點對您來說是最令人困惑的。

樹本身可以參數化。樹類的類型參數可以在用於創建節點的內部類中使用。也就是說,出於分配的目的,節點類可能應該在Tree類內部,以便它可以使用給予Tree類的T類型參數。

這樣,樹可以存儲任何東西(字符串,文件,雙打等)。 Node類簡單地作爲存儲任何類型T對象的好方法。

+0

你是對的混淆部分... :-)在描述中,我們被要求爲節點編寫一個單獨的類...我寫了 – 2012-04-24 01:51:47

0

這有點奇怪。如果我這樣做了,而作業沒有另外說明,我可能根本不會寫一個Tree類;我只是使用Node作爲遞歸數據結構,並讓樹的根節點代表整個樹。

由於它看起來並不像你應該這樣做,所以你可以讓Tree<T>成爲一個包裝類,該類包含一個對象的引用(Node<T>)。您可以將所需方法的邏輯放在任何一個類中。

代碼的起點可能是這個樣子:

public class Tree<T> { 

    private Node<T> root; 

    public Tree(Node<T> root) { 
     this.root = root; 
    } 

} 
+0

該描述明確指出,樹類應該支持我寫的方法我原來的帖子...但你能給我一個樹類的初始化語句的例子...?請......只是試圖讓我的頭在你說的話...這個其他紳士胡安也做了很多的感覺... – 2012-04-24 01:56:19

+0

添加了一個排序的例子。 – Taymon 2012-04-24 02:09:03

+0

這正是我如何開始寫樹類,但後來對整個地圖問題感到困惑... – 2012-04-24 02:11:04