1: function TS(x, tree) |
2: ϕ = array of len(x) zeroes |
3: function RECURSE(j, m, pz, po, pi) |
4: m = EXTEND(m, pz, po, pi) |
5: if
vj == leaf
then
|
6: for
to len(m) do
|
7: w = sum(UNWIND(m, i).w) |
8:
|
9: else
|
10:
|
11: iz = io = 1 |
12: k = FINDFIRST(m.d, dj) |
13: if
k ≠ nothing
then
|
14: iz, io = (mk.z, mk.o) |
15: m = UNWIND(m, k) |
16: RECURSE(h, m, izrh/rj, io, dj) |
17: RECURSE(c, m, izrc/rj, 0, dj) |
18: function EXTEND(m, pz, po, pi) |
19: l = len(m) |
20: m = copy(m) {m is copied so recursions down other branches are not affected.} |
21: ml+1.(d, z, o, w) = (pi, pz, po, l = 0 ? 1 : 0) |
22: for
to 1 do
|
23: mi+1.w = mi+1.w + po ⋅ mi.w ⋅ i/(l + 1) |
24: mi.w = pz ⋅ mi.w ⋅ (l + 1 − i)/(l + 1) |
25: return
m
|
26: function UNWIND(m, i) |
27: l = len(m) |
28: n = ml.w
|
29: m = copy(m1⋯l−1) |
30: for
to 1 do
|
31: if
mi.o ≠ 0 then
|
32: t = mj.w
|
33: mj.w = n ⋅ l/(j ⋅ mi.o) |
34: n = t − mj.w ⋅ mi.z ⋅ (l − j)/l
|
35: else
|
36: mj.w = (mj.w ⋅ l)/(mi.z ⋅ (l − j)) |
37: for
to l − 1 do
|
38: mj.(d, z, o) = mj+1.(d, z, o) |
39: return
m
|
40: RECURSE(1, [], 1, 1, 0) |
41: return
ϕ
|