write priority_queue

This commit is contained in:
2024-03-10 15:14:41 +00:00
parent 5a9929defd
commit 205219ddf3

View File

@ -9,37 +9,81 @@ namespace sjtu {
/** /**
* a container like std::priority_queue which is a heap internal. * a container like std::priority_queue which is a heap internal.
* Use Skew heap to implement this priority_queue.
*/ */
template<typename T, class Compare = std::less<T>> template <typename T, class Compare = std::less<T>>
class priority_queue { class priority_queue {
public: Compare comp;
/** struct Node {
* TODO constructors T value;
*/ Node *left, *right;
priority_queue() {} Node(const T &v) : value(v), left(nullptr), right(nullptr) {}
priority_queue(const priority_queue &other) {} } * root;
size_t node_count;
void FreeAll(Node *node) {
if (node == nullptr) return;
FreeAll(node->left);
FreeAll(node->right);
delete node;
}
void CopyAll(Node *&dest, Node *src) {
if (src == nullptr) return;
dest = new Node(src->value);
CopyAll(dest->left, src->left);
CopyAll(dest->right, src->right);
}
Node *SkewMerge(Node *a, Node *b) {
if (a == nullptr) return b;
if (b == nullptr) return a;
if (comp(a->value, b->value)) std::swap(a, b);
a->right = SkewMerge(a->right, b);
std::swap(a->left, a->right);
return a;
}
public:
priority_queue() : root(nullptr), node_count(0) {}
priority_queue(const priority_queue &other) {
root = nullptr;
CopyAll(root, other.root);
node_count = other.node_count;
}
/** /**
* TODO deconstructor * TODO deconstructor
*/ */
~priority_queue() {} ~priority_queue() {
FreeAll(root);
root = nullptr;
}
/** /**
* TODO Assignment operator * TODO Assignment operator
*/ */
priority_queue &operator=(const priority_queue &other) {} priority_queue &operator=(const priority_queue &other) {
if (this == &other) return *this;
FreeAll(root);
root = nullptr;
CopyAll(root, other.root);
node_count = other.node_count;
return *this;
}
/** /**
* get the top of the queue. * get the top of the queue.
* @return a reference of the top element. * @return a reference of the top element.
* throw container_is_empty if empty() returns true; * throw container_is_empty if empty() returns true;
*/ */
const T & top() const { const T &top() const {
if (empty()) throw container_is_empty();
return root->value;
} }
/** /**
* TODO * TODO
* push new element to the priority queue. * push new element to the priority queue.
*/ */
void push(const T &e) { void push(const T &e) {
Node *new_node = new Node(e);
root = SkewMerge(root, new_node);
++node_count;
} }
/** /**
* TODO * TODO
@ -47,30 +91,33 @@ public:
* throw container_is_empty if empty() returns true; * throw container_is_empty if empty() returns true;
*/ */
void pop() { void pop() {
if (empty()) throw container_is_empty();
Node *old_root = root;
root = SkewMerge(root->left, root->right);
delete old_root;
--node_count;
} }
/** /**
* return the number of the elements. * return the number of the elements.
*/ */
size_t size() const { size_t size() const { return node_count; }
}
/** /**
* check if the container has at least an element. * check if the container has at least an element.
* @return true if it is empty, false if it has at least an element. * @return true if it is empty, false if it has at least an element.
*/ */
bool empty() const { bool empty() const { return node_count == 0; }
}
/** /**
* merge two priority_queues with at most O(logn) complexity. * merge two priority_queues with at most O(logn) complexity.
* clear the other priority_queue. * clear the other priority_queue.
*/ */
void merge(priority_queue &other) { void merge(priority_queue &other) {
root = SkewMerge(root, other.root);
node_count += other.node_count;
other.root = nullptr;
other.node_count = 0;
} }
}; };
} } // namespace sjtu
#endif #endif