Terms in tree view - wrong order?
If I only get your goal correctly, I would suggest: for each node - get attached terms - for each term - get taxonomy_get_parents_all - reverse the array to have parent-to-child chain - form a list into a tree

Forming a tree could be yet another function with logic allowing to merge terms that share branches.