Answered step by step
Verified Expert Solution
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 PA 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:
definitself val leftNone, rightNone:
self.val valclass TreeNode:
def init self val leftNone, rightNone:
self.val val
self.left left
self.right right
#Updates the tree with a value of all
def traversex:
Insert your code here'
#Prints the tree as a string.
def printtree :
Insert your code here'
def myfunction :
traverse x
printtree 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 Your program must
traverse the tree and set all of the node values to 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 resub" 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
cecspapy 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 tagsv:ca Dec ::MSC v bit AMD on win
Type "help", "copyright", "credits" or "license" for more information.
import os
from cecspa import TreeNode TreeNode None, TreeNode None, None TreeNode None, None
As you can see, the tree values went from to In addition, the tree in the example above corresponds
to the below illustration. The circles represents nodes with a value in them originally, then turned to
after your program runs
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started