【问题标题】:How do I create a toString using another 'helper' class in my Tree java?如何在我的 Tree java 中使用另一个“助手”类创建一个 toString?
【发布时间】:2014-06-23 01:44:04
【问题描述】:

我试图弄清楚如何从下面给出的 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;
        }

在我的主类中,当我为我的树创建节点并尝试输出整个树时,它只输出一个带有括号表示的节点。那么如何使用它创建另一个 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);

}

}

【问题讨论】:

  • 你期待像 A(B,C(E,F(H,I),G),D) 这样的东西吗?
  • 是的。我只是不知道在我的 toString 方法中写什么,该方法使用括号表示返回我的输出。

标签: java data-structures tree linked-list


【解决方案1】:

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

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

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

编辑:使用“this”引用,因为我们在 Tree 类中。

【讨论】:

  • 好的,我明白了,但是如何创建一个 toString() 方法来返回我的括号表示
  • 你需要重写 Tree 类的 toString() 方法,然后调用它。
  • @Override public String toString() { Tree T = null;位置 v = null;返回括号表示(T,v); }
  • 我试过这样做,但编译时出错
  • 我也尝试使用您的编辑,但只输出节点“A”,即根。
【解决方案2】:

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

【讨论】:

  • 是的,得到同样的东西......'A'。
猜你喜欢
  • 1970-01-01
  • 2020-10-17
  • 2016-09-02
  • 2013-06-18
  • 1970-01-01
  • 2019-04-07
  • 2017-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多