# Algorithm to change order of tree

I'm looking for an algorithm name to change the order of the levels of a tree. I've done something that works but there's a lot of code and the $$O(n)$$ is very bad.

Here's an example, let say there are 3 levels. Each level representing something in a hierarchy project > budget > fiscal year

project = {a} budget = {b, c} fiscal year = {d, e}

   root
|
a
/ \
b   c
/\   /\
d  e  d e


I would like to build a tree with the level in different order

fiscal year > project > budget

   root
/ \
d   e
|   |
a   a
/\   /\
b c  b c


budget > fiscal year > project

   root
/  \
b   c
/\   /\
d  e d  e
|  | |  |
a  a a  a


The initial tree can be more complicated. Each node can have more than 2 children.

    root
/  \
a    b     - Projects {a, b}
/ \  / \
c  d  c  e   - Budgets {c, d, e}
|  | /\  |
f  g f g g   - Fiscal year with data {f, g}

• Just build the list of all possible tuples (Project, Budget, Fisc. Yr). This let you build any hierarchy tree you want in $O(N)$. – Vince May 16 at 12:21
• Have you considered mimicking a relation database? – Apass.Jack May 16 at 21:48
• @Apass.Jack the content is already in a relational database. The order of the level is customization and found it easier to do outside of the database. Unless there something I didn't know about. – the_lotus May 17 at 0:17
• @the_lotus "the content is already in a relational database." What is that content in that relational database? What is in a relational database is usually very different from a tree, the input given in the post. – Apass.Jack May 17 at 1:15
• @Apass.Jack it is, there's a list of projects, each project have different type of budgets and each budgets have money for different fiscal year. – the_lotus May 17 at 11:34