examples.k 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. \l trees.k
  2. TT:"ABEFG56N8PV"
  3. P0:P:0 0 1 2 20 4 4 6 7 20 9 10 10 10 9 14 15 20 17 18 20
  4. R:dfo[P]
  5. T:(TT 3 2 1 10 4 10 2 0 7 4 2 10 9 10 2 0 7 2 0 7 3)@<R
  6. // reorder the indices based on depth first pre-ordering
  7. P:redo[P;R]
  8. D:#'(P@)\'!#P
  9. `0:"\n -- vertical tree --\n"
  10. `0:t0[P;D],'" ",/:T
  11. (tr;l):t1[2]@P
  12. `0:"\n -- horizontal left adjusted --\n"
  13. `0:1_,/+(tr;T@l)
  14. (tr;l):t2[2]@P
  15. `0:"\n -- horizontal center adjusted --\n"
  16. `0:1_,/+(tr;T@l)
  17. // Generate a random depth vector with x nodes (min 2)
  18. rd:{0,(0|x-2){x+@/1(*1?#:)\1-&(1|-_-0.25*d),1+&d:1|x}\1}
  19. sh:{(tr;l):((t1;t2)[x-1])[2]@y
  20. `0:(,""),1_,/+(tr;(,/`c$97+!#y)@l)}
  21. `0:"\n -- random trees horiz left adj --"
  22. sh[1]'p'rd'10#10+!5;
  23. `0:"\n -- random trees horiz ctr adj --"
  24. sh[2]'p'rd'10#10+!5;
  25. // Generate random length labels
  26. / pad the picture to make room
  27. / center the labels
  28. sh:{ls:`c$97+(1+(#y)?5)?\:26
  29. (tr;l):pad[p]((t1;t2)[x-1])[p:1+|/#'ls]@y
  30. `0:(,""),/+(tr;ctl@ls@l)}
  31. `0:"\n -- random trees horiz left adj random width labels --"
  32. sh[1]'p'rd'10#10+!5;
  33. `0:"\n -- random trees horiz ctr adj random width labels --"
  34. sh[2]'p'rd'10#10+!5;
  35. `0:"\n\n -- random code --\n"
  36. `0:C:"@//1(*1?#:)\1-&(1|-_- 0.5*d),1+&d:1|=':{x}"
  37. / render parse tree
  38. prstree:{ (T;D;L):+{$[`A~t:@y;((t;x;`);(`;x+1;*y)),,/o[x+1]'1_y;,(t;x;y)]}[0]`p@x
  39. (D@(!#D)^1+w; L@:(!#L)^w; T@:(!#T)^w:&`A=T)}
  40. k:{$[`c~_@x;x;`k@x]}
  41. `0:"\n -- render parse tree --\n"
  42. (D;L;T):prstree C
  43. shw[k'L;p@D]