At the entrance I have a json data array. Each element has id(string, not null), folder(bool), parentId(string)

Based on this data, I have to create a file directory (or else as it is called "tree"). The analogy is - the file system in windows explorer (I think, like in most OS). I know how to create tree, node, leaf classes and the structure is less understood. The problem is in the algorithm that scatters the entire array into this structure. Can you tell me how to implement this or what algorithm?

  • 2
    You first give an example of your file. then an example of your classes (how does a leaf differ from node?) - teran

1 answer 1

  1. Go through the input array, build an associative array id -> node (for folders) / lead (for files).
  2. Go through the values ​​of the associative array (i.e., all folders and files), find the parent for each (search in the associative array by key), write the parent to the parent of the node, note to the parent's children (depending on how you store data). Nodes without paired link to tree.

Is done.