Recursion in computer science is a method of problem solving where the solution to a problem depends on solutions to smaller instances of the same problem.
180
votes
13answers
22k views
What is the most efficient/elegant way to parse a flat table into a tree?
Assume you have a flat table that stores an ordered tree hierarchy:
Id Name ParentId Order
1 'Node 1' 0 10
2 'Node 1.1' 1 10
3 'Node 2' 0 ...
60
votes
20answers
15k views
Can every recursion be converted into iteration?
A reddit thread brought up an apparently interesting question:
Tail recursive functions can trivially be converted into iterative functions. Other ones, can be transformed by using an explicit stack. ...
38
votes
6answers
9k views
How can I convert a series of parent-child relationships into a hierarchical tree?
I have a bunch of name-parentname pairs, that I'd like to turn into as few heirarchical tree structures as possible. So for example, these could be the pairings:
Child : Parent
H : G
F : G
...
80
votes
13answers
21k views
Way to go from recursion to iteration
I've used recursion quite a lot on my many years of programming to solve simple problems, but I'm fully aware that sometimes you need iteration due to memory/speed problems.
So, sometime in the very ...
42
votes
13answers
81k views
How to search by key=>value in a multidimensional array in PHP
Is there any fast way to get all subarrays where a key value pair was found in a multidimensional array? I can't say how deep the array will be.
Simple example array:
$arr = array(0 => ...
249
votes
16answers
64k views
What is tail-recursion?
Whilst starting to learn lisp, I've come across the term tail-recursive. What does it mean?
72
votes
23answers
16k views
Understanding recursion [closed]
Guys I'm having major trouble understanding recursion at school. Whenever the prof is talking about it I seem to get it but as soon as I try it on my own it completely blows my brains. I was trying to ...
47
votes
5answers
34k views
How to [recursively] Zip a directory in PHP?
Directory is something like:
home/
file1.html
file2.html
Another_Dir/
file8.html
Sub_Dir/
file19.html
I am using the same PHP Zip class used in PHPMyAdmin ...
29
votes
9answers
37k views
Recursively list files in Java
How do I recursively list all files under a directory in Java? Does the framework provide any utility?
I saw a lot of hacky implementations. But none from the framework or nio
130
votes
8answers
17k views
What Is Tail Call Optimization?
Very simply, what is tail-call optimization? More specifically, Can anyone show some small code snippets where it could be applied, and where not, with an explanation of why?
21
votes
7answers
14k views
Design patterns for converting recursive algorithms to iterative ones
Are there any general heuristics, tips, tricks, or common design paradigms that can be employed to convert a recursive algorithm to an iterative one? I know it can be done, I'm wondering if there are ...
43
votes
25answers
23k views
Recursion or Iteration?
Is there a performance hit if we use loop instead of recursion or vice versa in algorithms where both can serve the same purpose? Eg : Check if given string is palindrome.
I have seen many programmers ...
27
votes
1answer
1k views
Recursive function causing a stack overflow
I am trying to write a simple sieve function to calculate prime numbers in clojure. I've seen this question about writing an efficient sieve function, but I am not to that point yet. Right now I am ...
7
votes
3answers
2k views
PHP Create a Multidimensional Array from an array with relational data [duplicate]
Possible Duplicate:
Converting an array from one to multi-dimensional based on parent ID values
I am working in PHP.
I have the following array that has relational data (parent child ...
1
vote
3answers
2k views
Multidimensional array iteration
Say you have the following array:
$nodes = array(
"parent node",
"parent node",
array(
"child node",
"child node",
array(
"grand child node",
...