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}
Navjot Singh
1,2151 gold badge9 silver badges26 bronze badges
asked May 16, 2019 at 12:03
$\endgroup$
6
  • $\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$ Commented May 16, 2019 at 12:21
  • $\begingroup$ Have you considered mimicking a relation database? $\endgroup$ Commented May 16, 2019 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$ Commented May 17, 2019 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$ Commented May 17, 2019 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$ Commented May 17, 2019 at 11:34

2 Answers 2

0
$\begingroup$

If I may elaborate on @Optidad 's comment.

Input:

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

"Building the list of all possible tuplets" means, in other words, enumerating all max length paths, namely:

 0 1 2
 -----
 a b d
 a b e
 a c d
 a c e

Shuffle the columns (representing the levels of the original tree) as you want, e.g.:

 2 0 1
 -----
 d a b
 e a b
 d a c
 e a c

Taking this data record by record, the desired tree can be built directly.

Output:

 root
 / \
 d e
 | |
 a a
 /\ /\
b c b c
answered May 31, 2022 at 6:56
$\endgroup$
0
$\begingroup$

I would write a function to fill a new tree one record at a time, each record being percolated down by levels. (There will be as many such functions as different orderings that you want to handle.)

Then traverse the existing tree to extract all records and feed them to the filling function.

E.g., if you retrieve the records in the order abd, abe, acd, ace, you will feed dab, eab, dac, eac, giving the partial trees

r - d - a - b
r + d - a - b
 - e - a - b
r + d - a + b
 - c
 - e - a - b
r + d - a + b
 | - c
 - e - a + b
 - c

You said nothing about your representation of the tree nodes, so this is up to you. Anyway, consider my remark below your post.

answered May 31, 2022 at 9:01
$\endgroup$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.