Disons que j'ai une structure de répertoire aménagé dans un fichier texteTrouver tous les chemins uniques dans la structure de l'arbre
root1
child1
child2
grandchild1
grandchild2
child3
root2
child1
child2
grandchild1
greatgrandchild1
Comment puis-je transformer la structure de l'arbre ci-dessus dans un tableau imbriqué qui ressemble à ceci:
[
[ "root1", "child1" ],
[ "root1", "child2", "grandchild1" ],
[ "root1", "child2", "grandchild2" ],
[ "root1", "child3" ],
[ "root2", "child1" ],
[ "root2", "child2", "grandchild1", "greatgrandchild1" ]
]
modifier
Obtenir plus loin, mais encore avoir des problèmes à pied à travers l'arbre récursive
var $text = ''
+ 'root1\n'
+ ' r1 child1\n'
+ ' r1 child2\n'
+ ' r1 grandchild1\n'
+ ' r1 grandchild2\n'
+ ' r1 child3\n'
+ 'root2\n'
+ ' r2 child1\n'
+ ' r2 c1\n'
+ ' r2 c1 g1\n'
+ ' r2 child2\n'
+ ' r2 grandchild1\n'
+ ' r2 greatgrandchild1\n'
+ 'test!\n'
+ 'root3\n'
+ ' r3 child1\n'
+ ' r3 c1\n'
+ ' r3 c1 g1\n'
+ ' r3 child3\n'
+ ' r3 grandchild1\n'
+ ' r3 greatgrandchild1';
var dirGen = (function(trees) {
"use strict";
var indent = /[\s\t]/g;
var lTrim = /[\s\t]*/;
var $trees = decompose(trees);
var $root = [];
function init() {
var paths = $trees.map(treeMap)
$test(paths);
}
function treeMap(tree, n, arr) {
var base = new LinkedList();
return bfs(-1, tree, base);
}
function bfs(n, tree, base) {
var l, t;
n++;
//base case
if (n === tree.length) return trails(base);
l = tree.length;
t = tree[n];
var cur = { label: t.replace(lTrim, ""), depth: depth(t), children: [] };
//set root
if (n === 0) {
base.tree = cur;
return bfs(n, tree, base);
}
base.push(cur);
return bfs(n, tree, base);
}
function depth(str) {
var d = str.match(indent);
if (d === null) return 0;
return d.length;
}
function trails(arr) {
return arr;
}
function LinkedList() {}
LinkedList.prototype.push = function(node) {
var l = this.tree.children.length;
var j = l - 1;
if (l === 0) {
this.tree.children.push(node);
return;
}
//walk through children array in reverse to get parent
while (j > -1) {
var d = this.tree.children[j].depth;
//child
if (node.depth > d) {
console.log(this.tree.children[j], node)
return this.tree.children[j].children.push(node);
}
//sibling
if (node.depth === d) {
}
j--;
}
}
function decompose(arr) {
var treeBreak = /[\r\n](?=\w)/gm;
var lines = /[\r\n]+(?!\s)*/g;
return arr.split(treeBreak).map(function(str) {
return str.split(lines)
});
}
function $test(str) {
var json = JSON.stringify(str, null, 2);
var wtf = "<pre>" + json + "</pre>";
document.write(wtf);
}
return init;
})($text);
dirGen();
Le code jusqu'à présent me fait ce tableau JSON:
ce qui est la nouvelle structure être utilisé pour? On dirait qu'il pourrait être amélioré pour le rendre plus convivial à la boucle récursive. Aussi ce qui génère le fichier texte? Si c'est un script auquel vous avez accès, vous pouvez tuer 2 oiseaux avec une pierre – charlietfl
@charlietfl le but est d'avoir un module npm qui, quand vous l'initialiserez, cherchera un template de répertoire et automatiquement 'mkdirp' la structure du répertoire par joignant les indices de chaque tableau de chemin unique produit à partir de l'algorithme. –
si vous faites cela, pourquoi écrivez-vous au fichier texte?Cela n'a pas de sens que vous n'écriviez pas au tableau en même temps et que vous fassiez de ce tableau imbriqué plus une structure arborescente traditionnelle avec les mêmes noms de tableaux de nœuds enfants à chaque niveau – charlietfl