'What is the best data structure to represent a conditional list of survey questions?
In the current domain I'm working in, we can send surveys that have a list of questions. When designing the survey, you can create conditionals per question that determine which question it will ask next. Questions could have multiple conditionals. Some questions may not have any conditionals at all.
So an example might be:
If Q1 answer > 5, then go to Q3 else go to Q2.
Using IDs, I can make a list or map of Question objects work, but it's not great if I want to traverse from the beginning to figure out the path someone took through a Survey. So I'm wondering what recommendations people might have for another data structure to solve this problem.
So far I've thought about a tree or a directed acyclic graph (with conditional edges). I haven't found any good examples of using a tree to conditionally choose a path based on dynamic expressions.
Represented as tree:
Q1
/ \
Q2 Q3
/ \ |
Q4 Q5 Q5
| | |
Q6 Q7 Q7
Represented as DAG:
Q1
/\
Q2 Q3
/ \ /
Q4 Q5
| |
Q6 Q7
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|