catalan

generate all structurally distinct full binary trees with n leaves

心不动则不痛 提交于 2019-11-30 20:20:17
问题 This is a homework, I have difficulties in thinking of it. Please give me some ideas on recursions and DP solutions. Thanks a lot generate and print all structurally distinct full binary trees with n leaves in dotted parentheses form, "full" means all internal (non-leaf) nodes have exactly two children. For example, there are 5 distinct full binary trees with 4 leaves each. 回答1: U can use recursion, on i-th step u consider i-th level of tree and u chose which nodes will be present on this

Recurrence Approach : How can we generate all possibilities on braces?

醉酒当歌 提交于 2019-11-30 10:32:25
How can we generate all possibilities on braces ? N value has given to us and we have to generate all possibilities. Examples: 1) if N == 1, then only one possibility () . 2) if N==2, then possibilities are (()), ()() 3) if N==3, then possibilities are ((())), (())(),()()(), ()(()) ... Note: left and right braces should match. I mean )( is INVALID for the N==1 Can we solve this problem by using recurrence approach ? From wikipedia - A Dyck word is a string consisting of n X's and n Y's such that no initial segment of the string has more Y's than X's (see also Dyck language). For example, the

What is the fastest (known) algorithm to find the n-th Catalan number mod m?

て烟熏妆下的殇ゞ 提交于 2019-11-29 23:13:28
The problem is to find the n-th Catalan number mod m , where m is NOT prime , m = (10^14 + 7) . Here are the list of methods that I have tried: (max N = 10,000 ) Dynamic programming for table look-up, too slow Use Catalan formula ncr(2*n, n)/(n + 1) , again it wasn't fast enough due to the ncr function, can't speed up using exponentiation squaring because m is not prime. Hardcode a table of pre-generated Catalans , but it failed due to the file size limit. Recurrence relation C(i,k) = C(i-1,k-1) + C(i-1,k) , this is way too slow So I wonder is there any other faster algorithm to find the n-th

What is the fastest (known) algorithm to find the n-th Catalan number mod m?

*爱你&永不变心* 提交于 2019-11-28 20:33:18
问题 The problem is to find the n-th Catalan number mod m , where m is NOT prime , m = (10^14 + 7) . Here are the list of methods that I have tried: (max N = 10,000 ) Dynamic programming for table look-up, too slow Use Catalan formula ncr(2*n, n)/(n + 1) , again it wasn't fast enough due to the ncr function, can't speed up using exponentiation squaring because m is not prime. Hardcode a table of pre-generated Catalans , but it failed due to the file size limit. Recurrence relation C(i,k) = C(i-1,k

How to print all possible balanced parentheses for an expression?

天大地大妈咪最大 提交于 2019-11-27 15:18:08
问题 For example, with elements a,b,c,d , 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): (((ab)c)d), ((a(bc))d), ((ab)(cd)), (a((bc)d)) and (a(b(cd))) The first example multiplies a*b , then multiplies that product by c , then multiplies that product by d . The second example first multiplies b*c , then multiplies that product by a , then multiplies that product

Finding all combinations of well-formed brackets

谁说我不能喝 提交于 2019-11-26 21:28:44
This came up while talking to a friend and I thought I'd ask here since it's an interesting problem and would like to see other people's solutions. The task is to write a function Brackets(int n) that prints all combinations of well-formed brackets from 1...n. For Brackets(3) the output would be () (()) ()() ((())) (()()) (())() ()(()) ()()() markt Took a crack at it.. C# also. public void Brackets(int n) { for (int i = 1; i <= n; i++) { Brackets("", 0, 0, i); } } private void Brackets(string output, int open, int close, int pairs) { if((open==pairs)&&(close==pairs)) { Console.WriteLine(output

With ' N ' no of nodes, how many different Binary and Binary Search Trees possible?

▼魔方 西西 提交于 2019-11-26 21:17:50
For Binary trees: There's no need to consider tree node values, I am only interested in different tree topologies with 'N' nodes. For Binary Search Tree: We have to consider tree node values. I recommend this article by my colleague Nick Parlante (from back when he was still at Stanford). The count of structurally different binary trees (problem 12) has a simple recursive solution (which in closed form ends up being the Catalan formula which @codeka's answer already mentioned). I'm not sure how the number of structurally different binary search trees (BSTs for short) would differ from that of

Finding all combinations of well-formed brackets

北城以北 提交于 2019-11-26 07:58:05
问题 This came up while talking to a friend and I thought I\'d ask here since it\'s an interesting problem and would like to see other people\'s solutions. The task is to write a function Brackets(int n) that prints all combinations of well-formed brackets from 1...n. For Brackets(3) the output would be () (()) ()() ((())) (()()) (())() ()(()) ()()() 回答1: Took a crack at it.. C# also. public void Brackets(int n) { for (int i = 1; i <= n; i++) { Brackets("", 0, 0, i); } } private void Brackets

With &#39; N &#39; no of nodes, how many different Binary and Binary Search Trees possible?

梦想与她 提交于 2019-11-26 06:54:49
问题 For Binary trees: There\'s no need to consider tree node values, I am only interested in different tree topologies with \'N\' nodes. For Binary Search Tree: We have to consider tree node values. 回答1: I recommend this article by my colleague Nick Parlante (from back when he was still at Stanford). The count of structurally different binary trees (problem 12) has a simple recursive solution (which in closed form ends up being the Catalan formula which @codeka's answer already mentioned). I'm