-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityQueue.java
93 lines (82 loc) · 1.87 KB
/
PriorityQueue.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
package org.gradle;
//Priority Queue Implementation using Binary Heap
public class PriorityQueue {
//Total no of elements a heap can have
private static final int MAX_SIZE=12;
//No of elements in heap
private int size=10;
//root will be stored at index 1
private int heap[]=new int[MAX_SIZE+1];
private static final int INFINITE=999999999;
private int removedItem;
private int getParent(int index){
return index/2;
}
private int getLeftChild(int index){
return 2*index;
}
private int getRightChild(int index){
return 2*index+1;
}cup
private void shiftUp(int index){
while(index>1 && heap[getParent(index)]<heap[index]){
int temp=heap[getParent(index)];
heap[getParent(index)]=heap[index];
heap[index]=temp;
index=getParent(index);
}
}
private void insert(int item){
if(size== MAX_SIZE){
throw new ArrayIndexOutOfBoundsException("Heap is Full");
}else{
size=size+1;
heap[size]=item;
shiftUp(size);
}
}
private int extractMax(){
int maxItem=heap[1];
if(heap[1]==INFINITE){
maxItem=removedItem;
}
heap[1]=heap[size];
size=size-1;
shiftDown(1);
return maxItem;
}
//can be same as ExtractMax
private int remove(int index){
removedItem=heap[index];
heap[index]=INFINITE;
shiftUp(index);
return extractMax();
}
private void changePriority(int index,int newItem){
int oldItem=heap[index];
heap[index]=newItem;
if(newItem>oldItem){
shiftUp(index);
}
if(newItem<oldItem){
shiftDown(index);
}
}
private void shiftDown(int index){
int maxIndex=index;
int left=getLeftChild(index);
if(left<=size && heap[left]>heap[maxIndex]){
maxIndex=left;
}
int right=getRightChild(index);
if(right<=size && heap[right]>heap[maxIndex]){
maxIndex=right;
}
if(index!=maxIndex){
int temp=heap[index];
heap[index]=heap[maxIndex];
heap[maxIndex]=temp;
shiftDown(maxIndex);
}
}
}