Answered step by step
Verified Expert Solution
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 objectvalue 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:
Call insertNode method to insert node in the binary tree. Within insertNode method, insert method is calledYou have to write code for this.
insert method is called recursively.
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 TreeNodeT nodeData
data nodeData;
leftNode rightNode null; node has no children
locate insertion point and insert new node; ignore duplicate values
public void insertT 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 insertNodeT insertValue
if root null
root new TreeNodeinsertValue; create root node
else
root.insertinsertValue; call the insert method
begin preorder traversal
public void preorderTraversal
preorderHelperroot;
recursive method to perform preorder traversal
private void preorderHelperTreeNode node
YOUR CODE GOES HERE
begin inorder traversal
public void inorderTraversal
inorderHelperroot;
recursive method to perform inorder traversal
private void inorderHelperTreeNode node
if node null
return;
inorderHelpernodeleftNode; traverse left subtree
System.out.printfs node.data; output node data
inorderHelpernoderightNode; traverse right subtree
begin postorder traversal
public void postorderTraversal
postorderHelperroot;
recursive method to perform postorder traversal
private void postorderHelperTreeNode 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 ;
Overloaded Constructor initializing specific attributes
public MilkString name, int calories, String typeOfMilk, double fatContent
supername 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started