/me reads question again.
...only for strict definitions of "twist". He already has the tree as an array. He already has the depth of the nodes in the tree. The only thing he's missing is the not-so-magical trick of having H::T produce html to create the changes in depth. For that you need to know how much and which way the depth changes between any two nodes and then use a tmpl_loop to generate the right number of opens / closes.
Granted the code needs comments, but impossible to follow is a stretch. jeffa figured it out sans comments in the time it took me to type this respone...can't be that bad :)