堆(Heap)和二叉堆(Binary heap)
堆(Heap)
The operations commonly performed with a heap are:
- create-heap: create an empty heap
- heapify: create a heap out of given array of elements
- find-max or find-min: find the maximum item of a max-heap or a minimum item of a min-heap (aka, peek)
- delete-max or delete-min: removing the root node of a max- or min-heap, respectively
- increase-key or decrease-key: updating a key within a max- or min-heap, respectively
- insert: adding a new key to the heap
- merge: joining two heaps to form a valid new heap containing all the elements of both.
- meld(h1,h2): Return the heap formed by taking the union of the item-disjoint heaps h1 and h2. Melding destroys h1 and h2.
- size: return the number of items in the heap.
- isEmpty(): returns true if the heap is empty, false otherwise.
- ExtractMin() or ExtractMax(): Returns the node of minimum value from a min heap [or maximum value from a max heap] after removing it from the heap
- Union(): Creates a new heap by joining two heaps given as input.
- Shift-up: Move a node up in the tree, as long as needed (depending on the heap condition: min-heap or max-heap)
- Shift-down: Move a node down in the tree, similar to Shift-up
二叉堆(Binary heap)
A binary heap is a heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints:
- Shape property
- A binary heap is a complete binary tree; that is, all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of that level are filled from left to right.
- Heap property
- All nodes are either greater than or equal to or less than or equal to each of its children, according to a comparison predicate defined for the heap.
Heaps with a mathematical "greater than or equal to" (≥) comparison predicate are called max-heaps; those with a mathematical "less than or equal to" (≤) comparison predicate are called min-heaps. Min-heaps are often used to implement priority queues.
堆的两个特性:
- 结构性:用数组表示的完全二叉树;
- 有序性:任一结点的关键字是其子树所有结点的最大值(或最小值);最大堆(MaxHeap)也称为“大顶堆”;最小堆(MinHeap)也称为“小顶堆”;
最大堆和最小堆:
不是堆:
注意:从根结点到任意结点路径上结点序列的有序性!
【堆的抽象数据类型描述】
类型名称:最大堆(MaxHeap)
数据对象集:完全二叉树,每个结点的元素值不小于其子结点的元素值
操作集:最大堆H ∈ MaxHeap,元素item ∈ ElementType,主要操作有:
- MaxHeap Create(int MaxSize):创建一个空的最大堆;
- Boolean IsFull(MaxHeap H):判断最大堆H是否已满;
- Insert(MaxHeap H,ElementType item):将元素item插入最大堆H;
- Boolean IsEmpty(MaxHeap H):判断最大堆H是否为空;
- ElementType DeleteMax(MaxHeap H):返回H中最大元素(高优先级);
【最大堆的创建】
【最大堆的插入】
【最大堆的删除】
【最大堆的建立】
堆(Heap)和二叉堆(Binary heap)的更多相关文章
- 数据结构 之 二叉堆(Heap)
注:本节主要讨论最大堆(最小堆同理). 一.堆的概念 堆,又称二叉堆.同二叉查找树一样,堆也有两个性质,即结构性和堆序性. 1.结构性质: 堆是一棵被完全填满的二叉树,有可能的 ...
- D&F学数据结构系列——二叉堆
二叉堆(binary heap) 二叉堆数据结构是一种数组对象,它可以被视为一棵完全二叉树.同二叉查找树一样,堆也有两个性质,即结构性和堆序性.对于数组中任意位置i上的元素,其左儿子在位置2i上,右儿 ...
- 笔试算法题(46):简介 - 二叉堆 & 二项树 & 二项堆 & 斐波那契堆
二叉堆(Binary Heap) 二叉堆是完全二叉树(或者近似完全二叉树):其满足堆的特性:父节点的值>=(<=)任何一个子节点的键值,并且每个左子树或者右子树都是一 个二叉堆(最小堆或者 ...
- 二叉堆 及 大根堆的python实现
Python 二叉堆(binary heap) 二叉堆是一种特殊的堆,二叉堆是完全二叉树或者是近似完全二叉树.二叉堆满足堆特性:父节点的键值总是保持固定的序关系于任何一个子节点的键值,且每个节点的左子 ...
- Python实现二叉堆
Python实现二叉堆 二叉堆是一种特殊的堆,二叉堆是完全二元树(二叉树)或者是近似完全二元树(二叉树).二叉堆有两种:最大堆和最小堆.最大堆:父结点的键值总是大于或等于任何一个子节点的键值:最小堆: ...
- 数据结构图文解析之:二叉堆详解及C++模板实现
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...
- 二叉堆(一)之 图文解析 和 C语言的实现
概要 本章介绍二叉堆,二叉堆就是通常我们所说的数据结构中"堆"中的一种.和以往一样,本文会先对二叉堆的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本 ...
- 二叉堆(二)之 C++的实现
概要 上一章介绍了堆和二叉堆的基本概念,并通过C语言实现了二叉堆.本章是二叉堆的C++实现. 目录1. 二叉堆的介绍2. 二叉堆的图文解析3. 二叉堆的C++实现(完整源码)4. 二叉堆的C++测试程 ...
- 二叉堆(三)之 Java的实现
概要 前面分别通过C和C++实现了二叉堆,本章给出二叉堆的Java版本.还是那句话,它们的原理一样,择其一了解即可. 目录1. 二叉堆的介绍2. 二叉堆的图文解析3. 二叉堆的Java实现(完整源码) ...
- 二叉堆的实现(数组)——c++
二叉堆的介绍 二叉堆是完全二元树或者是近似完全二元树,按照数据的排列方式可以分为两种:最大堆和最小堆.最大堆:父结点的键值总是大于或等于任何一个子节点的键值:最小堆:父结点的键值总是小于或等于任何一个 ...
随机推荐
- 所思所想 js模板引擎
将服务端生成的HTML标记的事情交给了客户端来做 那么服务端的职责是什么呢? 职责就是处理最终的返回结果,纯数据 handler
- 如何解决链入js,innerHTML中文乱码问题呢?
描述:发生在做suhuotong网站的时候,添加在线客服代码的时候三个地方1.将js以UTF-8无BOM编码:VS修改或者使用NotePad++修改2.<meta http-equiv=&quo ...
- [转]使用CSS3实现树形控件
下面是一个使用HTML的ul标签制作的关于国家区划的组织结构图. 中国 北京 广东省 广州市 韶关市 海南省 海口市 美兰区 龙华区 秀英区 琼山区 三亚市 安徽省 合肥市 安庆市 United St ...
- VS2005保存文件很慢
VS2005出了点毛病,边的出奇的慢,简直不可忍受. 症状是:保存文件很慢,哪怕是修改一个变量,也要等上大概20秒 保存文件的时候,VS2005会在局域网内寻找一个主机当这个主机不在线的时候vs200 ...
- python 爬虫
import urllib2 as url import re urls = 'http://www.php100.com/html/it/' headers = {'User-Agent':'Moz ...
- python交互模式下cp65001异常
unknown encoding: cp65001异常 python安装后进入命令行交互模式,输入任何代码都报unknown encoding: cp65001异常 需要将编码(UTF-8)修改为 简 ...
- 面试题目-findmax的实现
#include <vector> #include <iostream> #include "printCollection.h" using names ...
- VisualSVN SERVER的安装和使用
SVN Server安装 Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说.下载的网址是:http://subversion.apache.org/packages. ...
- POJ 3207 2-sat
题目大意: 在圆上顺时针n个点,给定m个连接,可以通过圆内或者圆外相交,问能不能找到一种方式,使这些连接的边都不相交 这里很容易看出的是,这些边只有在圆外或者圆内两种连接方式,而且必须选择其中一种 所 ...
- K2 BPM医疗行业EMS解决方案
EMS,即Event Management System,K2医疗行业EMS解决方案包括四方面的内容. 详情链接:http://www.k2software.cn/zh-hans/ems-soluti ...