write priority_queue

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

View File

@ -9,68 +9,115 @@ 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;
* TODO deconstructor void FreeAll(Node *node) {
*/ if (node == nullptr) return;
~priority_queue() {} FreeAll(node->left);
/** FreeAll(node->right);
* TODO Assignment operator delete node;
*/ }
priority_queue &operator=(const priority_queue &other) {} void CopyAll(Node *&dest, Node *src) {
/** if (src == nullptr) return;
* get the top of the queue. dest = new Node(src->value);
* @return a reference of the top element. CopyAll(dest->left, src->left);
* throw container_is_empty if empty() returns true; CopyAll(dest->right, src->right);
*/ }
const T & top() const {
} Node *SkewMerge(Node *a, Node *b) {
/** if (a == nullptr) return b;
* TODO if (b == nullptr) return a;
* push new element to the priority queue. if (comp(a->value, b->value)) std::swap(a, b);
*/ a->right = SkewMerge(a->right, b);
void push(const T &e) { std::swap(a->left, a->right);
return a;
}
} public:
/** priority_queue() : root(nullptr), node_count(0) {}
* TODO priority_queue(const priority_queue &other) {
* delete the top element. root = nullptr;
* throw container_is_empty if empty() returns true; CopyAll(root, other.root);
*/ node_count = other.node_count;
void pop() { }
/**
} * TODO deconstructor
/** */
* return the number of the elements. ~priority_queue() {
*/ FreeAll(root);
size_t size() const { root = nullptr;
}
} /**
/** * TODO Assignment operator
* check if the container has at least an element. */
* @return true if it is empty, false if it has at least an element. priority_queue &operator=(const priority_queue &other) {
*/ if (this == &other) return *this;
bool empty() const { FreeAll(root);
root = nullptr;
} CopyAll(root, other.root);
/** node_count = other.node_count;
* merge two priority_queues with at most O(logn) complexity. return *this;
* clear the other priority_queue. }
*/ /**
void merge(priority_queue &other) { * 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->value;
}
/**
* TODO
* push new element to the priority queue.
*/
void push(const T &e) {
Node *new_node = new Node(e);
root = SkewMerge(root, new_node);
++node_count;
}
/**
* TODO
* delete the top element.
* throw container_is_empty if empty() returns true;
*/
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.
*/
size_t size() const { return node_count; }
/**
* 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 node_count == 0; }
/**
* merge two priority_queues with at most O(logn) complexity.
* clear the other priority_queue.
*/
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