Ух, я люблю этот вопрос. И это тоже довольно сложно! Это был мой первый случай рекурсивного подхода к определенной проблеме. И я думаю, что мне удалось понять это.
let root = 2;
// more complicated data (with 1 branch that doesn't connect to any other node)
let nodes = [[1, 2], [2, 3], [3, 4], [1, 5], [1, 6], [2, 8], [100, 101]];
function createTree(root, nodes){
let children = [];
for (let i = 0; i < nodes.length; i++){
const index_of_root = nodes[i].indexOf(root)
if (index_of_root !== -1){
children.push(nodes[i][Number(!index_of_root)]); // note that data like [1,2,4] or [1] will not work.
nodes.splice(i, 1);
i--; // after removing the element, decrement the iterator
}
}
let tree = {
id: String(root)
};
if (children.length !== 0){ // if there are any children,
tree.children = []; // add the children property to the tree object
for (let child of children){
tree.children.push(createTree(child, nodes)); // then add the tree of each of the children
}
}
return tree;
}
console.log(createTree(root, nodes));
Обычно, когда функция createTree()
замечает, что есть какой-либо узел, связанный с root, она создает объект дерева со свойством children. Это дочернее свойство заполняется всеми деревьями, возвращенными каждым из детей, связанных с root.
Если дочерних элементов нет, он просто возвращает объект дерева без дочерних элементов. Честно говоря, мой код может быть немного трудным для чтения из-за того, что он рекурсивный, поэтому он может помочь напечатать некоторые значения в середине функции.
Теперь этот с ограничением (только то, что if (index_of_root !== -1){
заменяется на if (index_of_root !== -1 && children.length !== 2){
):
let root = 2;
// more complicated data (with 1 branch that doesn't connect to any other node)
let nodes = [[1, 2], [2, 3], [3, 4], [1, 5], [1, 6], [2, 8], [100, 101]];
function createTree(root, nodes){
let children = [];
for (let i = 0; i < nodes.length; i++){
const index_of_root = nodes[i].indexOf(root)
if (index_of_root !== -1 && children.length !== 2){
children.push(nodes[i][Number(!index_of_root)]); // note that data like [1,2,4] or [1] will not work.
nodes.splice(i, 1);
i--; // after removing the element, decrement the iterator
}
}
let tree = {
id: String(root)
};
if (children.length !== 0){ // if there are any children,
tree.children = []; // add the children property to the tree object
for (let child of children){
tree.children.push(createTree(child, nodes)); // then add the tree of each of the children
}
}
return tree;
}
console.log(createTree(root, nodes)); //notice how [2, 8] pair is excluded from the tree
Надеюсь, это помогло. Ура :) 1022 *