I have an algorithm question. And i don’t know how to handle it with Bubble, although i’ve tried several options.
The context :
i have a Datatype called Unit. It’s connected to other units with two Fields : Children (List Of Units) and Parent (single Unit). It reproduces a tree structure.
Each Unit has at the beginning a weight of 1.
Through the application, i have a UI where users connect each unit with one other unit, ie building the tree. At each connection, one unit becomes a Parent and the other Unit becomes a Child of the Parent.
My algorithmic question is the following :
Once the tree is built, how can i calculate the weight of each unit through the tree, knowing that the weight formula = Sum(Children’s weight) + 1
Hints to do the math & check the result :
- The sum of all the weight = the number of Units
- At most, just one unit has no parent (and then has the max weight)
Here’s an example of such a result :
If anyone has an idea how to implement it in Bubble …
Thanks in advance for your help
Fun problem. You’ll want to look into Recursive Workflows.
However, there could be a simpler implementation. I realize this might not be possible depending on the rest of your implementation, but I would look at the possibility of storing the weight as the tree is built. You should have a known weight when you add any node to the tree. You could setup a database trigger to update the weight of the parent node. The trigger would act in a recursive way because each time you update the parent node it will trigger again for that parents node. Your end of the recursion would be to only run the database trigger when the parent node is not empty (meaning stop when there are no more parent nodes).
Thx for your answer.
i’ve tried to look at DB Triggers. But sometimes, Units can be exchanged (a Parent becomes a Child of its Child, and this Child becomes the Parent). In that case, the triggers doesn’t handle very well calculation …
The workaround i’ve found is to trigger manually a Recursive Workflow (you were right), once a connection is complete (made and validated) (or unmade or exchanged, …).
Thanks for your help
This topic was automatically closed after 70 days. New replies are no longer allowed.