• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to build a tree  RSS feed

 
Matt Herrington
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I've been trying to devise a way in Java to build a category tree based on two variables:

- Number of siblings (horizontal)
- Number of children (vertical)

So 2 siblings and 2 children would produce:



and so forth for more complex patterns. I have something like this:



which produces this:



which is almost what I want but not quite as it has extra "1-1-1-1" and "1-1-1-2". If I set children to 1 and siblings to 2, I get this:



which isn't complete as I would expect "1-2-1" and "1-2-2".

Could someone please help me fix my code? It seems I'm almost there but missing some key piece that I am not seeing.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You described two variables, but in your code there are three. I don't understand what the "level" variable is for.
 
Matt Herrington
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"level" is used like a counter to describe what the current depth is, ie this will increment for each child. Maybe it's not the best way to keep track of this but it was all I could think of. Suggestions are always welcome.
 
Jilesh Lakhani
Ranch Hand
Posts: 49
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've not tried it but seems like level < children can do the trick for you.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Herrington wrote:"level" is used like a counter to describe what the current depth is, ie this will increment for each child. Maybe it's not the best way to keep track of this but it was all I could think of. Suggestions are always welcome.


Unfortunately, that is not what it is doing. Remember that you are not only traversing down to the child, you are also looping across the siblings. The level get incremented down, but the increment is not undone as it iterates to the next sibling -- basically, each sibling starts off with a higher level.

Henry
 
Matt Herrington
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the replies. This edit seems to do the trick:



Now, if I wanted to generate a path of each category, for example:

Category "1-1-1" would have path: "/1/1-1/1-1-1", how would I do that? I have some code here but it doesn't work well and gives me a StackOverflow.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Herrington wrote:Thanks all for the replies. This edit seems to do the trick:





Isn't this ...



a bit of an overkill? What wrong with just this?



Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Herrington wrote:
Now, if I wanted to generate a path of each category, for example:

Category "1-1-1" would have path: "/1/1-1/1-1-1", how would I do that? I have some code here but it doesn't work well and gives me a StackOverflow.



Your getPath() method doesn't actually change the category parameter. It just recursively call itself with the same category parameter. And since you use that parameter to determine whether you should continue recursively (ie. number of dashes), it makes sense that it recursively goes until the stack overflows.

Also, your while loop, if successful once, is an endless loop.

Henry
 
Matt Herrington
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the help. Looking back on things more closely, that makes sense. After some more studying, this method here seems to do the trick. I don't need a recursive method as long as I update the category String to remove the dashes as I go along.



getPath("1-2-2-2") -> 1/1-2/1-2-2/1-2-2-2
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for posting your solution. Many ranchers don't bother responding after solving the problem.... so ... you earned a cow ...

Henry
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Trying to build a tree": Isn't that something someone over at Permies.com could help you with?

Oh, wait, different kind of tree :P
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!