Question: Python 3.x RECURSION Binary Tree ADT This is the code that I have so far but I'm unsure as where to go next or if
Python 3.x RECURSION Binary Tree ADT

This is the code that I have so far but I'm unsure as where to go next or if it's even right:
import treenode as tn
def alter_subtrees(tnode, left_tree_offset, right_tree_offset, root_offset=0): if tnode is None: return if tn.get_left(tnode): tn.set_data(tn.get_left(tnode), tn.get_data(tn.get_left(tnode) + left_tree_offset) alter_subtrees(tn.get_left(tnode), left_tree_offset, right_tree_offset, root_offset) if tn.get_right(tnode): tn.set_data(tn.get_right(tnode), tn.get_data(tn.get_right(tnode) + right_tree_offset) alter_subtrees(tn.get_right(tnode), left_tree_offset, right_tree_offset, root_offset)
This is the Code that I imported:
Treenode:
# Defines the tree node ADT # # A treenode is a simple container with three pieces of information # data: the contained information # left: a reference to another treenode or None # right: a reference to another treenode or None # Implementation notes: # This implementation uses a Python dictionary as a record. 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 Example Tree:
atree = tn.create(2) a = tn.create(7) b = tn.create(5) tn.set_left(atree, a) tn.set_right(atree, b) c = tn.create(11) d = tn.create(6) tn.set_left(a, c) tn.set_right(a, d)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
