Document 3 — Adding Another Node Structural Growth
The previous example had two root nodes: customer and order.
Now we add a third root node: product.
Nothing in the traversal code changes. We only change the source structure and describe the new node with an instruction.
1. The Original Source
{
"customer": {
"name": "Keshav"
},
"order": {
"id": "ORD-101"
}
}
2. Add One More Root Node
{
"customer": {
"name": "Keshav"
},
"order": {
"id": "ORD-101"
},
"product": {
"code": "P-100"
}
}
We have simply added product.
The traversal engine does not need to know in advance that this node was added.
3. Add Its Instruction
{
"customer": {
"transform": "logPath"
},
"order": {
"transform": "logPath"
},
"product": {
"transform": "logPath"
}
}
Now when traversal reaches product,
it finds its instruction, understands it, and continues the exact same process.
4. The Same External Function
function logPath(value, path) {
console.log("I am here in the tree:", path, value);
}
We did not create a new function for product.
The same external function is reused.
5. Runtime Result
I am here in the tree: customer.name Keshav
I am here in the tree: order.id ORD-101
I am here in the tree: product.code P-100
The Point
Adding a node does not require changing the traversal algorithm. The source describes the structure and the instructions describe what should happen at that structure.