Question: Use my code of 2 - 3 - 4 Trees I provided and modify it so that it works with 2 / 3 trees instead.

Use my code of 2-3-4 Trees I provided and modify it so that it works with 2/3 trees instead. It should support display, insertion and find methods.
Modify Division Operation:
Adjust the getNextChild method in the Tree class to compare data using division instead of subtraction.
Adjust Insertion Position:
Update the findItem method in the Node class to find the correct insertion position using division.
Split Nodes:
Modify the insert method in the Tree class to split nodes based on division.
///Code Provided that needs to be modified///
public class Tree234{
private Node234 root = new Node234();
public int find(long data){
Node234 current = root;
int currentNumber;
while (true){
if ((currentNumber = current.findItem(data))!=-1)
return currentNumber;
else if (current.isLeaf())
return -1;
else
current = getNextChild(current, data);
}
}
private Node234 getNextChild(Node234 node, long data){
int i;
int numItems = node.getNumItems();
for (i=0; i < numItems; i++){
if (data < node.getItem(i).data)
return node.getChild(i);
}
return node.getChild(i);
}
public void insert(long value){
DataItem data = new DataItem(value);
Node234 current = root;
while (true){
if (current.isFull()){
split(current);
current = getNextChild(current.getParent(), value);
}
else if (current.isLeaf()){
break;
} else {
current = getNextChild(current, value);
}
}
current.insertItem(data);
}
private void split(Node234 node){
DataItem itemB, itemC;
Node234 child2, child3, parent;
int itemIndex;
itemC = node.removeRightMostItem();
itemB = node.removeRightMostItem();
child2= node.disconnectChild(2);
child3= node.disconnectChild(3);
if (node == root){
parent = new Node234();
root = parent;
root.connectChild(0, node);
} else {
parent = node.getParent();
}
itemIndex = parent.insertItem(itemB);
int n = parent.getNumItems();
for (int j = n-1; j > itemIndex; j--){
Node234 t = parent.disconnectChild(j);
parent.connectChild(j +1, t);
}
Node234 newRight = new Node234();
newRight.insertItem(itemC);
newRight.connectChild(0, child2);
newRight.connectChild(1, child3);
parent.connectChild(itemIndex +1, newRight);
}
public void display(){
recursiveTraverse(root);
}
private void recursiveTraverse(Node234 node){
if (node == null)
return;
node.displayNode();
for (int i=0; i<4; i++){
recursiveTraverse(node.getChild(i));
}
}
}

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!