Branch Out - Monkey Edition The Monkey has swung home to find his tree all in pieces.

He likes order and scoops up all his 'tree-parts' and sets them out in 3 groups.

The Monkey's 3 nested lists:

STEP = [['S', '1', 'B', '3'], ['S', '3', 'B', '11'], ['S', '5', 'B', '12'], ['S', '4', 'B', '13'], ['S', '2', 'B', '14']]

TRANSITION = [['T', '2', 'B', '4'], ['T', '7', 'B', '4'], ['T', '3', 'S', '4'], ['T', '5', 'S', '5'], ['T', '1', 'S', '2'], ['T', '8', 'S', '2'], ['T', '6', 'S', '1'], ['T', '9', 'S', '2'], ['T', '4', 'S', '1'], ['T', '10', 'S', '1']]

BRANCH = [['B', '3', 'T', '1'], ['B', '3', 'T', '7'], ['B', '4', 'S', '3'], ['B', '11', 'T', '3'], ['B', '11', 'T', '5'], ['B', '12', 'T', '6'], ['B', '12', 'T', '8'], ['B', '13', 'T', '4'], ['B', '13', 'T', '9'], ['B', '14', 'T', '2'], ['B', '14', 'T', '10']]


Each element holds information as such:

# Example

STEP[0]  =  ['S', '1', 'B', '3']


Where:


'S' is the STEP type
'1' is the STEP number id
'B' is the linked BRANCH type
'3' is the linked BRANCH number id


Starting from a STEP the data is all linked, so using the linked reference you can find the next element and the next until another STEP is reached. 

This is some parameters of the data:


STEPS are connected to single BRANCHES
BRANCHES are connected to one or more TRANSITIONS
TRANSITIONS can be connected to a single BRANCH or STEP


The BRANCH data can have a fork where a single BRANCH id has one or more options for TRANSITIONS.

Re-create this monkey's tree (nested list format is fine) in the shortest way possible (he is not a fan of long instructions):

           [S1]
            |          
          B3|_________ 
            |         |
           [T1]     [T7]
            |         |
            |       B4|
            |         |
           [S2]     [S3]


And list all the routes on this tree:

Routes = [[S1, B3, T1, S2], [S1, B3, T7, B4, S3]]


UPDATE: This is Code-golf and from the data given you need to output the display of the tree ( This can be different from my example but requires the same elements to be displayed in any format) and as you cycle through the problem to output all the possible routes as a BONUS.

Notes:


This is a single-rooted tree
You may use a different format for input but please explicitly state the input format in your solution
Java (JDK), 570 bytes



t-&gt;{return "Routes = "+Arrays.deepToString(Arrays.stream(r(t.get(0).get(0),t,t.get(0).get(0)).split("\\[")).filter(d-&gt;d.endsWith("]")).map(d-&gt;d.replaceAll("]", "")).map(d-&gt;d.split("(?=[A-Z])")).collect(Collectors.toList()).toArray());};String r(String s, List&lt;List&lt;String&gt;&gt;t, String b){List&lt;String&gt;o = t.stream().filter(e-&gt;e.get(0).equals(s)).map(e-&gt;e.get(1)).collect(Collectors.toList());if(s.matches("^S(?!(?:1)$)\\d+"))return s;if(o.size()&gt;1){String x="";for(String n:o){x+="["+b+r(n,t,b+n)+"]";}return x;}if(o.size()==1)return s+r(o.get(0),t,b+o.get(0));return "]";}


As you may notice by my code I´m still learning a lot every day.
My answer is neighter pretty nor short and I honestly even hope this works for different testcases even though I tested a couple.
But I hope that this question gets the attention it deserves and there will be more answers I can learn from.

INPUT

String[][] STEP = { { "S1", "B3" },  ... ,{ "B14", "T2" }, { "B14", "T10" } };
List&lt;List&lt;String&gt;&gt; tupels = Arrays.stream(STEP).map(Arrays::asList).collect(Collectors.toList());
z(tupels);


I converted your given Input into an 2D-Java-Array String[][] and converted that into a List&lt;List&lt;String&gt;&gt;.

CODE

The first method calls a recursive method to get all routes and formats the output.

t-&gt;{
return "Routes ="+Arrays.deepToString(
  Arrays.stream(r(t.get(0).get(0),t,t.get(0).get(0)).split("\\["))
 .filter(d-&gt;d.endsWith("]")).map(d-&gt;d.replaceAll("]", ""))
 .map(d-&gt;d.split("(?=[A-Z])")).collect(Collectors.toList()).toArray());
};


The second method is the recursive method which gathers all the possible routes

String r(String s, List&lt;List&lt;String&gt;&gt;t, String b){
  List&lt;String&gt;o =t.stream().filter(e-&gt;e.get(0).equals(s)).map(e-&gt;e.get(1))
  .collect(Collectors.toList());
  if(s.matches("^S(?!(?:1)$)\\d+"))
    return s;
  if(o.size()&gt;1){
    String x="";
    for(String n:o){
      x+="["+b+r(n,t,b+n)+"]";
    }
    return x;
  }
  if(o.size()==1)
    return s+r(o.get(0),t,b+o.get(0));
  return "]";
}  


Try it online!