如何在我的Tree java中使用另一个'helper'类创建一个toString?

时间:2014-06-23 01:44:04

标签: java data-structures tree linked-list

我试图弄清楚如何从下面给出的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的节点。那么我如何使用它创建另一个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);

}

}

2 个答案:

答案 0 :(得分:0)

您需要进行预订遍历,只需在遍历子项时打开括号,然后再将其关闭。

这样的东西(不确定它是否有效,因为我没有测试它,只是为了给你一个想法)。

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());
}

编辑:使用&#34;这个&#34;引用因为我们在Tree类中。

答案 1 :(得分:0)

嗯,显然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
}