用C++和C#实现优先队列模板

Cpp Version

#include <iostream>
// #include <algorithm>

template<typename T, typename Compare = std::less<T>>
class PriorityQueue {
private:
    T* data;
    std::size_t _size;
    std::size_t _capacity;
    Compare cmp;

    void down(std::size_t parent) {
        while (parent * 2 <= _size) {
            std::size_t t = parent;
            if (cmp(data[parent * 2], data[t]))
                t = parent * 2;
            if (parent * 2 + 1 <= _size && cmp(data[parent * 2 + 1], data[t]))
                t = parent * 2 + 1;
            if (t == parent)
                return;
            std::swap(data[t], data[parent]);
            parent = t;
        }
    }

    void up(std::size_t child) {
        while (child / 2 && cmp(data[child], data[child / 2])) {
            std::swap(data[child], data[child / 2]);
            child /= 2;
        }
    }

    void resize() {
        T* newheap = new T[_capacity * 2];
        std::copy(data + 1, data + _capacity + 1, newheap + 1);
        delete[] data;
        data = newheap;
        _capacity *= 2;
    }

public:
    PriorityQueue(std::size_t n) {
        data = new T[n + 1];
        _capacity = n + 1;
        _size = 0;
    }

    ~PriorityQueue() {
        delete[] data;
    }

    T pop() {
        if (_size == 0) {
            std::cout << "Error! 优先队列为空,无法删除元素\n";
            // return T(); // 返回默认值
            throw std::runtime_error("Error! 优先队列为空,无法删除元素");
        }
        T t = data[1];
        data[1] = data[_size--];
        down(1);
        return t;
    }

    void push(const T& value) {
        if (_size + 1 >= _capacity)
            resize();
        data[++_size] = value;
        up(_size);
    }

    std::size_t size() const {
        return _size;
    }
};

C# Version

using System;


public class PriorityQueue<T>//where T: IComparable<T>
{
    T []heap;
    int _size;
    int _capacity;
    IComparer<T> cmp;
    void up(int son){
        T t = heap[son];
        while(son > 0 && (son - 1) / 2 >= 0){
            if(cmp.Compare(t, heap[(son -1) >> 1]) > 0){
                heap[son] = heap[(son - 1) >> 1];
                son = ((son - 1) >> 1);
            }
            else break;
        }
        heap[son] = t;
    }
    void down(int parent){
        T p = heap[parent];
        while(parent * 2 + 1 <= _size){
            int t = parent * 2 + 1;
            
            if((parent + 1) * 2 <= _size && cmp.Compare(heap[(parent + 1) * 2], heap[t]) > 0){
                t ++;
             }
             if(cmp.Compare(heap[t],p)> 0){
                heap[parent] = heap[t];
                parent = t;
             }
             else
                break;
        }
        heap[parent] = p;
    }
    public int size{
        get{
            return _size+1;
        }

    }
    public PriorityQueue(int n, IComparer<T> comparer){
        heap = new T[n];
        _capacity = n;
        _size = -1;
        cmp = comparer;
    }
    public void Push(T value){
        if(_size + 1 == _capacity){
            Array.Resize(ref heap,(_capacity + 1)*2);
            _capacity = (_capacity +1 )*2;
        }
        heap[++_size] = value;
        up(_size);
    }
    public T Pop(){
        if(_size <= -1){
            throw new InvalidOperationException("优先队列为空");
        }
        T t = heap[0];
        heap[0] = heap[_size--];
        down(0);
        return t;
    }

}

[


C++
C#
  • 作者: Mahiru (联系作者)
  • 发表时间: 2023/06/03 01:24
  • 更新时间: 2023/06/20 21:14
  • 留言 顶部