catalan

Calculate the nth Catalan number

僤鯓⒐⒋嵵緔 提交于 2019-12-11 08:39:46
问题 I wrote some code to calculate the Nth catalan number. However, it isn't returning the correct result when N=20 and onwards. The results when N<20 is correct though, so I'm not sure what is wrong. So, when N=20, its supposed to return 6564120420, but it returns 2269153124 for me. Can someone point me in the right direction? #include <iostream> using namespace std; unsigned long int countTree(unsigned int N) { //used to store catalan numbers unsigned long int catalan[N+1]; //N(0)=N(1)=1

Generate All Possible Trees

南楼画角 提交于 2019-12-06 10:27:44
问题 Given the following data type definition: data FormTree = Empty | Node FormTree FormTree deriving Show I want to write a function which generates an infinite list containing all possible trees sorted after length e.g. the amount of nodes. The following code almost does what I need but it only descends the tree on the right side by inserting additional nodes every time but I need it to alternate between both sides. allPossibleTrees :: [FormTree] allPossibleTrees = Empty : [Node x y | x <-

Generate permutation using a single stack

泪湿孤枕 提交于 2019-12-06 05:11:07
问题 Can anyone please explain algorithm to generate the permutations possible when using only a single stack and push and pop are the only operations allowed. Have searched about it a lot, but no definite answer. Also the total number of such permutations is given by catalan numbers. But I fail to get a proof for that. Kindly explain that as well if possible. Thanks!! 回答1: This problem uses an input queue and an output queue as well as a stack. The operations are "push an item from the input

What would be the time complexity of counting the number of all structurally different binary trees?

我的梦境 提交于 2019-12-05 13:02:04
Using the method presented here: http://cslibrary.stanford.edu/110/BinaryTrees.html#java 12. countTrees() Solution (Java) /** For the key values 1...numKeys, how many structurally unique binary search trees are possible that store those keys? Strategy: consider that each value could be the root. Recursively find the size of the left and right subtrees. */ public static int countTrees(int numKeys) { if (numKeys <=1) { return(1); } else { // there will be one value at the root, with whatever remains // on the left and right each forming their own subtrees. // Iterate through all the values that

Generate All Possible Trees

社会主义新天地 提交于 2019-12-04 17:55:09
Given the following data type definition: data FormTree = Empty | Node FormTree FormTree deriving Show I want to write a function which generates an infinite list containing all possible trees sorted after length e.g. the amount of nodes. The following code almost does what I need but it only descends the tree on the right side by inserting additional nodes every time but I need it to alternate between both sides. allPossibleTrees :: [FormTree] allPossibleTrees = Empty : [Node x y | x <- recursive, y <- recursive] where recursive = allPossibleTrees Executing take 5 allPossibleTrees gives:

Recursion with versus without memoization

孤街浪徒 提交于 2019-12-02 16:16:22
问题 I got homework in school to calculate Catalan number with recursion: 1st without memoization def catalan_rec(n): res = 0 if n == 0: return 1 else: for i in range (n): res += (catalan_rec(i))*(catalan_rec(n-1-i)) return res 2nd with: def catalan_mem(n, memo = None): if memo == None: memo = {0: 1} res = 0 if n not in memo: for i in range (n): res += (catalan_mem(i))*(catalan_mem(n-1-i)) memo[n] = res return memo[n] The weirdest thing happened to me: the memoization takes twice much time! When

Recursion with versus without memoization

北城以北 提交于 2019-12-02 10:40:51
I got homework in school to calculate Catalan number with recursion: 1st without memoization def catalan_rec(n): res = 0 if n == 0: return 1 else: for i in range (n): res += (catalan_rec(i))*(catalan_rec(n-1-i)) return res 2nd with: def catalan_mem(n, memo = None): if memo == None: memo = {0: 1} res = 0 if n not in memo: for i in range (n): res += (catalan_mem(i))*(catalan_mem(n-1-i)) memo[n] = res return memo[n] The weirdest thing happened to me: the memoization takes twice much time! When it should be the other way around! Can someone please explain this to me? This question inspired me to

Algorithm to generate mountain ranges with upstrokes and down-strokes (java)

Deadly 提交于 2019-12-01 05:13:06
I tried to do the classical problem to implement an algorithm to print all valid combinations of n pairs of parentheses. And I found this program (which works perfectly) : public static void addParen(ArrayList<String> list, int leftRem, int rightRem, char[] str, int count) { if (leftRem < 0 || rightRem < leftRem) return; // invalid state if (leftRem == 0 && rightRem == 0) { /* all out of left and right parentheses */ String s = String.copyValueOf(str); list.add(s); } else { if (leftRem > 0) { // try a left paren, if there are some available str[count] = '('; addParen(list, leftRem - 1,

Algorithm to generate mountain ranges with upstrokes and down-strokes (java)

浪尽此生 提交于 2019-12-01 02:55:51
问题 I tried to do the classical problem to implement an algorithm to print all valid combinations of n pairs of parentheses. And I found this program (which works perfectly) : public static void addParen(ArrayList<String> list, int leftRem, int rightRem, char[] str, int count) { if (leftRem < 0 || rightRem < leftRem) return; // invalid state if (leftRem == 0 && rightRem == 0) { /* all out of left and right parentheses */ String s = String.copyValueOf(str); list.add(s); } else { if (leftRem > 0) {

generate all structurally distinct full binary trees with n leaves

大兔子大兔子 提交于 2019-12-01 01:08:12
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. 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 level according to constraints: - there is parent on previous level - no single children present (by your