recursion

What does this recursive function do?

I got this question in an interview. So, this seems to me a messed up Fibonacci seq. sum generator and this gives a stackoverflow. Because if(n==0) should be if(n<3) (exit condition is wrong). What should be the precise answer to this question? What was expected as an answer? foo (int n) { if (n == 0) return 1; return foo(n-1) + foo(n-2...

Count Number of 1's in A Binary Representation of N, RECURSIVELY. in JAVA

I understand the concept that the number of 1's in N is the same as N/2 if it's even, and N/2 + 1 if the number is odd, but I don't understand how to do it recursively. Also, say I input 10101 into my machine, and I do N/2, it does 505 instead of the binary division. Do I have to convert back and forth each time? ...

Help me check homework for write a recursive method that counts the number of nodes in a chain of linked nodes.

I try many coding to solve the question bellow but also cannot find the answer. Can anyone help me solve my problem and tell me where is the wrong coding?? /** Task: Recusively counts the nodes in a chain. * @param start the first node * @returns the number of nodes in the linked chain */ public int countNodes(Node start) { if (start ==...

If I have an expression, which is partitially evaluable, is it a good idea to avoid tail-recursion?

Consider an haskell-expression like the following: (Trivial example, don't tell me what the obvious way is! ;) toBits :: Integral a => a -> [Bool] toBits 0 = [] toBits n = x : toBits m where (m,y) = n `divMod` 2 x = y /= 0 Because this function is not tail-recursive, one could also write: toBits :: Integral a => a -> [Bool] toB...

How to recognize what is, and what is not tail recursion?

Sometimes it's simple enough (if the self call is the last statement, it's tail recursion), but there are still cases that confuse me. A professor told me that "if there's no instruction to execute after the self-call, it's tail recursion". How about these examples (disregard the fact that they don't make much sense) : a) This one sho...

How to avoid this stackoverflow exception?

Here is the situation, I am developing a binary search tree and in each node of the tree I intend to store the height of its own for further balancing the tree during avl tree formation. Previously I had an iterative approach to calculate the height of a node during balancing the tree like the following. (The following code belongs to a...

php- recursive 2d array function

I have a 2d array say, Array A[60][150] and have another array, Array B[60][150]. Now what I am trying to do is: Given a point in Array A say x,y i want to access its neighbors to find similarity between the two elements. if they are similar then find its neighbors. So right now i am using recursive function to do it. But its throws an ...

Sum integers in 2d array using recursion ?

Hello! I need some help with this problem. I have to sum alle the integers in a 2d array using recursion. Below is what I have managed to do on my own, but I'm stuck. This code generates the sum 14, which should be 18. Any suggestions? Is the base case wrong? Or is the recursive method wrong? public class tablerecursion { public static...

Iterating over N dimensions in Python

I have a map, let's call it M, which contains data mapped through N dimensions. # If it was a 2d map, I could iterate it thusly: start, size = (10, 10), (3, 3) for x in range(start[0], start[0]+size[0]): for y in range(start[1], start[1]+size[1]): M.get((x, y)) # A 3d map would add a for z in ... and access it thusly M.get((...

Why is this recursion NOT infinite?

My friends and I are working on some basic Ruby exercises to get a feel for the language, and we've run into an interesting behavior that we're yet unable to understand. Basically, we're creating a tree data type where there's just one class, node, which contains exactly one value and an array of zero or more nodes. We're using rspec's...

Return statement that works but doesn't make much sense

I have the following function: int mult(int y, int z) { if (z == 0) return 0; else if (z % 2 == 1) return mult(2 * y, z / 2) + y; else return mult(2 * y, z / 2); } What I need to do is prove its correctness by induction. Now the trouble I'm having is that even though I know it works since I ran it I can't follow eac...

Recursive MySQL query?

I have a set of data that's organized hierarchically that should be able to grow to an arbitrary size. I need to retrieve the entire tree, but I can't figure out how to do it with just SQL. My current solution is to create a temporary table and use a recursive function to successively query branches of the tree and then store the result ...

Inserting multiple links into text, ignoring matches that happen to be inserted

The site I'm working on has a database table filled with glossary terms. I am building a function that will take some HTML and replace the first instances of the glossary terms with tooltip links. I am running into a problem though. Since it's not just one replace, the function is replacing text that has been inserted in previous itera...

Algorithm for iterating over an outward spiral on a discrete 2D grid from the origin

For example, here is the shape of intended spiral (and each step of the iteration) y | | 16 15 14 13 12 17 4 3 2 11 -- 18 5 0 1 10 --- x 19 6 7 8 9 20 21 22 23 24 | | Where the lines are the x and y axes. Here would be the actual values the algorithm would "retur...

.NET/MVC recursive record listing

I'm trying to make my content CMS more user friendly by listing content in the following fashion: Parent - Sub Page - - Sub Page - - - Sub Page - - - - etc... Using .NET/MVC2, where would this function be defined and how would it be called. This is my page listing my content: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Sh...

Knight's Tour algorithm help

Okay everybody, I know the knight's tour problem is popular for all cs students and I am having trouble getting mine to work. I use this recursive algorithm to progress through the moves, however, once I get to around move 50 I have to backtrack since no moves are available and I end up never completing the tour. I pass a ChessNode (hold...

Recursively grab all data based on a parent id

We have a table where rows recursively link to another row. I want to pull data associated with a given parentId and all it's children. Where parentId is one from the root row. I thought I have seen or done something like that before, but I am unable to find it now. Can this be done in SQL or is it better to do this in code? I want the...

Ruby Code Critique

I'm a Ruby newbie (started 4 days ago and hey it rhymes!) and decided to code a simple little tool for fun/learning. The following code was the result. It works fine, but I would really appreciate critique from some more experienced Ruby developers. I'm looking for comments on style, verbosity, and any misc. tips/tricks. By the way, I'm...

Python Recursion Question

Hello All. I'm currently trying to wrap my head around learning Python and I've come to a bit of a stall at recursive functions. In the text I'm using (Think Python), one of the exercises is to write a function that determines if number a is a power of number b using the following definition: "A number, a, is a power of b if it is di...

PHP: Recursively Process then remove DOMElements from DOMDocument

I'm using PHP's DOMDocument and related classes to work with XML. The XML contains processing instructions that must be processed in a specific order, from top to bottom, deepest to shallowest. I'm using a recursive function that takes the entire DOMDocument, then calls itself foreach child node, all the way down. I need to delete some...