title: Creating full hierarchy string from parent-child structure algorithm, recursion
body: <p>I am solving following task. There is a given hierarchy of elements. I have the lowest element (base) and I need to create full hierarchy string. I have a function, which returns parents of an element. That means I can ask for a parent, then parents of the parent, etc.</p>
  <p>Example: B is Parent of A, [C1, C2] are parents of B, ...</p>
  <p><img src="https://i.stack.imgur.com/THvIY.png" alt="Example" /></p>
  <p>In this example, the result should be array of 3 strings (lines 1,2,3), each containing the full hierarchy for the base element A.</p>
  <p>This is my pseudocode function:</p>
  <pre><code>Function getAllParents (Element)
  
      ParentCount = getParentCount(Element)
      result = Element
      IF (ParentCount &gt; 0) THEN
          FOR i = 0 to ParentCount
              ParentName = getParentName(Element, i)
              result =  result + &quot;,&quot; + getAllParents(parent)
          NEXT
      END IF
      
      IF (ParentCount = 0) then       
          result =  result + &amp;newLine
      END IF
      
      RETURN result
  
  END Function
  </code></pre>
  <p>It gives me following result for my example:</p>
  <pre><code>A,B,C1,D1,E1 
  C2,D2,E2  
  E3
  </code></pre>
  <p>How to achieve desired result?:</p>
  <pre><code>A,B,C1,D1,E1
  A,B,C2,D2,E2
  A,B,C2,D2,E3
  </code></pre>