Question

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 and print them. You can use Java Scanner class to take inputs from the user. The sample inputs/ outputs are given below:
Sample inputs and outputs:
(User’s inputs are shown in bold)
Enter 10 values: 5 15 9 7 20 18 8 6 1 11
The elements of the max-priority queue: 20 15 18 6 11 9 8 5 1 7
The element removed after first deletion: 20
The element removed after second deletion: 18

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

import java.util.*;
class solve{

static int []H = new int[10];
static int size = -1;

static int parent(int i)
{
return (i - 1) / 2;
}

static int leftChild(int i)
{
return ((2 * i) + 1);
}


static int rightChild(int i)
{
return ((2 * i) + 2);
}

static void shiftUp(int i)
{
while (i > 0 &&
H[parent(i)] < H[i])
{
  
swap(parent(i), i);

i = parent(i);
}
}

static void shiftDown(int i)
{
int maxIndex = i;

int l = leftChild(i);

if (l <= size &&
H[l] > H[maxIndex])
{
maxIndex = l;
}


int r = rightChild(i);

if (r <= size &&
H[r] > H[maxIndex])
{
maxIndex = r;
}

if (i != maxIndex)
{
swap(i, maxIndex);
shiftDown(maxIndex);
}
}


static void insert(int p)
{
size = size + 1;
H[size] = p;

shiftUp(size);
}

static int extractMax()
{
int result = H[0];

H[0] = H[size];
size = size - 1;
shiftDown(0);
return result;
}


static int getMax()
{
return H[0];
}

static void remove(int i)
{
H[i] = getMax() + 1;
shiftUp(i);
extractMax();
}

static void swap(int i, int j)
{
int temp= H[i];
H[i] = H[j];
H[j] = temp;
}

public static void main(String[] args)
{

System.out.println("Enter 10 values:")

Scanner sc = new Scanner(System.in);
for(int r =0 ; r < 10 ;r++)
{
int e= sc.nextInt();
insert (e);
}

int i = 0;

System.out.print("Element in the priority Queue : ");
while (i <= size)
{
System.out.print(H[i] + " ");
i++;
}

System.out.print("\n");

remove(0);
System.out.print("Priority queue after " +
"removing the first Element: ");
int l = 0;
while (l <= size)
{
System.out.print(H[l] + " ");
l++;
}

remove(0);
System.out.print("Priority queue after " +
"removing the second Element: ");
int l = 0;
while (l <= size)
{
System.out.print(H[l] + " ");
l++;
}

}
}


Add a comment
Know the answer?
Add Answer to:
Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the...
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
  • Java Program

    Write a program in Java to implement the min-priority queue using min-heap data structure. Implement the min-heap data structure using a String array of 5 cells. (Do not use Java in-built PriorityQueue class.) [In a min-heap, the root node and the intermediate node vales are always smaller than their children.] First, take 5 names from the user and insert them in the min-priority queue. Then print the elements of the queue. After that, delete two elements from the queue and...

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

  • solving using C. Use a singly linked list to implement a priority queue with two operations:...

    solving using C. Use a singly linked list to implement a priority queue with two operations: enqueue and dequeue. Each node contains an integer value, a priority, and a pointer to the next node. The priority is a value between 1- 10 (where 10 is the highest priority). When a value is added to the queue, it is added with a value and priority. When a value is removed from the priority queue, the first element with the highest priority...

  • Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap...

    Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either > (in a max heap) or s (in a min heap) the key of C. The node at the "top" of the heap (with no parents) is called the root node. In binary-tree based heap, it...

  • Write a C++ program to implement Queue ADT using singly linked structure. The program includes the...

    Write a C++ program to implement Queue ADT using singly linked structure. The program includes the following: Define the Queue class template in the header file QueueLinked.h. // QueueLinked.h #ifndef QUEUE_H #define QUEUE_H #include <iostream> using namespace std; template <typename T> class Queue { public: // Constructor Queue(); //Desctructor ~Queue(); // Makes the queue to the empty state. void make_empty(); // Checks if the queue is empty. bool empty() const; // Inserts item at the end of the queue. void...

  • Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regu...

    Data Structure Java code Q2 [ 20 pts]. In computer science, a priority queue is an abstract data type which is like a regular queue data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to the order in which they were enqueued A typical priority queue supports following...

  • In Java, create a program implementing the functionalities of a standard queue in a class called Queue3503....

    In Java, create a program implementing the functionalities of a standard queue in a class called Queue3503. You will test the functionalities of the Queue3503 class from the main() method of the Main class. In a queue, first inserted items are removed first and the last items are removed at the end (imagine a line to buy tickets at a ticket counter). Do NOT change your class name from "Main". The Main class should come first in your code. Your filename should be "Main.java". The Queue3503 class will contain:...

  • The tree diagram below depicts a heap being used to implement a priority queue. Enqueue the...

    The tree diagram below depicts a heap being used to implement a priority queue. Enqueue the value 6 onto the priority queue, then execute a dequeue. After those two operations have been completed, what does the underlying array look like? List the elements in the resulting heap in the order in which they appear in the array, from left to right.

  • QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities....

    QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...

  • Implement the class MaxHeapPriorityQueue as a heap with the following operations using python: - ...

    Implement the class MaxHeapPriorityQueue as a heap with the following operations using python: - MaxHeapPriorityQueue() creates a new heap that is empty. It needs no parameters and returns nothing. - parent(index) returns the value of the parent of heap[index]. index is between 1 and the size of the heap. If index<=1 or index>size of the heap, it returns None - leftChild(index) returns the value of the left child of heap[index], returns None if there is no value - rightChild(index) returns...

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