Question: Please implement a delete function for a two - three - four tree in the following code ( Please keep the names the same, as
Please implement a delete function for a twothree four tree in the following code Please keep the names the same, as there is a script used to test the codes:
public class TwoFourTree Internal class representing each node in the Tree private class TwoFourTreeItem int values ; Number of values stored in this node int value value value; Node can store up to three values boolean isLeaf true; True if this is a leaf node TwoFourTreeItem parent null; Parent node TwoFourTreeItem leftChild null, centerChild null, rightChild null; Children nodes TwoFourTreeItem centerLeftChild null, centerRightChild null; Additional children for node splitting Constructor for creating a node with one value public TwoFourTreeItemint value this.value value; Constructor for creating a node with two values public TwoFourTreeItemint value int value this.value value; this.value value; this.values ; Constructor for creating a node with three values public TwoFourTreeItemint value int value int value this.value value; this.value value; this.value value; this.values ; Root of the tree private TwoFourTreeItem root null; Function to add a value to the tree public boolean addValueint value if root null If the tree is empty, create a new root root new TwoFourTreeItemvalue; return true; return insertroot value; Insert the value starting from the root Function to check if a value exists in the tree public boolean hasValueint value return searchroot value; Search the value starting from the root Function to delete a value from the tree not implemented yet public boolean deleteValueint value return deleteroot value; Helper function to insert a value into the tree private boolean insertTwoFourTreeItem node, int value if nodeisLeaf If it's a leaf, we can add the value directly if nodevalues If the node has only one value node if value node.value node.value node.value; node.value value; Insert at the correct position else node.value value; Insert the value as the second value node.values ; Now the node is a node return true; else if nodevalues If the node is already a node if value node.value node.value node.value; node.value node.value; node.value value; Insert at the correct position else if value node.value node.value node.value; node.value value; else node.value value; Insert as the third value node.values ; Now the node is a node return true; If not a leaf, descend to the correct child node if value node.value if nodeleftChild null node.leftChild new TwoFourTreeItemvalue; Create a new left child if null else return insertnodeleftChild, value; Recursively insert into the left child else if nodevalues value node.value if nodecenterChild null node.centerChild new TwoFourTreeItemvalue; Create a new center child if null else return insertnodecenterChild, value; Recursively insert into the center child else if noderightChild null node.rightChild new TwoFourTreeItemvalue; Create a new right child if null else return insertnoderightChild, value; Recursively insert into the right child return true;
The rest of the code is in the attached images
Helper function to search for a value in the tree
private boolean searchTwoFourTreeItem node, int value
if node null return false; Return false if we reach a null node
Check if the value matches any of the node's values
if value node.valuenodevalues & value node.valuenodevalues && value node.value
return true;
else if value node.value
return searchnodeleftChild, value; Search in the left child
else if nodevalues value node.value
return searchnodecenterChild, value; Search in the center child
else
return searchnoderightChild, value; Search in the right child
Helper function to delete a value not yet implemented
private boolean deleteTwoFourTreeItem node, int value
return false; Placeholder for future deletion logic
Function to print the tree in inorder traversal
public void printInOrder
printInOrderroot; Start the print from the root
Helper function to print the tree with indentation for visualization
private void printInOrderTwoFourTreeItem node, int indent
if node null return;
if lnodeisLeaf printInOrdernodeleftChild, indent ; Traverse left subtree
printIndentindent;
System.out.printlnnodevalue; Print first value
if nodevalues
if node.isLeaf printInOrdernodecenterChild,
Adding values
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
