0
$\begingroup$

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}
$\endgroup$
  • $\begingroup$ Just build the list of all possible tuples (Project, Budget, Fisc. Yr). This let you build any hierarchy tree you want in $O(N)$. $\endgroup$ – Vince May 16 at 12:21
  • $\begingroup$ Have you considered mimicking a relation database? $\endgroup$ – Apass.Jack May 16 at 21:48
  • $\begingroup$ @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. $\endgroup$ – the_lotus May 17 at 0:17
  • $\begingroup$ @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. $\endgroup$ – Apass.Jack May 17 at 1:15
  • $\begingroup$ @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. $\endgroup$ – the_lotus May 17 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.