Trying to implement a method that returns an indented string representation of a normal Tree recursively. Expected output is in the image, any help/suggestions are appreciated.
>>54811096
for fucks sakes /g/ hastags
>>54811096
the indent goes above the for loop ya mongoloid
holy crap oop recursion looks nasty
>>54811222
I have tried putting it up there haha, but it just indents everything 1 level apart from the root node. Any idea where to go from there?
>>54811096
Do you even know what you are doing? Try writing out the steps in English first. Also, recursion is a fucking meme that does nothing but confuse readers, slow down programs, and give erections to academic faggots.
>>54811416
Well if i knew what i was doing why create this post?
post the rest of your code
>>54811416
>recursion is a fucking meme
only if you are a pajeet
>>54811096
your indent is generated based on node's children count instead of actual depth
Learn C
>>54811096
Didn't test it. I think it should work.private static final String INDENT = "\t";
public String toIndentedString() {
return toIndentedString(new StringBuilder(), INDENT).toString();
}
public StringBuilder toIndentedString(StringBuilder sb, String i) {
sb.append(i)
.append(rootValue)
.append(System.lineSeparator());
i += indent;
for (Thing child : children) {
child.toIndentedString(sb, i);
}
return sb;
}
>>54812885
oops, lowercase indent should be uppercase INDENT.
You don't need a for loop for the indent you can just use String.format("%"+indentLevel+"s", node)
you can keep track of the indent level by adding it as an argument
>>54811416
>working with tree data structures
>wants to avoid recursion
You're an idiot please leave this board.
>>54811416
LOL, you just too dumb to understand recursion.
>>54812894
Cheers for that, that looks like a good option but i need to keep it restricted to the one method, but i suppose if i cant figure something else out i could try combine the two you've provided somehow?