I have a small problem, that I cant solve for whatever reason.
I have a tree that looks like this:
Where
red dots - unavailable nodes,
green dots - available nodes,
purple dots - nodes with childs. Basic node structure:
{id = "whatever", available = false, parent = ..., childs= {...}} |
Now the fun part. Somehow I manage to get to node with id=10 (by clicking on it or maybe loop over whole tree, it does not matter). And I know all information about this node. Now I need to look for the next possible
green node. It must be 17, then 19, 16 and so on.
The simple solution is to brute force whole tree like this:
local currentNode = ...
local found = false
function bruteForce(node)
for i,subNode in ipairs(node.childs) do
-- found current
if subNode == currentNode then
found = true
-- next posible is found
elseif found and subNode.available then
found = false
return subNode
end
-- check childs
if #subNode.childs > 0 then
local n = bruteForce(subNode)
if n then return n end
end
end
end
bruteForce(root) |
But I dont like it. There must be a better way
Any ideas?
Comments
btw if you don't intend to modify the tree then you can preprocess it with a dfs once and build a 'next node' table from which you can get the next node immediately. you can even update this table when a new node is added to the tree.
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Thats what I have and it gives me correct results.
Fragmenter - animated loop machine and IKONOMIKON - the memory game
then you repeat the same process now with B instead of A. and you repeat this process until you find a green leaf.
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Sure that will work but I dont know how to code it ))) I tried various different implementations, recursive/while loops and it just does not work
Fragmenter - animated loop machine and IKONOMIKON - the memory game
and then you do not need recursion, just keep calling this on the new node it gives until you get a green node.
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Fragmenter - animated loop machine and IKONOMIKON - the memory game
so you first call next_using_dfs(16) then run next_using_dfs(12,16) then next_using_dfs(5,12) and so on.
Fragmenter - animated loop machine and IKONOMIKON - the memory game
and then you don't need two inputs, this function finds what you want by repeated calls.
Fragmenter - animated loop machine and IKONOMIKON - the memory game
I guess it can be optimized but for now its ok.
Now I need same thing but In opposite direction
Likes: MoKaLux
http://forum.giderosmobile.com/discussion/comment/63302/#Comment_63302
Im still trying to create a UI lib
This is 6th generation so far xD Rewritten almost from scratch about a week ago.
P.S. the gfx taken from godot engine.
Likes: MoKaLux, vitalitymobile, hgy29
Fragmenter - animated loop machine and IKONOMIKON - the memory game