Answer the question
In order to leave comments, you need to log in
How to create tree structure from adjacency list?
function list_to_tree(list) {
var map = {}, node, roots = [], i;
for (i = 0; i < list.length; i += 1) {
map[list[i].OBJID] = i; // initialize the map
list[i].PNEXT = []; // initialize the children
}
for (i = 0; i < list.length; i += 1) {
node = list[i];
if (node.PUPId !== "0") {
// if you have dangling branches check that map[node.parentId] exists
list[map[node.PUPId]].PNEXT.push(node);
} else {
roots.push(node);
}
}
return roots;
}
Answer the question
In order to leave comments, you need to log in
I didn't find PUPID in the structure (spoiler == source?)
function list_to_tree(list) {
return list.map( (v,i,a) =>{
v.OBJID = i; // оно надо?
v.PNEXT = []; // у нодов тоже должно быть?
if (v.PUPID && v.PUPID > 0) a[v.PUPID].PNEXT.push(v);
else return v;
}).filter(v=>v);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question