博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++STL之二叉堆
阅读量:7090 次
发布时间:2019-06-28

本文共 2384 字,大约阅读时间需要 7 分钟。

hot3.png

// myBinaryHeap.cpp : 定义控制台应用程序的入口点。// #include "stdafx.h"#include 
#include 
 #define  random(x) (rand()%x)using namespace std;  template 
class BinaryHeap{private:    int currentSize;    vector
 vecArray;    void buildHeap()    //将序列建堆的过程    {        for (int i = currentSize/2 ; i >= 0; i--)         {            perColateDown(i);        }    };    void perColateDown(int hole);public:    explicit BinaryHeap(int capacity = 100){currentSize = capacity;}    explicit BinaryHeap (const vector
& items):vecArray(items.size() + 10), currentSize(items.size())  //------vecArray(items.size() + 10)    {        for (int i = 0; i < items.size() ; i++)        {            vecArray[i] = items[i];        }        buildHeap();    }     bool isEmpty() const    {        return  0== currentSize;    };    const T& findMin() const    {        return vecArray[0];    }    const vector
 getVector()    {        return vecArray;    }     void insert(const T& x);    void deleteMin();    void deleteMin(T& minItem);    void makeEmpty();}; template 
void BinaryHeap
::insert(const T& x){    if (currentSize == vecArray.size() - 1)    {        vecArray.resize(vecArray.size() * 2);    }    int hole = ++ currentSize; //建立一个空穴,即数组的一个下标    for (; hole > 1 && x< vecArray[hole/2]; hole << 1)    {        vecArray[hole] = vecArray[hole/2];    }    vecArray[hole] = x;} template 
void BinaryHeap
::deleteMin(){    if (isEmpty()){    return;}    vecArray[1] = vecArray[currentSize --];    perColateDown(1);}template 
void BinaryHeap
::deleteMin(T& minItem){    if (isEmpty()){    return;}    minItem = vecArray[1] ;    vecArray[1] = vecArray[currentSize --];    perColateDown(1);} template 
void BinaryHeap
::perColateDown(int hole)   //向下计算当前数的实际位置{    int child ;     T temp = vecArray[hole];    for(; hole*2 < currentSize; hole = child)    {        child = hole * 2;        if (child != currentSize && vecArray[child+1] < vecArray[child])  // 若左子树大于右子树            child ++;        if (vecArray[child] < temp)  //  左子树和右子树的最小值和节点值比较,将最小值压入节点,继续寻找适合节点值的位置            vecArray[hole] = vecArray[child];        else            break;    }    vecArray[hole] = temp;}  int _tmain(int argc, _TCHAR* argv[]){    vector
 vecInt;    for (int i = 0; i < 10; i++)    {        vecInt.push_back(random(51));        cout << vecInt[i] <<" ";    }     cout  <
* bh =new BinaryHeap
(vecInt) ;    for (int i=0; i < 10; i++)        {        cout << bh->getVector()[i] <<" ";   //这里等于实现了堆排序算法    }    return 0;}

转载于:https://my.oschina.net/u/1782374/blog/368237

你可能感兴趣的文章
深度学习 Deep Learning UFLDL 最新Tutorial 学习笔记 4:Debugging: Gradient Checking
查看>>
【转】spring boot web相关配置
查看>>
oc53--autorelease注意事项
查看>>
sigmod2017.org
查看>>
MongoDB集群运维笔记
查看>>
Python代码优化及技巧笔记(一)
查看>>
Caused by: java.lang.NoClassDefFoundError: org/apache/neethi/AssertionBuilderFactory
查看>>
Ocelot 集成Butterfly 实现分布式跟踪
查看>>
(转)各种语言写网络爬虫有什么优点缺点
查看>>
如何用公式编辑器打带圈加号
查看>>
好用的端口监控软件:Port Explorer
查看>>
php coding中的一些小问题
查看>>
Cisco无线控制器配置Radius
查看>>
iota 币产生私钥的方法
查看>>
Mysql数据类型DECIMAL(M,D)用法
查看>>
006-Shell printf 命令
查看>>
leetcode 39. Combination Sum 40. Combination Sum II
查看>>
python测试开发django-4.获取url参数和name的作用
查看>>
C# IEnumerable和IEnumerator的区别,如何实现
查看>>
android adb命令行工具使用
查看>>