Question: I need the printPathsBackToRoot method to be modified so that it prints my desired output: I need it to output : Jacksonville Miami Jacksonville Chicago
I need the printPathsBackToRoot method to be modified so that it prints my desired output: I need it to output :
Jacksonville
Miami Jacksonville
Chicago Jacksonville
Omaha Miami Jacksonville
Boston Chicago Jacksonville
NYC Chicago Jacksonville
Orlando Omaha Miami Jacksonville
Atlanta NYC Chicago Jacksonville
Tampa NYC Chicago Jacksonville
The input file to create the tree has the values:Jacksonville Magenta
Miami Yellow L
Chicago DarkGreen R
Omaha Purple LL
Boston Yellow RL
NYC Magenta RR
Orlando Amber LLR
Atlanta Pink RRL
Tampa Magenta RRR
Here is what mine looks like:
public String printPathsBackToTheRoot
if root null return ;
StringBuilder result new StringBuilder;
Queue queue new LinkedList;
queue.addroot;
while queue.isEmpty
Node current queue.poll;
If the current node is a leaf node, print the path to the root
if currentleft null && current.right null
List path new ArrayList;
Node temp current;
while temp null
path.addtempcity;
temp temp.parent;
Collections.reversepath;
result.appendStringjoin pathappend
;
Add left and right children to the queue
if currentleft null
queue.addcurrentleft;
if currentright null
queue.addcurrentright;
return result.toStringtrim;
Can you please make a code file that creates a tree then outputs the values as specified i will give a picture public String printPathsBackToTheRoot
This method returns all nodetoroot paths. The nodes should be considered in the
elevator order, as described in Method You can simply use a queue like Method
for this. First, you should enqueue the left child and then the right child. For the
example tree, this method should return the following string. The parent field inside
every node will help you now to easily climb up to the root. Use newline characters
to introduce newlines in the return string.
Jacksonville
Miami Jacksonville
Chicago Jacksonville
Omaha Miami Jacksonville
Boston Chicago Jacksonville
NYC Chicago Jacksonville
Orlando Omaha Miami Jacksonville
Atlanta NYC Chicago Jacksonville
Tampa NYC Chicago Jacksonville public ChromaticTreeString inputFile
This constructor reads a binary tree encoding from the argument input file inputFile
and builds the unique binary tree. If the tree has nodes, then the file has lines,
one line for every node. See the example below, a binary tree and its corresponding
file representation.
The nodes are stored according to their levels in the tree. So first the root is stored,
then the nodes in the next level, and so on The nodes in the same level are stored
starting with the leftmost one. We are calling this type of traversal as the elevator
order traversal every level is visited one after another akin to an elevator system
Every line contains three elements the first element is a city name, the second
element is a color, and the third is a string made up of and representing the
path of insertion starting from the root to the node. An represents go left' and an
represents go right'.
For the root node, we do not store anything for its insertion path. The insertion paths
are unique; two cities won't have the same insertion path. In our implementation,
every node has a parent field that points to its parent.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
