Question: Python 3.x Binary Trees Treenode.py: def create(data, left=None, right=None): Create a new treenode for the given data. Pre-conditions: data: Any data value to be

Python 3.x Binary TreesPython 3.x Binary Trees Treenode.py: def create(data, left=None, right=None): """ Create a

Treenode.py:

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

Question 2 (8 points): Purpose: To do more thinking about binary trees Degree of Difficulty: Moderate During a trial a judge or jury must weigh the evidence presented by the defence and the prosecution. To help them with this matter. you must write a function called weigh_tree(root) that does the following: 1 Takes in as a parameter the root of a tree created by the Tree Node ADT. 2. Returns a tuple with 3 values: the summed total of the left subtree, the data value of the root node, and the summed total of the right subtree. You may assume the passed-in tree only contains numbers. 2 7 #11 6 The tree above should return: (24, 2, 5) What to Hand In .A file called a8q2.py. containing your function definition. .A file called a8q2.testing.py. containing your testing. Be sure to include your name, NSID, student number, course number and laboratory section at the top of the file

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!