二叉堆 C++实现

时间:2022-06-27 08:13:16
#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 = ; 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 + ) * sizeof(Type));
assert(elements != nullptr);
capacity = maxElements;
size = ;
elements[] = Type();
} template<typename Type>
inline void HeapStruct<Type>::destroy()
{
free(elements);
} template<typename Type>
inline void HeapStruct<Type>::makeEmpty()
{
size = ;
} template<typename Type>
void HeapStruct<Type>::buildHeap(Type a[], int num)
{
if (size + num >= capacity)
{
Type *temp = elements;
while (capacity < size + num)
capacity *= ;
elements = (Type*)malloc((capacity + ) * sizeof(Type));
assert(elements != nullptr);
memcpy(elements, temp, (size + ) * sizeof(Type));
free(temp);
}
int build_frequency = ;
for (int i = ; i < num; ++i)
elements[i + ] = a[i];
size = num;
for (int i = num / ; i > ; i--)
{
int j, child;
Type lastEmement = elements[i];
for (j = i; j * <= size; j = child)
{
child = * j;
build_frequency += ;
if (child != size && elements[child] > elements[child + ])
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 + > capacity)
{
Type *temp = elements;
capacity *= ;
elements = (Type*)malloc((capacity + ) * sizeof(Type));
assert(elements != nullptr);
memcpy(elements, temp, (size + ) * sizeof(Type));
free(temp);
}
int i;
for (i = ++size; elements[i / ] > element; i /= )
elements[i] = elements[i / ];
elements[i] = element;
} template<typename Type>
inline Type HeapStruct<Type>::deleteMin()
{
Type min = elements[];
Type lastEmement = elements[size--];
if (size * <= capacity)
{
Type *temp = elements;
capacity = size;
elements = (Type*)malloc((capacity + ) * sizeof(Type));
assert(elements != nullptr);
memcpy(elements, temp, (size + ) * sizeof(Type));
free(temp);
}
int i, child;
for (i = ; i * <= size; i = child)
{
child = * i;
if (child != size && elements[child] > elements[child + ])
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[];
} template<typename Type>
inline bool HeapStruct<Type>::isEmpty()
{
return size;
} #endif