Question

Using C++, data structures, C++ STL, inputs and expected outputs are shown below.

Max Heap Heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then t

In this question, you need to implement the data structure of Max-Heap with functions insert() and pop(). Libraries such as <

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<bits/stdc++.h>
using namespace std;
class maxheap
{
   int *arr;       //array represent the maxheap
   int size;       //size of heap
   int capacity;       //max capacity of heap
   public:
   maxheap(int c)
   { size=0;       //initially size of heap will be zero
   capacity = c;
   arr = new int[c]; //create new array of size c dynamically
   }
   void adjustheap(int index)//adjust heap from child to parent
   {   while(index>0&&arr[(index-1)/2]<arr[index])//(index-1)/2 represent parent if parent less then current then print
       {swap(arr[index],arr[(index-1)/2]);//swap is inbuilt function it swap the data
           index=(index-1)/2;
       }
      
   }
   void adjustheap_pop(int index)// adjust heap from parent to child
   {   int left_child = 2*index+1; //index of left child
        int right_child = 2*index+2; //index of right child
       int maximum = index; //maximum contain the index of element that is greater then root and maximum of left and right child
       if (left_child < size && arr[left_child] > arr[maximum])
           maximum = left_child;
       if (right_child < size && arr[right_child] >arr[maximum])
            maximum = right_child;
        if (maximum != index)
           {
            swap(arr[index], arr[maximum]);
           //cout << arr[index] << arr[maximum]<<endl;
           adjustheap_pop(maximum); //recursive call the same function
            }
         
      
      
   }
   void insert(int element)//use for insert the element
   {
       arr[size++]=element;
       adjustheap(size-1);  
   }
   int pop()//pop the element
   {
       int number=arr[0];
       if(size==1)//if size is 0 then return and decrease the size
       {size--;
       return number;
       }
       else//swap root and last child and decrese the size and adjust the heap
       {arr[0]=arr[--size];
       adjustheap_pop(0);
       }
       return number;// return the root
   }
   int sum()//calculate the sum
   {
       int sm=0;
       for(int i=0;i<size;i++)
       sm+=arr[i];
       return sm;  
   }
   void print1()// it is for printing the array if you want then use
   {
       for(int i=0;i<size;i++)
       cout << arr[i] << " ";

   }
  
};
int main()
{
   // if you want multiple test case then use this otherwise remove t and while loop
   int t;
   cin >> t;
   while(t--)
   {
   //I am taking capacity of heap as n because if all operation of insert then its highest size become n.
   int n;
   cin >> n;
   maxheap mh(n);
   while(n--)
   {
       char ch;
       int element;
       cin >> ch;
       if(ch=='a')
       {cin >> element;
       mh.insert(element);
       }
       else if(ch=='p')
       {mh.pop();
       //cout<< mh.pop()<<"\n";
       }
       else if (ch=='r')
       {
       cout<< mh.sum()<<"\n";
       //mh.print1();
       }

   }
   }
   //please rate it   
}

Thu 19:29 Terminal - Activities najss@najss-Inspiron-3543: ~ File Edit View Search Terminal Help 5 (base) najss@najss- Inspir

Add a comment
Know the answer?
Add Answer to:
Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap...
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
  • In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the...

    In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the element of the heap with the smallest key is the root of the binary tree. On the other hand, a max-heap has as root the element with the biggest key, and the relationship between the keys of a node and its parent is reversed of that of a min-heap. We also discussed an array-based implementation of heaps. In this assignment, your task is to...

  • Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the...

    Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the max-heap data structure using an integer array of 10 cells. (Do not use Java in-built PriorityQueue class.) [In a max-heap, the root node and the intermediate node vales are always greater than their children.] First, take 10 integer values from the user and insert them in the max-priority queue. Then print the elements of the queue. After that, delete two elements from the queue...

  • Data Structures using C BuildHeap and Heap Sort In preparation: If you have not done so...

    Data Structures using C BuildHeap and Heap Sort In preparation: If you have not done so already, you should complete Worksheet 33 to leam more about the heap data structure. In some applications it is useful to void buildHeap (struct dyArray heap) { initialize a Heap with an existing vector int max = dy Array Size(heap); int i; of values. The values are not assumed for (i = max/2-1; i >= 0; i--) to be organized into a heap, and...

  • 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)....

  • Implement the class MaxHeapPriorityQueue as a heap with the following operations: • MaxHeapPriorityQueue() creates a new...

    Implement the class MaxHeapPriorityQueue as a heap with the following operations: • MaxHeapPriorityQueue() creates a new heap that is empty. It needs no parameters and returns nothing. Note that as discussed in the video lecture, the index range of the array implementation of a heap is 1:n, NOT 0:n-1 • parent(index) returns the value of the parent of heap[index]. index is between 1 and the size of the heap. If index<=1 or index>size of the heap, it returns None •...

  • Write a Java program, In this project, you are going to build a max-heap using array...

    Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...

  • PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores...

    PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....

  • must be coded in c++ without any STL libraries sadly :( so im struggling on this...

    must be coded in c++ without any STL libraries sadly :( so im struggling on this problem, any help would be greatly appreciated, thanks in advance! :) assignment is due tomorrow and im really struggling on this last question :( a. Begin by implementing a BST for integers. The underlying structure is a linked list. You need these methods: i. BST(); -- Constructor ii. void put (int) – Inserts a value into the BST. iii. Void put(int[] a) – Inserts...

  • Need help with the trickle down This is the maxheap.h #ifndef MAXHEAP_H_INCLUDED #define MAXHEAP_H_INCLUDED #include <vector>...

    Need help with the trickle down This is the maxheap.h #ifndef MAXHEAP_H_INCLUDED #define MAXHEAP_H_INCLUDED #include <vector> #include <sstream> #include <string> #include <queue> #include <cmath> // pow() using namespace std; #define PARENT(i) ((i-1) / 2) #define LINE_WIDTH 60.0 template<class T> class MaxHeap{ // private: vector<T> heap; void bubbleUp(int id);    void Heapify() { int length = heap.size(); for(int i=length / 2 -1; i>=0; --i) trickleDown(i); }; public: MaxHeap( vector<T> &vector ) : heap(vector) { Heapify(); } ; MaxHeap() {}; void trickleDown(int...

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