-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinHeap.h
79 lines (73 loc) · 1.83 KB
/
MinHeap.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
#ifndef MIN_HEAP_H
#define MIN_HEAP_H
#include <utility>
#include <vector>
template<typename TKey, typename TValue>
class MinHeap
{
private:
// array for the elements which should be heap-sorted
std::vector<std::pair<TKey, TValue>> m_vec;
public:
MinHeap() {}
/// <summary>
/// insert key-value pair
/// </summary>
///
/// <param name="key">
/// the key that is used for sorting
/// </param>
///
/// <param name="value">
/// the value that is managed in this heap
/// </param>
void Push(TKey key, TValue value);
/// <summary>
/// remove the minimum element
/// </summary>
void Pop();
/// <summary>
/// get the minimum element
/// </summary>
///
/// <returns>
/// the minimum element
/// </returns>
std::pair<TKey, TValue> Top();
/// <summary>
/// get the key-value pair which the value is the same as the target
/// </summary>
///
/// <returns>
/// the key-value pair which the value is the same as the target
/// </returns>
std::pair<TKey, TValue> Get(TValue target);
/// <summary>
/// check whether this heap is empty or not
/// </summary>
///
/// <returns>
/// true if this heap is empty
/// </returns>
bool IsEmpty();
/// <summary>
/// change the key of the node which the value is the target.<para/>
/// In general, the newKey should be smaller than the old key.<para/>
/// </summary>
///
/// <parma name="target">
/// the target to change the key
/// </param>
///
/// <param name="newKey">
/// new key for the target
/// </param>
void DecKey(TValue target, TKey newKey);
private:
/// <summary>
/// heap-sort, heapify.<para/>
/// this function can be called recursively
/// </summary>
void Heapify(int index);
};
#endif