MetaCase Homepage
Forum Home Forum Home > > MetaEdit+
  New Posts New Posts RSS Feed - Iteration over a tree in MERL
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Iteration over a tree in MERL

 Post Reply Post Reply
Author
Message
cmorais View Drop Down
Contributor
Contributor
Avatar

Joined: 23.Mar.2018
Location: Ireland
Points: 12
Post Options Post Options   Thanks (0) Thanks(0)   Quote cmorais Quote  Post ReplyReply Direct Link To This Post Topic: Iteration over a tree in MERL
    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? Big smile

Thanks a lot!
Back to Top
stevek View Drop Down
MetaCase
MetaCase
Avatar

Joined: 11.Mar.2008
Points: 641
Answer Answer
Post Options Post Options   Thanks (1) Thanks(1)   Quote stevek Quote  Post ReplyReply Direct Link To This Post Posted: 11.Sep.2019 at 16:18
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()
}
Back to Top
cmorais View Drop Down
Contributor
Contributor
Avatar

Joined: 23.Mar.2018
Location: Ireland
Points: 12
Post Options Post Options   Thanks (0) Thanks(0)   Quote cmorais Quote  Post ReplyReply Direct Link To This Post Posted: 11.Sep.2019 at 17:06
Yeah, that's it!

I was reading the chp.6 instead of 5 Ermm

It worked fine, thank you a lot!


Edited by cmorais - 11.Sep.2019 at 17:07
Back to Top
cmorais View Drop Down
Contributor
Contributor
Avatar

Joined: 23.Mar.2018
Location: Ireland
Points: 12
Post Options Post Options   Thanks (0) Thanks(0)   Quote cmorais Quote  Post ReplyReply Direct Link To This Post Posted: 11.Sep.2019 at 19:35
FYI, the visual part of the MERL editor gets really funky on this LOL



Edited by cmorais - 11.Sep.2019 at 19:46
Back to Top
jpt View Drop Down
MetaCase
MetaCase
Avatar

Joined: 25.Mar.2008
Points: 239
Post Options Post Options   Thanks (0) Thanks(0)   Quote jpt Quote  Post ReplyReply Direct Link To This Post Posted: 11.Sep.2019 at 21:15
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
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.05
Copyright ©2001-2022 Web Wiz Ltd.

This page was generated in 0.047 seconds.