This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority_queue.h
96 lines (83 loc) · 2.03 KB
/
priority_queue.h
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
94
95
96
//
// Created by Limpol on 2022/9/24.
//
#pragma once
#include <vector>
template<typename T>
class maxHeap
{
public:
maxHeap() : currentSize(0)
{}
explicit maxHeap(int capacity) : array(capacity + 1), currentSize(0)
{}
explicit maxHeap(const std::initializer_list<T> &items) : array(items.size() + 10), currentSize(items.size())
{
array[0] = 0;
for (auto it=items.begin();it!=items.end();++it)
this->insert(*it);
}
[[nodiscard]] bool isEmpty() const
{ return currentSize == 0; }
const T &findMax() const
{ return array[1]; }
void insert(const T &x)
{
if(currentSize==array.size())
array.resize(array.size()*2);
int pointer = ++currentSize;
while(pointer>1&&array[pointer/2]<x)
{
array[pointer]=array[pointer/2];
pointer/=2;
}
array[pointer]=x;
}
void pop()
{
if (isEmpty())
return;
array[1] = array[currentSize--];
percolateDown(1);
}
void deleteMax(T &maxItem)
{
if (isEmpty())
return;
maxItem = array[1];
array[1] = array[currentSize--];
percolateDown(1);
}
void makeEmpty()
{ currentSize = 0; }
void print(std::ostream &out=std::cout) const
{
for (int i = 1; i <= currentSize; ++i)
out << array[i] << " ";
out << std::endl;
}
private:
int currentSize;
std::vector<T> array;
void buildHeap()
{
for (int i = currentSize / 2; i > 0; --i)
percolateDown(i);
}
void percolateDown(int hole)
{
int child;
T tmp = array[hole];
for (; hole * 2 <= currentSize; hole = child)
{
child = hole * 2;
if (child != currentSize && array[child + 1] > array[child])
++child;
if (array[child] > tmp)
array[hole] = array[child];
else
break;
}
array[hole] = tmp;
}
};