forked from reeucq/advanced-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinHeapTree.java
89 lines (78 loc) · 2.18 KB
/
MinHeapTree.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
import java.util.Scanner;
public class MinHeapTree {
/**
* Data Members
*/
int[] heap; // min heap tree to implement the priority queue
int size; // size of the priority queue
/**
* Constructor
* @param size: The size of the priority queue
*/
public MinHeapTree(int size) {
Scanner sc = new Scanner(System.in);
heap = new int[size];
this.size = size;
System.out.println("Enter elements: ");
for(int i = 0; i < size; i++) {
System.out.print("Element " + (i+1) + ": ");
heap[i] = sc.nextInt();
}
// build min heap
HeapTree.heapifyMin(heap, size);
}
/**
* insert(int x)
* This function is used to insert an element into the priority queue.
*
* Algorithm:
* 1. Insert the element at the end of the heap.
* 2. Adjust the heap.
*
* @param x the element to be inserted
*/
public void insert(int x) {
if(size == heap.length) {
System.out.println("Priority Queue is full");
return;
}
HeapTree.insertMin(heap, ++size, x);
}
/**
* delete()
* This function is used to delete an element from the priority queue.
* @return the element with the highest priority
*/
public int delete() {
if(isEmpty()) {
System.out.println("Priority Queue is empty");
return -1;
}
int min = HeapTree.deleteMin(heap, size);
size--;
return min;
}
/**
* 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 size == 0;
}
/**
* display()
* This function is used to display the elements of the priority queue.
*/
public void display() {
if(isEmpty()) {
System.out.println("Priority Queue is empty");
return;
}
System.out.print("Priority Queue: ");
for(int i = 0; i < size; i++) {
System.out.print(heap[i] + " ");
}
System.out.println();
}
}