二叉堆 C++实现

#ifndef __BINARY_HEAP_H__
#define __BINARY_HEAP_H__

#include <iostream>
#include <vector>
#include <assert.h>

template <typename Type>
class HeapStruct {
    friend std::ostream& operator<< <Type>(std::ostream& out, const HeapStruct<Type>& heap);

    int capacity;
    int size;
    Type *elements;
    void initialize(int maxElements);
public:
    HeapStruct(int maxElements) { initialize(maxElements); }
    void destroy();
    void makeEmpty();
    void insert(Type element);
    Type deleteMin();
    Type findMin();
    bool isEmpty();
    void buildHeap(Type a[], int num);
};

template <typename Type>
std::ostream& operator<<(std::ostream& out, const HeapStruct<Type>& heap)
{
    out << "{";
    int i;
    for (i = 1; i < heap.size; i++)
        out << heap.elements[i] << ",";
    out << heap.elements[i] << "}" << endl;
    return out;
}

template<typename Type>
inline void HeapStruct<Type>::initialize(int maxElements)
{
    elements = (Type*)malloc((maxElements + 1) * sizeof(Type));
    assert(elements != nullptr);
    capacity = maxElements;
    size = 0;
    elements[0] = Type();
}

template<typename Type>
inline void HeapStruct<Type>::destroy()
{
    free(elements);
}

template<typename Type>
inline void HeapStruct<Type>::makeEmpty()
{
    size = 0;
}

template<typename Type>
void HeapStruct<Type>::buildHeap(Type a[], int num)
{
    if (size + num >= capacity)
    {
        Type *temp = elements;
        while (capacity < size + num)
            capacity *= 2;
        elements = (Type*)malloc((capacity + 1) * sizeof(Type));
        assert(elements != nullptr);
        memcpy(elements, temp, (size + 1) * sizeof(Type));
        free(temp);
    }
    int build_frequency = 0;
    for (int i = 0; i < num; ++i)
        elements[i + 1] = a[i];
    size = num;
    for (int i = num / 2; i > 0; i--)
    {
        int j, child;
        Type lastEmement = elements[i];
        for (j = i; j * 2 <= size; j = child)
        {
            child = 2 * j;
            build_frequency += 2;
            if (child != size && elements[child] > elements[child + 1])
                child++;
            if (elements[child] < elements[j])
                elements[j] = elements[child];
            else
                break;
        }
        elements[j] = lastEmement;
    }
    std::cout << "构造频数为:" << build_frequency << endl;
}

template<typename Type>
inline void HeapStruct<Type>::insert(Type element)
{
    if (size + 1 > capacity)
    {
        Type *temp = elements;
        capacity *= 2;
        elements = (Type*)malloc((capacity + 1) * sizeof(Type));
        assert(elements != nullptr);
        memcpy(elements, temp, (size + 1) * sizeof(Type));
        free(temp);
    }
    int i;
    for (i = ++size; elements[i / 2] > element; i /= 2)
        elements[i] = elements[i / 2];
    elements[i] = element;
}

template<typename Type>
inline Type HeapStruct<Type>::deleteMin()
{
    Type min = elements[1];
    Type lastEmement = elements[size--];
    if (size * 2 <= capacity)
    {
        Type *temp = elements;
        capacity = size;
        elements = (Type*)malloc((capacity + 1) * sizeof(Type));
        assert(elements != nullptr);
        memcpy(elements, temp, (size + 1) * sizeof(Type));
        free(temp);
    }
    int i, child;
    for (i = 1; i * 2 <= size; i = child)
    {
        child = 2 * i;
        if (child != size && elements[child] > elements[child + 1])
            child++;
        if (elements[child] < elements[i])
            elements[i] = elements[child];
        else
            break;
    }
    elements[i] = lastEmement;
    return min;
}

template<typename Type>
inline Type HeapStruct<Type>::findMin()
{
    return elements[1];
}

template<typename Type>
inline bool HeapStruct<Type>::isEmpty()
{
    return size;
}

#endif
原文地址:https://www.cnblogs.com/sdlwlxf/p/5067461.html