Tuesday, August 19, 2014

Priority Queues Demo - An application of Heap ADT


#include <cstdlib>
#include <conio.h>
#include <iostream>

using namespace std;

char getChoice(); 
char menu(string menu_item[], int menu_size);

#define HEAP_SIZE 21
#define MENU_SIZE 8

int heap[HEAP_SIZE];
int n = 0;

string menu_item[] = {"[1] Insert an element", 
                      "[2] Delete the maximum element",
                      "[3] Print all Elements",
                      "[4] Print the maximum element",
                      "[5] Examine the left child",
                      "[6] Examine the right child",
                      "[7] Examine the parent",
                      "[8] Exit"};

char getChoice();
void insert(int heap[], int &n, int x);
char menu(string menu_item[], int menu_size);
int deletemax(int heap[], int &n);
void heapify(int heap, int r);
void print_elements();
bool find(string s, char c);
void heapify(int heap[], int r);
void swap(int &n1, int &n2); 
int max(int heap[]);
int search(int heap[], int number);


int main(int argc, char *argv[])
{
    char choice;
    int number;
    do
    {
       system("CLS") ;
       choice = menu(menu_item, MENU_SIZE);
       switch (choice) {
           case '1' :
                cout << "What number would you like to insert into the heap  ? ";
                cin >> number;
                insert(heap, n, number); 
                break;
           case '2' :             
                deletemax(heap, n);
                cout << "The maximum value has been deleted." << endl;
                break;                
           case '3' :
                print_elements();
                break;                    
           case '4' :
                if (n != 0)
                   cout << "\nThe maximum element is " << max(heap) << endl;
                else
                   cout << "\nHeap is empty!" << endl;   
                break;                     
           case '5' :
                if (n != 0) {
                   cout << "What number would you like to get the left child ? ";
                   cin >> number;
                   int index = search(heap, number);
                   if (index <= n) {
                      if ((index * 2) <= n) 
                         cout << "\nThe left child of " << number << " is " << heap[index * 2] << endl;
                      else
                         cout << endl << number << " has no left child." << endl;    
                   }   
                   else
                      cout << number << " not found!" << endl;                          
                }       
                else
                   cout << "\nHeap is empty!" << endl;   
                break;  
           case '6' :
                 if (n != 0) {
                   cout << "What number would you like to get the right child ? ";
                   cin >> number;
                   int index = search(heap, number);
                   if (index <= n) {
                      if ((index * 2 + 1) <= n) 
                         cout << "\nThe right child of " << number << " is " << heap[index * 2 + 1] << endl;
                      else
                         cout << endl << number << " has no right child." << endl;    
                   } 
                   else
                      cout << number << " not found!" << endl;     
                }       
                else
                   cout << "\nHeap is empty!" << endl;  
                break;
           case '7' :
                 if (n != 0) {
                   cout << "What number would you like to get the parent ? ";
                   cin >> number;
                   int index = search(heap, number);
                   if (index <= n) {
                     if ((index / 2) >= 1) 
                         cout << "\nThe parent of " << number << " is " << heap[index / 2] << endl;
                     else
                         cout << endl << number << " has no parent." << endl;    
                   }      
                   else
                      cout << number << " not found!" << endl;
                 } 
                 else
                    cout << "\nHeap is empty!" << endl;  
                break;     
                    
       }
       
       system("PAUSE");
    }  while (choice != '8');
    
    return EXIT_SUCCESS;
}
                     
char getChoice() 
{
     string choices = "12345678";
     char choice;
     do 
        choice =  getch();
     while (!find(choices,choice));   
     return choice;
}

bool find(string s, char c)
{
     bool found = false;
     int i = 0;
     while ((!found) && i < s.length())
     {
           if (c == s[i]) {
               found = true;
               break;
           }    
           i++;
     }
     return found;
} 

char menu(string menu_item[], int menu_size)
{
     char choice = 0;
     cout << "=============================" << endl; 
     cout << "  Hello! I'm a Heap Manager  " << endl;
     cout << "=============================" << endl; 
     for (int i = 0; i < menu_size; i++)
        cout << menu_item[i] << endl;
     cout << "=============================" << endl; 
     cout << "Enter your choice : ";
     choice = getChoice();
     cout << choice << endl;
     return choice;
}


void insert(int heap[], int &n, int x)
{
     int pnew = n + 1;
     heap[pnew] = x;
     while(pnew > 1 && heap[pnew /2] < x) {
       heap[pnew] = heap[pnew / 2];
       pnew = pnew / 2;           
     }
     heap[pnew] = x;
     n++;
}

int deletemax(int heap[], int &n)
{
    int max = heap[1];
    heap[1] = heap[n];
    n--;
    heapify(heap, 1);
    return max;
}    

int max(int heap[]) 
{
    return heap[1];
}

void heapify(int heap[], int r)
{
     int largest;
     int left = 2 * r;
     int right = 2 * r +1;
     if(left <= n && heap[left] > heap[r])
        largest = left;
     else
        largest = r;
        
     if (right <= n && heap[right] > heap[largest])
        largest = right;
        
     if (largest != r) {
        swap(heap[r], heap[largest]);
        heapify(heap, largest);  
     }                 
}   

void swap(int &n1, int &n2) 
{
     int temp;
     
     temp = n1;
     n1 = n2;
     n2 = temp;
     
}       

void print_elements()
{
     for (int i= 1; i <= n; i++) {
         cout << heap[i] << " ";
     }
}

int search(int heap[], int number)
{
    int i = 1;
    while (i <= n) {
       if (heap[i] == number) 
           break;
       i++;   
    }
    return i;
}    

No comments:

Post a Comment