let i = 0
function depthFirstPostOrderWithHistory(nodeList, parent, visitedNodes, callback) {
for (const node of nodeList) {
// 先递归处理子节点,传递当前节点作为父节点
if (node.nodeOutputList && node.nodeOutputList.length > 0) {
depthFirstPostOrderWithHistory(node.nodeOutputList, node, visitedNodes, callback);
}
console.log(node.node.title, JSON.parse(JSON.stringify(visitedNodes)) );
// 处理当前节点
visitedNodes.push(node); // 将当前节点加入已遍历节点列表
callback(node, parent, visitedNodes);
}
}
// 示例数据
const data = [
{
nodeOutputList: [],
node: {
id: "3be9efbd-364a-42c4-9aab-60374e4e0f11",
type: "BinaryExpression",
operator: "+",
title: "加法",
},
id: "3be9efbd-364a-42c4-9aab-60374e4e0f11",
},
{
nodeOutputList: [
{
nodeOutputList: [],
node: {
id: "dba3ee18-f7fd-4776-950b-22b32d2a8df0",
type: "CallExpression",
memberExpression: {
objectName: "console",
propertyName: "log",
},
title: "打印信息 1",
},
parentId: "edb2820e-1391-4b85-9036-2148174a97c0",
id: "dba3ee18-f7fd-4776-950b-22b32d2a8df0",
},
{
nodeOutputList: [
{
nodeOutputList: [],
node: {
id: "9e41443b-c87b-4d84-9e6b-aacd9ffd6ac1",
type: "ArrayExpression",
title: "数组 3",
},
parentId: "46373ed7-9084-4573-a8db-a700810d5b47",
id: "9e41443b-c87b-4d84-9e6b-aacd9ffd6ac1",
},
{
nodeOutputList: [
{
nodeOutputList: [],
node: {
id: "2940fd1e-cc96-411c-8d8a-6c919106bf89",
type: "CallExpression",
memberExpression: {
objectName: "console",
propertyName: "log",
},
title: "打印信息 4",
},
parentId: "03868576-fe60-40fb-90f8-537d7027389c",
id: "2940fd1e-cc96-411c-8d8a-6c919106bf89",
},
],
node: {
id: "03868576-fe60-40fb-90f8-537d7027389c",
type: "IfStatement",
title: "if 判断 3",
},
parentId: "46373ed7-9084-4573-a8db-a700810d5b47",
id: "03868576-fe60-40fb-90f8-537d7027389c",
},
],
node: {
id: "46373ed7-9084-4573-a8db-a700810d5b47",
type: "IfStatement",
title: "if 判断 2",
},
parentId: "edb2820e-1391-4b85-9036-2148174a97c0",
id: "46373ed7-9084-4573-a8db-a700810d5b47",
},
],
node: {
id: "edb2820e-1391-4b85-9036-2148174a97c0",
type: "IfStatement",
title: "if 判断 1",
},
id: "edb2820e-1391-4b85-9036-2148174a97c0",
},
// {
// nodeOutputList: [],
// node: {
// title: '返回',
// id: "c5ba6d56-66c9-403e-b8a6-7bfc9073cf7f",
// type: "ReturnStatement",
// },
// id: "c5ba6d56-66c9-403e-b8a6-7bfc9073cf7f",
// },
];
// 遍历并打印节点及其父节点、已遍历节点
const visitedNodes = [];
depthFirstPostOrderWithHistory(data, null, visitedNodes, (node, parent, visited) => {
});
console