Question: For Programming Assignment 2 ( PA 2 ) , you will learn how to traverse a binary tree using Python. A binary tree is a

For Programming Assignment 2(PA2), you will learn how to traverse a binary tree using Python. A binary
tree is a tree in which each node has at most two children.
Assume the node is represented by the following Python class:
class TreeNode:
def_init_(self, val=0, left=None, right=None):
self.val = valclass TreeNode:
def init (self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
#Updates the tree with a value of all 1.
def traverse(x):
'''Insert your code here' ''
#Prints the tree as a string.
def print_tree (x) :
'''Insert your code here' ''
def my_function (x) :
traverse (x)
print_tree (x)
print(") #this creates a new line after the printed result
self.left = left
self.right = right
self.val is the node's value, self.left and self.right represent the node's left and right nodes, respectively.
For this assignment, you will be given a tree as input and all of its values are 0. Your program must
traverse the tree and set all of the node values to 1. Then print out the tree. See the Examples section
for exactly what I expect.
In addition, usage of any string manipulation (instead of actually traversing the tree) for this assignment
is prohibited. This includes, but is not limited to, the usage of Python's "replace" and "re.sub" functions.
If you do, you will get an automatic zero for this assignment.
Your code cannot import any library.
To reduce any misunderstanding, you will be given a template
cecs328pa2.py file. All you need to do is
fill out the file with your code, specifically where it says "Insert your code here". Do not add or change
anything else within that file. You still need to add your name at the top of the code though, as a
comment.The below are run from the Python command line. This is how your code will be graded. Your program
absolutely needs to be able to be run from the command line, otherwise you will get zero credit.
Unlike the previous assignment, this assignment will be run from the Python shell (Python command
line).
This is how your code should run:Python 3.12.1(tags/v3.12.1:2305ca5, Dec 72023,22:03:25)[MSC v.193764 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import os
from cecs328pa 2 import *TreeNode(1, TreeNode(1, None, TreeNode(1, None, None)), TreeNode(1, None, None))
As you can see, the tree values went from 0 to 1. In addition, the tree in the example above corresponds
to the below illustration. The circles represents nodes with a value in them (0 originally, then turned to 1
after your program runs).
 For Programming Assignment 2(PA2), you will learn how to traverse a

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!