Search code examples
javascriptarraystreetransformtree-structure

how to transform an Array to an N-ary tree?


I have an Array that I need to transform it to an N-ary tree. I know the value of N, and the total number of nodes.

I give you an example in the picture below. The N-ary tree should be ordered as illustrated.

Link to image here

I can't figure it out. I need an algorithm to do that. The program I'm writing is in javascript but an answer in pseudocode is fine also.

Appreciate your help!

[Edited]

I found a solution using the algorithm from here: Construct a complete K-ary tree from preorder traversal


Solution

  • I found a solution using the algorithm from here:

    Construct a complete K-ary tree from preorder traversal