Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursion and return statements

I'm fairly new to Python and recursive functions as a whole, so pardon my ignorance.

I am trying to implement a binary search tree in Python and have the following insert method (taken out of a class):

def insert(self, key, root=None):
    '''Inserts a node in the tree'''
    if root == None:
        root = self.root
    if root.key == None:
        self._update(root, key)
        return 0
    else:
        tmp = root
        if key > tmp.key: # we work with the right subtree
            self.insert(key, root=tmp.right)
        elif key < tmp.key: # we work with the left subtree
            self.insert(key, root=tmp.left)
        else: # key already exists
            return 0

I'm not sure if this is legible, but it traverses the tree until it gets to a None value and updates the node with the key to insert.

Now, the method works nicely and correctly creates a BST from scratch. But there's a problem with the return statements, as it only returns 0 if there is no recursion performed.

>>> bst.insert(10)
0
>>> bst.insert(15)
>>> bst.root.right.key
15
>>>

"Inserting" the root key again returns 0 (from line 15) the way it should.

>>> bst.insert(10)
0

I can't figure out why this happens. If I put a print statement in line 6, it executes correctly, yet it just won't return anything past the first insertion. Why is this? (I'm pretty sure I'm missing some basic information regarding Python and recursion)

Thanks for your help,

Ivan

P.S.: I've read that recursion is not the best way to implement a BST, so I'll look into other solutions, but I'd like to know the answer to this before moving on.

like image 790
imiric Avatar asked Jun 01 '09 21:06

imiric


People also ask

Does a return statement stop a recursion?

A return statement won't stop all the prior recursive calls made from executing.

Is return necessary in recursion?

The main point of recursion is to execute the same functionality on a smaller 'sub-set' of the problem, and to do that you need to use return. If you have recursion without a return, then effectively you are using recursion just to do a loop, and in Python that is a pretty expensive thing to do.

What is recursion statement?

Recursion means "defining a problem in terms of itself". This can be a very powerful tool in writing algorithms. Recursion comes directly from Mathematics, where there are many examples of expressions written in terms of themselves. For example, the Fibonacci sequence is defined as: F(i) = F(i-1) + F(i-2)

What is return address in recursion?

Usually when there is a recursive call to a function then in the stack the return address points to the next instruction after the function call.


2 Answers

On your recursive lines, you do not return anything. If you want it to return 0, you should replace them with lines like:

return self.insert(key, root=tmp.left)

instead of just

self.insert(key, root=tmp.left)
like image 141
Nathaniel Flath Avatar answered Oct 06 '22 00:10

Nathaniel Flath


You are inside a function and want to return a value, what do you do? You write

def function():
    return value

In your case you want to return the value returned by a function call, so you have to do.

def function():
    return another_function()

However you do

def function():
    another_function()

Why do you think that should work? Of course you use recursion but in such a case you should remember the Zen of Python which simply says:

Special cases aren't special enough to break the rules.

like image 23
DasIch Avatar answered Oct 06 '22 00:10

DasIch