Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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).
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

T Sql Fundamentals

Authors: Itzik Ben Gan

4th Edition

0138102104, 978-0138102104

More Books

Students also viewed these Databases questions

Question

g. Is the value of b equal to 0.5? Pg45

Answered: 1 week ago

Question

Describe the linkages between HRM and strategy formulation. page 74

Answered: 1 week ago

Question

Identify approaches to improving retention rates.

Answered: 1 week ago