-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpriority-queue.ts
92 lines (76 loc) · 2.29 KB
/
priority-queue.ts
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
/*
A priority queue is a data structure for maintaining a set S of elements, each
with an associated value called a key. A max-priority queue supports the following
operations:
INSERT( x) inserts the element x into the set S, which is equivalent to the operation S =S U {x}
MAXIMUM() returns the element of S with the largest key.
EXTRACT-MAX() removes and returns the element of S with the largest key.
INCREASE-KEY(x, k) increases the value of element x’s key to the new value k,
which is assumed to be at least as large as x’s current key value.
*/
import Heap from "./heap";
class PriorityQueue<T> extends Heap<T> {
constructor() {
super();
}
/**
*returns the element of S with the largest key
*
* @returns
* @memberof PriorityQueue
*/
public maximum() {
return this.getValue(1);
}
/**
*removes and returns the element of S with the largest key
*
* @returns
* @memberof PriorityQueue
*/
public extractMax() {
if (this.getHeight() < 1) {
throw 'heap underflow';
}
const max = this.getValue(1);
this.heapifyRoot();
return max;
}
/**
*helper function for insertion to retain violated properties of heap
*
* @param {number} index
* @memberof PriorityQueue
*/
private heapifyInsertion(index: number) {
while (index > 1 && this.getValue(this.parent(index)) < this.getValue(index)) {
this.replace(this.parent(index), index);
index = this.parent(index);
}
}
/**
*increases the value of element x’s key to the new value k
*
* @param {number} index
* @param {T} value
* @memberof PriorityQueue
*/
public increaseKey(index: number, value: T) {
if (this.getValue(index) > value) {
throw 'new value is smaller than current';
}
this.setValue(index, value);
this.heapifyInsertion(index);
}
/**
*inserts the element x into the set S, which is equivalent to the operation S =S U {x}
*
* @param {T} value
* @memberof PriorityQueue
*/
public insert(value: T) {
this.increaseHeight();
this.setValue(this.getHeight(), value);
this.heapifyInsertion(this.getHeight());
}
}