Implement recursive algorithms with sub-workflows: Towers of Hanoi demo
Ready for 3.0What this workflow does
This workflow has 13 nodes and runs manually, when you click “Execute workflow” (plus 1 other trigger). It uses only n8n's built-in nodes.
How it runs
Trigger: Manually, when you click “Execute workflow” (plus 1 other trigger).
- Set number of discs (Edit Fields (Set)): sets 1 field: numberOfDiscs
- Max 1 disc (If): checks a condition and branches into true / false
- Create stacks (Code): runs custom JavaScript code
- X to Z (Code): runs custom JavaScript code
- X Z Y (Execute Sub-workflow): calls another n8n workflow
- A B C (Execute Sub-workflow): calls another n8n workflow
- Update (Code): runs custom JavaScript code
- Solution (Code): runs custom JavaScript code
- X to Z (Code): runs custom JavaScript code
- Y X Z (Execute Sub-workflow): calls another n8n workflow
…plus 1 more step. See the full node list below.
Before you upgrade to n8n 3.0
Nothing in this workflow is removed in n8n 3.0, so it should import and run after the upgrade.
- Code node: the task timeout drops from 300s to 60s. Test with a realistic data volume.
Nodes used (6)
- Edit Fields (Set)
n8n-nodes-base.set - If
n8n-nodes-base.if - Execute Sub-workflow
n8n-nodes-base.executeWorkflow - Execute Workflow Trigger
n8n-nodes-base.executeWorkflowTrigger - Code
n8n-nodes-base.code - Manual Trigger
n8n-nodes-base.manualTrigger
From the author
How it works This is an example of using sub-workflow nodes and a proof of concept showing that it’s possible to solve and explain recursive problems with n8n. Towers of Hanoi - Task Move a stack of n disks from rod A to rod C, using rod B as auxiliary. Only…