Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Tree structure — Gideros Forum

Tree structure

rrraptorrrraptor Member
edited June 2020 in General questions
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 :D Any ideas?
Tagged:

Comments

Sign In or Register to comment.