1+
2+ /* Minimum Priority Queue
3+ * It is a part of heap data structure
4+ * A heap is a specific tree based data structure
5+ * in which all the nodes of tree are in a specific order.
6+ * that is the children are arranged in some
7+ * respect of their parents, can either be greater
8+ * or less than the parent. This makes it a min priority queue
9+ * or max priority queue.
10+ */
11+
12+ // Functions: insert, delete, peek, isEmpty, print, heapSort, sink
13+
14+ public class MinPriorityQueue {
15+ private int [] heap ;
16+ private int capacity ;
17+ private int size ;
18+
19+ // calss the constructor and initializes the capacity
20+ MinPriorityQueue (int c ) {
21+ this .capacity = c ;
22+ this .size = 0 ;
23+ this .heap = new int [c + 1 ];
24+ }
25+
26+ // inserts the key at the end and rearranges it
27+ // so that the binary heap is in appropriate order
28+ public void insert (int key ) {
29+ if (this .isFull ())
30+ return ;
31+ this .heap [this .size + 1 ] = key ;
32+ int k = this .size + 1 ;
33+ while (k > 1 ) {
34+ if (this .heap [k ] < this .heap [k / 2 ]) {
35+ int temp = this .heap [k ];
36+ this .heap [k ] = this .heap [k / 2 ];
37+ this .heap [k / 2 ] = temp ;
38+ }
39+ k = k / 2 ;
40+ }
41+ this .size ++;
42+ }
43+
44+ // returns the highest priority value
45+ public int peek () {
46+ return this .heap [1 ];
47+ }
48+
49+ // returns boolean value whether the heap is empty or not
50+ public boolean isEmpty () {
51+ if (0 == this .size )
52+ return true ;
53+ return false ;
54+ }
55+
56+ // returns boolean value whether the heap is full or not
57+ public boolean isFull () {
58+ if (this .size == this .capacity )
59+ return true ;
60+ return false ;
61+ }
62+
63+ // prints the heap
64+ public void print () {
65+ for (int i = 1 ; i <= this .capacity ; i ++)
66+ System .out .print (this .heap [i ] + " " );
67+ System .out .println ();
68+ }
69+
70+ // heap sorting can be done by performing
71+ // delete function to the number of times of the size of the heap
72+ // it returns reverse sort because it is a min priority queue
73+ public void heapSort () {
74+ for (int i = 1 ; i < this .capacity ; i ++)
75+ this .delete ();
76+ }
77+
78+ // this function reorders the heap after every delete function
79+ private void sink () {
80+ int k = 1 ;
81+ while (2 * k <= this .size || 2 * k + 1 <= this .size ) {
82+ int minIndex ;
83+ if (this .heap [2 * k ] >= this .heap [k ]) {
84+ if (2 * k + 1 <= this .size && this .heap [2 * k + 1 ] >= this .heap [k ]) {
85+ break ;
86+ } else if (2 * k + 1 > this .size ) {
87+ break ;
88+ }
89+ }
90+ if (2 * k + 1 > this .size ) {
91+ minIndex = this .heap [2 * k ] < this .heap [k ] ? 2 * k : k ;
92+ } else {
93+ if (this .heap [k ] > this .heap [2 * k ] || this .heap [k ] > this .heap [2 * k + 1 ]) {
94+ minIndex = this .heap [2 * k ] < this .heap [2 * k + 1 ] ? 2 * k : 2 * k + 1 ;
95+ } else {
96+ minIndex = k ;
97+ }
98+ }
99+ int temp = this .heap [k ];
100+ this .heap [k ] = this .heap [minIndex ];
101+ this .heap [minIndex ] = temp ;
102+ k = minIndex ;
103+ }
104+ }
105+
106+ // deletes the highest priority value from the heap
107+ public int delete () {
108+ int min = this .heap [1 ];
109+ this .heap [1 ] = this .heap [this .size ];
110+ this .heap [this .size ] = min ;
111+ this .size --;
112+ this .sink ();
113+ return min ;
114+ }
115+
116+ public static void main (String [] args ) {
117+ // testing
118+ MinPriorityQueue q = new MinPriorityQueue (8 );
119+ q .insert (5 );
120+ q .insert (2 );
121+ q .insert (4 );
122+ q .insert (1 );
123+ q .insert (7 );
124+ q .insert (6 );
125+ q .insert (3 );
126+ q .insert (8 );
127+ q .print (); // [ 1, 2, 3, 5, 7, 6, 4, 8 ]
128+ q .heapSort ();
129+ q .print (); // [ 8, 7, 6, 5, 4, 3, 2, 1 ]
130+ }
131+ }
0 commit comments