0% found this document useful (0 votes)
6 views5 pages

New Microsoft Word Document

The document contains a Java implementation of a MaxHeap data structure and a PriorityQueue that utilizes it. The MaxHeap allows insertion of values, removal of the maximum value, and keeps track of the number of swaps made during operations. The PriorityQueue provides a user interface for enqueueing, dequeueing, displaying the heap, and showing the number of swaps, all through a command-line menu.

Uploaded by

muhammad.71932
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

New Microsoft Word Document

The document contains a Java implementation of a MaxHeap data structure and a PriorityQueue that utilizes it. The MaxHeap allows insertion of values, removal of the maximum value, and keeps track of the number of swaps made during operations. The PriorityQueue provides a user interface for enqueueing, dequeueing, displaying the heap, and showing the number of swaps, all through a command-line menu.

Uploaded by

muhammad.71932
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

import java.util.

Scanner;

class MaxHeap {

private int[] heapArray;

private int maxSize;

private int size;

private int swapCount; // Counter for swaps

public MaxHeap(int maxSize) {

this.maxSize = maxSize;

this.size = 0;

this.swapCount = 0; // Initialize swap count

heapArray = new int[maxSize];

public void insert(int value) {

if (size == maxSize) {

System.out.println("Heap is full. Cannot insert " + value);

return;

heapArray[size] = value;

int current = size;

size++;

while (current > 0 && heapArray[current] > heapArray[(current - 1) /


2]) {

swap(current, (current - 1) / 2);

current = (current - 1) / 2;

public int removeMax() {


if (size == 0) {

System.out.println("Heap is empty. Cannot remove.");

return -1;

int root = heapArray[0];

heapArray[0] = heapArray[size - 1];

size--;

heapifyDown(0);

return root;

private void heapifyDown(int i) {

int largest = i;

int left = 2 * i + 1;

int right = 2 * i + 2;

if (left < size && heapArray[left] > heapArray[largest]) {

largest = left;

if (right < size && heapArray[right] > heapArray[largest]) {

largest = right;

if (largest != i) {

swap(i, largest);

heapifyDown(largest);

private void swap(int i, int j) {


int temp = heapArray[i];

heapArray[i] = heapArray[j];

heapArray[j] = temp;

swapCount++; // Increment swap count

public void displayHeap() {

if (size == 0) {

System.out.println("Heap is empty.");

return;

System.out.print("Current values in the heap: ");

for (int i = 0; i < size; i++) {

System.out.print(heapArray[i] + " ");

System.out.println();

public int getSwapCount() {

return swapCount; // Return the number of swaps

class PriorityQueue {

private MaxHeap maxHeap;

public PriorityQueue(int maxSize) {

maxHeap = new MaxHeap(maxSize);

public void enqueue(int value) {


maxHeap.insert(value);

public int dequeue() {

return maxHeap.removeMax();

public void display() {

maxHeap.displayHeap();

public int getSwapCount() {

return maxHeap.getSwapCount(); // Get the number of swaps from the


heap

public class Main {

public static void main(String[] args) {

PriorityQueue priorityQueue = new PriorityQueue(10);

Scanner scanner = new Scanner(System.in);

while (true) {

System.out.println("1. Enqueue");

System.out.println("2. Dequeue");

System.out.println("3. Show remaining values");

System.out.println("4. Show number of swaps");

System.out.println("5. Exit");

System.out.print("Choose an option: ");

int option = scanner.nextInt();

switch (option) {
case 1:

System.out.print("Enter a value: ");

int value = scanner.nextInt();

priorityQueue.enqueue(value);

break;

case 2:

int dequeuedValue = priorityQueue.dequeue();

if (dequeuedValue != -1) {

System.out.println("Dequeued value: " +


dequeuedValue);

break;

case 3:

priorityQueue.display();

break;

case 4:

System.out.println("Number of swaps occurred: " +


priorityQueue.getSwapCount());

break;

case 5:

return; // Exit the loop

default:

System.out.println("Invalid option. Please choose a valid


option.");

You might also like