试题:实现堆
来源:互联网 发布:java高并发框架 编辑:程序博客网 时间:2024/05/17 20:22
堆的实现:
#include <vector>#include <iostream>using namespace std;template <typename RandomAccessIterator, typename T, typename CompareFunc>void __push__heap(RandomAccessIterator start, RandomAccessIterator end, T *, CompareFunc comp){if (end - start == 1){return;}unsigned int parent = end - start - 1;while (parent > 0){parent = (parent - 1) / 2;unsigned int lchild = parent * 2 + 1;unsigned int rchild = parent * 2 + 2;unsigned int max = lchild;if (start + rchild < end){max = comp(*(start + lchild), *(start + rchild)) ? rchild : lchild;}if (comp(*(start + parent), *(start + max))){T temp(*(start + parent));*(start + parent) = *(start + max);*(start + max) = temp;}else{return;}}}template <typename RandomAccessIterator, typename CompareFunc>void push__heap(RandomAccessIterator start, RandomAccessIterator end, CompareFunc comp){ __push__heap(start, end, &*start, comp);}template <typename RandomAccessIterator, typename T>void __push__heap(RandomAccessIterator start, RandomAccessIterator end, T *){ push__heap(start, end, less<T>());}template <typename RandomAccessIterator>void push__heap(RandomAccessIterator start, RandomAccessIterator end){ __push__heap(start, end, &*start);}template <typename RandomAccessIterator, typename T, typename CompareFunc>void __adjust__heap(RandomAccessIterator start, RandomAccessIterator end, unsigned int parent, T *, CompareFunc comp){ unsigned int last = end - start; unsigned int lchild = parent * 2 + 1; unsigned int rchild = parent * 2 + 2;while (lchild < last){ unsigned int max = lchild;if (rchild < last){max = comp(*(start + lchild), *(start + rchild)) ? rchild : lchild;}if (comp(*(start + parent), *(start + max))){T temp(*(start + parent));*(start + parent) = *(start + max);*(start + max) = temp;parent = max;lchild = parent * 2 + 1;rchild = parent * 2 + 2;}else{return;}}}template <typename RandomAccessIterator, typename T, typename CompareFunc>void __pop__heap(RandomAccessIterator start, RandomAccessIterator end, T *, CompareFunc comp){--end;T temp(*start);*start = *end;*end = temp;__adjust__heap(start, end, 0, &*start, comp);}template <typename RandomAccessIterator, typename CompareFunc>void pop__heap(RandomAccessIterator start, RandomAccessIterator end, CompareFunc comp){__pop__heap(start, end, &*start, comp);}template <typename RandomAccessIterator, typename T>void __pop__heap(RandomAccessIterator start, RandomAccessIterator end, T *){pop__heap(start, end, less<T>());}template <typename RandomAccessIterator>void pop__heap(RandomAccessIterator start, RandomAccessIterator end){__pop__heap(start, end, &*start);}template <typename RandomAccessIterator, typename CompareFunc>void sort__heap(RandomAccessIterator start, RandomAccessIterator end, CompareFunc comp){while (end - start > 1){__pop__heap(start, end, &*start, comp);--end;}}template <typename RandomAccessIterator, typename T>void __sort__heap(RandomAccessIterator start, RandomAccessIterator end, T *){sort__heap(start, end, less<T>());}template <typename RandomAccessIterator>void sort__heap(RandomAccessIterator start, RandomAccessIterator end){__sort__heap(start, end, &*start);}template <typename RandomAccessIterator, typename CompareFunc>void make__heap(RandomAccessIterator start, RandomAccessIterator end, CompareFunc comp){if (end - start <= 1){return;}unsigned int parent = (end - start - 2) / 2;while (parent > 0){__adjust__heap(start, end, parent, &*start, comp);--parent;} __adjust__heap(start, end, 0, &*start, comp);}template <typename RandomAccessIterator, typename T>void __make__heap(RandomAccessIterator start, RandomAccessIterator end, T *){ make__heap(start, end, less<T>());}template <typename RandomAccessIterator>void make__heap(RandomAccessIterator start, RandomAccessIterator end){ __make__heap(start, end, &*start);}
测试代码:
template <typename CompareFunc>void test(const char * msg, CompareFunc comp){ /* Test Heap with comp */ cout << endl << msg << endl; cout << "******* ====> *******" << endl;// test with vector{int ia[9] = { 0, 1, 2, 3, 4, 8, 9, 3, 5 };vector<int> ivec(ia, ia + 9);make__heap(ivec.begin(), ivec.end(), comp);for (int i = 0; i < ivec.size(); ++i){cout << ivec[i] << ' ';}cout << endl;ivec.push_back(7);push__heap(ivec.begin(), ivec.end(), comp);for (int i = 0; i < ivec.size(); ++i){cout << ivec[i] << ' ';}cout << endl;pop__heap(ivec.begin(), ivec.end(), comp);cout << ivec.back() << endl;ivec.pop_back();for (int i = 0; i < ivec.size(); ++i){cout << ivec[i] << ' ';}cout << endl;sort__heap(ivec.begin(), ivec.end(), comp);for (int i = 0; i < ivec.size(); ++i){cout << ivec[i] << ' ';}cout << endl;}// test with array 1{int ia[9] = { 0, 1, 2, 3, 4, 8, 9, 3, 5 };make__heap(ia, ia + 9, comp);sort__heap(ia, ia + 9, comp);for (int i = 0; i < 9; ++i){cout << ia[i] << ' ';}cout << endl;make__heap(ia, ia + 9, comp);for (int i = 0; i < 9; ++i){cout << ia[i] << ' ';}cout << endl;sort__heap(ia, ia + 9, comp);for (int i = 0; i < 9; ++i){cout << ia[i] << ' ';}cout << endl;pop__heap(ia, ia + 9, comp);cout << ia[8] << endl;}// test with array 2{int ia[6] = { 4, 1, 7, 6, 2, 5 };make__heap(ia, ia + 6, comp);for (int i = 0; i < 6; ++i){cout << ia[i] << ' ';}cout << endl;}cout << "******* <==== *******" << endl;}void test(const char * msg){ /* Test Heap with default comp: less<T>() */ cout << endl << msg << endl; cout << "******* ====> *******" << endl;// test with vector{int ia[9] = { 0, 1, 2, 3, 4, 8, 9, 3, 5 };vector<int> ivec(ia, ia + 9);make__heap(ivec.begin(), ivec.end());for (int i = 0; i < ivec.size(); ++i){cout << ivec[i] << ' ';}cout << endl;ivec.push_back(7);push__heap(ivec.begin(), ivec.end());for (int i = 0; i < ivec.size(); ++i){cout << ivec[i] << ' ';}cout << endl;pop__heap(ivec.begin(), ivec.end());cout << ivec.back() << endl;ivec.pop_back();for (int i = 0; i < ivec.size(); ++i){cout << ivec[i] << ' ';}cout << endl;sort__heap(ivec.begin(), ivec.end());for (int i = 0; i < ivec.size( ); ++i){cout << ivec[i] << ' ';}cout << endl;}// test with array 1{int ia[9] = { 0, 1, 2, 3, 4, 8, 9, 3, 5 };make__heap(ia, ia + 9);sort__heap(ia, ia + 9);for (int i = 0; i < 9; ++i){cout << ia[i] << ' ';}cout << endl;make__heap(ia, ia + 9);for (int i = 0; i < 9; ++i){cout << ia[i] << ' ';}cout << endl;sort__heap(ia, ia + 9);for (int i = 0; i < 9; ++i){cout << ia[i] << ' ';}cout << endl;pop__heap(ia, ia + 9);cout << ia[8] << endl;}// test with array 2{int ia[6] = { 4, 1, 7, 6, 2, 5 };make__heap(ia, ia + 6);for (int i = 0; i < 6; ++i){cout << ia[i] << ' ';}cout << endl;}cout << "******* <==== *******" << endl;}bool MyLessFunc(int first, int second){ return (first < second);}struct MyLessFunctor{ bool operator() (int first, int second) const { return (first < second); }};int main(){ test("test with default compare-function"); test("test with less<int>()", less<int>()); test("test with greater<int>()", greater<int>()); test("test with MyLessFunc-function", MyLessFunc); test("test with MyLessFunctor()", MyLessFunctor());return 0;}
- 试题:实现堆
- [各种面试题] 堆的实现
- 堆实现及常见面试题
- 程序员面试题--堆排序的C语言实现
- 剑指offer面试题30:用自定义堆实现
- 面试题:堆排序
- 堆实现
- 【面试题】【c++】 实现只能在堆上实例化的类
- C++ 堆排序算法的实现与改进(含笔试面试题)
- 面试题:堆和队列
- 面试题-堆和栈
- 用堆实现堆排序
- Python实现 [堆] [堆排序]
- 最大堆实现堆排序
- 堆以及堆排序实现
- 堆实现 及 堆排序
- 最大堆实现堆排序
- 堆结构的实现
- Configure,Makefile.am, Makefile.in, Makefile文件之间
- BOP - 寻找发贴‘水王’
- fedora下安装博通无线网卡驱动
- DUMPBIN命令使用详解
- ucenter同步登陆过程分析
- 试题:实现堆
- (转)Clouder CDH3B3开始后hadoop.job.ugi不再生效
- VS2010中 C++创建DLL图解
- MFC 涂鸦板
- Java语言中两种异常的差别
- MFC 集合类
- JDK配置
- Android与iOS:谁更适合HTML 5?
- 13周作业(2)