recursion

Tricky Segmentation faults with BST recursion in C

偶尔善良 提交于 2019-12-24 09:35:32
问题 I'm trying to add strings to a Binary Search Tree using a recursive insert method (the usual for BSTs, IIRC) so I can later print them out using recursion as well. Trouble is, I've been getting a segmentation faults I don't really understand. Related code follows (this block of code is from my main function): #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> // Stores the size of the C-strings we will use; // Standardized to 100 (assignment specifications say //

Thread Stack leak through a recursive function

感情迁移 提交于 2019-12-24 09:28:12
问题 I have following scenario, in C implementation with pthreads : The main thread(T1) creates another thread(T2) which waits for an event. On an event T2 calls a Tree Traversal recusive function that takes in a function pointer for an action to be executed on a Tree node. During Tree traversal, if the node is found the fn pointer is fired that creates a thread (T3), services the node and is supposed to normally die out. I am observing a huge memory leak that comes from the T3's stack. The

Finding minimum element in a list (recursively) - Python

霸气de小男生 提交于 2019-12-24 09:25:10
问题 I'm trying to find the minimum value in a list of integers using recursion. The main idea is that if the list is only one element long, this element is my minimum. Else, I divide the list onto two smaller lists of the same size, look for minimum value in both of them and then compare which one is smaller. The code looks like this: def minimum(a,l,p): if l == p: return a[l] else: m1 = minimum(a,l, (l+p)/2) m2 = minimum(a, (l+p)/2 + 1, p) if m1 < m2: return m1 else: return m2 It doesn't seem to

template class containing a stdvector of references to its own instances

吃可爱长大的小学妹 提交于 2019-12-24 09:15:56
问题 I'm relatively new to C++, and am trying for the first time to build a complex template structure. How can I declare, as member of a template class Foo, a std::vector of Foo* elements, but that could be of various types? #include <vector> template <typename T> class Foo { T mValue; std::vector< Foo<T>* > mFooParameters; // <---- I would like this vector to contain // any sort of Foo<T>* elements, // Foo<int>*, Foo<double>*, etc. }; Is it straightforward, possible but complicated, or

mysql recursive self join

房东的猫 提交于 2019-12-24 09:13:23
问题 create table test( container varchar(1), contained varchar(1) ); insert into test values('X','A'); insert into test values('X','B'); insert into test values('X','C'); insert into test values('Y','D'); insert into test values('Y','E'); insert into test values('Y','F'); insert into test values('A','P'); insert into test values('P','Q'); insert into test values('Q','R'); insert into test values('R','Y'); insert into test values('Y','X'); select * from test; mysql> select * from test; +----------

How to return value from recursive function in python?

六月ゝ 毕业季﹏ 提交于 2019-12-24 08:30:19
问题 I'm working with binary tree in python. I need to create a method which searches the tree and return the best node where a new value can be inserted. But i'm have trouble returning a value from this recursive function. I'm a total newbie in python. def return_key(self, val, node): if(val < node.v): if(node.l != None): self.return_key(val, node.l) else: print node.v return node else: if(node.r != None): #print node.v self.return_key(val, node.r) else: print node.v return node Printing node.v

Closed form Fibonacci Series

若如初见. 提交于 2019-12-24 08:26:15
问题 I am using Python to create a Fibonacci using this formula: I have this recursive Fibonacci function: def recursive_fibonacci(n): if n <= 1: return int((((1 / (5 ** 0.5)) * (1 + (5 ** 0.5))) ** n) - (((1 / (5 ** 0.5)) * (1 - (5 ** 0.5))) ** n)) else: return(recursive_fibonacci(n - 1) + recursive_fibonacci(n - 2)) To display it I am using this: nterms = 10 if nterms <= 0: print("Please Enter a positive integer") else: print("Recursive Fibonacci Sequence: " , [recursive_fibonacci(i) for i in

how do I get a recursive result by querying a self referencing table in mysql?

淺唱寂寞╮ 提交于 2019-12-24 08:23:35
问题 I have a self-referencing table 'comments' where comments.replyToId REFERENCES comments.ID. My question is, how do I query a database with a self-referencing table to get a result that is properly ordered so that I can represent the result as a tree in PHP? I've tried select * from comments as comments_1 left join comments as comments_2 on comments_1.id = comments_2.replyToId I'm trying to use the result of this in php 回答1: You're not going to get a recursive result out of MySQL directly.

Make a tree grow horizontally applying modifications to current node

孤街浪徒 提交于 2019-12-24 08:19:38
问题 Given an input, I am trying to build a tree which should grow horizontally applying transformations to that input and the consequent children. For example, given the input 'aab' and two transformation rules like: ab -> bba b -> ba A tree like this would need to be built: I have written the code, but the way I have done it, my tree works vertically, and I don't want that. I need it to work horizontally and I fail to see where/how I would write the recursion. Here is what I have right now:

Get number of nodes at given level of JSON tree

烂漫一生 提交于 2019-12-24 08:09:37
问题 I have a JSON tree structure with nodes that have children, and I would like to know how many nodes exist at a given level of the structure. I currently have this recursive structure: var getNumNodesAtLevel = function (node, curr, desired) { if (curr === (desired - 1)) { return node.children.length; } else { var children = node.children; children.forEach(function (child) { return getNumNodesAtLevel(child, curr + 1, desired); }); } }; This doesn't work - any help would be appreciated! 回答1: