Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Please complete the following comments that ask to add code public class BinaryTree implements BinaryTreeInterface { private BinaryNode root; public BinaryTree ( ) { root

Please complete the following comments that ask to add code
public class BinaryTree implements BinaryTreeInterface
{
private BinaryNode root;
public BinaryTree()
{
root = null;
}// end default constructor
public BinaryTree(T rootData)
{
this.root = new BinaryNode<>(rootData);
}// end constructor
public BinaryTree(T rootData, BinaryTree leftTree, BinaryTree rightTree)
{
initializeTree(rootData, leftTree, rightTree);
}// end constructor
public void setTree(T rootData, BinaryTree leftTree, BinaryTree rightTree)
{
initializeTree(rootData, leftTree, rightTree);
}// end setTree
public void setRootData(T rootData)
{
root.setData(rootData);
}// end setRootData
public T getRootData()
{
if (isEmpty())
throw new EmptyTreeException();
else
return root.getData();
}// end getRootData
public boolean isEmpty()
{
return root == null;
}// end isEmpty
public void clear()
{
root = null;
}// end clear
public int getHeight()
{
int height =0;
if (root != null)
/* Add code to getHeight method */
return height;
}// end getHeight
public int getNumberOfNodes()
{
int numberOfNodes =0;
if (root != null)
/* Add code to getNumberOfNodes method */
return numberOfNodes;
}// end getNumberOfNodes
protected void setRootNode(BinaryNode rootNode)
{
root = rootNode;
}// end setRootNode
protected BinaryNode getRootNode()
{
return root;
}// end getRootNode
private void initializeTree(T rootData, BinaryTree leftTree, BinaryTree rightTree)
{
root = new BinaryNode<>(rootData);
/* Add code check if left subtree exits and with node */
if ()
root.setLeftChild(leftTree.root);
if ((rightTree != null) && !rightTree.isEmpty())
{
/* Add code check if right subtree not equal to left subTree */
if ()
root.setRightChild(rightTree.root);
else
/* Add code to root.setRightChild with copy() method */
}// end if
/* Add code to check if leftTree subTree exist and leftTree subTree not equal to itself */
if ()
leftTree.clear();
/* Add code to check if rightTree subTree exist and rightTree subTree not equal to itself */
if ()
rightTree.clear();
}// end initializeTree

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions