我正试图找出如何从下面给出的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;
}
在我的主类中,当我为树创建节点并尝试输出整个树时,它只输出一个带有parentheticrepresentation的节点。那么我如何使用这个来创建另一个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());
}
编辑:使用"this"引用,因为我们在Tree类中。
很明显,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
}