forked from reeucq/advanced-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityQueueMax.java
116 lines (105 loc) · 3.86 KB
/
PriorityQueueMax.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.util.Scanner;
/**
* A priority queue is a data structure that stores elements in a way that they can be retrieved in a sorted order.
* The elements are stored in the queue based on their priority.
* The element with the highest priority is retrieved first.
* The priority queue is implemented using a heap data structure.
* The heap is a binary tree that satisfies the heap property.
* The heap property states that the parent node is greater than or equal to its children in a max heap and less than or equal to its children in a min heap.
* The priority queue can be implemented using a max heap or a min heap.
* The max heap is used to implement a priority queue that retrieves the element with the highest priority first.
* The min heap is used to implement a priority queue that retrieves the element with the lowest priority first.
* The priority queue supports the following operations:
* 1. Insert: This operation is used to insert an element into the priority queue.
* 2. Delete: This operation is used to delete an element from the priority queue.
* 3. IsEmpty: This operation is used to check if the priority queue is empty.
*/
public class PriorityQueueMax {
/**
* Data Members
*/
MaxHeapTree heap; // max heap tree to implement the priority queue
/**
* Constructor
* @param size: The size of the priority queue
*/
public PriorityQueueMax(int size) {
heap = new MaxHeapTree(size);
}
/**
* insert(int x)
* This function is used to insert an element into the priority queue.
* @param x the element to be inserted
*/
public void insert(int x) {
heap.insert(x);
}
/**
* delete()
* This function is used to delete an element from the priority queue.
* @return the element with the highest priority
*/
public int delete() {
return heap.delete();
}
/**
* isEmpty()
* This function is used to check if the priority queue is empty.
* @return true if the priority queue is empty, false otherwise
*/
public boolean isEmpty() {
return heap.isEmpty();
}
/**
* display()
* This function is used to display the elements of the priority queue.
*/
public void display() {
heap.display();
}
/**
* Main Method
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the priority queue: ");
int size = sc.nextInt();
PriorityQueueMax pq = new PriorityQueueMax(size);
while(true) {
System.out.println("1. Insert");
System.out.println("2. Delete");
System.out.println("3. IsEmpty");
System.out.println("4. View");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch(choice) {
case 1:
System.out.print("Enter the element to be inserted: ");
int x = sc.nextInt();
pq.insert(x);
break;
case 2:
int max = pq.delete();
if(max != -1)
System.out.println("Element deleted: " + max);
break;
case 3:
if(pq.isEmpty())
System.out.println("Priority Queue is empty");
else
System.out.println("Priority Queue is not empty");
break;
case 4:
pq.display();
break;
case 5:
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}
}