Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

There is two coding should be adjusted. I share below my codings and test failure message at the end. 1. Traversals.java 2. TreeNode.java Tree

 There is two coding should be adjusted. I share below my codings and test failure message at the end.

 


1. Traversals.java

2. TreeNode.java



Tree Traversals

Forthisassignment, you will implement 3 different ways of traversing a tree: pre-order traversal, in-order traversal, and post-order traversal. Since trees are naturally recursive structures, each of these traversals should be implemented recursively. Intuitively, pre-order, in-order, and post-order are all depth traversals that go as deep as they can before "taking a step back" and repeating. All three traversals are very similar; each follows the pattern of writing the data at the current node (C), recursing left (L), and recursing right (R), just in different orders. Recall that pre-order is CLR, in-order is LCR, and post-order is LRC. You may use Java's ArrayList or LinkedList for the traversal methods.

IMPORTANT:

  • You will be given 5 attempts on this assignment, with a 30 minute cooldown between submissions.
  • Please run your code before each submission to ensure that there are no formatting errors! If there are formatting errors in your code, your code will not be graded and a submission attempt will be logged. For more information, please review the Vocareum overview below.

TreeNode

A TreeNode class is provided to you and will be used to represent the nodes in the passed in tree. This file should be treated as read-only and should not be modified in any way. This TreeNode class contains getter and setter methods to access and mutate the structure of the nodes. Please make sure that you understand how this class works, as interaction with this class is crucial for this assignment.

Helper Methods

You'll also notice that the public method stubs we've provided do not contain the parameters necessary for recursion to work efficiently, so these public methods should act as "wrapper methods" for you to use. You will have to write private recursive helper methods and call them in these wrapper methods. All of these helper methods must be private. To reiterate, do not change the method headers for the provided methods.






General Tips


If you remember the recursion orders for each traversal, then the implementation of each traversal will be very similar!

Do not forget about the base case; this should be evaluated first in your helper method before continuing to recurse further down the tree.

  • We highly recommend copying the starter code and working in your preferred IDE in order to have access to features such as code completion, auto-formatting, and much more!


Here are general assignment guidelines that should be followed.

  • Do not include any package declarations in your classes.
  • Do not change any existing class headers, constructors, instance/global variables, or method signatures. For example, do not add throws to the method headers since they are not necessary. Instead, exceptions should be thrown as follows: throw new InsertExceptionHere("Error: some exception was thrown");
  • All helper methods you choose to write should be made private. Recall the idea of Encapsulation in Object-Oriented Programming!
  • Do not use anything that would trivialize the assignment. (e.g. Don't import/use java.util.ArrayList for an ArrayList assignment.)
  • Always be very conscious of efficiency. Even if your method is to be , traversing the structure multiple times is considered inefficient unless that is absolutely required (and that case is extremely rare).
  • If applicable, use the generic type of the class; do not use the raw type of the class. For example, use new LinkedList() instead of new LinkedList().

Use of the following statements should be avoided at all times.

package System.arraycopy() clone()
assert() Arrays class Array class
Thread class Collections class Collection.toArray()
Reflection APIs Inner or nested classes Lambda Expressions


The Vocareum (code editor) interface has six main components:

  • The Drop-Down in the top left. This lets you choose from multiple available files. Note that this drop-down will only be visible in assignments that require multiple files.
  • The Run button. This will compile your code and run a file scan. Running your code will not count towards your total allowed submission attempts, therefore you are free to run as many times as needed.
  • The Submit button. This will compile your code, run a file scan, grade your assignment, and output results to console. Note that for most assignments in this class, you will only be allowed a limited number of submissions. A submission is counted when the submit button is clicked, regardless of whether or not your code can compile or if there are any file issues. Therefore, we highly recommend that you run your code before submitting to ensure that there are no issues that will prevent your code from being graded and that every submission attempt will generate meaningful results.
  • The Reset button. This will revert all your changes and reset your code to the default code template.
  • The Code Window. This is where you will writeyour code. For large coding assignments, we highly recommend copying the starter code and working in your preferred IDE to have access to features such as code completion, auto-formatting, and much more!
  • The Output Window. This window will appear whenever you run or submit your code and will display the output for you to view.

**Below is given code. Please adjust coding by using below. **

1. Traversals.java

import java.util.List;

import java.util.ArrayList;

import java.util.LinkedList;

/**

* Your implementation of the pre-order, in-order, and post-order

* traversals of a tree.

*/

public class Traversals> {


/**

* DO NOT ADD ANY GLOBAL VARIABLES!

*/


/**

* Given the root of a binary search tree, generate a

* pre-order traversal of the tree. The original tree

* should not be modified in any way.

*

* This must be done recursively.

*

* Must be O(n).

*

* @param Generic type.

* @param root The root of a BST.

* @return List containing the pre-order traversal of the tree.

*/

public List preorder(TreeNode root) {


// WRITEYOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!


// BASE CASE

// if root is null then

// the tree is empty

// return an empty list

if (root == null) {

return null;

}

else {


// RECURSIVE CASE

// tree is not empty

// result holds the result

// declare a new ArrayList for result

// left holds the list of nodes

// of the left subtree in preorder

// recursively call preorder using

// getLeft of root as parameter

// save result to left

// right holds the list of nodes

// of the right subtree in preorder

// recursively call preorder using

// getLeft of root as parameter

// save result to left

List result = new ArrayList<>();

List left = preorder(root.getLeft());

List right = preorder(root.getRight());


// preorder is

// traverse root

// go to left subtree

// go to right subtree

// traverse root

// call add of result using

// getData of root

// go to left subtree

// if left is not null

// left subtree is not empty

// iterate each node of left

// using element as the current element

// call add of result using

// element as parameter

// go to right subtree

// if right is not null

// right subtree is not empty

// iterate each node of right

// using element as the current element

// call add of result using

// element as parameter

// return result

result.add(root.getData());

if (left != null) {

for (T element : left) {

result.add(element);

}

}

if (right != null) {

for (T element : right) {

result.add(element);

}

}

return result;

}

}


/**

* Given the root of a binary search tree, generate an

* in-order traversal of the tree. The original tree

* should not be modified in any way.

*

* This must be done recursively.

*

* Must be O(n).

*

* @param Generic type.

* @param root The root of a BST.

* @return List containing the in-order traversal of the tree.

*/

public List inorder(TreeNode root) {


// WRITEYOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!


// BASE CASE

// if root is null then

// the tree is empty

// return an empty list

if (root == null) {

return null;

}

else {


// RECURSIVE CASE

// tree is not empty

// result holds the result

// declare a new ArrayList for result

// left holds the list of nodes

// of the left subtree in inorder

// recursively call inorder using

// getLeft of root as parameter

// save result to left

// right holds the list of nodes

// of the right subtree in inorder

// recursively call inorder using

// getLeft of root as parameter

// save result to left

List result = new ArrayList<>();

List left = inorder(root.getLeft());

List right = inorder(root.getRight());


// inorder is

// go to left subtree

// traverse root

// go to right subtree

// go to left subtree

// if left is not null

// left subtree is not empty

// iterate each node of left

// using element as the current element

// call add of result using

// element as parameter

// traverse root

// call add of result using

// getData of root

// go to right subtree

// if right is not null

// right subtree is not empty

// iterate each node of right

// using element as the current element

// call add of result using

// element as parameter

// return result

if (left != null) {

for (T element : left) {

result.add(element);

}

}

result.add(root.getData());

if (right != null) {

for (T element : right) {

result.add(element);

}

}

return result;

}

}


/**

* Given the root of a binary search tree, generate a

* post-order traversal of the tree. The original tree

* should not be modified in any way.

*

* This must be done recursively.

*

* Must be O(n).

*

* @param Generic type.

* @param root The root of a BST.

* @return List containing the post-order traversal of the tree.

*/

public List postorder(TreeNode root) {


// WRITEYOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!


// BASE CASE

// if root is null then

// the tree is empty

// return an empty list

if (root == null) {

return null;

}

else {


// RECURSIVE CASE

// tree is not empty

// result holds the result

// declare a new ArrayList for result

// left holds the list of nodes

// of the left subtree in postorder

// recursively call postorder using

// getLeft of root as parameter

// save result to left

// right holds the list of nodes

// of the right subtree in postorder

// recursively call postorder using

// getLeft of root as parameter

// save result to left

List result = new ArrayList<>();

List left = postorder(root.getLeft());

List right = postorder(root.getRight());


// postorder is

// go to left subtree

// go to right subtree

// traverse root

// go to left subtree

// if left is not null

// left subtree is not empty

// iterate each node of left

// using element as the current element

// call add of result using

// element as parameter

// traverse root

// call add of result using

// getData of root

// go to right subtree

// if right is not null

// right subtree is not empty

// iterate each node of right

// using element as the current element

// call add of result using

// element as parameter

// return result

if (left != null) {

for (T element : left) {

result.add(element);

}

}

if (right != null) {

for (T element : right) {

result.add(element);

}

}

result.add(root.getData());

return result;

}

}

}

2. TreeNode.java

/**

* Node class used for implementing the BST.

*

* DO NOT MODIFY THIS FILE!!

*

* @author CS 1332 TAs

* @version 1.0

*/

public class TreeNode> {

private T data;

private TreeNode left;

private TreeNode right;

/**

* Constructs a TreeNode with the given data.

*

* @param data the data stored in the new node

*/

TreeNode(T data) {

this.data = data;

}

/**

* Gets the data.

*

* @return the data

*/

T getData() {

return data;

}

/**

* Gets the left child.

*

* @return the left child

*/

TreeNode getLeft() {

return left;

}

/**

* Gets the right child.

*

* @return the right child

*/

TreeNode getRight() {

return right;

}

/**

* Sets the data.

*

* @param data the new data

*/

void setData(T data) {

this.data = data;

}

/**

* Sets the left child.

*

* @param left the new left child

*/

void setLeft(TreeNode left) {

this.left = left;

}

/**

* Sets the right child.

*

* @param right the new right child

*/

void setRight(TreeNode right) {

this.right = right;

}

}

//*Please see below test failure message. Please adjust above two codings

And let me get the perfect score.



Step by Step Solution

There are 3 Steps involved in it

Step: 1

The feedback mentions that you need to adjust the Traversalsjava code to achieve a perfect score Heres a breakdown of the issues and corrections for e... 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_2

Step: 3

blur-text-image_3

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

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students explore these related Programming questions