仿STL中的堆演算法的一個實現

來源:互聯網
上載者:User

#include "HeapAlgorithm.h"
#include <algorithm>
#include <iostream>

using namespace std;

// push_heap為向堆中添加一個新的元素, 調用這個演算法的前提是[First, Last)之間的元素滿足堆的條件
// 新加入的元素為Last
void    push_heap(int* pFirst, int* pLast);

// pop_heap為從堆中刪除一個元素, 調用這個演算法的前提是[First, Last)之間的元素滿足堆的條件
// 被刪除的元素被放置到Last - 1位置,由於這裡是max-heap,所以被刪除的元素是這個序列中最大的元素
void    pop_heap(int* pFirst, int* pLast);

// make_heap將序列[First, Last)中的元素按照堆的性質進行重組
void    make_heap(int* pFirst, int* pLast);

// 對堆進行排序, 調用這個函數可以成功排序的前提是[pFirst, pLast)中的元素符合堆的性質
void    sort_heap(int* pFirst, int* pLast);

// 判斷一個序列[First, Last)是否滿足堆的條件,是就返回1,否則返回0
char    is_heap(int* pFirst, int* pLast);

void    test_heap_algo(int *pArray, int nLength);
void    test_heap_algo_in_stl(int *pArray, int nLength);
void    display_array(int *pArray, int nLength);

int main()
{
    int Array[] =  {0, 1, 2, 6, 4, 3, 9, 8, 7, 5, 11};
    int Array2[] = {0, 1, 2, 6, 4, 3, 9, 8, 7, 5, 11};

    test_heap_algo(Array, sizeof(Array) / sizeof(int));
    test_heap_algo_in_stl(Array2, sizeof(Array2) / sizeof(int));

    return 0;
}

// 靜態函數, 用於根據堆的性質調整堆
static void adjust_heap(int *pFirst, int nHoleIndex, int nLen, int nValue);

// push_heap為向堆中添加一個新的元素, 調用這個演算法的前提是[First, Last)之間的元素滿足堆的條件
// 新加入的元素為Last
void push_heap(int* pFirst, int* pLast)
{
    int nTopIndex, nHoleIndex, nParentIndex;
    int nValue;

    if (NULL == pFirst || NULL == pLast)
    {
        perror("NULL pointer!/n");
        return;
    }

    nTopIndex = 0;
    nHoleIndex = (int)(pLast - pFirst - 1);
    nParentIndex = (nHoleIndex - 1) / 2;
    nValue = *(pLast - 1);
    // 如果需要插入的節點值比父節點大, 上溯繼續尋找
    while (nHoleIndex > nTopIndex && pFirst[nParentIndex] < nValue)
    {
        pFirst[nHoleIndex] = pFirst[nParentIndex];
        nHoleIndex = nParentIndex;
        nParentIndex = (nHoleIndex - 1) / 2;
    }
    pFirst[nHoleIndex] = nValue;
}

// pop_heap為從堆中刪除一個元素, 調用這個演算法的前提是[First, Last)之間的元素滿足堆的條件
// 被刪除的元素被放置到Last - 1位置,由於這裡是max-heap,所以被刪除的元素是這個序列中最大的元素
void pop_heap(int* pFirst, int* pLast)
{
    int nValue;

    if (NULL == pFirst || NULL == pLast)
    {
        perror("NULL pointer!/n");
        return;
    }

    nValue = *(pLast - 1);
    *(pLast - 1) = *pFirst;
    adjust_heap(pFirst, 0, (int)(pLast - pFirst - 1), nValue);
}

// make_heap將序列[First, Last)中的元素按照堆的性質進行重組
void make_heap(int* pFirst, int* pLast)
{
    int nLen, nParentIndex;

    if (NULL == pFirst || NULL == pLast)
    {
        perror("NULL pointer!/n");
        return;
    }

    if (1 > (nLen = (int)(pLast - pFirst)))
        return;

    nParentIndex = (nLen - 1) / 2;

    while (true)
    {
        // 對父節點進行調整, 把父節點的值調整到合適的位置
        adjust_heap(pFirst, nParentIndex, nLen, pFirst[nParentIndex]);
        if (0 == nParentIndex)
            return;
        nParentIndex--;
    }
}

// 對堆進行排序, 調用這個函數可以成功排序的前提是[pFirst, pLast)中的元素符合堆的性質
void sort_heap(int* pFirst, int* pLast)
{
    // 調用pop_heap函數, 不斷的把當前序列中最大的元素放在序列的最後
    while(pLast - pFirst > 1)
        pop_heap(pFirst, pLast--);
}

// 判斷一個序列[First, Last)是否滿足堆的條件,是就返回1,否則返回0
char is_heap(int* pFirst, int* pLast)
{
    int nLen, nParentIndex, nChildIndex;

    if (NULL == pFirst || NULL == pLast)
    {
        perror("NULL pointer!/n");
        return 0;
    }

    nLen = (int)(pLast - pFirst);
    nParentIndex = 0;
    for (nChildIndex = 1; nChildIndex < nLen; ++nChildIndex)
    {
        if (pFirst[nParentIndex] < pFirst[nChildIndex])
            return 0;

        // 當nChildIndex是偶數時, 那麼父節點已經和它的兩個子節點進行過比較了
        // 將父節點遞增1
        if ((nChildIndex & 1) == 0)
            ++nParentIndex;
    }

    return 1;
}

// 一個靜態函數僅供adjust_heap調用以證實JJHOU的結論
static void push_heap(int *pFirst, int nHoleIndex, int nTopIndex, int nValue)
{
    int nParentIndex;

    nParentIndex = (nHoleIndex - 1) / 2;
    while (nHoleIndex > nTopIndex && pFirst[nParentIndex] < nValue)
    {
        pFirst[nHoleIndex] = pFirst[nParentIndex];
        nHoleIndex = nParentIndex;
        nParentIndex = (nHoleIndex - 1) / 2;
    }
    pFirst[nHoleIndex] = nValue;
}

// 對堆進行調整, 其中nHoleIndex是目前堆中有空洞的節點索引, nLen是待調整的序列長度
// nValue是需要安插進入堆中的值
static void adjust_heap(int *pFirst, int nHoleIndex, int nLen, int nValue)
{
    int nTopIndex, nSecondChildIndex;

    nTopIndex = nHoleIndex;
    nSecondChildIndex = 2 * nTopIndex + 2;
    while (nSecondChildIndex < nLen)
    {
        if (pFirst[nSecondChildIndex] < pFirst[nSecondChildIndex - 1])
            --nSecondChildIndex;
        pFirst[nHoleIndex] = pFirst[nSecondChildIndex];
        nHoleIndex = nSecondChildIndex;
        nSecondChildIndex = 2 * nHoleIndex + 2;
    }
    if (nSecondChildIndex == nLen)
    {
        pFirst[nHoleIndex] = pFirst[nSecondChildIndex - 1];
        nHoleIndex = nSecondChildIndex - 1;
    }

    // 以下兩個操作在這個函數中的作用相同, 證實了<<STL源碼剖析>>中P178中JJHOU所言
    //pFirst[nHoleIndex] = nValue;
    push_heap(pFirst, nHoleIndex, nTopIndex, nValue);
}

void    test_heap_algo(int *pArray, int nLength)
{
    std::cout << "/ntest_heap_algo()/n";
    make_heap(pArray, pArray + nLength);
    display_array(pArray, nLength);

    push_heap(pArray, pArray + nLength);
    display_array(pArray, nLength);

    pop_heap(pArray, pArray + nLength);
    display_array(pArray, nLength);

    if (is_heap(pArray, pArray + nLength - 1))
    {
        std::cout << "is heap!/n";
    }
    else
    {
        std::cout << "is not heap!/n";
    }

    make_heap(pArray, pArray + nLength);
    display_array(pArray, nLength);

    if (is_heap(pArray, pArray + nLength))
    {
        std::cout << "is heap!/n";
    }
    else
    {
        std::cout << "is not heap!/n";
    }

    sort_heap(pArray, pArray + nLength);
    display_array(pArray, nLength);
}

void    test_heap_algo_in_stl(int *pArray, int nLength)
{
    std::cout << "/ntest_heap_algo_in_stl()/n";

    std::make_heap(pArray, pArray + nLength);
    display_array(pArray, nLength);

    std::push_heap(pArray, pArray + nLength);
    display_array(pArray, nLength);

    std::pop_heap(pArray, pArray + nLength);
    display_array(pArray, nLength);

    // 注意is_heap不是STL中支援的演算法, 貌似只有SGI的實現才有這個函數!
    if (is_heap(pArray, pArray + nLength - 1))
    {
        std::cout << "is heap!/n";
    }
    else
    {
        std::cout << "is not heap!/n";
    }

    std::make_heap(pArray, pArray + nLength);
    display_array(pArray, nLength);

    if (is_heap(pArray, pArray + nLength))
    {
        std::cout << "is heap!/n";
    }
    else
    {
        std::cout << "is not heap!/n";
    }

    std::sort_heap(pArray, pArray + nLength);
    display_array(pArray, nLength);
}

void    display_array(int *pArray, int nLength)
{
    for (int i = 0; i < nLength; ++i)
        std::cout << pArray[i] << " ";
    std::cout << std::endl;
}

文章出處:http://www.diybl.com/course/3_program/c++/cppsl/2008914/142815.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.