-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_queue.h
More file actions
164 lines (157 loc) · 4.49 KB
/
priority_queue.h
File metadata and controls
164 lines (157 loc) · 4.49 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#ifndef SJTU_PRIORITY_QUEUE_HPP
#define SJTU_PRIORITY_QUEUE_HPP
#include <cstddef>
#include <functional>
#include "exceptions.h"
namespace sjtu
{
/**
* a container like std::priority_queue which is a heap internal.
*/
template<typename T, class Compare = std::less<T>>
class priority_queue {
public:
struct Node{
T element;
Node *left;
Node *right;
int npl;
int cnt;
Node() : left(nullptr), right(nullptr), npl(-1), cnt(1) {}
explicit Node(T e, Node *l = nullptr, Node *r = nullptr, int n = 0, int c = 1) : element(e), left(l), right(r), npl(n), cnt(c){}
};
Node *root;
Compare helper;
/**
* TODO constructors
*/
priority_queue() : root(nullptr) {}
Node *copy(Node *rhs)
{
if (rhs == nullptr) return nullptr;
return new Node(rhs->element, copy(rhs->left), copy(rhs->right), rhs->npl, rhs->cnt);
}
priority_queue(const priority_queue &other)
{
root = copy(other.root);
}
/**
* TODO destructor
*/
void clear(Node *t)
{
if (t == nullptr) return;
clear(t->left);
clear(t->right);
delete t;
t = nullptr;//attention!!!
}
~priority_queue()
{
clear(root);
}
/**
* TODO Assignment operator
*/
priority_queue &operator=(const priority_queue &other)
{
if (this == &other) return *this;
clear(root);
root = copy(other.root);
return *this;
}
/**
* get the top of the queue.
* @return a reference of the top element.
* throw container_is_empty if empty() returns true;
*/
const T & top() const
{
if (empty()) throw container_is_empty();
return root->element;
}
/**
* TODO
* push new element to the priority queue.
*/
void push(const T &e)
{
Node *tmp = new Node(e);
try
{
root = merge(tmp, root);
}
catch(runtime_error)
{
delete tmp;
throw runtime_error();
}
}
/**
* TODO
* delete the top element.
* throw container_is_empty if empty() returns true;
*/
void pop()
{
if (empty()) throw container_is_empty();
Node *tmp = root;
root = merge(root->left, root->right);
delete tmp;
}
/**
* return the number of the elements.
*/
size_t size() const
{
if (root == nullptr) return 0;
return root->cnt;
}
/**
* check if the container has at least an element.
* @return true if it is empty, false if it has at least an element.
*/
bool empty() const
{
return root == nullptr;
}
/**
* merge two priority_queues with at least O(logn) complexity.
* clear the other priority_queue.
*/
void swapSon(Node *t)
{
Node *tmp = t->left;
t->left = t->right;
t->right = tmp;
}
Node *merge(Node *t1, Node *t2)
{
if (t1 == nullptr) return t2;//递归出口
if (t2 == nullptr) return t1;
// if (t1->element < t2->element) return merge1(t1, t2);
if (helper(t1->element, t2->element)) return merge1(t2, t1);
return merge1(t1, t2);
}
//merge t2 to the right of t1
Node *merge1(Node *t1, Node *t2)
{
t1->cnt += t2->cnt;//proved true
if (t1->left == nullptr) t1->left = t2;
else
{
t1->right = merge(t1->right, t2);
if (t1->left->npl < t1->right->npl) swapSon(t1);
t1->npl = t1->right->npl + 1;
}
return t1;
}
void merge(priority_queue &other)
{
if (this == &other) return;
root = merge(root, other.root);
other.root = nullptr;
}
};
}
#endif