120 lines
2.7 KiB
HTML
120 lines
2.7 KiB
HTML
<script>
|
|
var N = {
|
|
Create:(inMeta, ...inChildren)=>
|
|
{
|
|
var output = {
|
|
Meta:inMeta,
|
|
|
|
Children:[],
|
|
Parents:[],
|
|
WalkID:0,
|
|
|
|
edited:[],
|
|
editing:[]
|
|
};
|
|
inChildren.forEach( inChild => N.ConnectChild(output, inChild) );
|
|
return output;
|
|
},
|
|
ConnectChild:(inParent, inChild)=>
|
|
{
|
|
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];
|
|
if(next.WalkID !== inWalkID)
|
|
{
|
|
next.WalkID = inWalkID;
|
|
let results = inIterator(next);
|
|
if(results !== false)
|
|
{
|
|
N.Walk(next, inKey, inIterator, inWalkID);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
WalkChildren:(inNode, inIterator, inWalkID) => N.Walk(inNode, "Children", inIterator, inWalkID),
|
|
WalkParents :(inNode, inIterator, inWalkID) => N.Walk(inNode, "Parents", inIterator, inWalkID)
|
|
};
|
|
</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)
|
|
);
|
|
|
|
let orchard = N.Create("orchard", tree1, tree2);
|
|
|
|
|
|
/*************************************************************/
|
|
|
|
// 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);
|
|
});
|
|
|
|
console.log(additionNodes);
|
|
console.log(multiplyNodes);
|
|
</script> |