0

I'm trying to transverse through a binary tree to add "newvalue" to each nodes current value.

def(node, 2):

Before:

      1
     / \
    2   3
   /\   /
  5  6  7 

After:

      3
     /\
    4  5
   /\  /
  7  8 9

How should I approach this recursively?!

3
  • 3
    Do you have more code you meant to post? Commented Mar 8, 2013 at 5:06
  • 1
    Cant you just do a pre/post/inorder traversal and just update values? Commented Mar 8, 2013 at 5:07
  • Can anyone explain how to do it?! Commented Mar 8, 2013 at 5:57

2 Answers 2

2

Harold provided a very clean and general example. Here's a take, with annotations, that will work for a binary tree

With a recursive algorithm, you've got to think of two things: 1. The base case 2. The recursive call

In your example: - Base case = Node has no children. No recursive calls will take place - Recursive call = Each node is its own mini-tree, so call the function on each child

def increase_tree_values(node, delta):
    """ Increases/decreases the value of all nodes in the tree by the value 'delta'
    """

    # increase the current node's value
    node.value += delta

    # recursively call function on left child, if it exists
    if node.left_child:
        increase_tree_values(node.left_child, delta)

    # recursively call function on right child, if it exists
    if node.right_child:
        increase_tree_values(node.right_child, delta)



# presuming you have some binary tree called 'tree' already constructed
# increase all nodes in 'tree' by 2
increase_tree_values(tree, 2)

Let me know if you have any questions. If this exercise was presented to you in school, they're really just trying to get you to identify this as a simple tree traversal, where you change the value of the nodes as you traverse through the tree. What I would recommend is for you to try your hand at programming all the different tree traversals without any notes. You'll learn a lot about trees and recursion that way.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the clarifications, just a beginner here. I can do it with iteration but recursive confused me good.
No problem. It takes a while to get a hang of, but I guarantee you that it's worth the effort. My first year professor put it in terms of set theory, where some sets are often defined in terms of their subsets. Of course, this could lead to an infinite regression, so you need a base case to know when to stop. Also, it's good to upvote and accept answers that you find helpful. The people who answer them get "reputation points", which keeps the wheels of Stackoverflow churning ;-)
1
def add_value(node, delta):
    node.value += delta
    for child in node.children:
        add_value(child, delta)

2 Comments

Do add some explanation as well, to make it a better answer.
The code should have commentaries if it does'nt explain itself. This code does.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.