如何获取隐藏节点之间的上一个和下一个连接节点详细信息(单个/多个考虑1个实体)

问题描述 投票:0回答:1
let nodes = [
    { id: 1, node_name: "pdo-1", is_hidden: false }, //
    { id: 2, node_name: "pdo-2", is_hidden: true }, //Hidden Node
    { id: 3, node_name: "pdo-3", is_hidden: true }, //Hidden Node
    { id: 4, node_name: "pdo-4", is_hidden: true }, //Hidden Node
    { id: 5, node_name: "pdo-5", is_hidden: false }, //
    { id: 6, node_name: "pdo-6", is_hidden: true }, //Hidden Node
    { id: 7, node_name: "pdo-7", is_hidden: true }, //Hidden Node
    { id: 8, node_name: "pdo-8", is_hidden: false }, //
    { id: 9, node_name: "pdo-9", is_hidden: true }, //Hidden Node
    { id: 10, node_name: "pdo-10", is_hidden: false }, //
  ];
 let edges = [
    { from: 1, to: 2, arrows: "to" },
    { from: 2, to: 3, arrows: "to" },//Hidden Edge
    { from: 3, to: 4, arrows: "to" },//Hidden Edge
    { from: 4, to: 5, arrows: "to" },
    { from: 5, to: 6, arrows: "to" },//Hidden Edge
    { from: 6, to: 7, arrows: "to" },//Hidden Edge
    { from: 7, to: 8, arrows: "to" },
    { from: 8, to: 9, arrows: "to" },//Hidden Edge
    { from: 9, to: 10, arrows: "to" },
  ];

即1,5,8,10 是非隐藏节点

2,3,4,6,7,9 是隐藏节点

想要将-一行中的单个或多个隐藏节点-作为1个实体。

let expectedNewEdge = [
    { from: 1, to: 5, arrows: "to", label: " + " }, // 3 hidden node connected in series
    { from: 5, to: 8, arrows: "to", label: " + " }, // 2 hidden node connected in series
    { from: 8, to: 10, arrows: "to", label: " + " },// 1 hidden node connected as single
  ];
const EdgeGenerator = () => {
let updateEdge = [];
const newEdges = edges
  .filter((edge) => {
    console.log("edge", edge);
    const PrevNode = nonHiddenNodes.find((node) => node.id === edge.from);
    const NextNode = nonHiddenNodes.find((node) => node.id === edge.to);
    updateEdge.push({
      id: edge.to,
      previous: PrevNode?.id,
      next: NextNode?.id,
    });
    console.log("NextNode---", NextNode);
    console.log("PrevNode ", PrevNode);
    return PrevNode && NextNode;
  })
  .map((edge) => ({
    from: edge.from,
    to: edge.to,
    arrows: "to",
  }));
console.log("newEdges", newEdges);
console.log("updateEdge", updateEdge);

};

错误:我的代码没有将单个或多个串联连接的隐藏节点作为1个实体

如何获取隐藏节点NextPrevious连接的节点id(不应该是隐藏节点)(可能是单个或多个串联,需要考虑1个实体),更新边缘以隐藏 JavaScript 中非隐藏节点之间的隐藏节点和边缘连接?

javascript reactjs recursion tree binary-tree
1个回答
0
投票

我会忽略按键

arrows
label
node_name
,因为它们似乎与您的问题无关。

从评论中我了解到你的“树”实际上是退化的,并且确实是一个链接列表。因此,我建议将数据结构转换为链表(标识其头),然后迭代该链表。当前一个节点和当前节点具有不同的可见性时,需要执行以下操作:要么记录前一个节点的 id(因为它将是输出边的

from
键),要么从当前节点的 id 创建一个边对象(如
to
)与之前记录的
from
值相结合。

实施:

function createLinkedList(nodes, edges) {
    // Key nodes and provide "next" keys
    const list = new Map(nodes.map(node => [node.id, { ...node, prev: null, next: null }])); 
    // Create linked list association:
    for (const {from, to} of edges) {
        (list.get(from).next = list.get(to)).prev = list.get(from);
    }
    // Identify the head of the list
    return [...list.values()].find(node => !node.prev);
}

function* iterVisibleLinks(nodes, edges) {
    let current = createLinkedList(nodes, edges)?.next;
    while (current) {
        if (current.is_hidden !== current.prev.is_hidden) {
            if (current.is_hidden) {
                from = current.prev.id;
            } else {
                yield { from, to: current.id };
            }
        }
        current = current.next;
    }
}

// Example run:

let nodes = [{ id: 1, node_name: "pdo-1", is_hidden: false },{ id: 2, node_name: "pdo-2", is_hidden: true },{ id: 3, node_name: "pdo-3", is_hidden: true },{ id: 4, node_name: "pdo-4", is_hidden: true },{ id: 5, node_name: "pdo-5", is_hidden: false },{ id: 6, node_name: "pdo-6", is_hidden: true },{ id: 7, node_name: "pdo-7", is_hidden: true },{ id: 8, node_name: "pdo-8", is_hidden: false },{ id: 9, node_name: "pdo-9", is_hidden: true },{ id: 10, node_name: "pdo-10", is_hidden: false },];
let edges = [{ from: 1, to: 2, arrows: "to" },{ from: 2, to: 3, arrows: "to" },{ from: 3, to: 4, arrows: "to" },{ from: 4, to: 5, arrows: "to" },{ from: 5, to: 6, arrows: "to" },{ from: 6, to: 7, arrows: "to" },{ from: 7, to: 8, arrows: "to" },{ from: 8, to: 9, arrows: "to" },{ from: 9, to: 10, arrows: "to" },];

const result = Array.from(iterVisibleLinks(nodes, edges));
console.log(result);

© www.soinside.com 2019 - 2024. All rights reserved.