[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Java
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 16
Thread images: 1
File: toIndentedString.png (29 KB, 1206x597) Image search: [Google]
toIndentedString.png
29 KB, 1206x597
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/ has
 tags
>>
>>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?
Thread replies: 16
Thread images: 1

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.