Input: PST pst, Mao < PSTNode, TPSTNode > map
Output: TPSTNode root_tpst
for each route node p of pst in level-order do
if Children(p).size is not 0 then
  TPSTNode t = map.get(p);
  if p is the root node of pst then
(5)    root_tpst = ;
(6)   end
(7)   List<PSTNode> child_pst;
(8)   List<TPSTNode> child_tpst;
(9)   child_pst = Children(p);
(10)   if Type(t) == Sequence or Loop then
(11)     Sort(child_pst);
(12)   end
(13)   for each node c of child pst do
(14)    map.get(c).setParent(t);
(15)    child_tpst.add(map.get(c));
(16)   end
(17)   t.setChild(child_tpst);
(18) end
(19) end
(20) return root_tpst;
Algorithm 3: Construct TPST.