#pragma once
#include<iostream>
using namespace std;
#include<vector>

template<class T>    //仿函数
struct Less
{
    bool operator()(const T&l, const T&r)  //重载括号
    {
        return l < r;
    }
};

template<class T>
struct Greater
{
    bool operator()(const T&l, const T&r)
    {
        return l>r;
    }
};

template<class T,class Compare=Greater<T>>
class Heap
{
public:
    Heap()
    {}

Heap(const T* a, size_t size)   //建堆
    {
        for (int i = 0; i < size; ++i)      //先将数据push进vector中
        {
            _a.push_back(a[i]);
        }

for (int j = (_a.size() - 2) / 2; j > 0; j--)  //从非叶子节点开始,向下调整
        {
            AdjustDown(j);
        }
    }

bool Isempty()   //判空
    {
        return _a.empty();
    }

T& top()    //取栈顶元素
    {
        if (!_a.empty())
        return _a[0];
    }

void Pop()   //在树中删除节点
    {
        if (_a.empty())
            return;

//交换第一个节点与最后一个节点的值,删除最后一个节点,然后向下调整
        swap(_a[0], _a[_a.size() - 1]);  
        _a.pop_back();
        if (!_a.empty())
            AdjustDown(0);
    }

void Push(const T& x)    //在树中插入节点
    {
        _a.push_back(x);
        if (!_a.empty())
            AdjustUp(_a.size() - 1);   //向上调整最后一个节点
    }
protected:
    void AdjustUp(int child)    //向上调整
    {    
        while (child > 0)
        {
            Compare com;
            int parent = (child - 1) / 2;
            if (com(_a[parent] , _a[child]))
            {
                swap(_a[parent], _a[child]);
                child = parent;
            }    
            else
            {
                break;
            }
        }
    }

void AdjustDown(int root)   //小堆,子节点大于父节点
    {
        int parent = root;
        int child = parent * 2 + 1;   //先为左节点
        while (child+1<_a.size())
        {
            Compare com;
            if (_a[child + 1]>_a[child])   //找出孩子节点中较大的那个数
                ++child;

if (child<_a.size()&&com(_a[parent] , _a[child]))   //比较父节点与孩子节点的大小然后作出相应的处理
            {
                swap(_a[parent], _a[child]);
                parent = child;
                child = parent * 2 + 1;
            }
            else
            {
                break;
            }
        }
    }
private:
    vector<T>  _a;
};

Heap堆的更多相关文章

  1. Heap堆的理解以及在IAR中如何设置堆的大小

    文章首发于浩瀚先森博客 堆栈的概念在脑海里已经存在有一段时间了,今天就测试来整理下Heap堆.栈以后再说. 堆区不像全局变量和局部变量总是有指定的内存大小,它是为了在程序运行时动态分配内存而设定的一块 ...

  2. java - Stack栈和Heap堆的区别

    首先分清楚Stack,Heap的中文翻译:Stack—栈,Heap—堆.         在中文里,Stack可以翻译为“堆栈”,所以我直接查找了计算机术语里面堆和栈开头的词语:        堆存储 ...

  3. Heap(堆)和stack(栈)有的区别是什么。

    java的内存分为两类,一类是栈内存,一类是堆内存.栈内存是指程序进入一个方法时,会为这个方法单独分配一块私属存储空间,用于存储这个方法内部的局部变量,当这个方法结束时,分配给这个方法的栈会释放,这个 ...

  4. JAVA Stack栈和Heap堆的区别(转)

          首先分清楚Stack,Heap的中文翻译:Stack—栈,Heap—堆.         在中文里,Stack可以翻译为“堆栈”,所以我直接查找了计算机术语里面堆和栈开头的词语:      ...

  5. 逻辑运算符、三元运算符、for循环、stack(栈),heap(堆),方法区,静态域

    Lesson One 2018-04-17 19:58:39 逻辑运算符(用于逻辑运算,左右两边都是 true 或 false) 逻辑与-& 和 短路与-&& 区别: & ...

  6. Stack栈 Heap堆

    Stack(栈) 栈(stack) 又名堆栈,它是一种运算受限的线性表.其限制是仅允许在表的一端进行插入和删除运算.这一端被称为栈顶,相对地,把另一端称为栈底.向一个栈插入新元素又称作进栈.入栈或压栈 ...

  7. linux heap堆分配

    heap堆分配在用户层面:malloc函数用于heap内存分配 void* malloc(size_t size); 进程的虚拟内存地址布局: 对用户来说,主要关注的空间是User Space.将Us ...

  8. 如何给女朋友讲明白:Java 中 Stack(栈) 与 Heap(堆)

    背景 Java 中 Stack(栈) 与 Heap(堆) 是面试中被经常问到的一个话题. 有没有对 Java 中 Stack(栈) 与 Heap(堆) 烂熟于心的童鞋,请举手!!!(怎么没人举手-) ...

  9. Delphi 堆栈 [ heap(堆) 和 stack(栈) ]

    程序需要的内存空间分为 heap(堆) 和 stack(栈); heap(堆) 是自由存储区, stack(栈) 是自动存储区; 使用 heap 需要手动申请.手动释放, 使用 stack 是自动申请 ...

  10. 栈 堆 stack heap 堆内存 栈内存 内存分配中的堆和栈 掌握堆内存的权柄就是返回的指针 栈是面向线程的而堆是面向进程的。 new/delete and malloc/ free 指针与内存模型

    小结: 1.栈内存 为什么快? Due to this nature, the process of storing and retrieving data from the stack is ver ...

随机推荐

  1. Apache Hive (二)Hive安装

    转自:https://www.cnblogs.com/qingyunzong/p/8708057.html Hive的下载 下载地址http://mirrors.hust.edu.cn/apache/ ...

  2. 636. Exclusive Time of Functions 进程的执行时间

    [抄题]: Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU ...

  3. 637. Average of Levels in Binary Tree 二叉树的层次遍历再求均值

    [抄题]: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

  4. 云计算 Restfull API 设计之旅

    http://fedoraproject.org/wiki/Cloud_APIs_REST_Style_Guide#Introduction_to_REST   http://docs.spring. ...

  5. EZOJ #227

    传送门 分析 我们发现第一段数和最后一段数对答案的贡献系数为1/-1,其余为0/2/-2 而且对于相邻两段不能系数均非0 于是可以dp 代码 #include<iostream> #inc ...

  6. 9个使用前必须再三小心的 Linux 命令-乾颐堂

    Linux shell/terminal 命令非常强大,即使一个简单的命令就可能导致文件夹.文件或者路径文件夹等被删除. 在一些情况下,Linux 甚至不会询问你而直接执行命令,导致你丢失各种数据信息 ...

  7. [C#] IEnumerable vs IQueryable

    这篇博客将介绍IEnumerable和IQueryable之间的区别. 1. IQueryable是继承自IEnumerable接口的.所以IEnumerable能做的,IQueryable都能做. ...

  8. mongodb学习-创建唯一索引(在已存在的集合创建)

    如果在已存在的集合创建,可能会存在相同的值如下: 我们可以使用(2.x版本) db.users.ensureIndex({uid:1, name:1}, {unique:true, dropDups: ...

  9. java.lang.NoClassDefFoundError: Could not initialize class com解决方案

    编写的时候遇到这样一个bug, java.lang.NoClassDefFoundError: Could not initialize class com 纠结了两天多,但是,没有找到答案,这个问题 ...

  10. 在win10 + ie11上使用控件

    1.1. 在Win10+IE11上提示创建文件错误的问题 解决方法: 1.打开Internet选项   2.取消勾选启用保护模式   选择"不再显示此消息"