Re: quad tree

new topic     » goto parent     » topic index » view thread      » older message » newer message

A simple recursive depth-first traversal..

constant T = { 
{2,3,4,5, 1}, 
{0,6,0,0, 2}, 
{0,0,0,0, 3}, 
{0,0,0,0, 4}, 
{0,0,0,0, 5}, 
{0,0,0,0, 6} 
} 
 
 
procedure visit(sequence node) 
	printf(1, "Visiting node %d\n", node[5]) 
end procedure 
 
 
procedure traverse(sequence tree, integer root, integer proc_id) 
	for i = 1 to 4 do 
		if tree[root][i] then 
                        -- Visit the i:th child node 
			call_proc(proc_id, {tree[tree[root][i]]}) 
                        -- Traverse the subtree that begins with the child 
			traverse(tree, tree[root][i], proc_id) 
		end if 
	end for 
end procedure 
 
 
 
traverse(T, 1, routine_id("visit")) 
 

Not exactly optimal, but you get the idea.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu