title: How to print all possible balanced parentheses for an expression?
body: <p>For example, with elements <code>a,b,c,d</code>, there are 5 possible ways to take neighboring elements and reduce them into a single element, where exactly two elements must be combined at a time (below represented by parentheses):</p>
  
  <pre><code>(((ab)c)d), ((a(bc))d), ((ab)(cd)), (a((bc)d)) and (a(b(cd)))
  </code></pre>
  
  <p>The first example multiplies <code>a*b</code>, then multiplies that product by <code>c</code>, then multiplies that product by <code>d</code>. The second example first multiplies <code>b*c</code>, then multiplies that product by <code>a</code>, then multiplies that product by <code>d</code>. </p>
  
  <p>Any valid parenthesized expression of 2n elements will necessarily have n <code>(</code> and n <code>)</code> with the property that, reading from left to right, there are always at least as many <code>(</code> as <code>)</code>. </p>
  
  <p>I know that for n numbers, the number of ways is the (n-1)th <a href="http://en.wikipedia.org/wiki/Catalan_number" rel="nofollow noreferrer">Catalan number</a>. But how does one accurately generate all the resulting groupings?</p>
  
  <p>Thanks </p>
  
  <p>(As an aside: There are over 160 equivalent formulations of this problem, all based on different combinatorial objects enumerated by the Catalan Numbers. For the most up to date list of these, see <a href="http://math.mit.edu/~rstan/ec/catadd.pdf" rel="nofollow noreferrer">Richard Stanley's Catalan Addendum</a>.)</p>