Answered step by step
Verified Expert Solution
Link Copied!

Question

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 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

Database Systems Design Implementation And Management

Authors: Peter Rob, Carlos Coronel

3rd Edition

0760049041, 978-0760049044

Students also viewed these Databases questions