Implement the BinaryHeap ADT in a file BinaryHeap.cpp #ifndef BINARY_HEAP_H #define BINARY_HEAP_H #include #include #include using namespace std; // BinaryHeap class // // CONSTRUCTION: with an optional capacity (that defaults to 100) // // ******************PUBLIC OPERATIONS********************* // void insert( x )       --> Insert x // void deleteMin( )   --> Remove smallest item // void deleteMin(minItem)--> Remove smallest item and store the minimum in minItem // C findMin( )           --> Return smallest item // bool isEmpty( )        --> Return true if empty; else false // void makeEmpty( )      --> Remove all items template class BinaryHeap { public:      BinaryHeap( int capacity = CAP ) : items( capacity ), currentSize( 0 )      {   }         bool isEmpty( ) const      {           return currentSize == 0;       }         /**       * Find the smallest item in the priority queue.       * Return the smallest item       */      const C & findMin( ) const      {              assert(!isEmpty());              return items[ 1 ];      }          /**       * Insert item x, allowing duplicates.       */      void insert( const C & x )      {              if( currentSize == items.size( ) - 1 )                      items.resize( items.size( ) * 2 );                     // Percolate up              int hole = ++currentSize;              C copy = x;                     items[ 0 ] = copy;             for( ; x < items[ hole / 2 ]; hole /= 2 )                    items[ hole ] = items[ hole / 2 ];            items[ hole ] = items[ 0 ];      }             /**    * Remove the minimum item.       */   void deleteMin() {    assert(!isEmpty());                 items[1] = items[currentSize];       currentSize = currentSize - 1;       percolateDown(1); }   /**    * Remove the minimum item and place it in minItem.    */   void deleteMin(C& minItem) { assert(!isEmpty());       minItem = items[1];       items[1] = items[currentSize];       currentSize = currentSize - 1;       percolateDown(1); }   void makeEmpty() { currentSize = 0; }   static const int CAP = 100;   private:      int currentSize; // Number of elements in heap      vector items; // The heap array         /**       * Establish heap order property from an arbitrary       * arrangement of items. Runs in linear time.      */      void buildHeap( )      {              for( int i = currentSize / 2; i > 0; --i )                      percolateDown( i );      }         /**       * Internal method to percolate down in the heap.       * hole is the index at which the percolate begins.       */      void percolateDown( int hole )    {              int child;             C tmp = items[ hole ];                   for( ; hole * 2 <= currentSize; hole = child )              {                      child = hole * 2;                      if( child != currentSize && items[ child + 1 ] < items[ child ] )                             ++child;                      if( items[ child ] < tmp )                             items[ hole ] = items[ child ];                      else                            break;              }             items[ hole ] = tmp;

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question


Implement the BinaryHeap ADT in a file BinaryHeap.cpp

#ifndef BINARY_HEAP_H
#define BINARY_HEAP_H
#include <assert.h>
#include <vector>
#include <iostream>
using namespace std;

// BinaryHeap class
//
// CONSTRUCTION: with an optional capacity (that defaults to 100)
//
// ******************PUBLIC OPERATIONS*********************
// void insert( x )       --> Insert x
// void deleteMin( )   --> Remove smallest item
// void deleteMin(minItem)--> Remove smallest item and store the minimum in minItem
// C findMin( )           --> Return smallest item
// bool isEmpty( )        --> Return true if empty; else false
// void makeEmpty( )      --> Remove all items

template <typename C>
class BinaryHeap
{ public:  
   BinaryHeap( int capacity = CAP ) : items( capacity ), currentSize( 0 )  
   {   }  
  
   bool isEmpty( ) const  
   {   
       return currentSize == 0;   
   }  
  
   /**   
   * Find the smallest item in the priority queue.   
   * Return the smallest item   
   */  
   const C & findMin( ) const  
   {      
       assert(!isEmpty());      
       return items[ 1 ];  
   }      
   /**   
   * Insert item x, allowing duplicates.   
   */  
   void insert( const C & x )  
   {      
       if( currentSize == items.size( ) - 1 )          
           items.resize( items.size( ) * 2 );      
      
       // Percolate up      
       int hole = ++currentSize;      
       C copy = x;             
       items[ 0 ] = copy;     
       for( ; x < items[ hole / 2 ]; hole /= 2 )        
           items[ hole ] = items[ hole / 2 ];    
       items[ hole ] = items[ 0 ];  
   }      
  
   /**
   * Remove the minimum item.   
   */  
void deleteMin()
{

   assert(!isEmpty());             

   items[1] = items[currentSize];      
currentSize = currentSize - 1;      
percolateDown(1);
}  

/**   
* Remove the minimum item and place it in minItem.   
*/  
void deleteMin(C& minItem)
{
assert(!isEmpty());      

minItem = items[1];      
items[1] = items[currentSize];      
currentSize = currentSize - 1;      
percolateDown(1);
}  

void makeEmpty()
{
currentSize = 0;
}  

static const int CAP = 100;  

private:  
   int currentSize; // Number of elements in heap  
   vector<C> items; // The heap array  
  
   /**   
   * Establish heap order property from an arbitrary   
   * arrangement of items. Runs in linear time.  
   */  
   void buildHeap( )  
   {      
       for( int i = currentSize / 2; i > 0; --i )          
           percolateDown( i );  
   }  
  
   /**   
   * Internal method to percolate down in the heap.   
   * hole is the index at which the percolate begins.   
   */  
   void percolateDown( int hole )
   {      
       int child;     
       C tmp = items[ hole ];    
      
       for( ; hole * 2 <= currentSize; hole = child )      
       {          
           child = hole * 2;          
           if( child != currentSize && items[ child + 1 ] < items[ child ] )             
               ++child;          
           if( items[ child ] < tmp )             
               items[ hole ] = items[ child ];          
           else            
               break;      
       }     
       items[ hole ] = tmp;  
   }
};

#endif

Defines a public member function printJobs () in Binaryäeap.cpp which prints out the print jobs in
printQueue.
• Calls the printJobs () member function in main () to print out the print jobs.
• Prompts user to enter n - the number of jobs that the printer will run.
• Calls deleteMin () member function n times.
• Calls the printJobs () member function in main () to print out the remaining print jobs.
The expected result:
The priority of print job? 6
The priority of print job? 20
The priority of print job? 5
The priority of print job? 3
The priority of print job? 1
The priority of print job? 36
The priority of print job? 8
The priority of print job? 12
The priority of print job? 6
The print jobs: 1 3 6 6 5 36 8 20 12
The number of jobs the printer will run? 3
The print jobs: 6 8 6 20 12 36
Transcribed Image Text:Defines a public member function printJobs () in Binaryäeap.cpp which prints out the print jobs in printQueue. • Calls the printJobs () member function in main () to print out the print jobs. • Prompts user to enter n - the number of jobs that the printer will run. • Calls deleteMin () member function n times. • Calls the printJobs () member function in main () to print out the remaining print jobs. The expected result: The priority of print job? 6 The priority of print job? 20 The priority of print job? 5 The priority of print job? 3 The priority of print job? 1 The priority of print job? 36 The priority of print job? 8 The priority of print job? 12 The priority of print job? 6 The print jobs: 1 3 6 6 5 36 8 20 12 The number of jobs the printer will run? 3 The print jobs: 6 8 6 20 12 36
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 5 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY