2021-05-05 21:53:04 -04:00
|
|
|
<script>
|
|
|
|
var N = {
|
|
|
|
Create:(inMeta, ...inChildren)=>
|
|
|
|
{
|
|
|
|
var output = {
|
|
|
|
Meta:inMeta,
|
|
|
|
|
|
|
|
Children:[],
|
|
|
|
Parents:[],
|
|
|
|
WalkID:0,
|
|
|
|
|
|
|
|
edited:[],
|
|
|
|
editing:[]
|
|
|
|
};
|
2021-05-05 22:50:54 -04:00
|
|
|
inChildren.forEach( inChild => N.ConnectChild(output, inChild) );
|
2021-05-05 21:53:04 -04:00
|
|
|
return output;
|
|
|
|
},
|
2021-05-05 22:50:54 -04:00
|
|
|
ConnectChild:(inParent, inChild)=>
|
2021-05-05 21:53:04 -04:00
|
|
|
{
|
|
|
|
inParent.Children.push(inChild);
|
|
|
|
inChild.Parents.push(inParent);
|
|
|
|
},
|
|
|
|
Walk:(inNode, inKey, inIterator, inWalkID) =>
|
|
|
|
{
|
|
|
|
let array = inNode[inKey];
|
|
|
|
for(let i=0; i<array.length; i++)
|
|
|
|
{
|
|
|
|
let next = array[i];
|
2021-05-05 22:50:54 -04:00
|
|
|
if(next.WalkID !== inWalkID)
|
2021-05-05 21:53:04 -04:00
|
|
|
{
|
|
|
|
next.WalkID = inWalkID;
|
|
|
|
let results = inIterator(next);
|
|
|
|
if(results !== false)
|
|
|
|
{
|
|
|
|
N.Walk(next, inKey, inIterator, inWalkID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-05-05 22:50:54 -04:00
|
|
|
WalkChildren:(inNode, inIterator, inWalkID) => N.Walk(inNode, "Children", inIterator, inWalkID),
|
|
|
|
WalkParents :(inNode, inIterator, inWalkID) => N.Walk(inNode, "Parents", inIterator, inWalkID)
|
2021-05-05 21:53:04 -04:00
|
|
|
};
|
|
|
|
</script>
|
|
|
|
<script>
|
|
|
|
let tree1 = N.Create("root1",
|
|
|
|
N.Create("branch1",
|
|
|
|
N.Create("leaf1"),
|
|
|
|
N.Create("leaf2"),
|
|
|
|
N.Create("leaf3"),
|
|
|
|
),
|
|
|
|
N.Create("branch2",
|
|
|
|
N.Create("leaft3"),
|
|
|
|
N.Create("leaft4")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
let leaves = [];
|
|
|
|
let leavesCollect = n=>
|
|
|
|
{
|
|
|
|
if(n.Children.length == 0)
|
|
|
|
{
|
|
|
|
leaves.push(n);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
N.WalkChildren(tree1, leavesCollect, 1);
|
|
|
|
|
|
|
|
let tree2 = N.Create("root2",
|
|
|
|
N.Create("branch3",
|
|
|
|
N.Create("leaf5"),
|
|
|
|
N.Create("leaf6")
|
|
|
|
),
|
|
|
|
N.Create("branch4", ...leaves)
|
|
|
|
);
|
|
|
|
|
2021-05-05 22:50:54 -04:00
|
|
|
let orchard = N.Create("orchard", tree1, tree2);
|
|
|
|
|
2021-05-05 21:53:04 -04:00
|
|
|
|
2021-05-05 22:50:54 -04:00
|
|
|
/*************************************************************/
|
2021-05-05 21:53:04 -04:00
|
|
|
|
2021-05-05 22:50:54 -04:00
|
|
|
// phase 0, start node
|
|
|
|
let modified = tree1;
|
|
|
|
|
|
|
|
// phase 1, walk up and flag additions
|
|
|
|
let additionNodes = [];
|
|
|
|
let additionGather = n =>
|
|
|
|
{
|
|
|
|
additionNodes.push(n);
|
|
|
|
};
|
|
|
|
|
|
|
|
// phase 2 walk down and flag multiply, also collect leaves
|
|
|
|
let multiplyNodes = [];
|
|
|
|
let multiplyLeaves = [];
|
|
|
|
let multiplyGather = n =>
|
|
|
|
{
|
|
|
|
multiplyNodes.push(n)
|
|
|
|
n.Children.length == 0 ? multiplyLeaves.push(n) : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// phase 3 walk up from leaves and flag additions
|
|
|
|
// (re-uses phase 1)
|
|
|
|
|
|
|
|
///////////////////////
|
|
|
|
|
|
|
|
// phase 0
|
|
|
|
modified.WalkID = "add-1";
|
|
|
|
|
|
|
|
//phase 1
|
|
|
|
N.Walk(modified, "Parents", additionGather, modified.WalkID);
|
|
|
|
|
|
|
|
//phase 2
|
|
|
|
N.Walk(modified, "Children", multiplyGather, modified.WalkID);
|
|
|
|
|
|
|
|
//phase 3
|
|
|
|
multiplyLeaves.forEach(leaf=>{
|
|
|
|
N.Walk(leaf, "Parents", additionGather, modified.WalkID);
|
|
|
|
});
|
2021-05-05 21:53:04 -04:00
|
|
|
|
2021-05-05 22:50:54 -04:00
|
|
|
console.log(additionNodes);
|
|
|
|
console.log(multiplyNodes);
|
2021-05-05 21:53:04 -04:00
|
|
|
</script>
|