【代码】c++堆的简单实现
2016-05-28 18:36
337 查看
堆对象的创建与实现的核心思想就是上调(adjustup)与下调(adjustdown)的算法思想,上调用于创建堆时,从第一个非叶子节点开始向根节点根据需求调整为大堆或者小堆
下调如图示:
![](http://s5.51cto.com/wyfs02/M00/7F/D5/wKiom1cvCpfjuPkkAAAkCLyIQjQ600.png)
当我们进行插入时,会影响堆的结构,这时我们用尾插,然后上调如图示:
![](http://s4.51cto.com/wyfs02/M01/7F/D5/wKiom1cvDRehwFD4AAAlAV7a9cM800.png)
接下来就可以创建堆类,代码如下仅供参考:
#include<iostream>
#include<vector>
template <class T>
struct CompMax
{
bool operator()(const T& a, const T& b)
{
return a > b;
}
};
template <class T>
struct CompMin
{
bool operator()(const T& a,const T& b)
{
return a < b;
}
};
template <class T,class Com=CompMax<T> >//仿函数做模板参数,可根据需求修改比较方法
class
4000
Heap
{
public:
Heap(const T* arr, size_t size, Com _comp)
:comp(_comp)
{
_List.resize(size);
int index = 0;
for (index = 0; index < size; ++index)
{
_List[index] = arr[index];
}
index = (_List.size()- 2) / 2;
while (index>=0)
_adjustdown(index--);
}
void Push(const T &x)
{
_List.push_back(x);
size_t index = _List.size() - 1;
_adjustup(index);
}
void Pop()
{
_List.pop_back();
}
T& Top()
{
return _List[0];
}
protected:
void _adjustup(size_t index)
{
size_t child = index;
size_t parent = (child - 1) / 2;
while (child)
{
if (child % 2)
{
if (child + 1<_List.size())
child =comp(_List[child],_List[child+1]) ? child : child + 1;
}
else
{
child = comp(_List[child] ,_List[child - 1]) ? child : child - 1;
}
if (!comp(_List[child],_List[parent]))
{
std::swap(_List[parent], _List[child]);
}
child = parent;
parent = (parent - 1) / 2;
}
}
void _adjustdown(size_t index)
{
size_t parent = index;
size_t child = parent * 2 + 1;
while (child < _List.size())
{
if (child + 1 < _List.size())
child = comp(_List[child] , _List[child + 1]) ? child : child + 1;
if (!comp(_List[parent], _List[child]))
{
std::swap(_List[child], _List[parent]);
parent = child;
child = (parent + 1) * 2;
}
else
break;
}
}
protected:
vector<T> _List;
Com comp;
}; 如有不足希望指正,如有问题也希望提出,谢谢-3-。
本文出自 “pawnsir的IT之路” 博客,请务必保留此出处http://10743407.blog.51cto.com/10733407/1771228
下调如图示:
![](http://s5.51cto.com/wyfs02/M00/7F/D5/wKiom1cvCpfjuPkkAAAkCLyIQjQ600.png)
当我们进行插入时,会影响堆的结构,这时我们用尾插,然后上调如图示:
![](http://s4.51cto.com/wyfs02/M01/7F/D5/wKiom1cvDRehwFD4AAAlAV7a9cM800.png)
接下来就可以创建堆类,代码如下仅供参考:
#include<iostream>
#include<vector>
template <class T>
struct CompMax
{
bool operator()(const T& a, const T& b)
{
return a > b;
}
};
template <class T>
struct CompMin
{
bool operator()(const T& a,const T& b)
{
return a < b;
}
};
template <class T,class Com=CompMax<T> >//仿函数做模板参数,可根据需求修改比较方法
class
4000
Heap
{
public:
Heap(const T* arr, size_t size, Com _comp)
:comp(_comp)
{
_List.resize(size);
int index = 0;
for (index = 0; index < size; ++index)
{
_List[index] = arr[index];
}
index = (_List.size()- 2) / 2;
while (index>=0)
_adjustdown(index--);
}
void Push(const T &x)
{
_List.push_back(x);
size_t index = _List.size() - 1;
_adjustup(index);
}
void Pop()
{
_List.pop_back();
}
T& Top()
{
return _List[0];
}
protected:
void _adjustup(size_t index)
{
size_t child = index;
size_t parent = (child - 1) / 2;
while (child)
{
if (child % 2)
{
if (child + 1<_List.size())
child =comp(_List[child],_List[child+1]) ? child : child + 1;
}
else
{
child = comp(_List[child] ,_List[child - 1]) ? child : child - 1;
}
if (!comp(_List[child],_List[parent]))
{
std::swap(_List[parent], _List[child]);
}
child = parent;
parent = (parent - 1) / 2;
}
}
void _adjustdown(size_t index)
{
size_t parent = index;
size_t child = parent * 2 + 1;
while (child < _List.size())
{
if (child + 1 < _List.size())
child = comp(_List[child] , _List[child + 1]) ? child : child + 1;
if (!comp(_List[parent], _List[child]))
{
std::swap(_List[child], _List[parent]);
parent = child;
child = (parent + 1) * 2;
}
else
break;
}
}
protected:
vector<T> _List;
Com comp;
}; 如有不足希望指正,如有问题也希望提出,谢谢-3-。
本文出自 “pawnsir的IT之路” 博客,请务必保留此出处http://10743407.blog.51cto.com/10733407/1771228
相关文章推荐
- 【代码】C++实现二叉树基本操作及测试用例
- 【代码】C++实现广义表及其测试用例
- 【干货】C++通过模板特化实现类型萃取实例--实现区分基本类型与自定义类型的memcpy
- 【总结】C++静态成员函数及测试用例
- 【总结】C++静态成员变量的特性总结及测试用例
- 【总结】C++静态成员变量的特性总结及测试用例
- 【总结】C++基类与派生类的赋值兼容规则
- 【C语言位运算的应用】如何按bit位翻转一个无符号整型
- 双向链表的C++实现
- C++单链表的设计与实现
- 求最大公约数的设计与C语言实现
- 用C语言实现二分查找算法
- C++复数类的运算符重载
- c++日期类的实现级运算符的重载
- C++实现链表的进本操作及测试用例
- C语言实现顺序表的增删查改以及排序
- C语言实现C到C++的注释转换
- C语言用结构体写一个通讯录
- C语言模拟实现memset.memcmp函数
- 模拟实现C语言strncpy.strncat.strncmp函数