Skip to main content
added 436 characters in body
Source Link
Mat.S
  • 1.8k
  • 7
  • 24
  • 37

Sorry if this is a general question but I am a beginner in Python and many times when I see other people code using recursion, they create a helper function for the main function and then call that helper function which itself is recursive.

This seems a bit different from the simplest cases of recursion for example (sum of lists, factorial) where the function only calls itself.

Can someone explain this technique more carefully perhaps with examples?

Much appreciated.

Example 1: (Reversing linked list using recursion)

def revert_list(self):
    self.head = self._revert_helper(self.head)
    

def _revert_helper(self, node):
    temp = None
    if node.forward == None: 
        return node
    else:
        temp = self._revert_helper(node.forward)
        node.forward.forward = node
        node.forward = None
    return temp

Example 2: (Binary Search Tree)

def __contains__(self, key):
    return self._bstSearch(self._root, key)

# Returns the value associated with the key.
def valueOf(self, key):
    node = self._bstSearch(self._root, key)
    assert node is not None, "Invalid may key."
    return node.value

# Helper method that recursively searches the tree for a target key:
# returns a reference to the Node. This allows 
# us to use the same helper method to implement
# both the contains and valueOf() methods of the Map class.

def _bstSearch(self, subtree, target):
    if subtree is None:  # base case
        return None
    elif target < subtree.key: # target is left of the subtree root
        return self._bstSearch(subtree.left) 
    elif target > subtree.key: # target is right of the subtree root
        return self.bstSearch(subtree.right) 
    else:                      # base case
        return subtree 

Sorry if this is a general question but I am a beginner in Python and many times when I see other people code using recursion, they create a helper function for the main function and then call that helper function which itself is recursive.

This seems a bit different from the simplest cases of recursion for example (sum of lists, factorial) where the function only calls itself.

Can someone explain this technique more carefully perhaps with examples?

Much appreciated.

Sorry if this is a general question but I am a beginner in Python and many times when I see other people code using recursion, they create a helper function for the main function and then call that helper function which itself is recursive.

This seems a bit different from the simplest cases of recursion for example (sum of lists, factorial) where the function only calls itself.

Can someone explain this technique more carefully perhaps with examples?

Much appreciated.

Example 1: (Reversing linked list using recursion)

def revert_list(self):
    self.head = self._revert_helper(self.head)
    

def _revert_helper(self, node):
    temp = None
    if node.forward == None: 
        return node
    else:
        temp = self._revert_helper(node.forward)
        node.forward.forward = node
        node.forward = None
    return temp

Example 2: (Binary Search Tree)

def __contains__(self, key):
    return self._bstSearch(self._root, key)

# Returns the value associated with the key.
def valueOf(self, key):
    node = self._bstSearch(self._root, key)
    assert node is not None, "Invalid may key."
    return node.value

# Helper method that recursively searches the tree for a target key:
# returns a reference to the Node. This allows 
# us to use the same helper method to implement
# both the contains and valueOf() methods of the Map class.

def _bstSearch(self, subtree, target):
    if subtree is None:  # base case
        return None
    elif target < subtree.key: # target is left of the subtree root
        return self._bstSearch(subtree.left) 
    elif target > subtree.key: # target is right of the subtree root
        return self.bstSearch(subtree.right) 
    else:                      # base case
        return subtree 
Source Link
Mat.S
  • 1.8k
  • 7
  • 24
  • 37

Recursion and Helper Function

Sorry if this is a general question but I am a beginner in Python and many times when I see other people code using recursion, they create a helper function for the main function and then call that helper function which itself is recursive.

This seems a bit different from the simplest cases of recursion for example (sum of lists, factorial) where the function only calls itself.

Can someone explain this technique more carefully perhaps with examples?

Much appreciated.