Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are recursive tree algorithms implemented to the nested recursive array

There is an Array which inspired from the hash tree object. But the structure is not well-designed and little bit complicated.

const directories = [
  "/main",
  [
    "folder",
    ["subFolder", ["directory1", "directory2", "directory3"]],
    "folder2",
    ["subFolder", ["directory4", "directory5"]],
    "folder3",
    [
      "subFolder",
      ["directory4", "directory5", "directory6", "directory7"],
      "subFolderWrapper",
      ["folder1", ["subFolder", ["child1", "child2", "child3", "child4"]]]
    ]
  ]
]

A recursive function must be created and returned a new array based on the nested relations from the given.

something like this
const result = [
  "/main/",
  [
    "/main/folder",
    [
      "/main/folder/subFolder",
      [
        "/main/folder/subFolder/directory1",
        "/main/folder/subFolder/directory2",
        "/main/folder/subFolder/directory3"
      ]
    ],
    "/main/folder2",
    [
      "/main/folder2/subFolder",
      [
        "/main/folder2/subFolder/directory4",
        "/main/folder2/subFolder/directory5",
        "/main/folder2/subFolder/directory6",
        "/main/folder2/subFolder/directory7"
      ]
    ],
    "/main/folder3",
    [
      "/main/folder3/subFolder",
      [
        "/main/folder3/subFolder/directory4",
        "/main/folder3/subFolder/directory5",
        "/main/folder3/subFolder/directory6",
        "/main/folder3/subFolder/directory7"
      ],
      "/main/folder3/subs",
      [
        "/main/folder3/subFolderWrapper/folder1",
        [
          "/main/folder3/subs/folder1/subFolder",
          [
            "/main/folder3/subs/folder1/subFolder/directory1",
            "/main/folder3/subs/folder1/subFolder/directory2",
            "/main/folder3/subs/folder1/subFolder/directory3",
            "/main/folder3/subs/folder1/subFolder/directory4"
          ]
        ]
      ]
    ]
  ]
];

I tried different kinds of logic in this function below but it's kind of different tree implementation that I didn't saw before. It looks like it's some kinds of cheating needs to be applied. Because there are two kinds of arrays that I know. One of them is flat also known as single dimensional and the other one is a two-dimensional array.

function traverse(item) {
  for(let index in item){

    if (Array.isArray(item[index])) {
        // logic for creating nested array
        traverse(item[index]);
     }
     else {
         // logic for non array strings
     }
  }
} 

Counted the nested invocation how much time the recursive function occurs. Subtracted or added to depthCounter variable from the current index of item for reaching and visiting the next node like BFS did.

I am curious about what is the best way of achieving this process.

like image 383
flushEmpire Avatar asked May 08 '26 13:05

flushEmpire


1 Answers

You could iterate the arrays and take non arrays as path and store this for nested arrays.

function getArrays(array, path = '') {
    var temp;
    return array.map(v => Array.isArray(v)
        ? getArrays(v, temp)
        : (temp = path + (path && '/') + v)
    );
}

const directories = ["/main", ["folder", ["subFolder", ["directory1", "directory2", "directory3"]], "folder2", ["subFolder", ["directory4", "directory5"]], "folder3", ["subFolder", ["directory4", "directory5", "directory6", "directory7"], "subFolderWrapper", ["folder1", ["subFolder", ["child1", "child2", "child3", "child4"]]]]]];

console.log(getArrays(directories));
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 59
Nina Scholz Avatar answered May 10 '26 03:05

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!