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

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

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

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

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

  • Write a java class, MaxHeap, to implement a max-heap of values of type double. Use an...

    Write a java class, MaxHeap, to implement a max-heap of values of type double. Use an array and be prepared to grow the array. The array implementation will probably be more efficient. Next, write three java sorting methods: a) One should be the heapsort algorithm. b) the second should sort the array by inserting all the elements from the array into a heap defined by the MaxHeap class, and then removing all the items from the heap and putting them...

  • (Data Strcture) Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a...

    (Data Strcture) Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a Singly Linked-List and Implementing a Priority Queue Tasks/Assignments(s) ■ Write your own program to implement priority queue using the Linked-List and implement the Enqueue, Dequeue and Display methods. implement the Merge methods in your main program that receive Q1 and Q2 and merge the two queues into one queue. Public static PriorityQueue MergeQueue(PriorityQueue Q1,PriorityQueue Q2) Write main program to test priority queue class and...

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
ADVERTISEMENT