
在许多应用场景中,我们经常会遇到需要处理树形或层级结构的数据。例如,一个多级分类系统、组织架构或文件目录。本教程关注的是一个典型的JavaScript对象数组表示的树形结构,其每个节点都包含以下关键属性:
以下是一个示例数据结构:
const data = [
{
key: "id1",
name: "Category 1",
curr: 0,
total: 0,
nodes: [
{
key: "id2",
name: "Applications",
curr: 20,
total: 30,
nodes: [
{
key: "id3",
name: "Gaming",
curr: 5,
total: 10,
nodes: []
},
{
key: "id4",
name: "Operating System",
curr: 15,
total: 20,
nodes: []
}
]
}
]
},
{
key: "id5",
name: "Category 2",
curr: 0,
total: 0,
nodes: [
{
key: "id6",
name: "Sub Category",
curr: 12,
total: 48,
nodes: [
{
key: "id7",
name: "Inside Sub",
curr: 12,
total: 48,
nodes: []
}
]
}
]
},
{
key: "id8",
name: "Last One",
curr: 0,
total: 0,
nodes: []
}
];我们的目标是编写一个函数,接收这个数据数组和一个key值。当找到与key匹配的节点时,不仅要递增该节点的curr值,还要沿着其父节点链向上,递增所有祖先节点的curr值,但不包括最顶层(根级别,即data数组中的直接元素)的节点。
例如,如果传入key为 "id4",那么"id4"节点及其父节点"id2"的curr值都应该递增1。而"id1"作为根级别节点,其curr值不应改变。
要实现上述功能,我们需要一个能够深度遍历树结构,并在找到目标节点后,将“更新”状态回溯给其父节点的机制。同时,还需要一个方法来区分根节点和其他节点,以避免更新根节点的curr值。
核心思路如下:
以下是实现上述逻辑的JavaScript函数:
/**
* 递归更新树形结构中指定节点及其祖先的curr值。
*
* @param {Array<Object>} nodes 当前层级的节点数组。
* @param {string} key 目标节点的唯一标识符。
* @param {number} depth 当前递归的深度,根节点为0。
* @returns {boolean} 如果当前节点或其子节点被更新,则返回true;否则返回false。
*/
function updateNodeAndParentsRecursively(nodes, key, depth = 0) {
// 遍历当前层级的所有节点
for (let node of nodes) {
// 检查当前节点是否是目标节点,或者其子节点(通过递归调用)是否是目标节点
// 使用 ?? [] 确保即使 node.nodes 为 null/undefined 也能安全调用
if (node.key === key || updateNodeAndParentsRecursively(node.nodes ?? [], key, depth + 1)) {
// 如果当前节点或其子树中发生了更新
// 并且当前节点不是根级别节点(depth > 0),则递增其curr值
if (depth > 0) {
node.curr++;
}
// 返回 true,向上级节点传递更新状态
return true;
}
}
// 如果遍历完当前层级所有节点及其子树,都没有找到目标节点或发生更新,则返回 false
return false;
}updateNodeAndParentsRecursively(nodes, key, depth = 0):
for (let node of nodes):
if (node.key === key || updateNodeAndParentsRecursively(node.nodes ?? [], key, depth + 1)):
if (depth > 0) { node.curr++; }:
return true;:
return false;:
现在我们来演示如何使用这个函数。
// 原始数据
const data = [
{
key: "id1",
name: "Category 1",
curr: 0,
total: 0,
nodes: [
{
key: "id2",
name: "Applications",
curr: 20,
total: 30,
nodes: [
{
key: "id3",
name: "Gaming",
curr: 5,
total: 10,
nodes: []
},
{
key: "id4",
name: "Operating System",
curr: 15,
total: 20,
nodes: []
}
]
}
]
},
{
key: "id5",
name: "Category 2",
curr: 0,
total: 0,
nodes: [
{
key: "id6",
name: "Sub Category",
curr: 12,
total: 48,
nodes: [
{
key: "id7",
name: "Inside Sub",
curr: 12,
total: 48,
nodes: []
}
]
}
]
},
{
key: "id8",
name: "Last One",
curr: 0,
total: 0,
nodes: []
}
];
// 调用函数更新 'id4'
console.log("--- 更新 'id4' 之前 ---");
console.log(JSON.stringify(data, null, 2));
updateNodeAndParentsRecursively(data, 'id4');
console.log("\n--- 更新 'id4' 之后 ---");
console.log(JSON.stringify(data, null, 2));
/*
预期输出(部分关键节点):
...
{
key: "id1",
name: "Category 1",
curr: 0, // 保持不变,因为是根节点
total: 0,
nodes: [
{
key: "id2",
name: "Applications",
curr: 21, // 从20递增到21
total: 30,
nodes: [
// ...
{
key: "id4",
name: "Operating System",
curr: 16, // 从15递增到16
total: 20,
nodes: []
}
]
}
]
},
...
*/
// 再次调用函数更新 'id7'
console.log("\n--- 再次更新 'id7' 之前 ---");
console.log(JSON.stringify(data, null, 2));
updateNodeAndParentsRecursively(data, 'id7');
console.log("\n--- 再次更新 'id7' 之后 ---");
console.log(JSON.stringify(data, null, 2));
/*
预期输出(部分关键节点):
...
{
key: "id5",
name: "Category 2",
curr: 0, // 保持不变,因为是根节点
total: 0,
nodes: [
{
key: "id6",
name: "Sub Category",
curr: 13, // 从12递增到13
total: 48,
nodes: [
{
key: "id7",
name: "Inside Sub",
curr: 13, // 从12递增到13
total: 48,
nodes: []
}
]
}
]
},
...
*/通过这种递归与状态回溯结合的方法,我们能够优雅且高效地解决在树形结构中,根据特定节点更新其自身及其所有指定祖先节点属性的问题,同时灵活控制更新的范围。
以上就是递归更新树形结构中特定节点及其祖先的数值的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号