Question

In: Computer Science

Question: How can the code below be implemented in java? TreapNode* deleteNode(TreapNode* root, int key) {...

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;

}

Solutions

Expert Solution


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.


Related Solutions

Java question - How can the code below be modified to accept multi digit input. String...
Java question - How can the code below be modified to accept multi digit input. String e = "1+9+8"; int r = e.charAt(0)-'0'; for (int i = 1; i < e.length(); i+=2){    if (e.charAt(i) == '+'){ r += e.charAt(i+1)-'0'; } else{ r -= e.charAt(i+1)-'0'; } The only built in String methods that can be used are lowercase(), length(), and charAt(). Arrays and parseInt() cannot be used. So we want to know how we can get an answer if a...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q =...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); cout << *p; return 0; } Question 31 options: 10 20 compiler error Runtime error Question 32 A union’s members are exactly like the members of a Structure. Question 32 options: True False Question 33 Given the code below, what are the errors. #include <iostream> using namespace...
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a routine that will mark (use a negative number like -999 for the info field) every node in the tree that currently has only a left son. You can assume the root of the tree has both a right and left son. When finished tell me how many nodes had only a left son as well as how many nodes are in the tree in...
java code Question 2: What are the type and value of each of the expressions below?...
java code Question 2: What are the type and value of each of the expressions below? int [] numbers = { 3, 6, 15, 22, 100, 0 }; double [] decimals = { 3.5, 4.5, 2.0, 2.0, 2.0 }; String [] words = {"alpha", "beta", "gamma"}; numbers[3] + numbers[2] decimals[2] - decimals[0] + numbers[4] words[1].charAt( numbers [0] ) numbers[4] * decimals[1] <= numbers[5] * numbers[0]
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
Question: Can I get the code in Java for this assignment to compare? Please and thank you....
Question: Can I get the code in Java for this assignment to compare? Please and thank you. Can I get the code in Java for this assignment to compare? Please and thank you. Description Write a Java program to read data from a text file (file name given on command line), process the text file by performing the following: Print the total number of words in the file. Print the total number of unique words (case sensitive) in the file. Print...
java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int...
java Modify doubly Linked List code to include following index (rank) based access operations int RetrieveAt(int index) void DeleteAt(int index) void Swap(int index, int index) index starts at 0 (just like array's subscript) If the index is out of bound, RetrieveAt returns 0, DeleteAt does nothing, do nothing in Swap. Write your own testing program to test the modified class -----------------------------------------DLinkedlist.java---------------------------- public class DLinkedList { private class Node { String data; Node next; Node prev; public Node(String s) { data...
HOW CAN I WRITE A PROGRAMING CODE IN ARDUINO DIE ABOUT THE ROOT MEAN SQUARE VALUE...
HOW CAN I WRITE A PROGRAMING CODE IN ARDUINO DIE ABOUT THE ROOT MEAN SQUARE VALUE OF A SIGNAL
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT