I created decision-tree because I was creating a sort of online quiz and I wanted an abstraction to keep track of state as the user completed questions. With decision-tree, you can create a tree-like structure in which answers to questions determine which question or result is shown next as the user continues through the quiz. As the path (or branch) is constructed, it is stored so that the application can know how the user arrived at a result or allow them to backtrack if necessary.
Let’s take a look at an example. First, we need to construct the tree which consists of objects representing questions, options, and results. I apologize in advance for how geeky this example is:
Once a decision-tree instance is created, we can navigate it by setting answers to questions and calling next like:
In this example, if the node returned by next has no leadsTo property we can assume that we've reached a result. In the navigation above, we would have arrived at the result:
If we wanted to arrive at a different result, we can call prev to go back to the previous question and change our answer like:
Now our result would be:
You can see decision-tree in action at the binx health website where it’s being used to power Find My Test, an assessment that helps people determine what kind of sexually-transmitted infection (STI) testing is suited for them: https://app.mybinxhealth.com/find-my-test
I hope this article was helpful in explaining how you would use decision-tree. If you find this utility useful, please star it on GitHub and consider sponsoring me!
A utility for traversing decision trees by selecting options - ianwalter/decision-tree