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
Why Choose Us
Top quality papers
We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.
Professional academic writers
We have hired a team of professional writers experienced in academic and business writing. Most of them are native speakers and PhD holders able to take care of any assignment you need help with.
Free revisions
If you feel that we missed something, send the order for a free revision. You will have 10 days to send the order for revision after you receive the final paper. You can either do it on your own after signing in to your personal account or by contacting our support.
On-time delivery
All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.
Original & confidential
We use several checkers to make sure that all papers you receive are plagiarism-free. Our editors carefully go through all in-text citations. We also promise full confidentiality in all our services.
24/7 Customer Support
Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
Our Services
No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.
Essays
You are welcome to choose your academic level and the type of your paper. Our academic experts will gladly help you with essays, case studies, research papers and other assignments.
Admissions
Admission help & business writing
You can be positive that we will be here 24/7 to help you get accepted to the Master’s program at the TOP-universities or help you get a well-paid position.
Reviews
Editing your paper
Our academic writers and editors will help you submit a well-structured and organized paper just on time. We will ensure that your final paper is of the highest quality and absolutely free of mistakes.
Reviews
Revising your paper
Our academic writers and editors will help you with unlimited number of revisions in case you need any customization of your academic papers