2

I am given the number of trees of size N with branching factor b and attempting to generate Catalans number without the use of a separate recursive function to generate the the number of nested for loops.

Ive tried this so far

int catalan(int size, int b)
{

  int n = 0;
  int c = b;

  if (size < 2)
  {
    return 1;
  }

  if (b <= 0)
  {
    return n;
  }

  while(c--)
  {   
    for (int s = 0; s < size; s++)
    {
      n += (catalan(s,b) * catalan(size-1-s-c, b) );
    }
    catalan(size--,c);
  }

  return n;
}

for a size of 4 and branching factor of 2 I expect this function to return 14 but instead I receive 21.

thanks in advance.

jack1950
  • 21
  • 1
  • Could you please edit your question and clarify what you want to obtain and what is your problem? You say that you don't want a "separate recursive function", but you already have only one function. So what is the problem exactly? Moreover capitalize appropriately your question, remove the thanks line, fix the spelling and the repeated words. Show that you care about the question and someone else may care to answer. Add a [MCVE](https://stackoverflow.com/help/mcve), i.e. an appropriate `main()`. What debugger have you used? – Costantino Grana Feb 09 '19 at 07:14
  • What is the purpose of `catalan(size--,c);`? Would it be somewhat different from `size--`? – Hanjoung Lee Feb 09 '19 at 10:12

0 Answers0