-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathpriority_queue.h
More file actions
56 lines (49 loc) · 1.56 KB
/
Copy pathpriority_queue.h
File metadata and controls
56 lines (49 loc) · 1.56 KB
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
//
// priority_queue.h
// AlgorithmMuseum
//
// Created by Xiaohang Su on 1/29/17.
// Copyright © 2017 Xiaohang Su. All rights reserved.
//
#ifndef priority_queue_h
#define priority_queue_h
#include <iostream>
#include <vector>
#include "Heapsort.h"
template <typename comparable> class priority_queue : public Heapsort<comparable> {
public:
comparable heap_maximum(const std::vector<comparable> &A) {
return A[0];
}
comparable heap_extract_max(std::vector<comparable> &A) { int heapSize = A.size();
if (heapSize < 1) {
std::cout << "Error: Heap underflow!" << std::endl;
}
int maxNum = A[0];
A[0] = A[heapSize - 1];
heapSize = heapSize - 1;
A.pop_back();
this->max_heapify(A, 0);
return maxNum;
}
void heap_increase_key(std::vector<comparable> &A, int i, const comparable& key) {
if (key < A[i]) {
std::cout << "Error: New key " << key << "is smaller than current key " << A[i] << std::endl;
}
A[i] = key;
while (i > 0 && A[this->PARENT(i)] < A[i]) {
this->swap(A[i], A[this->PARENT(i)]);
i = this->PARENT(i);
}
}
// pass min value of comparable type
// use for compare
void max_heap_insert(std::vector<comparable> &A, const comparable& key, const comparable& COMPARABLE_MIN) {
int heapSize = A.size();
heapSize++;
A.push_back(COMPARABLE_MIN);
heap_increase_key(A, heapSize - 1, key);
}
private:
};
#endif /* priority_queue_h */