Categories: Homework on time

Tree Mirroring Project Python coded solution to create a mirrored binary tree.Please python objects in the solution if possible and of the utmost importanc

Tree Mirroring Project Python coded solution to create a mirrored binary tree.Please python objects in the solution if possible and of the utmost importance to incorporate the code included in the project overview pdf for building the binary treePlease include in-depth explanatory comments for each & any class, function, variable, and loop/statement thoroughly explaining its individual function / role in the solution.Please be sure to reference the Expected Output file for an indication of the expected output for the solution. Binary Tree Mirroring
2.0 Project Description
2.1 Basic Program
In order to start paying back your enormous student loans, you have set up a very profitable
stolen car parts operation. Now that you have pretty much made all of the money that you need,
you are planning on turning your operation over to one of your trusted lieutenants: B or C
(everyone uses code names for security).
B has a larger organization and has been doing better than C. However, being the good criminal
manager that you are, you think that it might be because B steals from Philadelphia and C has to
steal from Pittsburg. In order to make a decision about who gets to inherit your organization, you
are planning on making a top-to-bottom change in the organization to see how each group
performs in a new environment. You’ve created a binary tree that represents how your
organization is currently set up. You want to now create a mirror image of it so that you’ll know
how you want to go about restructuring it.
You are going to create a program in python code that will allow you to create a tree. Once
you have done this, you will print it out. Then you will create a mirror of the tree and print that
tree out. This means that you will have to complete a function named mirror that alters the tree
so that the final tree is a mirror image of the original tree. For example, if we use on this method
on the tree shown on the left, we end up with the tree shown on the right.
The tree that you will create for this project looks like this:
Note that you are limited to a tree depth of 5. The solution to this project can be implemented
using either Python lists or Python objects.
2.2 Files Names As Program Parameters
You must take one file (Nodex.txt) as query string arguments. Include the following code in the
program:
import sys
if len(sys.argv) != 2:
raise ValueError(‘Please provide one file name.’)
sys.argv[0] = sys.argv[0][0:len(sys.argv[0]) – sys.argv[0][::-1].find(‘/’)]
inputFile1 = sys.argv[0]+sys.argv[1]
print(“nThe file that will be used for input is {0}”.format(sys.argv[1]))
2.3 Reading from a File
To read the nodes from a file (“Node.txt”), you must first use the Python inputfile1 =
open(filename,”r”) command. At this point you can start reading from the file using variable =
inputfile1.readline().
2.4 Statistics
Another major part of this project is recording statistics. A summary of all the statistics you must
keep are presented in the following table:
Statistic
Number of Nodes
Number of Nodes On Left Side
[of initial tree]
Number of Nodes On Right Side
[of initial tree]
Smallest Node Value
Largest Node Value
Time
Description
This is the total number of nodes in your tree
Total number of nodes on the left side of your
tree. (left of the root node)
Total number of nodes on the right side of
your tree. (right of the root node)
The smallest value of a node stored in your
tree
The largest value of a node stored in your
tree
You need to record the total time of your
program. This time should include the time to
read the songs words, process the music
player commands, and record all the statistics.
It is suggested that you start a timer as one of
the first things you do in the program and stop
it immediately before printing out the
statistics. The following is a little sample code
that shows how you can time events.
import time
start = time.time()

end = time.time()
print(end – start)
When the program is finished reading the command text file, it should print out a list of statistics.
The output should look exactly like the following:
**********************
***** Statistics *****
**********************
Number of Nodes xxx
Number of Nodes On Left Side xxx
Number of Nodes On Right Side xxx
Smallest Node Value xxx
Largest Node Value xxx
Total Time of Program: xxxxx milliseconds
The x’s should be replaced with actual numbers.
When the table is printed out, the format should be as follows:
A
B
D
F
H
I
G
J
K
E
L
N
O
M
P
Q
C
R
T
V
W
U
X
Y
S
Z
2.5 Additional Notes
The Python language allows you to create strings of different lengths using multiplication.
The statement:
spaces =”aaa” * 3
would result in a string of 9 “a”.
This may be useful when you are deciding how you want to go about printing out the trees.
Code to be implemented into the solution that builds the tree:
class Node:
def __init__(self,name):
self.ID = name
self.left = None
self.right = None
self.parent = None
#
# Function to create and label all of the nodes in the tree
#
def labelTree(treeNode):
global treeDepth
global treeLoc
treeDepth += 1 # we are now one level deeper in the tree
print(“nParent: “,treeNode.ID)
treeNode.parent = treeLoc # set parent node to the previous node processed
treeLoc = treeNode # set the tree pointer node to the node passed to the function
print(“I have set the parent for {0} to {1}”.format(treeLoc.ID, treeNode.parent.ID))
if (treeDepth < 6 and (len(nodeNames) > 0)):
treeLoc.left = Node(nodeNames.pop(0)) # create left child
print(“Left : “,treeLoc.left.ID)
if (len(nodeNames) > 0):
treeLoc.right = Node(nodeNames.pop(0)) # create right child
print(“Right: “,treeLoc.right.ID)
if (treeLoc.left != None):
labelTree(treeLoc.left) # preocess the left hand side using recursion
if (treeLoc.right != None):
labelTree(treeLoc.right) # process the right hand side using recursion
treeDepth -= 1 # we are now moving one level up in the tree when we return
treeLoc = treeLoc.parent # done with this set of children, reset current tree pointer to parent node
return
# start timer
start = timer()
#
# Parse the node name file and load the names into a list
#
if len(sys.argv) != 2:
raise ValueError(‘Please provide a file name.’)
sys.argv[0] = sys.argv[0][0:len(sys.argv[0]) – sys.argv[0][::-1].find(‘/’)]
inputFile1 = sys.argv[0]+sys.argv[1]
print(“nThe file that will be used for input is {0}”.format(sys.argv[1]))
infile1 = open(inputFile1,”r”)
nodeNames = infile1.readline().rstrip().split()
#
# Create the binary tree
#
treeDepth = 1
treeLoc = None
#
# Create root node of tree
#
root = Node(nodeNames.pop(0))
treeLoc = root
#
# Create the rest of tree and label each of the nodes
#
print(“nBuilding the tree:n”)
labelTree(root)
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1
The file that will be used for input is Nodes.txt
Building the tree:
Parent: A I have set the parent for A to A Left : B Right: C
Parent: B I have set the parent for B to A Left : D Right: E
Parent: D I have set the parent for D to B Left : F Right: G
Parent: F I have set the parent for F to D Left : H Right: I
Parent: H I have set the parent for H to F
Parent: I I have set the parent for I to F
Parent: G I have set the parent for G to D Left : J Right: K
Parent: J I have set the parent for J to G
Parent: K I have set the parent for K to G
Parent: E I have set the parent for E to B Left : L Right: M
Parent: L I have set the parent for L to E Left : N Right: O
Parent: N I have set the parent for N to L
Parent: O I have set the parent for O to L
Parent: M I have set the parent for M to E Left : P Right: Q
Parent: P I have set the parent for P to M
Parent: Q I have set the parent for Q to M
Parent: C I have set the parent for C to A Left : R Right: S
Parent: R I have set the parent for R to C Left : T Right: U
Parent: T I have set the parent for T to R Left : V Right: W
Parent: V I have set the parent for V to T
Parent: W I have set the parent for W to T
Parent: U I have set the parent for U to R Left : X Right: Y
Parent: X I have set the parent for X to U
Parent: Y I have set the parent for Y to U
Parent: S I have set the parent for S to C Left : Z
Parent: Z I have set the parent for Z to S
Original tree: A L: B L: D L: F L: H R: I R: G L: J R: K R: E L: L L: N R: O
R: M L: P R: Q R: C L: R L: T L: V R: W R: U L: X R: Y R: S L: Z
Starting creation of the mirror tree.
1
Parent: A Left : C Right : B
Parent: B Left : E Right : D
Parent: D Left : G Right : F
Parent: F Left : I Right : H
Parent: H
Parent: I
Parent: G Left : K Right : J
Parent: J
Parent: K
Parent: E Left : M Right : L
Parent: L Left : O Right : N
Parent: N
Parent: O
Parent: M Left : Q Right : P
Parent: P
Parent: Q
Parent: C Left : S Right : R
Parent: R Left : U Right : T
Parent: T Left : W Right : V
Parent: V
Parent: W
Parent: U Left : Y Right : X
Parent: X
Parent: Y
Parent: S
Parent: Z
Mirror tree: A L: C L: S R: Z R: R L: U L: Y R: X R: T L: W R: V R: B L: E
L: M L: Q R: P R: L L: O R: N R: D L: G L: K R: J R: F L: I R: H
***** Statistics ***** ********************** Number of Nodes 26 Number of
Nodes On Left Side 15 Number of Nodes On Right Side 10 Smallest Node Value
A Largest Node Value Z Total Time of Program: 0.02603810 milliseconds
Process finished with exit code 0
2

Purchase answer to see full
attachment

Don't use plagiarized sources. Get Your Custom Essay on
Tree Mirroring Project Python coded solution to create a mirrored binary tree.Please python objects in the solution if possible and of the utmost importanc
Just from $13/Page
Order Essay
superadmin

Recent Posts

Consider the following information, and answer the question below. China and England are internation

Consider the following information, and answer the question below. China and England are international trade…

4 years ago

The CPA is involved in many aspects of accounting and business. Let’s discuss some other tasks, othe

The CPA is involved in many aspects of accounting and business. Let's discuss some other…

4 years ago

For your initial post, share your earliest memory of a laser. Compare and contrast your first percep

For your initial post, share your earliest memory of a laser. Compare and contrast your…

4 years ago

2. The Ajax Co. just decided to save $1,500 a month for the next five years as a safety net for rece

2. The Ajax Co. just decided to save $1,500 a month for the next five…

4 years ago

How to make an insertion sort to sort an array of c strings using the following algorithm: * beg, *

How to make an insertion sort to sort an array of c strings using the…

4 years ago

Assume the following Keynesian income-expenditure two-sector model:

Assume the following Keynesian income-expenditure two-sector model:                                                AD = Cp + Ip                                                Cp = Co…

4 years ago