There is a tree structure of the plot with numerous branches, depending on the choice of the player. Each fork contains only two options.

This is arranged in the form of rows in the database table, where for each point its name is indicated, as well as the name of both points that a player can get from the current one.

We need a script to build an associative array, where each cell will contain two cells corresponding to possible transition points from this cell.

I understand how to recurse into the depths in order to build one branch of such a tree, I cannot understand how to return to the raw variants after that

    1 answer 1

    Here is an example of prefix tree traversal.

    public void PreOrderTraversal(Action action) { PreOrderTraversal(action, _head); } private void PreOrderTraversal(Action action, BinaryTreeNode node) { if (node != null) { action(node.Value); PreOrderTraversal(action, node.Left); PreOrderTraversal(action, node.Right); } } 

    The Action parameter defines the action on each of the nodes.

    UPDATE
    I did not understand your question correctly. Loading tree from xml file (same principle).

     public MyTree<string> LoadTree() { MyTree<string>.Node[] NewNode = new MyTree<string>.Node[xNodes.Count]; for (int i = xNodes.Count - 1; i >= 0; i--) { string self_name = xNodes[i].InnerText; string true_id = xNodes[i].Attributes["true_id"].Value; string false_id = xNodes[i].Attributes["false_id"].Value; if (true_id == "null" && false_id == "null") { NewNode[i] = new MyTree<string>.Node(self_name); } else { NewNode[i] = new MyTree<string>.Node(self_name, NewNode[int.Parse(true_id)], NewNode[int.Parse(false_id)]); } } MyTree<string> Tree = new MyTree<string>(); Tree.Root = NewNode[0]; return Tree; } 

    The whole project can be seen in my DropBox

    • This is the code that bypasses the already finished tree. And I need a code that will build me a tree on the table of values. - Konorlevich
    • Pavel, thank you. But there is a problem. The code you submitted was written in a language I did not know. If you do not mind, please add comments explaining the actions. I feel that the otgadka in the hands, but I can not decipher. - Konorlevich