title: Prune a tree built from a txt file
body: <p>I have a txt file, which contains a sequence of numbers like the following:</p>
  
  <pre><code>1 1 3 2
  2 2 1 1
  3 2 3 1
  2 2 2 2
  1 1 1 1
  3 2 3 2
  1 1 3 1
  2 1 1 1
  </code></pre>
  
  <p>(It is only an example to explain my problem.) Each row, in the file txt, has four positions separated by a space. The first and the third can assume three values: 1, 2 or 3.
  The remaining positions can assume only two values: 1 or 2. </p>
  
  <p>Each row represents the path of a tree. The tree consists of a root node and levels of additional nodes that form the hierarchy: the first and the third levels can have three nodes (1, 2 or 3) and so on, as previously described. </p>
  
  <p>Then, the tree described by previous example is the following:</p>
  
  <p><a href="https://i.stack.imgur.com/lnMHQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lnMHQ.jpg" alt="enter image description here"></a></p>
  
  <p>I would prune the tree as described in the following picture:</p>
  
  <p><a href="https://i.stack.imgur.com/zQ83i.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zQ83i.jpg" alt="enter image description here"></a></p>
  
  <p>The nodes circled in red must be reduced to just one node, because each node in the circles has only one leaf.</p>