Question: Tree 2 3 4 Iterator.java import java.util. * ; public class Tree 2 3 4 Iterator implements Iterator { / / Your code here public

Tree234Iterator.java import java.util.*;
public class Tree234Iterator implements Iterator {
// Your code here
public Tree234Iterator(Node234 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 0;
}
}LabProgram.java import java.io.*;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class LabProgram {
public static void main(String[] args){
// Create a new Tree234 instance
Tree234 tree = new Tree234();
// Generate and insert random integers
ThreadLocalRandom rand = ThreadLocalRandom.current();
ArrayList expected = new ArrayList();
HashSet added = new HashSet();
for (int i =0; expected.size()20; i++){
int randomInteger = rand.nextInt(100,1000);
if (!added.contains(randomInteger)){
added.add(randomInteger);
expected.add(randomInteger);
tree.insert(randomInteger);
}
}
Collections.sort(expected);
// 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 =0;
for (Integer actualInt : tree){
actual.add(actualInt);
iterationCount++;
// If this iteration exceeded the expected number of iterations then
// print a failure message
if (iterationCount > expected.size()){
System.out.print("FAIL: 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.print(expected.equals(actual)? "PASS" : "FAIL");
System.out.println(": Iteration through tree's keys:");
System.out.println(" Actual: "+ actual);
System.out.println(" Expected: "+ expected);
}
} Node234.java // Node234 class - represents a node in a 2-3-4 tree
class Node234{
protected int A;
protected int B;
protected int C;
protected int keyCount;
protected Node234 left;
protected Node234 middle1;
protected Node234 middle2;
protected Node234 right;
public Node234(int keyA){
this(keyA, null, null);
}
public Node234(int keyA, Node234 leftChild, Node234 middle1Child){
A = keyA;
B =0;
C =0;
keyCount =1;
left = leftChild;
middle1= middle1Child;
middle2= null;
right = null;
}
// Appends 1 key and 1 child to this node.
// Preconditions:
//1. This node has 1 or 2 keys
//2. key > all keys in this node
//3. Child subtree contains only keys > key
public void appendKeyAndChild(int key, Node234 child){
if (keyCount ==1){
B = key;
middle2= child;
}
else {
C = key;
right = child;
}
keyCount++;
}
// Returns the left, middle1, middle2, or right child if the childIndex
// argument is 0,1,2, or 3, respectively.
// Returns null if the childIndex argument is 0 or >3.
public Node234 getChild(int childIndex){
if (childIndex ==0){
return left;
}
else if (childIndex ==1){
return middle1;
}
else if (childIndex ==2){
return middle2;
}
else if (childIndex ==3){
return right;
}
return null;
}
// Returns 0,1,2, or 3 if the child argument is this node's left,
// middle1, middle2, or right child, respectively.
// Returns -1 if the child argument is not a child of this node.
public int getChildIndex(Node234 child){
if (child == left){
return 0;
}
else if (child == middle1){
return 1;
}
else if (child == middle2){
return 2;
}
else if (child == right){
return 3;
}
return -1;
}
// Returns this node's A, B, or C key, if the keyIndex argument is
//0,1, or 2, respectively.
// Returns 0 if the keyIndex argument is 0 or >2.
public int getKey(int keyIndex){
if (keyIndex ==0){
return A;
}
else if (keyIndex ==1){
return B;
}
else if (keyIndex ==2){
return C;
}
return 0;
}
// Returns this node's key count.
public int getKeyCount(){
return keyCount;
}
// Returns 0,1, o
Tree 2 3 4 Iterator.java import java.util. * ;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!