UNPKG

@antv/g6

Version:

A Graph Visualization Framework in JavaScript

48 lines 1.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dfs = dfs; exports.bfs = bfs; /** * <zh/> 执行深度优先遍历 * * <en/> perform depth first traversal * @param node - <zh/> 起始节点 | <en/> start node * @param visitor - <zh/> 访问节点函数 | <en/> visitor function * @param navigator - <zh/> 获取子节点函数 | <en/> get children function * @param mode - <zh/> 访问模式,BT: 自底向上访问,TB: 自顶向下访问 | <en/> traverse mode, BT: bottom to top, TB: top to bottom * @param depth - <zh/> 当前深度 | <en/> current depth */ function dfs(node, visitor, navigator, mode, depth = 0) { if (mode === 'TB') visitor(node, depth); const children = navigator(node); if (children) { for (const child of children) { dfs(child, visitor, navigator, mode, depth + 1); } } if (mode === 'BT') visitor(node, depth); } /** * <zh/> 执行广度优先遍历 * * <en/> perform breadth first traversal * @param node - <zh/> 起始节点 | <en/> start node * @param visitor - <zh/> 访问节点函数 | <en/> visitor function * @param navigator - <zh/> 获取子节点函数 | <en/> get children function */ function bfs(node, visitor, navigator) { const queue = [[node, 0]]; while (queue.length) { const [current, depth] = queue.shift(); visitor(current, depth); const children = navigator(current); if (children) { for (const child of children) { queue.push([child, depth + 1]); } } } } //# sourceMappingURL=traverse.js.map