Page 1 of 1

Please help attaching original code for requirements and the reworked code that does not compile. Please help. Requireme

Posted: Thu May 26, 2022 9:32 am
by answerhappygod
Please help attaching original code for requirements and the
reworked code that does not compile. Please help.
Requirements--
Modify the tree234.java program so that it creates
and works with 2-3 trees instead. It should display the tree and
allow searches. It should also allow items to be inserted. In
writing insert(), remember that no splits happen until the
appropriate leaf has been located. Then the leaf will be split if
it’s full. You’ll need to be able to split the root too. The
split() routine is recursive and can handle situations with a full
parent of a full child. This will allow insertion of an unlimited
number of items. Note that in the split() routine you’ll need to
split the parent before you can decide where the items go and where
to attach the children.
There are no restrictions on this project and you
can use any java library classes that you need.
//**If you intend to add an additional class please
identify it with (--------new class name------) in order for when I
attempt to compile I know where to add it**//
public class Tree {
private Node root;

public Tree(){
root = new Node();
}
public Node getNextChild(Node n, int id){
int numItems = n.getNumItems();
int i;
for(i = 0 ; i < numItems ;i++){
if(id < n.getStudent(i).id)
return n.getChild(i);
}
return n.getChild(i);
}
public int search(int id){
Node curr = root;
int childNo;
while(true){
childNo = curr.findStudent(id);
if(childNo != -1)
return childNo;
else if(curr.isLeaf())
return -1;
else
curr = getNextChild(curr,id);
}
}
public void insert(Student s){
Node curr = root;

while(true){
if(curr.isFull()){
split(curr); // split is done
curr = curr.getParent();//go up a level and restart search
}
else if(curr.isLeaf())
break;
else
curr = getNextChild(curr , s.id);
}
curr.insertStudent(s);
}

public void split(Node n){
Student B, C;
Node parent, child2, child3;
C = n.removeStudent();
B = n.removeStudent();
child2 = n.disconnectChild(2);
child3 = n.disconnectChild(3);

Node newNode = new Node();

if(n == root){
root = new Node();
parent = root;
root.connectChild(n, 0);
}
else{
parent = n.getParent();
}
//deal with parent
int index = parent.insertStudent(B);
int num = parent.getNumItems();

for(int i = num-1; i > index ; i--){
Node temp = parent.disconnectChild(i);
parent.connectChild(temp, i+1);
}
parent.connectChild(newNode, index+1);

//deal with newnode
newNode.insertStudent(C);
newNode.connectChild(child2, 0);
newNode.connectChild(child3, 1);
}
}
public class Tree234 {
private Tree234Node root = new Tree234Node();
public void inorderTraversal() {
inorderTraversal(root);
}
private void inorderTraversal(Tree234Node r) {
if (r.checkIfLeaf()) {
for (int i = 0; i <
r.getTotalItems(); i++) {

r.getData(i).printData();
}
} else {
for (int i = 0; i <
r.getTotalItems() + 1; i++) {

inorderTraversal(r.getChild(i));
if (i
< r.getTotalItems()) {

r.getData(i).printData();
}
}
}
}
public void sort1(int[] arr1) {
int x = 0;
sort1(root, arr1, x);
}
private int sort1(Tree234Node node, int[] arr1,
int x) {
if (node.checkIfLeaf()) {
for (int i = 0; i <
node.getTotalItems(); i++) {
arr1[x]
= node.getData(i).data;
x++;
}
return x;
} else {
for (int i = 0; i <
node.getTotalItems() + 1; i++) {
x =
sort1(node.getChild(i), arr1, x);
if (i
< node.getTotalItems()) {

arr1[x] = node.getData(i).data;

x++;
}
}
return x;
}
}
public void add(int x) {
Tree234Node current = root;
TreeData add_data = new
TreeData(x);
while (true) {
if
(current.checkIfFull()) {

splitNode(current);
current
= current.getParent();
current =
getNXTChild(current, x);
} else if
(current.checkIfLeaf()) {

break;
} else {
current =
getNXTChild(current, x);
}
}
current.addItem(add_data);
}
public void splitNode(Tree234Node n) {
TreeData rightMost;
TreeData nextItem;
Tree234Node parent;
Tree234Node ch2;
Tree234Node ch3;
int idx;
nextItem = n.removeData();
rightMost = n.removeData();
ch2 = n.removeChild(2);
ch3 = n.removeChild(3);
Tree234Node rightNode = new
Tree234Node();
if (n == root) {
root = new
Tree234Node();
parent = root;
root.addChild(0,
n);
} else {
parent =
n.getParent();
}
idx =
parent.addItem(rightMost);
int n_items =
parent.getTotalItems();
for (int i = n_items - 1; i >
idx; i--) {
Tree234Node temp =
parent.removeChild(i);
parent.addChild(i + 1,
temp);
}
parent.addChild(idx + 1,
rightNode);
rightNode.addItem(nextItem);
rightNode.addChild(0, ch2);
rightNode.addChild(1, ch3);
}
public Tree234Node getNXTChild(Tree234Node n, int
data) {
int num = n.getTotalItems();
int i;
for (i = 0; i < num; i++) {
if (data <
n.getData(i).data) {
return
n.getChild(i);
}
}
return n.getChild(i);
}
public void printTree() {
printTree(root, 0, 0);
}
private void printTree(Tree234Node node, int l,
int numb) {
node.printNode();
int total_items_in_node =
node.getTotalItems();
for (int i = 0; i <
total_items_in_node + 1; i++) {
Tree234Node the_next_node
= node.getChild(i);
if (the_next_node !=
null) {

printTree(the_next_node, l + 1, i);
} else {

return;
}
}
Please Advise