SQLite
Vertical siblings
with last_sibling (id) as ( select max (id) from h group by pid ),tree (id,branch,path) as ( select 1 as id ,'' as branch ,'001' as path union all select h.id ,t.branch || case when ls.id is not null then ' ' else '|' end || ' ' ,t.path || '_' || substr ('00000' || h.id,-5) from tree t left join last_sibling ls on ls.id = t.id join h on h.pid = t.id ),vertical_space (n) as ( select 1 union all select vs.n + 1 from vertical_space vs where vs.n < 2 )select t.branch || case vs.n when 1 then '|____' || ' ' || cast (t.id as text) else '|' endfrom tree t cross join vertical_space vsorder by t.path,vs.n desc;