algorithm - print all paths from root to leaves n-ary tree -
i trying print paths root leaves in n-ary tree. code prints paths leaves, prints subpaths too.
for example, let's 1 path 1-5-7-11. prints 1-5-7-11, prints 1-5-7, 1-5, on.
how can avoid printing subpaths ?
here code in matlab
thanks
stack=java.util.stack(); stack.push(0); cp = []; q = []; labels = ones(1,size(output.vertices,2)); while ~stack.empty() x = stack.peek(); e = 1:size(output.edges,2) if output.edges{e}(1) == x && labels(output.edges{e}(2)+1) == 1 w = output.edges{e}(2); stack.push(w); cp = union(cp,w); break end end if e == size(output.edges,2) q = []; v=1:size(cp,2) q = union(q,cp(v)); end disp(q) stack.pop(); labels(x+1) = 0; cp = cp(find(cp~=x)); end end
Comments
Post a Comment