Skip to content

Commit 96082f8

Browse files
Add files via upload
1 parent 96dde36 commit 96082f8

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

MAX PRIORITY QUEUE.txt

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import java.util.*;
2+
public class PQ {
3+
// Complete this class
4+
private ArrayList<Integer> heap;
5+
6+
public PQ(){
7+
heap = new ArrayList<>();
8+
}
9+
boolean isEmpty() {
10+
if(heap.size()==0)
11+
{
12+
return true;
13+
}
14+
else
15+
{
16+
return false;
17+
}
18+
}
19+
20+
int getSize() {
21+
return heap.size();
22+
}
23+
24+
int getMax() {
25+
if(isEmpty())
26+
{
27+
return Integer.MIN_VALUE;
28+
}
29+
return heap.get(0);
30+
}
31+
32+
void insert(int element) {
33+
heap.add(element);
34+
35+
int childIndex = heap.size()-1;
36+
int parentIndex = (childIndex-1)/2;
37+
while(childIndex>0)
38+
{
39+
if(heap.get(childIndex)>heap.get(parentIndex))
40+
{
41+
int temp = heap.get(childIndex);
42+
heap.set(childIndex,heap.get(parentIndex));
43+
heap.set(parentIndex,temp);
44+
childIndex=parentIndex;
45+
parentIndex= (childIndex-1)/2;
46+
}
47+
else
48+
{
49+
return;
50+
}
51+
}
52+
}
53+
54+
int removeMax() {
55+
if(isEmpty())
56+
{
57+
return Integer.MIN_VALUE;
58+
}
59+
int ans = heap.get(0);
60+
heap.set(0,heap.get(heap.size()-1));
61+
heap.remove(heap.size()-1);
62+
63+
int parentIndex = 0;
64+
int leftIndex = 2*(parentIndex)+1;
65+
int rightIndex = 2*(parentIndex)+2;
66+
int maxIndex = parentIndex;
67+
68+
while(leftIndex<heap.size())
69+
{
70+
if(heap.get(leftIndex)>heap.get(maxIndex))
71+
{
72+
maxIndex = leftIndex;
73+
}
74+
if(rightIndex<heap.size() && heap.get(rightIndex)> heap.get(maxIndex))
75+
{
76+
maxIndex = rightIndex;
77+
}
78+
if(maxIndex==parentIndex)
79+
{
80+
break;
81+
}
82+
int temp = heap.get(maxIndex);
83+
heap.set(maxIndex,heap.get(parentIndex));
84+
heap.set(parentIndex,temp);
85+
parentIndex = maxIndex;
86+
leftIndex = 2*(parentIndex)+1;
87+
rightIndex = 2*(parentIndex)+2;
88+
89+
}
90+
return ans;
91+
}
92+
}

REMOVE MIN.txt

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import java.util.ArrayList;
2+
3+
class PriorityQueueException extends Exception {
4+
5+
}
6+
7+
public class PQ {
8+
9+
private ArrayList<Integer> heap;
10+
11+
public PQ() {
12+
heap = new ArrayList<Integer>();
13+
}
14+
15+
boolean isEmpty(){
16+
return heap.size() == 0;
17+
}
18+
19+
int size(){
20+
return heap.size();
21+
}
22+
23+
int getMin() throws PriorityQueueException{
24+
if(isEmpty()){
25+
// Throw an exception
26+
throw new PriorityQueueException();
27+
}
28+
return heap.get(0);
29+
}
30+
31+
void insert(int element){
32+
heap.add(element);
33+
int childIndex = heap.size() - 1;
34+
int parentIndex = (childIndex - 1) / 2;
35+
36+
while(childIndex > 0){
37+
if(heap.get(childIndex) < heap.get(parentIndex)){
38+
int temp = heap.get(childIndex);
39+
heap.set(childIndex, heap.get(parentIndex));
40+
heap.set(parentIndex, temp);
41+
childIndex = parentIndex;
42+
parentIndex = (childIndex - 1) / 2;
43+
}else{
44+
return;
45+
}
46+
}
47+
}
48+
49+
50+
int removeMin() throws PriorityQueueException{
51+
if(isEmpty()){
52+
// Throw an exception
53+
throw new PriorityQueueException();
54+
}
55+
int temp = heap.get(0);
56+
heap.set(0, heap.get(heap.size() - 1));
57+
heap.remove(heap.size() - 1);
58+
int parentindex = 0;
59+
int minIndex = parentindex;
60+
int leftChildIndex = 1;
61+
int rightChildIndex = 2;
62+
63+
while(leftChildIndex < heap.size()){
64+
65+
if(heap.get(leftChildIndex) < heap.get(minIndex)){
66+
minIndex = leftChildIndex;
67+
}
68+
if(rightChildIndex < heap.size() && heap.get(rightChildIndex) < heap.get(minIndex)){
69+
minIndex = rightChildIndex;
70+
}
71+
if(minIndex == parentindex){
72+
break;
73+
}else{
74+
int temp1 = heap.get(parentindex);
75+
heap.set(parentindex, heap.get(minIndex));
76+
heap.set(minIndex, temp1);
77+
parentindex = minIndex;
78+
leftChildIndex = 2 * parentindex + 1;
79+
rightChildIndex = 2 * parentindex + 2;
80+
}
81+
}
82+
return temp;
83+
84+
}
85+
86+
}

0 commit comments

Comments
 (0)