Heap,堆,大堆和小堆用仿函数实现
2016-03-17 18:50
274 查看
#pragma once #include<iostream> #include<vector> using namespace std; template<class T> class Heap { public: //vector调用自己的构造函数 Heap() {} Heap(T* a, size_t size) { for (size_t i = 0; i < size; ++i) _array.push_back(a[i]); //size-1是数组最大下标,再减1除二是父节点下标 for (int i = (_array.size() - 1 - 1) / 2; i >= 0; --i) { AdjustDown(i); } } void Push(const T& x) { _array.push_back(x); AdjustUp(_array.size()-1); } void Pop() { if (_array.size()>0) { swap(_array[_array.size() - 1],_array[0]); _array.pop_back(); AdjustDown(0); } } protected: void AdjustDown(int prtIndex) { int leftIndex = 0; while (prtIndex<_array.size()) { leftIndex = prtIndex * 2 + 1; if (leftIndex + 1 < _array.size() && _array[leftIndex + 1] < _array[leftIndex]) leftIndex = leftIndex + 1; if (leftIndex<_array.size() && _array[prtIndex] > _array[leftIndex]) { swap(_array[prtIndex], _array[leftIndex]); prtIndex = leftIndex; } else break; } } void AdjustUp(int chiIndex) { int parIndex = (chiIndex - 1) / 2; while (chiIndex > 0)//父节点不会小于0,(0-1)/2等于0,此处应用chiIndex来判断 { if (_array[parIndex] > _array[chiIndex]) { swap(_array[parIndex], _array[chiIndex]); chiIndex = parIndex; parIndex = (chiIndex - 1) / 2; } else break; } } protected: vector<T> _array; }; #include"Heap.h" void Test1() { int array[10] = { 10, 2, 3, 4, 8, 1, 6, 5, 9, 7 }; Heap<int> h1(array,10); h1.Push(0); h1.Pop(); } #pragma once #include<iostream> #include<vector> using namespace std; //仿函数:指一个类满足函数operator,此时是比较大小 template<class T> struct Great { bool operator()(const T& l, const T& r) { return l > r; } }; template<class T> struct Less { bool operator()(const T& l, const T& r) { return l < r; } }; template<class T,class Comp=Less<int>> class Heap { public: //vector调用自己的构造函数 Heap() {} Heap(T* a, size_t size) { for (size_t i = 0; i < size; ++i) _array.push_back(a[i]); //size-1是数组最大下标,再减1除二是父节点下标 for (int i = (_array.size() - 1 - 1) / 2; i >= 0; --i) { AdjustDown(i); } } void Push(const T& x) { _array.push_back(x); AdjustUp(_array.size() - 1); } void Pop() { if (_array.size()>0) { swap(_array[_array.size() - 1], _array[0]); _array.pop_back(); AdjustDown(0); } } protected: void AdjustDown(int prtIndex) { int leftIndex = 0; while (prtIndex<_array.size()) { leftIndex = prtIndex * 2 + 1; //此处应对称方法,传递的参数,比较大小: if (leftIndex + 1 < _array.size() && _com(_array[leftIndex + 1] , _array[leftIndex])) leftIndex = leftIndex + 1; if (leftIndex<_array.size() && _com(_array[leftIndex],_array[prtIndex])) { swap(_array[prtIndex], _array[leftIndex]); prtIndex = leftIndex; } else break; } } void AdjustUp(int chiIndex) { int parIndex = (chiIndex - 1) / 2; while (chiIndex > 0)//父节点不会小于0,(0-1)/2等于0,此处应用chiIndex来判断 { if (_com(_array[chiIndex], _array[parIndex])) { swap(_array[parIndex], _array[chiIndex]); chiIndex = parIndex; parIndex = (chiIndex - 1) / 2; } else break; } } protected: vector<T> _array; Comp _com; }; void Test2() { int array[10] = { 10, 2, 3, 4, 8, 1, 6, 5, 9, 7 }; Heap<int,Great<int>> h1(array,10); h1.Push(100); h1.Pop(); }
相关文章推荐
- 一看就懂:图解C#中的值类型、引用类型、栈、堆、ref、out
- 浅析栈区和堆区内存分配的区别
- PHP SPL标准库之数据结构堆(SplHeap)简单使用实例
- 浅谈C#中堆和栈的区别(附上图解)
- C语言之栈和堆(Stack && Heap)的优缺点及其使用区别
- Java中堆和栈的区别详解
- 详解Java的堆内存与栈内存的存储机制
- java中堆和栈的区别分析
- C++中静态存储区与栈以及堆的区别详解
- 大话JMM
- [数据结构]堆
- java 虚拟机--新生代与老年代GC
- java堆内存与栈内存
- 堆和栈的区别
- 转贴一份挺好的堆和栈区别的资料
- C语言之内存分配
- 初学算法-基于最小堆的优先级队列C++实现
- Head First C学习日志 第六章用堆进行动态存储
- 关于java内存使用的相关问题(寄存器、栈、堆、静态存储、常理存储、非RAM存储)
- 关于栈、堆、静态变量区的访问效率