imp:{(.0#`)`$(x,(1&#x)#"."),/:y} (P;L;C;D;N):(=)@/:(Pc;Lc;Cc;Dc;Nc):Cs:"(^). " LP:|/"^("=\: / parse vector of nodes into a parent vector prs:{(!#x)^'*|{(s;p):x;p,:**|s $["("=y;s,:,(-1+#p;1) "^"=y;s,:,(-1+#p;1+*|*|s) ")"=y;[s:(-*|*|s)_s;p:-1_p; p,:**|s] ] (s;p)}/[(,0N 0;!0);x]} / re-sort both the values and parent indices according to a permutation srt:{(x@z;(#'g:=p p:@[p;w;:;p@(-/1(~^rm?p@)\)/w:(&|/0,rm=\:p)^rm] rm,:rm2:(P n@)#2+(L n@)#&2=#'g:=p p:@[p;w;:;p@p@w:(&|/0,rm2=\:p)] (n;p):(-#rm)_/:srt[n;p;<@[&#p;rm;:;1]] $[2=+/~p;1_/:(n;0|p-1);(n;p)]} copy:{[n;p;e;l] (sn;sp):(*/l)#/:(Pc;0),'(-*l-1)#/:e#/:(n;p) sp:@[(#p)+(0|sp-e-*l)+(*l)*(&#/|l);(*l)*!*|l;:;0N] (sn;sp)} beta0:{[n;p;off] (l;e):0 1+lc[p]'off idx:(2;1+idx?l)_idx:off+!e-off:*off p:@[p;(off=p@)#*idx;:;p[off]] idx[0]:(n[off+1]=n@)#*idx l:1 0+#'|idx ro:<,/@[&#p;*idx;,;(|l)#1] (n;p):(n;p),'copy[n;p;e;l] p:@[p;&^p;:;p@*idx] (n;p):srt[n;p;