Question: Tree 2 3 4 Iterator.java import java.util. * ; public class Tree 2 3 4 Iterator implements Iterator { / / Your code here public
TreeIterator.java import java.util.;
public class TreeIterator implements Iterator
Your code here
public TreeIteratorNode treeRootNode
Your code here
public boolean hasNext
Your code here remove placeholder line below
return false;
public Integer next
Your code here remove placeholder line below
return ;
LabProgramjava import java.io;
import java.util.;
import java.util.concurrent.ThreadLocalRandom;
public class LabProgram
public static void mainString args
Create a new Tree instance
Tree tree new Tree;
Generate and insert random integers
ThreadLocalRandom rand ThreadLocalRandom.current;
ArrayList expected new ArrayList;
HashSet added new HashSet;
for int i ; expected.size; i
int randomInteger rand.nextInt;
if added.containsrandomInteger
added.addrandomInteger;
expected.addrandomInteger;
tree.insertrandomInteger;
Collections.sortexpected;
Build the actual list of integers by iterating through the tree. Keep
track of the number of iterations and if more iterations occur than
expected, then stop.
ArrayList actual new ArrayList;
int iterationCount ;
for Integer actualInt : tree
actual.addactualInt;
iterationCount;
If this iteration exceeded the expected number of iterations then
print a failure message
if iterationCount expected.size
System.out.printFAIL: More than the expected expected.size;
System.out.println iterations, occurred. The iterator's
"hasNext method implementation may be incorrect.";
return;
Print the pass or fail messsage
System.out.printexpectedequalsactual "PASS" : "FAIL";
System.out.println: Iteration through tree's keys:";
System.out.println Actual: actual;
System.out.println Expected: expected;
Nodejava Node class represents a node in a tree
class Node
protected int A;
protected int B;
protected int C;
protected int keyCount;
protected Node left;
protected Node middle;
protected Node middle;
protected Node right;
public Nodeint keyA
thiskeyA null, null;
public Nodeint keyA, Node leftChild, Node middleChild
A keyA;
B ;
C ;
keyCount ;
left leftChild;
middle middleChild;
middle null;
right null;
Appends key and child to this node.
Preconditions:
This node has or keys
key all keys in this node
Child subtree contains only keys key
public void appendKeyAndChildint key, Node child
if keyCount
B key;
middle child;
else
C key;
right child;
keyCount;
Returns the left, middle middle or right child if the childIndex
argument is or respectively.
Returns null if the childIndex argument is or
public Node getChildint childIndex
if childIndex
return left;
else if childIndex
return middle;
else if childIndex
return middle;
else if childIndex
return right;
return null;
Returns or if the child argument is this node's left,
middle middle or right child, respectively.
Returns if the child argument is not a child of this node.
public int getChildIndexNode child
if child left
return ;
else if child middle
return ;
else if child middle
return ;
else if child right
return ;
return ;
Returns this node's A B or C key, if the keyIndex argument is
or respectively.
Returns if the keyIndex argument is or
public int getKeyint keyIndex
if keyIndex
return A;
else if keyIndex
return B;
else if keyIndex
return C;
return ;
Returns this node's key count.
public int getKeyCount
return keyCount;
Returns o
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
