// http://onestepback.org/articles/same_fringe/ / test data a:(1;2 3) aa:(1;1 2 3) b:(1 2;3) c:(1 4;5) d:0 2 5_0 1 5 6 11 13 19_!20 / naive K solution ~/,//'(a;b) / all paths in tree x lp:{:[@x;,();,/(!#x),/:'_f'x]} / recursively retrieve atoms of x ra:{:[@x;x;,/_f'x]} / iterate through tree, returning atom and state it:{:[~#z;(x;y;z);@d:y . i:*-1#z;:(d;y;-1_ z);(x;y;(-1_ z),i,/:|!#d)]}. / generate triple: (atom;tree;paths-to-process) gt:{(;x;|!#x)} / retrieve next atom in y using iterator x na:{{(#x 2)&+n~*x}x/@[y;0;:;_n]} / same fringe: iterative solution sf:{while[#(x:na[it]x)2;if[~x[0]~*y:na[it]y;:0]];~#na[it;y]2} / e.g. sf[gt a;gt b] sf[gt a;gt c] sf[gt a;gt aa] // parallel printing / recursive solution p2:{:[@y;`0:(x#" "),/:\$(y;z);_f[x+1]'[y;z]];} p2[0;a;a+100] / iterative solution jt:{:[~#z;(x;y;z);@d:y . i:*-1#z;:((d;#i);y;-1_ z);(x;y;(-1_ z),i,/:|!#d)]}. pt:{while[#(x:na[jt]x)2;pr[x;y:na[jt]y]];pr[x;na[jt]y];} pr:{`0:{(y#" "),\$x}.'(*x;*y)} / e.g. pt[gt a;gt a+100] pt[gt a;gt b]