Question

In: Computer Science

Explain this code: The structure used is max heap using array. C++ (i is the index...

Explain this code: The structure used is max heap using array. C++

(i is the index of the element to be deleted)

void del(int i)
{
   int left,right,temp;

   arr[i]=arr[n-1];
   n=n-1;
   left=2*i+1; /*left child of i*/
   right=2*i+2; /* right child of i*/
   while(right < n)
   {
       if( arr[i]>=arr[left] && arr[i]>=arr[right] )
           return;
       if( arr[right]<=arr[left] )
       {
           temp=arr[i];
           arr[i]=arr[left];
           arr[left]=temp;
           i=left;
       }
       else
       {
           temp=arr[i];
           arr[i]=arr[right];
           arr[right]=temp;
           i=right;
       }
       left=2*i+1;
       right=2*i+2;
   }/*End of while*/
   if( left==n-1 && arr[i]    {   temp=arr[i];
       arr[i]=arr[left];
       arr[left]=temp;
   }
}

*********************************************************************************88

FULL CODE : fur more background info

#include
using namespace std;

int arr[100], n;

void insert(int num,int loc){
   int par;
   while(loc>0)
   {
       par=(loc-1)/2;
       if(num<=arr[par])   {
           arr[loc]=num;
           return;
       }
       arr[loc]=arr[par];
       loc=par;
   }/*End of while*/
   arr[0]=num; /*assign num to the root node */
}/*End of insert()*/

void del(int i)
{
   int left,right,temp;

   arr[i]=arr[n-1];
   n=n-1;
   left=2*i+1; /*left child of i*/
   right=2*i+2; /* right child of i*/
   while(right < n)
   {
       if( arr[i]>=arr[left] && arr[i]>=arr[right] )
           return;
       if( arr[right]<=arr[left] )
       {
           temp=arr[i];
           arr[i]=arr[left];
           arr[left]=temp;
           i=left;
       }
       else
       {
           temp=arr[i];
           arr[i]=arr[right];
           arr[right]=temp;
           i=right;
       }
       left=2*i+1;
       right=2*i+2;
   }/*End of while*/
   if( left==n-1 && arr[i]    {   temp=arr[i];
       arr[i]=arr[left];
       arr[left]=temp;
   }
}

void display(){
int i;
   if(n==0){
       cout<< "Heap is empty\n" ;
       return;
   }
   for(i=0;i        cout<<" "<    cout<<"\n";
}/*End of display()*/


main(){
   int choice,num;
   n=0;/*Represents number of nodes in the heap*/
   while(1){
       cout<<"1. Insert\n";
       cout<<"2. Delete root\n";
       cout<<"3. Display\n";
       cout <<"4. Find maximum element\n";
       cout<<"5. Quit\n";
       cout<<"Enter your choice : " ;
       cin>> choice ;
       switch(choice){
       case 1:
           cout<<"Enter the number to be inserted : " ;
           cin>>num ;
           insert(num,n);
           n=n+1;
           break;
       case 2:
           del(0);
           break;
       case 3:
           display();
           break;
       case 4:
           cout << "Maximum element in the heap is "<< arr[0] << endl;
           break;
       case 5:
           exit(0);
       default:
           cout<<"Wrong choice\n" ;
       }/*End of switch */
   }/*End of while */
}/*End of main()*/

Solutions

Expert Solution

void del(int i)//element at index i will be deleted
{
int left,right,temp;//declaring variables

//while deleting an element from max heap,
//the element at current index i, is replaced by last element of the heap
arr[i]=arr[n-1];//replacing element at index i, with last element

n=n-1;//decreasing size of heap

//now we need to perform down heaping from index i
//down heap: swapping value at present index i with one of its larger childs index (either left=2*i+1 or right=2*i+2) if it is greater than the current element

left=2*i+1; /*left child of i*/
right=2*i+2; /* right child of i*/
while(right < n)//while right is less than then n//performing down heaping
{
if( arr[i]>=arr[left] && arr[i]>=arr[right] )//if current element at index i, is greater than both of its childs then
return;//simply returning //because heap property satisfied
//one of its child must be greater than current element at i
//now finding it
if( arr[right]<=arr[left] )//if left child is greater then
{//swapping left child value and current element at index i value
temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
i=left;
}
else
{//if right child is greater then
//swapping right child value and current element at index i value
temp=arr[i];
arr[i]=arr[right];
arr[right]=temp;
i=right;
}
left=2*i+1;
right=2*i+2;
}/*End of while*/
//if left child is greater then
if( left==n-1 && arr[i] <arr[left])//this line is modified
{ ////swapping left child value and current element at index i value
       temp=arr[i];
arr[i]=arr[left];
arr[left]=temp;
} //after completing down heaping
//exiting from method
}


//PLS give a thumbs up if you find this helpful, it helps me alot, thanks.


//if you have any doubts, ask me in the comments


Related Solutions

C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a...
Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a class called Node: Have a Name and Priority.Data set - 10 is the highest priority, 1 is lowest priority. Enqueue and dequeue in the following order. Function  Name, Priority Enqueue  Joe, 3 Enqueue  Fred,1 Enqueue Tuyet,9 Enqueue  Jose, 6 Dequeue Enqueue  Jing, 2 Enqueue  Xi, 5 Enqueue  Moe, 3 DequeueEnqueue  Miko, 7 Enqueue Vlady, 8 Enqueue Frank, 9 Enqueue  Anny, 3 DequeueEnqueue  Xi, 2 Enqueue  Wali, 2 Enqueue  xChe, 6 Enqueue  xVerra, 8 Dequeue Dequeue Dequeue Dequeue...
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
In C Programing Create a stack using an array. Define the index variable of the array...
In C Programing Create a stack using an array. Define the index variable of the array name to be stacktop. Initially set stacktop to -1. Next create two functions push and pop. Both take as input 3 items: a pointer to the stack in memory, stacktop, and maxstack. Make sure to inc stacktop by one and also remember that stacktop[0] is the bottom of the stack and by stack rule cannot be accessed until all the other items are popped....
A max-heap can be used as a max-priority queue, which is an abstract data type having...
A max-heap can be used as a max-priority queue, which is an abstract data type having the operations Insert, Maximum, Extract-Max, and Increase-Key. a. Describe how to implement a FIFO queue with a priority queue, and analyze the time complexity of the implementation (Insert and Delete operations), assuming that a heap is used for the priority queue. b. Describe how to implement a LIFO queue (i.e. a stack) with a priority queue, and analyze the time complexity of the implementation...
Write a code using c# Maximum Sub Array.
Write a code using c# Maximum Sub Array.
c++ program You are to use a Heap data structure for this assignment I currently work...
c++ program You are to use a Heap data structure for this assignment I currently work for an investment/insurance company and I’m looking for clients to call, ones with funds.  I need to have a schedule that shows the list of customers to call and the order to be called.  The list of customers names and phone numbers are in the file ‘NamesAndPhoneV2.txt’.  A second file contains a net worth value for each client.  The files are separated for security and protection reasons, but...
What is an array data structure? What is an array index? What are the benefits of...
What is an array data structure? What is an array index? What are the benefits of array structures? What are the drawbacks of array structures? What is a grid structure? Give examples of when an array could be used. Give examples of when a grid could be used.
How do I write a C# and a C++ code for creating a character array containing...
How do I write a C# and a C++ code for creating a character array containing the characters 'p', 'i', 'n','e','P','I','N','E' only and then using these lower and capital case letter character generate all possible combinations like PInE or PinE or PIne or PINE or piNE etc. and only in this order so if this order is created eg. NeIP or EnPi or NeIP or IPnE and on. You can generate all the combinations randomly by creating the word pine...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT