The SimpleTree class shown below extends the BinaryTree class that was given out
ID: 3559546 • Letter: T
Question
The SimpleTree class shown below extends the BinaryTree class that was given out as part of Homeworks #7 and #8, and as part of SampleCode #10. You are to write four methods for the SimpleTree class shown below called findMin(), findMax(), removeMin() and removeMax().
The methods are already in the SimpleTree class, but they do not do anything yet. The findMin() and findMax() methods should return the smallest or largest node value (respectively) as determined by the compareTo() method. And theremoveMin() and removeMax() methods should remove the smallest or largest node value (respectively).
Note that the BinaryTree class implements a Binary Search Tree.
In the following commands below that create a Queue and then add and remove items from it, how many elements are left in theQueue after all the statements are executed?
Question 7 options:
0
4
5
6
7
In Question 7 above, if you were to replace each of the System.out.println() statements (denoted in comments as Output 1, Output 2 and Output 3) with the statement q.offer(q.remove( )); for each of the three lines, then q would contain which order of Integer values after all instructions have executed?
Question 8 options:
3, 5, 9, 2, 4, 1, 8
3, 5, 9, 1, 8, 2, 4
5, 9, 2, 4, 1, 8, 3
3, 2, 4, 5, 9, 1, 8
2, 4, 1, 8, 3, 5, 9
In the following commands below that create a Stack and then add and remove items from it, what value does x have after the statements have been executed?
Question 9 options:
16
12
19
5
0
In Question 9 above, after the instructions execute, z has what value?
Question 10 options:
4
9
5
12
16
In the ZList class from Questions 1-6, what part of the list is a Node at when its next value is equal to null?
0
4
5
6
7
Explanation / Answer
public E findMin()
{
E result = null;
if(root == null)
System.out.println("The tree is empty.");
else
{
BinaryTreeNode<E> node = root;
while(node.left != null)
{
node = node.left;
}
result = node.element;
}
return result;
}
public E findMax()
{
E result = null;
if(root == null)
System.out.println("The tree is empty.");
else
{
BinaryTreeNode<E> node = root;
while(node.right != null)
{
node = node.right;
}
result = node.element;
}
return result;
}
public void removeMin()
{
if(root == null)
System.out.println("The tree is empty.");
else
{
if(root.left == null)
{
root = root.right;
}
else
{
BinaryTreeNode<E> curr = root;
BinaryTreeNode<E> node = root.left;
while(node.left != null)
{
curr = node;
node = node.left;
}
curr.left = node.right;
}
count--;
}
}
public void removeMax()
{
if(root == null)
System.out.println("The tree is empty.");
else
{
if(root.right == null)
{
root = root.left;
}
else
{
BinaryTreeNode<E> curr = root;
BinaryTreeNode<E> node = root.right;
while(node.right != null)
{
curr = node;
node = node.right;
}
curr.right = node.left;
}
count--;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------
7.
The number of elements are left in the Queue after all the statements are executed: 4
--------------------------------------------------------------------------------------------------------------------------------------------
8.
3, 5, 9, 2, 4, 1, 8
--------------------------------------------------------------------------------------------------------------------------------------------
9.
The value of x: 19
--------------------------------------------------------------------------------------------------------------------------------------------
10.
The value of z: 9
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.