In: Computer Science
Question: How can the code below be implemented in java?
TreapNode* deleteNode(TreapNode* root, int key)
{
// Base case
if (root == NULL) return root;
// IF KEYS IS NOT AT ROOT
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
// IF KEY IS AT ROOT
// If left is NULL
else if (root->left == NULL)
{
TreapNode *temp = root->right;
delete(root);
root = temp; // Make right child as root
}
// If Right is NULL
else if (root->right == NULL)
{
TreapNode *temp = root->left;
delete(root);
root = temp; // Make left child as root
}
// If ksy is at root and both left and right are not NULL
else if (root->left->priority < root->right->priority)
{
root = leftRotate(root);
root->left = deleteNode(root->left, key);
}
else
{
root = rightRotate(root);
root->right = deleteNode(root->right, key);
}
return root;
}
public TreapNode deleteNode(TreapNode root, int key)
{
// Base case
if (root == NULL) return root;
// IF KEYS IS NOT AT ROOT
if (key < root.key)
root.left =
deleteNode(root.left, key);
else if (key > root.key)
root.right =
deleteNode(root.right, key);
// IF KEY IS AT ROOT
// If left is NULL
else if (root.left == NULL)
{
TreapNode temp =
root.right;
root = temp; //
Make right child as root
}
// If Right is NULL
else if (root.right == NULL)
{
TreapNode temp =
root.left;
root = temp; //
Make left child as root
}
// If ksy is at root and both left and
right are not NULL
else if (root.left.priority <
root.right.priority)
{
root =
leftRotate(root);
root.left =
deleteNode(root.left, key);
}
else
{
root =
rightRotate(root);
root.right =
deleteNode(root.right, key);
}
return root;
}
**************************************************
Thanks for your question. We try our best to help you with detailed
answers, But in any case, if you need any modification or have a
query/issue with respect to above answer, Please ask that in the
comment section. We will surely try to address your query ASAP and
resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.