Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's trivial to express your tree structure in JSON. The set of path choices in each question node is an array of question nodes. This is a classic recursive data structure. </p> <p>Do that in your JSON, then add another branch alongside it, which is a series of decision-point choices in an array: [3, 1, 2], representing a path through the tree, from the root of the tree to a leaf: At the root, choose option 3; at the node you reach by following option 3, choose option 1; at that node, choose option 2. You're there. This is a simple loop through the choices array. The meaning of each integer in the choices array is dependent on which path segment the previous one sent you down. It's a state machine expressed as a data structure: When you're traversing the tree, the the current node is your state and the next number in the choice-path array is your input. </p> <p>I started numbering at 1 because you did -- but bear in mind that arrays in JavaScript start indexing at ZERO, not 1. The first item in array a is a[0], not a[1]. </p> <pre><code>var json = JSON.parse(thingy); var choices = json.Choices; // array of user choices var curNode = json.Tree; // root of the tree for ( var i = 0; i &lt; choices.length; ++i ) { // Do whatever you need to with the current node curNode = curNode.Children[choices[i]]; } </code></pre> <p>But add lots of range checking, of course!</p> <p>It gains nothing, and adds confusion, to try to express both the tree, and the path through the tree, in the same data structure -- if that's even what you're contemplating. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload