Question

implement the following functions using recusrion: subst(tnode, t, r). Purpose: To substitute a target value t...

implement the following functions using recusrion:

  1. subst(tnode, t, r). Purpose: To substitute a target value t with a replacement value r wherever it appears as a data value in the given tree. Returns None, but modifies the given tree.

So given a binary tree as tnode, find the target node, t, with and replace it with the replacement, r.

Ex. tnode

1

2 3

4 5

t = 5, r = 10

1

2 3

4 10

Using the follwoing code:

def create(data, left=None, right=None):
    """
    Create a new treenode for the given data.
    Pre-conditions:
        data:  Any data value to be stored in the treenode
        left:  Another treenode (or None, by default)
    Post-condition:
        none
    Return:
        the treenode created
    """
    return {'data':data, 'left':left, 'right':right}


def get_data(treenode):
    """
    Retrieve the contents of the data field.
    Pre-conditions:
        node: a node created by create()
    Post-conditions:
        none
    Return
        the data value stored previously in the node
    """
    return treenode['data']


def get_left(tnode):
    """
    Retrieve the contents of the left field.
    Pre-conditions:
        tnode: a treenode created by create()
    Post-conditions:
        none
    Return
        the value stored in left field
    """
    return tnode['left']


def get_right(tnode):
    """
    Retrieve the contents of the right field.
    Pre-conditions:
        tnode: a treenode created by create()
    Post-conditions:
        none
    Return
        the value stored in right field
    """
    return tnode['right']


def set_data(tnode, val):
    """
    Set the contents of the data field to val.
    Pre-conditions:
        tnode: a node created by create()
        val:  a data value to be stored
    Post-conditions:
        stores the new data value, replacing the existing value
    Return
        none
    """
    tnode['data'] = val


def set_left(tnode, val):
    """
    Set the contents of left field to val.
    Pre-conditions:
        tnode: a treenode created by create()
        val:   a treenode, or the value None
    Post-conditions:
        stores the val in left field, replacing the existing value
    Return
        none
    """
    tnode['left'] = val

def set_right(tnode, val):
    """
    Set the contents of right field to val.
    Pre-conditions:
        tnode: a treenode created by create()
        val:   a treenode, or the value None
    Post-conditions:
        stores the val in right field, replacing the existing value
    Return
        none
    """
    tnode['right'] = val

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def subst(tnode,t,r):
if tnode is None:
return None
if tnode['data']==t:
tnode['data']=r#if tnode's data is same as target value then replace
subst(tnode['left'],t,r)#calling function on left subtree
subst(tnode['right'],t,r)#calling function on right subtree

Add a comment
Know the answer?
Add Answer to:
implement the following functions using recusrion: subst(tnode, t, r). Purpose: To substitute a target value t...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT