溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

【數(shù)據(jù)結(jié)構(gòu)】用模版實(shí)現(xiàn)大小堆、實(shí)現(xiàn)優(yōu)先級(jí)隊(duì)列,以及堆排序

發(fā)布時(shí)間:2020-07-26 12:23:06 來源:網(wǎng)絡(luò) 閱讀:722 作者:安下 欄目:編程語言

    一、用模版實(shí)現(xiàn)大小堆


    如果不用模版的話,寫大小堆,就需要分別實(shí)現(xiàn)兩次,但是應(yīng)用模版的話問題就簡(jiǎn)單多了,我們只需要實(shí)現(xiàn)兩個(gè)仿函數(shù),Greater和Less就行了,仿函數(shù)就是用類實(shí)現(xiàn)一個(gè)()的重載就實(shí)現(xiàn)了仿函數(shù)。這個(gè)看下代碼就能理解了。再設(shè)計(jì)參數(shù)的時(shí)候,需要把模版設(shè)計(jì)成模版的模版參數(shù),因?yàn)橐獙?shí)現(xiàn)大小堆嘛!當(dāng)我們實(shí)現(xiàn)好一個(gè)大堆或者小隊(duì)的邏輯后只需要用模版接收的Greater或Less類定義一個(gè)變量,就能實(shí)現(xiàn)通用功能了。


template<typename T>
struct Less
{
    bool operator()(const T& l, const T& r)
    {
        return l < r;
    }
};

template<class T>
struct Greater
{
    bool operator()(const T& l, const T& r)
    {
        return l>r;
    }
};

template <class T,template<class> class compare = less>
class Heap
{
public:
    Heap()
    {}

    Heap(T* a,size_t size)
    {
        size_t index = 0;
        while (index < size)
        {
            _a.push_back(a[index]);
            index++;
        }

        for (int i = (_a.size() - 2) / 2; i >= 0; i--)
            _AdjustDown(i);
    }

    void push(const T& x)
    {
        _a.push_back(x);
        _AdjustUp(_a.size() -1);
    }

    void pop()
    {
        size_t size = _a.size();
        assert(size > 0);
        swap(_a[0], _a[size - 1]);
        _a.pop_back();
        size = _a.size();
        _AdjustDown(0);
    }

    size_t top()
    {
        assert(!_a.empty());

        return _a[0];
    }

    bool empty()
    {
        return _a.size() == 0;
    }

    size_t Size()
    {
        return _a.size();
    }

    void Print()
    {
        for (int i = 0; i < _a.size(); i++)
        {
            cout << _a[i] << " ";
        }
        cout << endl;
    }

protected:
    void _AdjustUp(int child)
    {
        int parent = (child - 1) / 2;
        compare<T> com;  //如果是大堆傳過來就是用大堆的邏輯,小堆就實(shí)現(xiàn)小堆的邏輯
        while (child > 0)
        {
            //找出孩子中的最大孩子
            if (com(_a[child] , _a[parent]))
            {
                swap(_a[child], _a[parent]);
                child = parent;
                parent = (child - 1) / 2;
            }
            else
            {
                break;
            }
        }

    }

    void _AdjustDown(size_t parent)
    {
        size_t child = 2 * parent + 1;
        compare<T> com; //如果是大堆傳過來就是用大堆的邏輯,小堆就實(shí)現(xiàn)小堆的邏輯
        while (child < _a.size())
        {
            //找出孩子中的最大孩子
            if (child + 1 < _a.size() && com(_a[child+1] ,_a[child]))
            {
                ++child;
            }
            //把
            if (com(_a[child] , _a[parent]))
            {
                swap(_a[parent], _a[child]);
                parent = child;
                child = child * 2 + 1;
            }
            else
            {
                break;
            }
        }

    }
protected:
    vector<T> _a;
};


   二、用模版實(shí)現(xiàn)優(yōu)先級(jí)隊(duì)列


前面實(shí)現(xiàn)了大小堆,這里我們可以使用適配器,直接調(diào)用大小堆,來實(shí)現(xiàn)優(yōu)先級(jí)隊(duì)列。


template<class T, template<class> class compare = Less>
class priorityQueue
{
private:
    Heap<T, compare> _hp; 
public:
    void push(const T& x)
    {
        _hp.push(x);
    }

    void pop()
    {
        _hp.pop();
    }

    T& Top()
    {
        return _hp.top();
    }

    void Print()
    {
        _hp.Print();
    }

};


    三、堆排序的實(shí)現(xiàn)


    堆排序的實(shí)現(xiàn)簡(jiǎn)單思路,(升序)先構(gòu)造出來一個(gè)大堆,調(diào)整堆后,將堆頭和最后一個(gè)數(shù)據(jù)交換,最大值就換到了數(shù)組的最后,然后在調(diào)整堆,但是size需要減少1,因?yàn)樽畲蟮囊呀?jīng)調(diào)整到最后,如果再加上它調(diào)整又會(huì)回到堆頭。

int*& HeapSort(int* a, size_t size)
{
    for (int i = (size - 2) / 2; i >= 0; i--)
    {
        _AdjustDown(a, size, i);
    }

    for (int i = 0; i < size; i++)
    {
        swap(a[0], a[size - i - 1]);
        _AdjustDown(a, size - i - 1, 0);
    }

    return a;
}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI