Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The code below provides a TreeNode class. The TreeNode class is missing code for the node insertion and two of the types of binary tree

The code below provides a TreeNode class. The TreeNode class is missing code for the node insertion and two of the types of binary tree traversals (postorder, preorder). The node insertion for a binary search tree must ensure that the nodes on the left are all less than the parent node and the nodes on the right are all greater. Your assignment is to complete the insert method for the binary search tree, the missing traversals, and add a new method to search for a specific object (by name or by an attribute value) that returns a Boolean true if the object/value is in the tree. In order to demonstrate the tree, you will populate the tree with your own custom objects from your inheritance subclass, demonstrate the new traversals and the search method. Be sure to identify how you are comparing your custom objects for appropriate placement in the binary search tree using comments in your inheritance subclass code.
Hint:
1) Call insertNode method to insert node in the binary tree. Within insertNode method, insert method is called(You have to write code for this.)
2) insert method is called recursively.
3) Do not insert new nodes as follows: root.left, root.left.right, etc. Make use of insertNode methods. Traversal helper methods are recursive.
//class TreeNode definition
public class TreeNode>{
// package access members
TreeNode leftNode; // left node
T data; // node value
TreeNode rightNode; // right node
// constructor initializes data and makes this a leaf node
public TreeNode(T nodeData){
data = nodeData;
leftNode = rightNode = null; // node has no children
}
// locate insertion point and insert new node; ignore duplicate values
public void insert(T insertValue){
// insert in left subtree
**YOUR CODE GOES HERE
// insert in right subtree
** YOUR CODE GOES HERE
//class Tree definition
public class Tree>{
private TreeNode root;
// constructor initializes an empty Tree of integers
public Tree(){
root = null;
}
// insert a new node in the binary search tree
public void insertNode(T insertValue){
if (root == null){
root = new TreeNode<>(insertValue); // create root node
}
else {
root.insert(insertValue); // call the insert method
}
}
// begin preorder traversal
public void preorderTraversal(){
preorderHelper(root);
}
// recursive method to perform preorder traversal
private void preorderHelper(TreeNode node){
** YOUR CODE GOES HERE
// begin inorder traversal
public void inorderTraversal(){
inorderHelper(root);
}
// recursive method to perform inorder traversal
private void inorderHelper(TreeNode node){
if (node == null){
return;
}
inorderHelper(node.leftNode); // traverse left subtree
System.out.printf("%s ", node.data); // output node data
inorderHelper(node.rightNode); // traverse right subtree
}
// begin postorder traversal
public void postorderTraversal(){
postorderHelper(root);
}
// recursive method to perform postorder traversal
private void postorderHelper(TreeNode node){
** YOUR CODE GOES HERE
Inheritance subclass:
public class Milk extends foodClass{
// Attributes specific to Milk
private String typeOfMilk;
private double fatContent; // Fat content in percentage
// Default Constructor
public Milk(){
super();
this.typeOfMilk = "default";
this.fatContent =0.0;
}
// Overloaded Constructor initializing specific attributes
public Milk(String name, int calories, String typeOfMilk, double fatContent){
super(name, calories);
this.typeOfMilk = typeOfMilk;
this.fatContent = fatContent;
}
// Override toString method
@Override
public String toString(){
return super.toString()+"| Type of Milk: "+ typeOfMilk +"| Fat Content: "+ fatContent +"%";
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Step: 3

blur-text-image

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

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions