Question

In: Computer Science

I need to make pseudo code for hybrid priority queue using both minheap and maxheap by...

I need to make pseudo code for hybrid priority queue using both minheap and maxheap by ARRAY.
i need to have a function that extends the size of array once it meets certain condition.

Also, I need to implement function remove, insert(k,v), top, toggle, switchtoMin, switchtoMax, state, isEmpty, and size.
Please guide me the pseudo code with above conditions

Solutions

Expert Solution

If u have any doubts please comment

Priority Queue is similar to queue where we insert an element
from the back and remove an element from front, but with a one
difference that the logical order of elements in the priority queue
depends on the priority of the elements. The element with highest
priority will be moved to the front of the queue and one with
lowest priority will move to the back of the queue.
Thus it is possible that when you enqueue an element at the back
in the queue,
it can move to front because of its highest priority.
We can use heaps to implement the priority queue.
It will take O(log N) time to insert and delete each element
in the priority queue.

Based on heap structure, priority queue also has two types
max- priority queue and min - priority queue.

MAX_PRIORITY QUEUE:-

Extract Maximum: In this operation, the maximum element will
be returned and the last element of heap will be placed at index 1
and max_heapify will be performed on node 1 as placing last element
on index 1 will violate the property of max-heap.

int extract_maximum (int Arr[ ])
{
    if(length == 0)
    {
   print “Can’t remove element as queue is empty”;
        return -1;
    }
    int max = Arr[1];
    Arr[1] = Arr[length];
    length = length -1;
    max_heapify(Arr, 1);
    return max;
}
Increase Value: In case increasing value of any node, may violate the property of max-heap, so we will swap the parent’s
value with the node’s value until we get a larger value on parent node.

void increase_value (int Arr[ ], int i, int val)
{
    if(val < Arr[ i ])
    {
        print ”New value is less than current value, can’t be inserted”;
        return;
    }
    Arr[ i ] = val;
    while( i > 1 and Arr[ i/2 ] < Arr[ i ])
    {
        swap|(Arr[ i/2 ], Arr[ i ]);
        i = i/2;
    }
}
Insert Value : //this function inserts the element to the queue

void insert_value (int Arr[ ], int val)
{
    length = length + 1;
    Arr[ length ] = -1; //assuming all the numbers greater than 0 are to be inserted in queue.
    increase_val (Arr, length, val);
}

void remove_value(){ // removes the value from the queue
   return Arr[--length]
}
int top(){ // return the top element from the queue
   return Arr[length-1]
}

boolean isEmpty(){ // return 0 if the queue is empty
   return length=0;
}
int Size(){ //return the length of the queue
   return length;
}

int switchTomin(int A[],int n){ // switches to the max_heap to the min_heap
   int i=(n-2)/2
   while(i>=0)
       min_heapify(A,i--,n);  
}
int switchTomax(int A[],int n){ //switches to the min_heap to the max_heap
   int i=(n-2)/2
   while(i>=0)
       max_heapify(A,i--,n);  
}
min_heapify(int A[],int i,int state){
   int left=left(i);
   int right=right(i);
   int smallest=i;
   if(left<size && A[left]<A[i]){
       smallest=left;
   }
   if(right<size && A[right]<A[smallest]){
       smallest=right;
   }
   if(smallest=i){
       swap(A[i],A[smallest]);
       Heapify(A,smallest,size);
   }
}
max_heapify(int A[],int i,int state){
   int left=left(i);
   int right=right(i);
   int largest=i;
   if(left>size && A[left]>A[i]){
       largest=left;
   }
   if(right>size && A[right]>A[largest]){
       largest=right;
   }
   if(largest!=i){
       swap(A[i],A[largest]);
       Heapify(A,largest,size);
   }
}
MIN_PRIORITY QUEUE:-

Extract Minimum: In this operation, the minimum element will
be returned and the last element of heap will be placed at index 1
and min_heapify will be performed on node 1 as placing last element
on index 1 will violate the property of min-heap.

int extract_manimum (int Arr[ ])
{
    if(length == 0)
    {
   print “Can’t remove element as queue is empty”;
        return -1;
    }
    int min = Arr[1];
    Arr[1] = Arr[length];
    length = length -1;
    min_heapify(Arr, 1);
    return min;
}
decrese Value: In case decresing value of any node, may violate the property of min-heap, so we will swap the parent’s value with the node’s value
until we get a smaller value on parent node.

void decrese_value (int Arr[ ], int i, int val)
{
    if(val > Arr[ i ])
    {
        print ”New value is greater than current value, can’t be inserted”
        return;
    }
    Arr[ i ] = val;
    while( i > 1 and Arr[ i/2 ] > Arr[ i ])
    {
        swap|(Arr[ i/2 ], Arr[ i ]);
        i = i/2;
    }
}
Insert Value :

void insert_value (int Arr[ ], int val)
{
    length = length + 1;
    Arr[ length ] = -1; //assuming all the numbers greater than 0 are to be inserted in queue.
    decrease_val (Arr, length, val);
}

void remove_value(){
   return Arr[--length]
}
int top(){
   return Arr[length-1]
}

boolean isEmpty(){
   return length=0;
}
int Size(){
   return length;
}

switchTomax(int A[],int n){
   int i=(n-2)/2
   while(i>=0)
       heapify(A,i--,n);  
}

int switchTomin(int A[],int n){
   int i=(n-2)/2
   while(i>=0)
       min_heapify(A,i--,n);  
}
int switchTomax(int A[],int n){
   int i=(n-2)/2
   while(i>=0)
       max_heapify(A,i--,n);  
}
min_heapify(int A[],int i,int state){
   int left=left(i);
   int right=right(i);
   int smallest=i;
   if(left<size && A[left]<A[i]){
       smallest=left;
   }
   if(right<size && A[right]<A[smallest]){
       smallest=right;
   }
   if(smallest=i){
       swap(A[i],A[smallest]);
       Heapify(A,smallest,size);
   }
}
max_heapify(int A[],int i,int state){
   int left=left(i);
   int right=right(i);
   int largest=i;
   if(left>size && A[left]>A[i]){
       largest=left;
   }
   if(right>size && A[right]>A[largest]){
       largest=right;
   }
   if(largest!=i){
       swap(A[i],A[largest]);
       Heapify(A,largest,size);
   }
}

here toggle function is not return because toggle means switching one state to other state that is already done in the SwitchTomin and SwitchTomax functins.


Related Solutions

Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being...
Priority Queue Application: Use your Java's Priority Queue. Make a priority queue to represent customers being served at the Department of Motor Vehicles. Start with 100 customers in a List. In a loop, generate a priority for each customer (1-5) In a second loop, add the users to a priority queue Print the List and the Priority Queue
Say that we want to maintain both a Queue and a Priority Queue. This means that...
Say that we want to maintain both a Queue and a Priority Queue. This means that when you do Enqueue you also add the item to the Priority Queue and when you do Dequeue you also remove the item from the Priority Queue. And vise-versa. Show how to do it. Write pseudo codes or explain in words and the running time.
Describe in pseudo-code, a linear-time algorithm for reversing a queue Q. To access the queue, you...
Describe in pseudo-code, a linear-time algorithm for reversing a queue Q. To access the queue, you are only allowed to use the basic functions of the queue ADT defined as follows (Hint: Using a stack, the basic stack functions defined in the textbook and in the class). class Queue { public: int size(); bool isEmpty(); Object front(); void enqueue(Object o); Object dequeue(); };
I need this in PSEUDO CODE: This looks long, but only because I have to give...
I need this in PSEUDO CODE: This looks long, but only because I have to give you my answer to the first part. First part of the questions (already answered) GDOT has contacted you to help write code to control the cross walk signals in Georgia. You must create a Crosswalk Signal class with three hidden attributes (Walk, Hurry and Wait), two constructors (a default that sets all lights to on and an overloaded that sets Hurry to on for...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is the right-most node. The remove (de-queue) operation returns the node with the highest priority (key). If displayForward() displays List (first-->last) : 10 30 40 55 remove() would return the node with key 55. Demonstrate by inserting keys at random, displayForward(), call remove then displayForward() again. You will then attach a modified DoublyLinkedList.java (to contain the new priorityInsert(long key) and priorityRemove() methods). Use the provided...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is the right-most node. The remove (de-queue) operation returns the node with the highest priority (key). If displayForward() displays List (first-->last) : 10 30 40 55 remove() would return the node with key 55. You will then attach priorityInsert(long key) and priorityRemove() methods). AND Use the provided PQDoublyLinkedTest.java to test your code. BOTH CODES SHOULD WORK TOGETHER, YOU JUST HAVE TO ADD priorityInsert(int). PLEASE PROVIDE...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is...
Implement a priority queue using a DoublyLinkedList where the node with the highest priority (key) is the right-most node. The remove (de-queue) operation returns the node with the highest priority (key). If displayForward() displays List (first-->last) : 10 30 40 55 remove() would return the node with key 55. Demonstrate by inserting keys at random, displayForward(), call remove then displayForward() again. You will then attach a modified DoublyLinkedList.java (to contain the new priorityInsert(long key) and priorityRemove() methods), and a driver...
#include <iostream> #include <queue> //This contains the STL's efficient implementation of a priority queue using a...
#include <iostream> #include <queue> //This contains the STL's efficient implementation of a priority queue using a heap using namespace std; /* In this lab you will implement a data structure that supports 2 primary operations: - insert a new item - remove and return the smallest item A data structure that supports these two operations is called a "priority queue". There are many ways to implement a priority queue, with differernt efficiencies for the two primary operations. For this lab,...
I need solution of this homework Question: Queues, Deques and Priority Queues. Enter the necessary code
I need solution of this homework Question: Queues, Deques and Priority Queues. Enter the necessary code
I need to make changes to code following the steps below. The code that needs to...
I need to make changes to code following the steps below. The code that needs to be modified is below the steps. Thank you. 1. Refactor Base Weapon class: a.            Remove the Weapon abstract class and create a new Interface class named WeaponInterface. b.            Add a public method fireWeapon() that returns void and takes no arguments. c.             Add a public method fireWeapon() that returns void and takes a power argument as an integer type. d.            Add a public method activate()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT