[英]How do I create a toString using another 'helper' class in my Tree java?
我試圖找出如何從下面給出的parentheticRepresentation類創建toString()方法。
public static <E> String parentheticRepresentation(Tree<E> T, Position<E> v) {
String s = v.element().toString();
if (T.islnternal(v)) {
Boolean firstTime = true;
for (Position<E> w : T.children(v))
if (firstTime) {
s += " ( " + parentheticRepresentation(T, w);
firstTime = false;
}
else s += ", " + parentheticRepresentation(T, w);
s += " ) ";
}
return s;
}
在我的主類中,當我為樹創建節點並嘗試輸出整個Tree時,它僅輸出一個帶有parentheticRepresenation的Node。 因此,如何使用此方法創建另一個toString()類,以便在調用輸出我的樹時,它像上面的類中那樣為我提供表示形式。 任何幫助,將不勝感激!
public static void main(String[] args) {
LinkedTree<Character> T = new LinkedTree();
// add root
T.addRoot('A');
// add children of root
T.createNode('B', (TreeNode) (T.root()), new NodePositionList());
TreePosition C = T.createNode('C', (TreeNode) (T.root()),
new NodePositionList());
T.createNode('D', (TreeNode) (T.root()), new NodePositionList());
// add children of node C
T.createNode('E', C, new NodePositionList());
TreePosition F = T.createNode('F', C, new NodePositionList());
T.createNode('G', C, new NodePositionList());
// add childrn of Node F
T.createNode('H', F, new NodePositionList());
T.createNode('I', F, new NodePositionList());
// print out tree
System.out.println("Size = " + T.size());
System.out.println("Here is the tree:");
System.out.println(T);
}
}
您將需要進行預遍歷,只需在遍歷子級時打開括號,然后再將其關閉即可。
像這樣的東西(因為我沒有測試它,所以不知道它是否有效,只是給你一個主意)。
public static <E> String parentheticRepresentation(Tree<E> T, Position<E> v) {
String s = "";
if (T.islnternal(v)) {
s = v.element().toString(); //adds node to string
if(T.children(v).size() > 0)
{
s += "("; //opens parenthesis for children
for (Position<E> w : T.children(v))
{
s += parentheticRepresentation(T, w) + ",";
}
s = s.deleteCharAt(s.length() - 1); //deletes last comma
s += ")"; //closes parenthesis for children
}
}
return s;
}
要覆蓋toString()方法,請在Tree類中添加類似的內容。
@Override
public String toString(){
//Just speculating the TreeHelper name, but its calling the helper method.
return TreeHelper.parentheticRepresentation(this,this.root());
}
編輯:因為我們在Tree類中,所以使用“ this”引用。
好吧,很明顯,OP在我的數據結構類中。
這就是我所做的(使用給定的方括號表示法)。
private static <E> String parentheticRepresentation (Tree <E> T, Position <E> v){
String s = v.element().toString();
if (T.isInternal(v)){
Boolean firstTime = true;
Iterator <Position<E>> it = T.children(v).iterator();
while (it.hasNext()){
Position<E> w = it.next();
if (firstTime) {
s+= " ( " + parentheticRepresentation (T,w);
firstTime = false;
}
else s+= ", " + parentheticRepresentation (T,w);
s+= " )";
}}
return s;
}
public String toString()
{
return parentheticRepresentation(this,this.root());
}
問題是我不斷收到我的子方法的堆棧溢出錯誤(在下面添加)
編輯:我修復了堆棧溢出錯誤,但知道我有與OP相同的問題。 僅輸出根(A)。
public Position<E> parent(Position<E> v) throws InvalidPositionException,
BoundaryViolationException {
TreePosition<E> p = checkPosition(v);
Position<E> parentPosition = p.getParent();
if (parentPosition == null)
throw new BoundaryViolationException("No parent");
return parentPosition;
}
public Iterable<Position<E>> children(Position<E> v)
throws InvalidPositionException {
TreePosition <E> p = checkPosition(v);
if (isExternal(v)) //STACK OVERFLOW
throw new InvalidPositionException("");
return p.getChildren();
}
public boolean isInternal(Position<E> v) throws InvalidPositionException {
checkPosition(v);
return (children(v) != null);
}
public boolean isExternal(Position<E> v) throws InvalidPositionException {
checkPosition(v);
return (children(v) == null); // STACK OVERFLOW
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.