一个不错的模板Heap实现及Heap排序的实现
2005-12-24 23:41
417 查看
// MaxHeap.h
/* MaxHeap: binary tree of which data of any node >= either its child */
/* Using a dynamic array to store nodes, while i stands for a parent node,
2i and 2i + 1 are its children */
template<class T>
class MaxHeap
{
public:
MaxHeap (int maxHeapSize = 10);
/* heap points to array a when initializing, for preventing to delete the array a
when calling ~MaxHeap (), make heap point nothing */
~MaxHeap () { heap = 0; delete [] heap;}
int size () const { return currentSize;}
T max () {if (currentSize == 0) throw OutOfBounds ();
return heap[1];}
MaxHeap<T>& insert (const T x);
MaxHeap<T>& sift ();
void initialize (T a[], int arraySize);
private:
int currentSize, maxSize;
T *heap;
};
// implementation should be included in declaring file
template<class T>
MaxHeap<T>::MaxHeap (int maxHeapSize)
{
maxSize = maxHeapSize;
heap = new T[maxSize + 1]; // heap[1] as begining
currentSize = 0;
}
/* insert a new node with the valude of x */
template<class T>
MaxHeap<T>& MaxHeap<T>::insert (const T x)
{
int i;
if (currentSize == maxSize) //throw NoMem();
printf ("no memory!/n");
else
{
// looking for the position of the new node from the new node
// bottom-up
i = ++currentSize;
while (i != 1 && x > heap[i / 2])
{
heap[i] = heap[i / 2]; // exchange the position of the new node and its parent
i /= 2; // up
}
heap[i] = x;
}
return *this;
}
/* exchange the root and the last node, minish the currentSize by 1
and adjust the structure of the whole tree */
template<class T>
MaxHeap<T>& MaxHeap<T>::sift ()
{
if (currentSize != 0) //throw OutOfBounds ();
{
T y = heap[currentSize]; // the last node
// allocate heap[1] (the max node) to the previous y position
// then minish currentSize by 1
heap[currentSize--] = heap[1];
// looking for y's position from the root
// top-down
int i = 1, j = 2; // initialize i as the root, and j as left child
while (j <= currentSize)
{
// select the bigger child of the node
if (j < currentSize && heap[j + 1] > heap[j]) j++;
if (y < heap[j])
{
heap[i] = heap[j]; // exchange the position of parent and child
i = j; // down
j *= 2;
}
else
break;
}
heap[i] = y; // the new position of y
}
return *this;
}
template<class T>
void MaxHeap<T>::initialize (T a[], int arraySize)
{
delete [] heap;
heap = a;
currentSize = 1; // a[1] as root
maxSize = arraySize - 1; // a[0] doesn't count
// in the beginning, a[1] as root, then insert the rest of array a one by one
for (int i = 2; i < arraySize; i++)
insert (a[i]);
}
// heapSort.cpp
//#include "MaxHeap.h"
#include <iostream>
using namespace std;
template<class T>
/* sorting a[1 : arraySize - 1] */
void heapSort (T a[], int arraySize)
{
MaxHeap<T> H(1);
H.initialize (a, arraySize); // a[0] doesn't count
for (int i = 1; i < arraySize; i++)
H.sift ();
}
int main ()
{
int a[10] = {0,6,8,9,4,1,3,7,2,5};
heapSort<int> (a, 10);
for (int i = 1; i < 10; i++)
cout << a[i] << " ";
cin.get();
return 0;
}
/* MaxHeap: binary tree of which data of any node >= either its child */
/* Using a dynamic array to store nodes, while i stands for a parent node,
2i and 2i + 1 are its children */
template<class T>
class MaxHeap
{
public:
MaxHeap (int maxHeapSize = 10);
/* heap points to array a when initializing, for preventing to delete the array a
when calling ~MaxHeap (), make heap point nothing */
~MaxHeap () { heap = 0; delete [] heap;}
int size () const { return currentSize;}
T max () {if (currentSize == 0) throw OutOfBounds ();
return heap[1];}
MaxHeap<T>& insert (const T x);
MaxHeap<T>& sift ();
void initialize (T a[], int arraySize);
private:
int currentSize, maxSize;
T *heap;
};
// implementation should be included in declaring file
template<class T>
MaxHeap<T>::MaxHeap (int maxHeapSize)
{
maxSize = maxHeapSize;
heap = new T[maxSize + 1]; // heap[1] as begining
currentSize = 0;
}
/* insert a new node with the valude of x */
template<class T>
MaxHeap<T>& MaxHeap<T>::insert (const T x)
{
int i;
if (currentSize == maxSize) //throw NoMem();
printf ("no memory!/n");
else
{
// looking for the position of the new node from the new node
// bottom-up
i = ++currentSize;
while (i != 1 && x > heap[i / 2])
{
heap[i] = heap[i / 2]; // exchange the position of the new node and its parent
i /= 2; // up
}
heap[i] = x;
}
return *this;
}
/* exchange the root and the last node, minish the currentSize by 1
and adjust the structure of the whole tree */
template<class T>
MaxHeap<T>& MaxHeap<T>::sift ()
{
if (currentSize != 0) //throw OutOfBounds ();
{
T y = heap[currentSize]; // the last node
// allocate heap[1] (the max node) to the previous y position
// then minish currentSize by 1
heap[currentSize--] = heap[1];
// looking for y's position from the root
// top-down
int i = 1, j = 2; // initialize i as the root, and j as left child
while (j <= currentSize)
{
// select the bigger child of the node
if (j < currentSize && heap[j + 1] > heap[j]) j++;
if (y < heap[j])
{
heap[i] = heap[j]; // exchange the position of parent and child
i = j; // down
j *= 2;
}
else
break;
}
heap[i] = y; // the new position of y
}
return *this;
}
template<class T>
void MaxHeap<T>::initialize (T a[], int arraySize)
{
delete [] heap;
heap = a;
currentSize = 1; // a[1] as root
maxSize = arraySize - 1; // a[0] doesn't count
// in the beginning, a[1] as root, then insert the rest of array a one by one
for (int i = 2; i < arraySize; i++)
insert (a[i]);
}
// heapSort.cpp
//#include "MaxHeap.h"
#include <iostream>
using namespace std;
template<class T>
/* sorting a[1 : arraySize - 1] */
void heapSort (T a[], int arraySize)
{
MaxHeap<T> H(1);
H.initialize (a, arraySize); // a[0] doesn't count
for (int i = 1; i < arraySize; i++)
H.sift ();
}
int main ()
{
int a[10] = {0,6,8,9,4,1,3,7,2,5};
heapSort<int> (a, 10);
for (int i = 1; i < 10; i++)
cout << a[i] << " ";
cin.get();
return 0;
}
相关文章推荐
- 程序员代码面试指南--用一个栈实现另一个栈的排序
- 实现一个简单的c++ list容器(含sort排序 链表归并算法实现)
- Android添加手机黑名单,手机来电拦截实现详解与Demo,一个不错的练手项目,涵盖Android四大组件。
- 一个特殊的排序需求的javascript实现代码
- java实现根据一个类的内容排序
- 写一个【栈】模板,并实现“任意进制转换器”
- 通过js脚本复制网页上的一个表格的不错实现方法
- C++基于模板顺序表的实现(带排序)
- 模板实现冒泡(不同类型的元素)排序
- 一个不错的js实现的 日期下拉菜单
- 一个mapreduce同时实现计数与排序
- 求助哇, 我这里有一个存储过程 我想实现排序 通过判断然后 在排序 大家 请看下面的存储过程
- DataGrid排序错误:必须至少有一个对象实现
- 用一个栈实现另一个栈的排序
- 实现一个算法,对公司员工年龄进行排序,时间效率要求O(n)
- 一个不错的实现折叠效果的方法
- 冒泡排序之C语言实现与冒泡排序之c++模板实现
- 用python实现一个不排序的列表功能
- 一个利用CAN实现车辆控制的简单模板
- 一个不错的给图片添加说明文字的动态层的实现代码