• Post Reply Bookmark Topic Watch Topic
  • New Topic

Build a tree structure from an array in Java  RSS feed

 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ! I came across a question with an answer. I am not able to understand the answer because I never used tree, node etc. Can someone please give me a brief description of what it does ?
I'd really appreciate it if you could help me. Thanks.

Consider an array of strings that come from html tags. This needs to be turned into a tree structure. Here, any Hn is the child of the most recent Hn-1


Desired output -



ANSWER -


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ali Gordon wrote:Hi ! I came across a question with an answer. I am not able to understand the answer because I never used tree, node etc.

Hmm. Then maybe it's a bit soon to be tackling something like this. Trees can be quite tricky, and they usually involve recursive logic, which is itself quite difficult to get your head around. I still find non-trivial recursion tough, and I've been at this lark for a long time.

However, a "Node", very simply, describes the point at which a branch divides. If you look at a real tree it usually starts out with a single trunk; and then, at some point, the trunk will divide into two (or more) branches. Well a Node describes that point; it also usually describes the direction that each branch goes in so, for a binary Node, it will probably have "left" and "right" branches - which are themselves Nodes, because the branch may divide again. In your case, your "parent" may have many "children", so you have a Collection of them.

That makes it an n-Tree, which are even more involved than binary ones.

Can someone please give me a brief description of what it does ?
I'd really appreciate it if you could help me. Thanks.

Quite honestly, I think you should probably read up about trees first. When you're happy you understand them, then try and think how you would write something like this using pencil and paper. And when you've done that, then come back to this code and see if you follow it.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I long ago gave up trying to understand recursion. I grit my teeth and wait … and it usually works all right.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!