Question

write a c++ program for a heap implementation of integer values, which highest priority element is...

write a c++ program for a heap implementation of integer values, which highest priority element is the one with the smallest key value. The implementation uses a minimum heap. You need to modify the heap operations to keep the minimum, rather than maximum, element in the root.

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

#include <iostream>

using namespace std;

int heap[1000]; //heap array with max size 1000

/* Heap is 1 indexed, and child node for index i

is 2*i(left_child) and 2*i + 1(right_child) */

//min_heapify function

void min_heapify(int *a, int i, int n)

{

  if (i > n)

      return;

  else

  {

      //find index with minimum child and swap with parent

      int left_child = 2 * i;

      int right_child = 2 * i + 1;

      int minIndex = i;

      if (left_child <= n && a[left_child] < a[i])

          minIndex = left_child;

      if (right_child <= n && a[right_child] < a[minIndex])

          minIndex = right_child;

      if (minIndex != i)

      {

          swap(a[minIndex], a[i]);

          // heapify min index child

          min_heapify(a, minIndex, n);

      }

  }

}

//fuction to insert in min heap

void insert(int *a, int &n)

{

  printf("Enter element to insert: ");

  int x;

  cin >> x;

  n++;//increase size of heap by one

  a[n] = x;

  int i = n;

  int parent = n / 2;

  //swap with parent untill it gets correct postion in heap

  while (parent > 0 && heap[parent] > heap[i])

  {

      swap(heap[parent], heap[i]);

      i = parent;

      parent = i / 2;

  }

}

//function to remove min element

int extract_min(int *a, int &n)

{

  //store first element, swap last element with first

  //decrease heap size

  //min heapify first

  int res = a[1];

  swap(a[1], a[n]);

  n--;

  min_heapify(a, 1, n);

  return res;

}

void printHeap(int *a, int n)

{

  if (n == 0)

      printf("min heap is empty!\n");

  else

  {

      for (int i = 1; i <= n; ++i)

      {

          cout << heap[i] << " ";

      }

      cout << endl;

  }

}

int main()

{

  int n = 0;

menu:

  cout << endl;

  cout << "1. Print min heap" << endl;

  cout << "2. Insert" << endl;

  cout << "3. Extract min Element" << endl;

  cout << "0. Exit" << endl;

  int choice;

  cin >> choice;

  if (choice == 1)

  {

      printHeap(heap, n);

      goto menu;

  }

  else if (choice == 2)

  {

      insert(heap, n);

      goto menu;

  }

  else if (choice == 3)

  {

      if (n == 0)

          printf("min heap is empty!\n");

      else

          cout << "min element is : " << extract_min(heap, n);

      goto menu;

  }

  else

  {

      return 0;

  }

  return 0;

}


Sample IO:

Add a comment
Know the answer?
Add Answer to:
write a c++ program for a heap implementation of integer values, which highest priority element is...
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...

  • I need this to be in C Write the implementation file, priority queue.c, for the interface...

    I need this to be in C Write the implementation file, priority queue.c, for the interface in the given header file, priority queue.h. Turn in your priority queue.c file and a suitable main program, main.c, that tests the opaque object. priority queue.h is attached as a file to this assignment but is also listed here for your convenience. Your implementation file should implement the priority queue using a heap data structure. Submissions that implement the priority queue without using a...

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

  • Needed in C please Write the implementation file, priority_queue.c, for the interface in the given header file, priority_queue.h. Turn in your priority_queue.c file and a suitable main program, main.c...

    Needed in C please Write the implementation file, priority_queue.c, for the interface in the given header file, priority_queue.h. Turn in your priority_queue.c file and a suitable main program, main.c, that tests the opaque object. priority_queue.h is attached as a file to this assignment but is also listed here for your convenience. Your implementation file should implement the priority queue using a heap data structure. Submissions that implement the priority queue without using a heap will not receive any credit. #ifndef...

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

  • 4. Write a program that finds the smallest element in a one-dimensional array containing 20 integer...

    4. Write a program that finds the smallest element in a one-dimensional array containing 20 integer values in the range 0 to N Hint: Consider following fragment of code Program in C language: for(c=0;c<size;c++) {     if(array[c] < minimum)     {         minimum = array[c];         location = c;     } } this c code is not running and missing some code . please add the missing code so when i paste it will run and provide flowchart

  • Min heap class implementation in Python. Implement a min-using an array. Your min-heap class will have...

    Min heap class implementation in Python. Implement a min-using an array. Your min-heap class will have one private attribute, an array of integers. Implement the following methods for the min-heap class You may not use the built-in min function. init - Constructor makes an empty heap str - Prints the heap out in any way you want for debugging only) makenull(self) - Makes the heap empty insert(self,x) - Insert element x into the heap parent(self,i) - Returns the index of...

  • Write a program that reads two integer values. It then calculates and displays the sum and...

    Write a program that reads two integer values. It then calculates and displays the sum and average of all values between them, Le. if the first value is vi and the second value is 2, then the program calculates and displays the sum and average of all values in the closed range Iv1,2 Your Program must satisfy the following constraints: A. make sure that v1 is less than 2 B. Use a function named getStats(that takes two integer yalues as...

  • In C++ language, need a full executable program!! Build a templated max heap using a linked...

    In C++ language, need a full executable program!! Build a templated max heap using a linked implementation. Insert 100 unique random int’s into the heap. Display the heap. Then, delete the first 50 int’s that were inserted. Display the heap. Keep track of those first 50 int’s using an array or a vector. Display the heap in such a manner that the viewer is convinced that it is a heap. Now, repeat these actions but using an array implementation. Comparing...

  • Q#1 Write a C++ program that reads n integer values and stores them in an array...

    Q#1 Write a C++ program that reads n integer values and stores them in an array of maximum 20 integers. Read from the user a valid value of n (n should not exceed 20). After reading the n array elements find the maximum and minimum elements.

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