recursion

how can i list every path in a directed graph? (C#)

please just point me in the right direction or tell me what to look up to solve this: I have a "tree" object that holds "node" objects. (actually it's called a directed graph). Each node holds the fields string "name" and "list" that contains the next nodes. How can I create lists of all possible node names from the head node to the fo...

Recursive Query using CTE in SQL Server 2005

OK, here's what I'm trying to do. I'm using a CTE query in MSSQL2005. The objective of the query is to recurse through Parent child relationships of product categories and return the number of products under each category (this includes any products contained in children categories) My current version only returns the product count for ...

Find lowest common parent in recursive SQL table

Hi, Suppose I have a recursive table (e.g. employees with managers) and a list of size 0..n of ids. How can I find the lowest common parent for these ids? For example, if my table looks like this: Id | ParentId ---|--------- 1 | NULL 2 | 1 3 | 1 4 | 2 5 | 2 6 | 3 7 | 3 8 | 7...

Retrieving a Binary-Tree node's depth non-recursively

Can anyone point out a way of getting the depth of a Node in a Binary Tree (not a balanced one, or BST) without using recursion? Ideally in Java/C/C# The node is represented as: class Node { Node Left; Node Right; string Value; int Depth; } Using Level Order with a FIFO list was my first thought, but I was stumped at detectin...

"Necessary" Uses of Recursion in Imperative Languages

I've recently seen in a couple of different places comments along the lines of, "I learned about recursion in school, but have never used it or felt the need for it since then." (Recursion seems to be a popular example of "book learning" amongst a certain group of programmers.) Well, it's true that in imperative languages such as Java a...

PHP function doesn't return value

I have a function which, given a filename and directory path, checks if the directory already contains a file with the same name and if so returns an amended filename (by appending a number after the first part of the filename). (The get_filenames() function is a CodeIgniter helper function which creates an array of all the filenames in ...

[XSLT] Creating a recursive XSL:If statement?

I'm trying to set up an XSL:IF statement that only shows entries that have a node that falls between two values. Simple enough, right? It's just a if greater than and if less than. Problem is, instead of testing it against one node I need to test it against up to 52. Let's say I have some XML that looks like this: <container> <entr...

Is recursively rendering a Partial View in Asp.Net Mvc a bad idea?

I want to output a menu structure that will look like this <ul> <li> MenuItemName1 <ul> <li>Child Item</li> </ul> </li> <li> MenuItemName2 </li> </ul> I have a menuitem class that contains the name, url and children menu items. I would like to create a partial view that renders each item as ...

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 => array(id=>1,name=>"cat 1"), 1 => array(id=>2,name=>"cat 2"), 2 => array(id=>3,name=>"cat 1") ); When I search for key=name a...

Optimizing recursive calls over data structures

Is there an algorithm to optimize a highly recursive function into an iteration over a data structure? For example, given the following function... // template <typename T> class BSTNode is defined somewhere else // It's a generic binary search tree. template <typename T, typename R> void in_order(BSTNode<T>* root, R (*routine)(T)) { ...

Recursively printing data structures in Perl

I am currently learning Perl. I have Perl hash that contains references to hashes and arrays. The hashes and arrays may in turn contain references to other hashes/arrays. I wrote a subroutine to parse the hash recursively and print them with proper indentation. Though the routine works as expected, my instructor was not convinced about...

What is the simplest way to get all the parents of a record using the id / parent_id model in mysql/php?

I'm looking for the simplest way to recursively get all the parent elements from a database using the adjacency list / single table inheritance model (id, parent_id). My select currently looks like this: $sql = "SELECT e.id, TIME_FORMAT(e.start_time, '%H:%i') AS start_time, $title AS title, ...

How to have a local position and global position in a game.

I have a base class called Component. I also have 2 interfaces I2DComponent, and I3DComponent. I am currently working on the I2DComponent. The implementation: /// <summary> /// Represtions a 2D object. These objects will be drawn after 3D objects, /// and will have modifiers automatically provided in the editor. /// </summary> public ...

Using Perl, how can I rename files in all subdirectories of a drive?

How can I rename all files on a drive with .wma and .wmv extensions to .txt extension using Perl regardless how deep they are in the directory structure? ...

Please walk me through this "Erlang Programming" recursive sample

From page 90 of Erlang Programming by Cesarini and Thomson, there is an example that has no detailed discussion. I'm quite the newbie to functional programming and recursive thinking, so I'm not familiar in solving problems this way. "For example, the following function merges two lists (of the same length) by interleaving their values...

Clojure function reading from stream locks

I have a function that reads one token from an input stream that is called (get-next-indicator stream indicator) and returns it. I am trying to use it to construct a map. But, when I run it, it locks up. If I remove one of the get-next-indicator function, it does work. Does both functions try to read the stream at the same time is this...

Calculate score in a pyramid score system

I am trying to calculate gamescores for a bunch over users and I haven't really got it yet. It is a pyramid game where you can invite people, and the people you invite is placed beneth you in the relations tree. So if i invite X and X invites Y i get kickback from both of them. Let's say 10%^steps... So from X i get 10% of his score an...

PHP recursive search and replace array elements

Hi all, I want to recursively search and replace elements in an array. The array is tree based so looks like Object Children Object type A Object type B Object Children Object type A Object etc. I want to be able to replace certain items with other items, so for example, I want to replace all entries in the ...

C#: Recursive functions with Lambdas

The below does not compile: Func<int, int> fac = n => (n <= 1) ? 1 : n * fac(n - 1); Local variable 'fac' might not be initialized before accessing How can you make a recursive function with lambdas? [Update] Here are also two links that I found interesting to read: Eric Lippert's "Why does a recursive lambda cause a defini...

Why does this not compile in F#

This compiles and works: let rec HelloEternalWorld _ = Console.ReadLine() |> printf "Input: %s\n" HelloEternalWorld 0 HelloEternalWorld 0 This does not compile: let rec HelloEternalWorld = Console.ReadLine() |> printf "%s\n" HelloEternalWorld HelloEternalWorld I try to understand why not? ...