Question
Please show work. Thank you for your help.
7. (10 pts) First, give in pseudocode a procedure DELETE(A, i) that deletes Ali] from max binary heap A that currently has n
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Let us assume the array A to be of the form A[0,1,....,n-1]. A maximum binary heap is a complete binary tree in which each parent is greater than its children. A complete binary tree is a binary tree in which each level, except possibly the last level, is completely filled and all the nodes are as left as possible.

While doing insertion or deletion in a binary tree we need to maintain the heap property.

In binary heap we represent a tree using an array. If the index of the parent node in the array is 'i' then the index of its children in the array will be '2*i+1' and '2*i+2'. If the index of the child node in the array is 'i' then the index of its parent in the array will be '(i-1)/2'. We will be using these rules to calculate the parent index from child index and vice versa.

It is difficult to delete an element from the tree which has children(1 or 2). On the other hand, it is really easy to delete an element with no children.

We have to delete the ith element from the array. This element can have no child, one child or two children. So we do not know the no. of children this element will have. So we swap this element with the last element in the array as the last element is always a leaf node. Then we delete this element on the last position by reducing the size of the array and find the correct position for the element which is now in the ith position by comparing it with its parent node. If it is greater than its parent then it is swapped. Now we compare the new position of the element in the array with its children. If it is smaller than its children then it is swapped.So we first find the correct position of the array in the upward direction then we move all the way down in the tree.

Let n be a global element which maintains the size of the heap. Whenever DELETE function is called n is reduced by and whenever INSERT function is called n is incremented by 1.

DELETE(A,i) //function name

int temp1 = A[i]; // temp1 stores the element to be deleted
int temp2 = A[n-1]; // temp2 stores the last element in the array

swap(temp1, temp2); // values of temp1 and temp2 are swapped

int childIndex = i; // now the element which is on the position acts as the parent and we need to compare it with its children

int parentIndex = (childIndex-1)/2;


while(childIndex!=0 && A[parentIndex]<A[childIndex]) {
int temp = A[parentIndex];
A[parentIndex] = A[childIndex];
A[childIndex] = temp;
childIndex = parentIndex;
parentIndex = (childIndex-1)/2;
}

int parent =childIndex; //now this child element acts as parent element and its compared with its children

while(parent<n) {
              
               int child1 = 2*parent+1;
               int child2 = 2*parent+2;
              
               int p = A[parent];
              
               int c1 = Integer.MAX_VALUE;
               int c2 = Integer.MAX_VALUE;
              
               if(child1<n) {
                   c1 = A[child1];
               }
               if(child2<n) {
                   c2 = A[child2];
               }
              
               if(p>c1 && p>c2) {
                   break;
               }
               else if(p>c1) {
A[child2] = p;
A[parent] = c2;
                   parent = c2;
               }
               else if(p>c2) {
A[child1] = p;
A[parent] = c1;
                   parent = c1;
               }
               else {
                   if(c1>c2) {
A[child1] = p;
A[parent] = c1;
                       parent = c1;
                   }
                   else {
A[child2] = p;
A[parent] = c2;
                       parent = c2;
                   }
               }

}

n--; // element has been deleted so size of array reduces by 1

}

Now we have got the array in the correct form and we just need to delete the last element. We can simply make a global size element n and subtract 1 from it whenever delete function is called

The time complexity of the algorithm is O(h) , where h is the height of the tree as the comparisons are happening between parent and child nodes (which are on different levels in the tree). The height of a complete binary tree is h = [ceil(log2(n+1))-1] . So the time complexity of the algorithm is O(log2n).

Add a comment
Know the answer?
Add Answer to:
Please show work. Thank you for your help. 7. (10 pts) First, give in pseudocode a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 5. A three-heap with n elements can be stored in an array A, where A[O] contains...

    5. A three-heap with n elements can be stored in an array A, where A[O] contains the root of the tree. a) Draw the three-heap that results from inserting 5, 2, 8, 3, 6, 4, 9, 7, 1 in that order into an initially empty three-heap. You do not need to show the array representation of the heap. You are only required to show the final tree, although if you draw intermediate trees. b) Assuming that elements are placed in...

  • Subject: Algorithm need this urgent please thank you. 4. Give pseudocode for an algorithm that will solve the following problem. Given an array A[1..n) that contains every number between 1 and n +...

    Subject: Algorithm need this urgent please thank you. 4. Give pseudocode for an algorithm that will solve the following problem. Given an array A[1..n) that contains every number between 1 and n +1 in order, except that one of the numbers is missing. Find the miss sorted ing mber. Your algorithm should run in time (log n). (Hint: Modify Binary search). A pseudocode means an algorithm with if statements and loops, etc. Don't just write a paragraph. Also, if your...

  • problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to...

    problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to write its pseudo-code). To sort an array A, you will then call Det-QuickSort(A, 1, n). You also need to provide the worst case time complexity analysis of your algorithm. 2. (20 points) Given a set of n distinct numbers, we wish to find the k largest in sorted order using a comparison-based algorithm. Give an algorithm that implements each of the following methods, and...

  • I need question 9-10 answered. Thank you Question 1 iShow the resulting binary search tree if...

    I need question 9-10 answered. Thank you Question 1 iShow the resulting binary search tree if we are to insert following elements into the tree in given order, [34, 12, 23, 27,31,9,11,45, 20, 37. i) Show the resulting balanced binary search tree if we are to insert following sorted elements into the tree, [9,12,21, 23, 29, 31, 34, 45, 48, 52, 55] iii What is the pre-order traversal of the balanced binary search tree? v) What is the post-order traversal...

  • 1. In Lab 4, you developed a program to build a Max Heap, and then Heap...

    1. In Lab 4, you developed a program to build a Max Heap, and then Heap Sort. Update the program by adding two additional functions: (a) AddData(A, N, V) where V is the new value added. (b) Delete a data Delete by giving the index of the data position Make sure to display the array after calling each of the function. 2. Write a program to implement Binary Search Algorithm, which will return the index of the data searched (V)....

  • Hello I have an automata question could you help me? [1Points] Give a formal description of a Tu...

    Hello I have an automata question could you help me? [1Points] Give a formal description of a Turing Machine M  that takes two parameters: an integer and an array of integers and decides whether the given integer is an element of the array or not. You can assume that all the integers are between 0 and 9. The input string will be written on the tape of the Turing machine. The first square of the tape contains the integer, the...

  • please check my answers if it wrong answer me a) (25 points) Suppose that you are...

    please check my answers if it wrong answer me a) (25 points) Suppose that you are asked to analyze the performance. Algorithms operate on 1D array of size nor 2D a of the algorithms below, write down the Big O or order of grow terms of n. If the algorithm cannot be applied to the array, write 0(1), O(log n), O(n), O(n logn), 90), O(n"), O(n!). The will only be given for reasonable Big O answers. & algorithms for their...

  • These questions come from the operating system course, please read the questions carefully, please do not give irrelevant answers, thank you. Part III Answer the following questions (20 points ea...

    These questions come from the operating system course, please read the questions carefully, please do not give irrelevant answers, thank you. Part III Answer the following questions (20 points each) 13. Suppose a file system maintains a free list with a bit map that is implemented as an array of 16 bit unsigned integers. Assume that other code has been written to allocate and initialize this array with enough words allocated to handle N blocks. Write pseudocode to set or...

  • Can you please help with the below? 1)   Which of the following is true about using...

    Can you please help with the below? 1)   Which of the following is true about using a 2-3-4 tree? a.   It is designed to minimize node visits while keeping to an O(log n) search performance b.   It is designed to self-balance as new values are inserted into the tree c.   As soon as a node becomes full, it performs the split routine d.   None of the above 2)   Which of the following is true about a binary search tree? a.  ...

  • Would appreciate the answer in the Java coding language please and thank you! 10d 10h left...

    Would appreciate the answer in the Java coding language please and thank you! 10d 10h left Java 7 1. Check the Structure Autocomplete Ready 1 > import java.io.*;... 10 ALL A binary tree uses a multi-node data structure where each node may have 0 to 2 child nodes, and has one stored value, its node number in this case. A tree may either be: 11 class Result { * Complete the 'isValid' function below. • An empty tree, the root...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT