List Trees and Recursive functions

Discuss GUI programming with the MUI Royale plugin here
Post Reply
djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

List Trees and Recursive functions

Post by djrikki »

Hello,

Struggling with a recursive function, always found them difficult to follow, can anyone come up with the function here?

Okay lets suppose I have 3 list trees, lets give them names, winlayout-1, winlayout-2 and winlayout-3.

Each of the list trees has an arrangement of nodes and leafs on them already with various names, but each node id has the name winlayout-XXX - where XXX is a randomly number from 0 to 999 padded to 3 digits, e.g. winlayout-067, winlayout-999 etc...

Now, suppose the user wants to create a new leaf on winlayout-2 so he hits a button.

The button calls the function we are trying to create, it generates a new id winlayout-XXX and traverses all 3 list trees to find out whether any nodes or leafs currently have this id.

If it finds a node or leaf already with this id it generates a new id winlayout-XXX and traverses all 3 list trees again until it finds no matches within the list trees.

The function will return two parameters 'state' and 'result'.

If 'state' is True it means that a new unique ID has been generated with the id stored in 'result'. If the 'state' is False, it means there are already 999 leafs or nodes in the structure so no new id has been generated - i.e. The List Trees are full.

I really need to get to the bottom of this and having a working function so any help much appreciated!
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com
djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

Re: List Trees and Recursive functions

Post by djrikki »

In fact for clarity lets rename the list trees layout-000, layout-001 and layout-002, and yes there isn't necessarily three, limitless in fact, although assume a ceiling of 999.
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: List Trees and Recursive functions

Post by Bugala »

If i understood right, partially and roughly (untested code)

Code: Select all

node = { id=123 }
listtrees = {
            [1] = {}
            [2] = {}
            [3] = {}
               }

global ID
Global Found


Function CreateNode(idtolookfor)

ID = idtolookfor
Found = false

foreach(listtrees, CheckForId)

return(Found)

endfunction




function CheckForID(cell, data)
if gettype(data) <> #TABLE then return

if data.id = ID then found = TRUE
foreach(data, CheckForID)

endfunction


foundornot = CreateNode(456)
debugprint(foundornot)

This supposes that each node has id or it will crash when checking for the id.
This GetType(data) part is important because your node might look like this for example:

Code: Select all

node = { [1] = {another node}
             [2] = {another node}
             x = 1
             y = 30
             id = 789
          }

And when using ForEach for the data, which is a subnode, it will then look through everything in that node, which means it will look through [1] and [2], which are other nodes and where this is supposed to be using them, but it will also go to that function with data being what "x" contains (1) or what "y" contains (30) and also even with what "id" contains (789).

Since it might be difficult to understand just by text, I will give you this running example of the previous.

When foreach is used to this specific node that i just created, one of the times when it goes through that function will be following:

Code: Select all

function CheckForID(cell, data)
if gettype(data) <> #TABLE then return

if data.id = ID then found = TRUE
foreach(data, CheckForID)
endfunction

Code: Select all

Function CheckForID(id, 789)
if gettype(data) <> #TABLE then return (= in this case it would return, since datas type is #NUMBER, not #TABLE)

if 789.id = ID (Crash, there is not such variable as id on 789, not to mention, that it isnt even a table)
foreach(789, CheckForID)  (789 doesnt have any items inside it, as it is number, and hence it cannot use foreach command at all, and naturally causes a crash again)
endfunction 
djrikki
Posts: 682
Joined: Wed Apr 06, 2011 12:26 am

Re: List Trees and Recursive functions

Post by djrikki »

Thanks Bugala, but I was asking about MUI ListTree objects not traversing through Hollywood tables.
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: List Trees and Recursive functions

Post by Bugala »

ah, yes indeed. Should have checked in what part of forum this was. Thats the downside of using "New posts" that you often miss in what category they are posted in.
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: List Trees and Recursive functions

Post by airsoftsoftwair »

AFAICS the only thing you want to do is find a vacant id for a new leaf or node, right? Why do you want to traverse the entire tree for that? Why not just use IDs that are increasing all the time, e.g "treeitem1", "treeitem2", "treeitem3" ... of course this will wrap once Hollywood hits the upper boundary of the data type but this will happen at 1.8*10^308 or so, so no worries ;) Or am I misunderstanding something here?
Post Reply