我有一张图表,其中的节点可以连接到多个其他节点。
每个节点都是数组中的一个对象。在每个节点的对象中,有一个包含与此节点链接的所有节点的id和深度的数组:
const nodes = [
{ "id": 37, "depth": 0, "children": [210, 395, 265], "next": [] },
{ "id": 210, "depth": 1, "children": [37, 260, 259, 391],"next": [] },
{ "id": 256, "depth": 2, "children": [265], "next": [] },
{ "id": 259, "depth": 2, "children": [210, 397, 396], "next": [] },
{ "id": 260, "depth": 2, "children": [210], "next": [] },
{ "id": 265, "depth": 1, "children": [37, 256, 388, 394, 271, 269], "next": [] },
{ "id": 269, "depth": 2, "children": [265], "next": [] },
{ "id": 271, "depth": 2, "children": [265], "next": [] },
{ "id": 388, "depth": 2, "children": [265], "next": [] },
{ "id": 391, "depth": 2, "children": [210], "next": [] },
{ "id": 394, "depth": 2, "children": [265], "next": [] },
{ "id": 395, "depth": 1, "children": [37], "next": [] },
{ "id": 396, "depth": 3, "children": [259, 413], "next": [] },
{ "id": 397, "depth": 3, "children": [259], "next": [] },
{ "id": 413, "depth": 4, "children": [396], "next": [] }
];
我想遍历图形,其中深度为0的节点是根节点。
问题在于节点的子数组包含所有链接到它的节点。深度为2的节点指向深度为1的节点。
因此,我想在节点对象中创建一个新数组,比如nodes.next,并且去掉指向深度低于自身的节点的子节点。
经过一段时间的努力,我有两种方法可以实现。首先,我检查子数组的长度是否大于1。然后,我依赖于事实,即不应将children数组中的节点推送到next数组中,该节点恰好位于索引0处。这并不是非常可靠的。
我发现第二个解决方案中困难的是检查子数组中节点的深度是否高于当前迭代中节点的深度。如果是,则将其推送到节点的下一个数组中。我希望您能展示更好的方法来做到这一点,因为这种解决方案并不美观。
let currentDepth;
let childDepth;
let currentID;
let childID;
const getChildDepth = (childID) => {
for (let i = 0; i < nodes.length; i++) {
if (childID === nodes[i].id) {
childDepth = nodes[i].depth
}
}
};
for (let i = 0; j < nodes.length; j++) {
currentDepth = nodes[j].depth;
currentID = nodes[j].id;
if (nodes[j].children.length > 1) {
for (let i = 0; i < nodes[j].children.length; i++) {
childID = nodes[j].children[i];
getChildDepth(childID);
if (childDepth > currentDepth) {
nodes[j].next.push(childID)
}
}
}
}
样例输出:
const nodes = [
{ "id": 37, "depth": 0, "children": [210, 395, 265], "next": [210, 395, 265] },
{ "id": 210, "depth": 1, "children": [37, 260, 259, 391],"next": [260, 259, 391] },
{ "id": 256, "depth": 2, "children": [265], "next": [] },
{ "id": 259, "depth": 2, "children": [210, 397, 396], "next": [397, 396] },
{ "id": 260, "depth": 2, "children": [210], "next": [] },
{ "id": 265, "depth": 1, "children": [37, 256, 388, 394, 271, 269], "next": [256, 388, 394, 271, 269] },
{ "id": 269, "depth": 2, "children": [265], "next": [] },
{ "id": 271, "depth": 2, "children": [265], "next": [] },
{ "id": 388, "depth": 2, "children": [265], "next": [] },
{ "id": 391, "depth": 2, "children": [210], "next": [] },
{ "id": 394, "depth": 2, "children": [265], "next": [] },
{ "id": 395, "depth": 1, "children": [37], "next": [] },
{ "id": 396, "depth": 3, "children": [259, 413], "next": [413] },
{ "id": 397, "depth": 3, "children": [259], "next": [] },
{ "id": 413, "depth": 4, "children": [396], "next": [] }
];