Question

In: Computer Science

A max-heap can be used as a max-priority queue, which is an abstract data type having...

A max-heap can be used as a max-priority queue, which is an abstract data type having the operations Insert, Maximum, Extract-Max, and Increase-Key.

a. Describe how to implement a FIFO queue with a priority queue, and analyze the time complexity of the implementation (Insert and Delete operations), assuming that a heap is used for the priority queue.

b. Describe how to implement a LIFO queue (i.e. a stack) with a priority queue, and analyze the time complexity of the implementation (Insert (Push) and Delete (Pop) operations), assuming that a heap is used for the priority queue.

Solutions

Expert Solution

The idea of FIFO is first in first out ..we will maintain a MIN_HEAP and heap accepts element having value and it's rank. Rank starts with 1 and gets increased as elements come in. So when pop called for FIFO then rank with minumum element comes out which simulates FIFO because rank starts with 1 and keeps on increasing as elements come in.

Note: Comparison is based on Rank instead of value.

Sample pseudo code for data along with rank which helps in which order data will be popped out.

class Data<T> {
  T x;
  int rank;

  Data(T x, int rank) {
    this.x = x;
    this.rank = rank;
  }
}

Sample pseudo code for FIFO.

class FIFO<T> {
  Queue<Data<T>> queue; // min heap, comparison is based on rank instead of value
  int rank = 1;
  
  void push(T x) {
    queue.push(new Data<T>(x, rank++));
  }

  T pop() {
    return queue.poll();
  }
}

Similarly for LIFO we will maintain MAX_HEAP and rest part will remain same.

Sample pseudo code for LIFO.

class LIFO<T> {
  Queue<Data<T>> queue; // max heap, comparison is based on rank instead of value
  int rank = 1;
  
  void push(T x) {
    queue.push(new Data<T>(x, rank++));
  }

  T pop() {
    return queue.poll();
  }
}

Related Solutions

Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a...
Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a class called Node: Have a Name and Priority.Data set - 10 is the highest priority, 1 is lowest priority. Enqueue and dequeue in the following order. Function  Name, Priority Enqueue  Joe, 3 Enqueue  Fred,1 Enqueue Tuyet,9 Enqueue  Jose, 6 Dequeue Enqueue  Jing, 2 Enqueue  Xi, 5 Enqueue  Moe, 3 DequeueEnqueue  Miko, 7 Enqueue Vlady, 8 Enqueue Frank, 9 Enqueue  Anny, 3 DequeueEnqueue  Xi, 2 Enqueue  Wali, 2 Enqueue  xChe, 6 Enqueue  xVerra, 8 Dequeue Dequeue Dequeue Dequeue...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector class to hold the data array
A deque (short for double-ended queue, but pronounced “deck”) is an abstract data type that supports...
A deque (short for double-ended queue, but pronounced “deck”) is an abstract data type that supports adding and removing at both the front and back. So, at a bare minimum, a deque has four operations: addFront(), removeFront(), addBack(), removeBack(). Suppose you have a deque D containing the numbers (1, 2, 3, 4, 5, 6, 7, 8), in this order. Suppose further that you have an initially empty queue Q. Give pseudo-code description of a method that uses only D and...
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying...
Building a Priority Queue in C++ using a min-heap. Not using C++ Standard Template Library. Trying to understand how this works. Thank you
Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to...
Using the linked list abstract data type “Queue ADT”, write a menu dirven user interfece to teach each of the operations in the ADT. Any errors discovered during the processing should be printed as a part of the test result. Please Use C++ language.
These questions are about an abstract data type for sets of integers. The representation used will...
These questions are about an abstract data type for sets of integers. The representation used will be sorted lists without duplicates. To help answer the questions, the following pseudocode of merge-sort for lists may be useful. It is assumed head(x) and tail(x) return the head (first element) and tail (remaining elements) of a list, respectively, and a procedure is available for splitting a list into two lists of approximately equal size. // returns sorted version of list x mergesort(x):        ...
Explain this code: The structure used is max heap using array. C++ (i is the index...
Explain this code: The structure used is max heap using array. C++ (i is the index of the element to be deleted) void del(int i) {    int left,right,temp;    arr[i]=arr[n-1];    n=n-1;    left=2*i+1; /*left child of i*/    right=2*i+2; /* right child of i*/    while(right < n)    {        if( arr[i]>=arr[left] && arr[i]>=arr[right] )            return;        if( arr[right]<=arr[left] )        {            temp=arr[i];            arr[i]=arr[left];   ...
Consider an almost-priority-queue data structure, which allows two operations: insert and extract almost min. extract almost...
Consider an almost-priority-queue data structure, which allows two operations: insert and extract almost min. extract almost min operation outputs either the first minimum or a second minimum item from the current structure (and doesn’t tell you which it outputted). Prove that not both these operations can be done in o(log n) time even if amortization is allowed.
Where in a max heap with over 15 elements can you find the third-largest value? Select...
Where in a max heap with over 15 elements can you find the third-largest value? Select all that apply. a child of the right child of the root, a child of the left child of the root, leaf node, left or right child of the root, root
1.  What is an abstract data type? In an ADT, what is known and what is hidden?
1.  What is an abstract data type? In an ADT, what is known and what is hidden?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT