建立huffman樹,當然用堆排序

來源:互聯網
上載者:User

閑著去逛論壇,看到有人問建huffman樹。記得當初學資料結構時我沒寫過,所以也來試試。不過這個當作業實在是太遲了:)

/**//*
做huffman用堆排序應該效率最高了吧,不過自己寫一個太麻煩,就直接用stl算了:
*/
#include cstdio>
#include vector>
#include algorithm>

template class Type >
struct node
{
    node *left, *right;
    int value;
    const Type *data;
    struct ptrcmp // 仿函數,用於堆排序的比較
    {
        bool operator () (const node* a, const node* b)
        {
            return a->value > b->value; // 最小堆
        }
    };
};

template class Type >
node Type >* buildHuffmanTree(const Type data[], int count)
{
    typedef node Type > Node;
    
    // 初始化堆
    ::std::vector Node* > heap(count, NULL);
    heap.clear();
    for(int i= 0; i  count; i++)
    {
        Node *n = new Node;
        n->left = NULL;
        n->right = NULL;
        n->data = &data[i];
        n->value = data[i]; // 如果Type不能轉化到int,則必須重載一個operator int()
        heap.push_back(n);
    }
    ::std::make_heap(heap.begin(), heap.end(), Node::ptrcmp());

    // 用堆排序找到最小的2個元素,建立它們的父節點
    while(heap.size() > 1)
    {
        Node *n1, *n2, *t;
        ::std::pop_heap(heap.begin(), heap.end(), Node::ptrcmp());
        n1 = heap.back();
        heap.pop_back();
        ::std::pop_heap(heap.begin(), heap.end(), Node::ptrcmp());
        n2 = heap.back();
        t = new Node; // 父節點
        t->left = n1;
        t->right = n2;
        t->value = n1->value + n2->value; // 權值為孩子們權的和

        // 再把這個父節點放回堆
        heap.back() = t;
        ::std::push_heap(heap.begin(), heap.end(), Node::ptrcmp());
    }
    return heap.front();
}

template class Type >
void destoryTree(node Type >* root)
{
    if (root->left) destoryTree(root->left);
    if (root->right) destoryTree(root->right);
    delete root;
}

template class Type >
void drawTree(node Type >* root)
{
    printf("%d",root->value);
    if (root->left)
    {
        printf("(");
        drawTree(root->left);
        printf(",");
    }
    if (root->right)
    {
        drawTree(root->right);
        printf(")");
    }
}

// 測試案例
int main()
{
    int data[] = {2,3,4,5,1,2,3,65,8,2,};
    node int > *root = buildHuffmanTree(data, sizeof(data)/sizeof(data[0]));

    drawTree(root); // 結果為:95(30(13(6(3,3),7(3(1,2),4)),17(8,9(4(2,2),5))),65)
    printf("\n");

    destoryTree(root);
    return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.