Iteration over a tree in MERL |
Post Reply |
Author | |
cmorais
Contributor Joined: 23.Mar.2018 Location: Ireland Points: 12 |
Post Options
Thanks(0)
Posted: 11.Sep.2019 at 15:45 |
Hi folks, I know it might be pretty straightfoward, but I'm stuck in the MERL. I have a graph that has a tree structure. And I want to iterate over the tree. Example: A-->B-->C-->D \--->E I'm using a specific role for each element: 'Output struct :' newline do ~father~child.() { ' Type: ' type newline; } So, whenever a leaf of the tree has a father-child relation, show the child's information, and if the child has leaves, iterate as such. I could test it with a crappy hand-made code ( this same do ~father~child.() 3 times nested), but as I don't know the tree deepth, I need a better option. Can you help me? Thanks a lot!
|
|
stevek
MetaCase Joined: 11.Mar.2008 Points: 641 |
Answer Post Options Thanks(1) |
Hi!
I think what you're looking for is recursion: Let's build the solution bottom-up. At each node, what you want to do is output its information then recurse to all its subnodes: _handleNode() ' Type: ' type ', Name: ' id newline do ~father~child.() { _handleNode() } And to start things off, if this really is a tree, you want to find the top object (i.e. the one which isn't a child of anybody) and call _handleNode() there: foreach .() where not ~child { _handleNode() }
|
|
cmorais
Contributor Joined: 23.Mar.2018 Location: Ireland Points: 12 |
Post Options
Thanks(0)
|
Yeah, that's it! I was reading the chp.6 instead of 5 It worked fine, thank you a lot!
Edited by cmorais - 11.Sep.2019 at 17:07 |
|
cmorais
Contributor Joined: 23.Mar.2018 Location: Ireland Points: 12 |
Post Options
Thanks(0)
|
FYI, the visual part of the MERL editor gets really funky on this
Edited by cmorais - 11.Sep.2019 at 19:46 |
|
jpt
MetaCase Joined: 25.Mar.2008 Points: 239 |
Post Options
Thanks(0)
|
Yes, this way you can see that the generator is recursive.
The same can also be seen when running the debugger on the recursive generator. Edited by jpt - 21.Sep.2019 at 05:54 |
|
Post Reply | |
Tweet |
Forum Jump | Forum Permissions You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |