堆数据结构的实现以及堆排序
2014-08-15 20:44
375 查看
1、堆的数据结构使用数组进行存储的
2、堆的数据结构按照完全二叉树的结构进行描述,所以这里关于堆的孩子节点和父节点的关系,构成了堆数据中数据获取的一个入口,下标为i的父节点的两个孩子节点的下标分别是2*i ,2*i+1 不同的起始下标,表示可能有所不同。
3、最大堆可以用于排序,复杂度在O(Nlog(N)),对还是实现优先权队列的数据结构基础
4、下面的代码详细描述了最大堆的一些关键操作
2、堆的数据结构按照完全二叉树的结构进行描述,所以这里关于堆的孩子节点和父节点的关系,构成了堆数据中数据获取的一个入口,下标为i的父节点的两个孩子节点的下标分别是2*i ,2*i+1 不同的起始下标,表示可能有所不同。
3、最大堆可以用于排序,复杂度在O(Nlog(N)),对还是实现优先权队列的数据结构基础
4、下面的代码详细描述了最大堆的一些关键操作
#ifndef MAXHEAP_H #define MAXHEAP_H #include <memory.h> #include <iostream> using namespace std; /** 最大堆 */ class MaxHeap { public: int heapSize; int * heap; public: MaxHeap(); MaxHeap(int heapSize); virtual ~MaxHeap(); void output_heap(); void init_heap(int a[],int n); int left(int i); int right(int i); void max_heapify(int i); void max_heapify2(int i); void build_max_heap(); void heap_sort(); }; #endif // MAXHEAP_H
#include "MaxHeap.h" MaxHeap::MaxHeap(){ } MaxHeap::~MaxHeap(){ delete []heap; } MaxHeap::MaxHeap(int heapSize):heapSize(heapSize){ heap = new int [heapSize] ; memset(heap,0,sizeof(int)*heapSize); } //左孩子的index int MaxHeap::left(int i){//下标从0开始的原因 return 2*i+1; } //右孩子的index int MaxHeap::right(int i){ return 2*i +2; } /** 输出堆的内容 **/ void MaxHeap::output_heap(){ for(int i=0;i<heapSize;++i){ cout<<heap[i]<<" "; } cout<<endl; } /** 利用数组初始化堆内容 **/ void MaxHeap::init_heap(int a[], int n){ if(n>heapSize)return ; else{ heapSize=n; //memcpy(heap,a,heapSize); for(int i=0;i<heapSize;++i){ heap[i]=a[i]; } } } /** 调整堆内数组使得其满足堆的性质 调整从i节点开始,这个操作可以保证i节点及其子孙节点都满足 最大堆的特点 */ void MaxHeap::max_heapify(int i){ int l= left(i); int r= right(i); int max_index=0; //最大孩子的下标 if(l<heapSize&&heap[l]>heap[i]){ max_index=l; }else { max_index=i; } if(r<heapSize&&heap[r]>heap[max_index]){ max_index=r; } if(max_index!=i){ swap(heap[i],heap[max_index]); max_heapify(max_index);//防止造成子孙节点中出现不满足堆的性质的情况发生 } } //堆调整的非递归代码实现 void MaxHeap::max_heapify2(int i){ while(i<heapSize/2){ int l = left (i); int r = right (i); int max_index=0; if(r<heapSize){ max_index = heap[l] > heap[r] ? l : r; max_index = heap[max_index] > heap[i] ? max_index : i; }else{ max_index = heap[l] > heap[i] ? l:i; } if(max_index!=i){ swap(heap[i],heap[max_index]); i = max_index; } } } /** 堆的建立操作 */ void MaxHeap::build_max_heap(){ for(int i= heapSize/2 +1;i>=0;--i){//从第一个非叶子节点开始调整为最大堆特征 max_heapify(i); } } /** 堆排序,将最后一个元素同堆顶元素交换 每次都能保证取得的是当前最大的元素 max_heapfy(0),调整第一个元素 **/ void MaxHeap::heap_sort(){ build_max_heap(); int t = heapSize; for(int i=heapSize-1;i>0;--i){ swap(heap[i],heap[0]); heapSize--; max_heapify(0);//取出之后对造成的不满足最大堆进行调整 } heapSize=t; }
相关文章推荐
- 【数据结构】用模版实现大小堆、实现优先级队列,以及堆排序
- 【数据结构】用模版实现大小堆、实现优先级队列,以及堆排序
- 堆排序 HeapSort C语言实现 附严蔚敏数据结构P282 代码错误
- BitSet数据结构以及jdk中实现源码分析
- 数据结构中栈的实现以及其基本操作
- 【 数据结构】实现二叉树以及其基本操作
- 算法数据结构 单链表的实现+操作 以及和顺序表的对比
- 【数据结构】有向图、无向图以及最短路(Dijkstra)算法的C#实现(Template模式)
- 数据结构 二叉树遍历之前序、中序、后序以及层次遍历实现
- BitSet数据结构以及jdk中实现源码分析
- Java实现堆以及堆排序
- 数据结构学习之链表(单向、单循环以及双向)(递归实现)
- 数据结构(C#)--图结构的实现输出以及图结构的深度和广度优先搜索和Dijkstra 算法的最小路径以及最小生成树的实现
- 堆排序的代码实现 - 数据结构和算法93
- 如果系统要使用超大整数(超过long长度范围),请你设计一个数据结构来存储这种超大型数字以及设计一种算法来实现超大整数加法运算)
- 如果系统要使用超大整数(超过long长度范围),请你设计一个数据结构来存储这种超大型数字以及设计一种算法来实现超大整数加法运算
- c++实现堆类以及堆排序
- 堆排序(基本思想以及算法实现)
- 【数据结构&&算法系列】堆排序简单介绍及其实现
- 【算法与数据结构】冒泡、插入、归并、堆排序、快速排序的Java实现代码