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",
},
];
const visitedNodes = [];
depthFirstPostOrderWithHistory(data, null, visitedNodes, (node, parent, visited) => {
});
console