Home > Net >  Returning list of different results that are created recursively in Python
Returning list of different results that are created recursively in Python

Time:01-03

Lately I've been working with some recursive problems in Python where I have to generate a list of possible configurations (i.e list of permutations of a given string, list of substrings, etc..) using recursion. I'm having a very hard time in finding the best practice and also in understanding how to manage this sort of variable in recursion.

I'll give the example of the generate binary trees problem. I more-or-less know what I have to implement in the recursion:

  1. If n=1, return just one node.
  2. If n=3, return the only possible binary tree.
  3. For n>3, crate one node and then explore the possibilities: left node is childless, right node is childless, neither node is childless. Explore these possibilites recursively.

Now the thing I'm having the most trouble visualising is how exactly I am going to arrive to the list of trees. Currently the practice I do is pass along a list in the function call (as an argument) and the function would return this list, but then the problem is in case 3 when calling the recursive function to explore the possibilites for the nodes it would be returning a list and not appending nodes to a tree that I am building. When I picture the recursion tree in my head I imagine a "tree" variable that is unique to each of the tree leaves, and these trees are added to a list which is returned by the "root" (i.e first) call. But I don't know if that is possible. I thought of a global list and the recursive function not returning anything (just appending to it) but the problem I believe is that at each call the function would receive a copy of the variable.

How can I deal with generating combinations and returning lists of configurations in these cases in recursion? While I gave an example, the more general the answer the better. I would also like to know if there is a "best practice" when it comes to that.

CodePudding user response:

Currently the practice I do is pass along a list in the function call (as an argument) and the function would return this list

This is not the purest way to attack a recursive problem. It would be better if you can make the recursive function such that it solves the sub problem without an extra parameter variable that it must use. So the recursive function should just return a result as if it was the only call that was ever made (by the testing framework). So in the example, that recursive call should return a list with trees.

Alternatively the recursive function could be a sub-function that doesn't return a list, but yields the individual values (in this case: trees). The caller can then decide whether to pack that into a list or not. This is more pythonic.

As to the example problem, it is also important to identify some invariants. For instance, it is clear that there are no solutions when n is even. As to recursive aspect: once you have decided to create a root, then both its left and right sided subtree will have an odd number of nodes. Of course, this is an observation that is specific to this problem, but it is important to look for such problem properties.

Finally, it is equally important to see if the same sub problems can reoccur multiple times. This surely is the case in the example problem: for instance, the left subtree may sometimes have the same number of nodes as the right subtree. In such cases memoization will improve efficiency (dynamic programming).

To further illustrate the way to tackle these problems, here is a solution for the example problem: one which uses the main function for the recursive calls, and using memoization:

class Solution:
    memo = { 1: [TreeNode()] }
    
    def allPossibleFBT(self, n: int) -> List[Optional[TreeNode]]:
        if n not in self.memo:
            self.memo[n] = [
                TreeNode(0, left, right)
                for atLeft in range(1, n, 2)
                    for left in self.allPossibleFBT(atLeft)
                        for right in self.allPossibleFBT(n - 1 - atLeft)
            ]
        return self.memo[n]

CodePudding user response:

Don't pass information into the recursive calls, unless they need that information to compute their local result. It's much easier to reason about recursion when you write without side effects. So instead of having the recursive call put its own results into a list, write the code so that the results from the recursive calls are used to create the return value.

Let's take a trivial example, converting a simple loop to recursion, and using it to accumulate a sequence of increasing integers.

def recursive_range(n):
    if n == 0:
        return []
    return recursive_range(n - 1)   [n]

We are using functions in the natural way: we put information in with the arguments, and get information out using the return value (rather than mutation of the parameters).

In your case:

Now the thing I'm having the most trouble visualising is how exactly I am going to arrive to the list of trees.

So you know that you want to return a list of trees at the end of the process. So the natural way to proceed, is that you expect each recursive call to do that, too.

How can I deal with generating combinations and returning lists of configurations in these cases in recursion? While I gave an example, the more general the answer the better.

The recursive calls return their lists of results for the sub-problems. You use those results to create the list of results for the current problem.

You don't need to think about how recursion is implemented in order to write recursive algorithms. You don't need to think about the call stack. You do need to think about two things:

  • What are the base cases?
  • How does the problem break down recursively? (Alternately: why is recursion a good fit for this problem?)

The thing is, recursion is not special. Making the recursive call is just like calling any other function that would happen to give you the correct answer for the sub-problem. So all you need to do is understand how solving the sub-problems helps you to solve the current one.

  • Related