recursion

Java recursion sum

I need a class Person to describe persons. Each person has a name and an array consisting of Person-objects, which represent the person's children. The person class has a method getNumberOfDescendants, which returns an integer equal to the total number of descendants of the person, i.e. his children plus grandchildren plus their children...

Null Object Pattern, Recursive Class, and Forward Declarations

Hi folks, I'm interested in doing something like the following to adhere to a Null Object design pattern and to avoid prolific NULL tests: class Node; Node* NullNode; class Node { public: Node(Node *l=NullNode, Node *r=NullNode) : left(l), right(r) {}; private: Node *left, *right; }; NullNode = new Node(); Of course, as written...

convert a recursive structure to xml with jsp

Let's say I have a recursive data structure class Tree { private Tree right; private Tree left; private int data; .... } I want to convert it to xml with jsp, so my ui tree widget can load the xml page with Ajax and construct a tree (with expandable/collapsable nodes, etc). The xml would look something like ...

Multiple Constraint Knapsack Problem

If there is more than one constraint (for example, both a volume limit and a weight limit, where the volume and weight of each item are not related), we get the multiply-constrained knapsack problem, multi-dimensional knapsack problem, or m-dimensional knapsack problem. How do I code this in the most optimized fashion? Well, one can dev...

What is the copy constructor bug causing parsing errors?

I'm writing a compiler for a small language, and my Parser class is currently in charge of building an AST for use later. However, recursive expressions are not working correctly because the vector in each AST node that holds child nodes are not working correctly. Currently my AST's header file looks like this: class AST { public: e...

memory and depth (vs. breadth) of recursion in c

I have two questions related to memory. First some background. I am a novice-intermediate c programmer. I have written several different tree like data-structures with variable number of nodes at each level. One such structure, can have as its data a number of integer variables, which themselves are primary data for integer trees. I ha...

Linq To Sql and Recursion

I'm just getting my feet wet with .Net and Linq-to-Sql, so please bear with me. I've got three tables: User, AccountDetails and UserProfile. User is the parent to both AccountDetails and UserProfile, and the relationships are all 1 to 1. When I get a User object, I get the AccountDetail and UserProfile properties as expected. However...

Tree structure category sum that cascades to root node.

Hi All, I'm going to come right out and say that I am not the worlds greatest Mathematician :D So this problem may well be simple to most of you. Unfortunately it's confusing me and have had several stabs at a workable solutions. As with any tree, one can have many branches, many branches can have more branches and so forth until they ...

Concatenate null-terminated strings recursively

I'm inventing interview questions that require analysis of C++ code that does something simple with pointers and recursion. I tried to write strcat() in recursive manner: size_t mystrlen( const char* str ) { if( *str == 0 ) { return 0; } return 1 + mystrlen( str + 1 ); } void mystrcpy( char* to, const char* from ) {...

Is it possible to recursively require all files in a directory in Ruby?

I am working on an API that needs to load all of the .rb files in its current directory and all subdirectories. Currently, I am entering a new require statement for each file that I add but I would like to make it where I only have to place the file in one of the subdirectories and have it automatically added. Is there a standard comma...

Binary tree search algorithm errors

I have a binary tree that I need to search through. I'm not searching for one specific node of the tree, but rather over every node of the tree to gain information about them. I have a simple recursive search right now, but every time it runs I get a stack overflow error. It's a full binary tree of depth 7... if (curDepth < 6 && !search...

How to print string association from phone number as text?

I need some help with a method i'm writing for a project. the method changes a phone number into a list of text strings. You know that 2-9 have letters associated with them on a phone. i would like to make a converter that will change a 7 digit number to a list of strings. i would like to see all possibilities. i already cut out all th...

ruby hash with self referencing elements / recursive hash

I need a ruby hash H with keys :a, :b, :c so that H[:b] = H[:a] + 1; H[:c] = H[:b] + 2 etc. How can I define such a hash in a one line declaration, like H = {:a=>1, :b => H[:a] + 1, :c => H[:b] +2, ... } ? I need something similar to DataMapper properties declaration: property :path, FilePath property :md5sum, String, :default => ...

Need help understanding how this recursive function is working

Here's a function (credit to user Abbot, for providing it in another question) def traverse(ftp): level = {} for entry in (path for path in ftp.nlst() if path not in ('.', '..')): ftp.cwd(entry) level[entry] = traverse(ftp) ftp.cwd('..') return level Here's what I don't understand: When python ent...

Recursive directory searcer PHP

How can i write an recursice directory searcher that takes a geven string and return the whole path plus the filename in php? ...

Recursive mod_rewrite for search engine friendly urls

I've been reading through a previous solution to a recursive mod_rewrite problem that is similar to what I'm trying to do, the difference is I'm sending all queries through an index.php file and so don't need to specify the script within the query. Essentially I want to recursively convert any number of parameters within a search engin...

Recursive woes - reducing an input string

Hi all, I'm working on a portion of code that is essentially trying to reduce a list of strings down to a single string recursively. I have an internal database built up of matching string arrays of varying length (say array lengths of 2-4). An example input string array would be: {"The", "dog", "ran", "away"} And for further examp...

Invoke Windows Workflow Recursively

I have a workflow, that at a certain point, needs to be triggered recursively. I can't seem to figure out how to do this. I tried the following code but context ends up being null?? private void codeTriggerChildren_ExecuteCode(object sender, EventArgs e) { ActivityExecutionContext context = sender as ActivityExecutionContext; ...

Calculate the DEPTH of a binary search tree?

Original problem: Calculate the number of nodes [SOLVED]. I have another similar problem, and both of these have been killing me for a while now. I can calculate the number of words fine now. Thank you very much for your assistance. The issue is now calculating the summation of depths [the sum of the individual depths for all childre...

Calculate the depth of a binary search tree?

I am having difficulty calculating the summation of depths [the sum of the individual depths for all children of the root] for a given BST. I have the total number of nodes for the tree, and I am trying to calculate the average depth for the tree, requiring I have this depth sum. Recursion and I don't get along very well.. I am finding ...