
本文详解如何通过递归函数,将具有 linksTo 跳转逻辑的问答结构数据,转换为所有可能的完整访问路径;重点解决路径累积、节点索引映射、终止条件判断及结果标准化输出等核心问题。
本文详解如何通过递归函数,将具有 `linksTo` 跳转逻辑的问答结构数据,转换为所有可能的完整访问路径;重点解决路径累积、节点索引映射、终止条件判断及结果标准化输出等核心问题。
在构建动态问卷、决策流程图或交互式引导系统时,常需将「带跳转逻辑的树形问答」展开为所有可达路径。原始数据中每个问题通过 options[].linksTo 指向下一个问题索引,形成隐式有向图;而目标是穷举所有从首个问题出发、沿有效链接走到底(即某选项无 linksTo)的完整路径序列。
关键挑战在于:
- 问题需按 questionIdx 快速查找(避免每次线性遍历);
- 每条路径是深度优先遍历(DFS) 的结果,需在递归中持续累积当前路径;
- 遇到无 linksTo 的选项时,该路径终止并存入结果集;
- 多分支(≥2 个选项)需独立递归,互不干扰。
✅ 正确实现思路:预建索引 + DFS 递归回溯
首先将 questions 数组转换为以 questionIdx 为键的对象字典,实现 O(1) 查找:
const questionMap = mockData.questions.reduce((map, q) => {
map[q.questionIdx] = q;
return map;
}, {} as Record<number typeof mockdata.questions>);</number>
接着定义主函数 getAllPaths,封装递归逻辑与结果收集:
function getAllPaths(
startQuestion: typeof mockData.questions[number],
questionMap: Record<number typeof mockdata.questions>
): { pathIdx: number; show: true; path: Array }[] {
const result: ReturnType<typeof getallpaths> = [];
let pathIndex = 0;
function visit(currentQuestion: typeof mockData.questions[number], currentPath: any[] = []) {
currentQuestion.options.forEach(option => {
// 构建当前步骤对象(含 questionIdx, question, answerLabel,必要时加 linksTo)
const step = {
questionIdx: currentQuestion.questionIdx,
question: currentQuestion.question,
answerLabel: option.answerLabel,
...(option.linksTo !== undefined && { linksTo: option.linksTo })
};
const newPath = [...currentPath, step];
if (option.linksTo !== undefined) {
// 存在跳转:递归访问目标问题
const nextQuestion = questionMap[option.linksTo];
if (!nextQuestion) {
console.warn(`Warning: linksTo ${option.linksTo} not found in questions`);
return;
}
visit(nextQuestion, newPath);
} else {
// 终止路径:保存完整路径
result.push({
pathIdx: ++pathIndex,
show: true,
path: newPath
});
}
});
}
visit(startQuestion);
return result;
}
// 使用示例
const paths = getAllPaths(mockData.questions[0], questionMap);
console.log(JSON.stringify(paths, null, 2));</typeof></number>
⚠️ 注意事项与健壮性增强
- 空链接容错:若 linksTo 指向不存在的问题索引,应显式报错或跳过,避免运行时崩溃;
-
循环检测(进阶):当前实现未防循环引用(如 Q1 → Q2 → Q1)。如需支持,可传入 visitedSet: Set
记录已访问问题索引,在 visit 开头校验; - 类型安全建议:使用 TypeScript 定义清晰接口(如 Tree, Question, Option, PathStep, PathsData),提升可维护性;
- 性能提示:本方案时间复杂度为 O(N × M),其中 N 是总路径数,M 是平均路径长度;对超大规模分支树,可考虑迭代 DFS 或加入路径长度限制。
✅ 总结
该递归方案摒弃了错误的“数组映射式扁平化”思路,转而采用经典 DFS 回溯模型:
✅ 用哈希映射替代线性查找,保障效率;
✅ 每次递归携带当前路径副本,天然隔离各分支状态;
✅ 明确区分“中间步”(带 linksTo)与“终点步”(无 linksTo),逻辑边界清晰;
✅ 输出结构严格匹配目标格式(含 pathIdx, show: true, 标准化 path 数组)。
只需调用 getAllPaths(mockData.questions[0], questionMap),即可获得全部合法路径——这是构建可执行决策流、路径分析仪表盘或测试用例生成器的坚实基础。










