私と一緒に負担してください...私は、サイトの規則に従ってこれを実行させて頂く場合か分からない...しかし、私は私のチャンスを取る、私は学生です。.. 。:-)ツリーノード<T>の、について説明
は、私はクラスが何をすべきか理解に苦労しています...大学の割り当てを持っている...私は、3つの異なる機会に私の先生に行っていると私は彼から得た答えはありませんでした助けてください。とにかく割り当ての詳細が続くようになり...
は、ノードのコンテナとして機能するTree
というクラスを作成します。ツリークラスは以下のメソッドをサポートする必要があります。
公共ボイド追加(ノードの親ノードの子){} - は親ノード
公共ボイドのremoveChild(Nodeparent、ノードの子){}に新しい子ノードを追加します - 親ノードから子ノードを削除します。
パブリック・ノードgetRootNode(){}は - ツリー
公共ボイドsetRoot(ノードルート){}のルートを返し - ツリーのルートノードを設定
{}所与のタイプのツリーを検索
公共ボイドDFS(ノード子) - - パブリックブール(Tデータ){}が含まTREの深さ優先検索を実行します各ノード(インデント)
- ツリーと出力の幅優先探索を実行 - 各ノード(インデント)
公共ボイドBFS(ノードの子){}はEと出力ツリークラスは、ジェネリック型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
:-)良いもの.... –
便利な区別が1つあります:[this](http://en.wikipedia.org/wiki/Tree_%28data_structure%29)種類のツリー、[this ](http://en.wikipedia.org/wiki/Tree_%28graph_theory%29)種類の木?私は前者を推測していただろうが、隣接リストを使うという要件は後者を示唆している。 – Taymon