recursion

What is recursion and when should I use it?

One of the topics that seems to come up regularly on mailing lists and online discussions is the merits (or lack thereof) of doing a Computer Science Degree. An argument that seems to come up time and again for the negative party is that they have been coding for some number of years and they have never used recursion. So the question is...

Expression Evaluation and Tree Walking using polymorphism? (ala Steve Yegge)

This morning, I was reading Steve Yegge's: When Polymorphism Fails, when I came across a question that a co-worker of his used to ask potential employees when they came for their interview at Amazon. As an example of polymorphism in action, let's look at the classic "eval" interview question, which (as far as I know) was brough...

What is the best way to learn recursion?

When I started in programming I started with c++ and was doing recursion in my second semester in a data structures class. I don't even remember how we started it, I think it was linked lists, but its a concept that I picked up easily and can jump into quickly, even when I haven't written any recursive code in quite some time. What is t...

Is there some way to speed up recursion by remembering child nodes?

For example, Look at the code that calculates the n-th Fibonacci number: fib(int n) { if(n==0 || n==1) return 1; return fib(n-1) + fib(n-2); } The problem with this code is that it will generate stack overflow error for any number greater than 15 (in most computers). Assume that we are calculating fib(10). In this pro...

Find number of files in all subdirectories

Is there a way to find the number of files of a specific type without having to loop through all results inn a Directory.GetFiles() or similar method? I am looking for something like this: int ComponentCount = MagicFindFileCount(@"c:\windows\system32", "*.dll"); I know that I can make a recursive function to call Directory.GetFiles , ...

What is tail-recursion?

Whilst starting to learn lisp, I've come across the term tail-recursive. What does it mean? ...

How to keep a "things done" count in a recursive algorithm in Java?

I have a recursive algorithm which steps through a string, character by character, and parses it to create a tree-like structure. I want to be able to keep track of the character index the parser is currently at (for error messages as much as anything else) but am not keen on implementing something like a tuple to handle multiple return...

How do you get the last record generated in a recursive CTE?

In the code below I am using a recursive CTE(Common Table Expression) in SQL Server 2005 to try and find the top level parent of a basic hierarchical structure. The rule of this hierarchy is that every CustID has a ParentID and if the CustID has no parent then the ParentID = CustID and it is the highest level. DECLARE @LookupID int --O...

Recursion in Live VC++ Code

Would you use recursion in live VC++ code? ...

Can Ruby convert an acts_as_nested_set to a JSON hash cleanly without recursion?

Is there a fast and clean way of returning a JSON hash back from any node in a Ruby on Rails' acts_as_nested_set without using recursion? Here's the recursive solution for reference: class Node < ActiveRecord::Base has_many :products def json_hash if children.size > 0 children.collect { |node| { node.name => node.json_has...

Recursive function for an xml file (hierarchial data).

Hi, I want to recursively traverse an XML file, and need a little help with the C# code. My XML file looks like: <categories> <category id="1"> </category> <category id="2"> <category id="3"> </category> <category id="4"> <category id="5"> </category> </category> </c...

Is it possible to make a recursive SQL query ?

I have a table similar to this: CREATE TABLE example ( id integer primary key, name char(200), parentid integer, value integer); I can use the parentid field to arrange data into a tree structure. Now here's the bit I can't work out. Given a parentid, is it possible to write an SQL statement to add up all the value fields und...

Recursive lambda expression to traverse a tree in C#

Can someone show me how to implement a recursive lambda expression to traverse a tree structure in C#. ...

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 using recursion as a means to show off when a simple iteration algorithm can fit the bill. Does the compiler play a vital role in deciding...

Building a Table Dependency Graph With A Recurssive Query

I am trying to build a dependency graph of tables based on the foreign keys between them. This graph needs to start with an arbitrary table name as its root. I could, given a table name look up the tables that reference it using the all_constraints view, then look up the tables that reference them, and so on, but this would be horrible...

Real-world examples of recursion

Can anyone suggest real-world problems where a recursive approach is the natural solution besides DFS? (I don't consider towers-of-Hanoi, fibonacci sequence, or factorial real-world problems. They are a bit contrived in my mind.) ...

How do you recursively unzip archives in a directory and its subdirectories from the Unix command-line?

The unzip command doesn't have an option for recursively unzipping archives. If I have the following directory structure and archives: /Mother/Loving.zip /Scurvy/Sea Dogs.zip /Scurvy/Cures/Limes.zip And I want to unzip all of the archives into directories with the same name as each archive: /Mother/Loving/1.txt /Mother/Loving.zip ...

Topological sort, recursive, using generators.

Data: a dependency list, already verified to be acyclic. So here, 'a' depends on 'b','c','d' (c depends on d), etc... A = { 'a' : dict(b=1, c=1), 'c' : dict(d=1), 'd' : dict(e=1,f=1,g=1), 'h' : dict(j=1) } I'd like to have a top-down, recursive solution to let's say, find the chain starting at 'a': a, c, d, e, g...

Shortest Root to Leaf Path

What is the easiest way, preferably using recursion, to find the shortest root-to-leaf path in a BST (Binary Search Tree). Java prefered, pseudocode okay. Thanks! ...

Recursive overloading semantics in the Scala REPL - JVM languages

Using Scala's command line REPL: def foo(x: Int): Unit = {} def foo(x: String): Unit = {println(foo(2))} gives error: type mismatch; found: Int(2) required: String It seems that you can't define overloaded recursive methods in the REPL. I thought this was a bug in the Scala REPL and filed it, but it was almost instantly closed with...