Binary Heap

(referrence: cmu_binary_heap)

Definition

A binary heap is a complete binary tree arranged in heap ordering property.

There are two types of ordering:

1. min-heap

The value of each node >= the value of its parent. Root is minimum-value element.

2. max-heap

The value of each node <= the value of its parent. Root is maximum-value element.

Usually, the word "heap" refers to a min-heap.

Example of min-heap

Example of max-heap

Array Implementation

A complete binary tree can be uniquely represented by storing its level order traversal in an array.

We skip the index zero cell of the array for the convenience of implementation. Consider k-th element of the array:

its left child index: 2 * k

its right child index: 2 * k + 1

its parent index: k / 2

Insert

The new element is initially appended to the end of the heap (as the last element of the array). The heap property is repaired by comparing the added element with its parent and moving the added element up a level (swapping positions with the parent).

 1 public void insert(Comparable x)
 2 {
 3     if(size == heap.length - 1) doubleSize();
 4
 5     //Insert a new item to the end of the array
 6     int pos = ++size;
 7
 8     //Percolate up
 9     for(; pos > 1 && x.compareTo(heap[pos/2]) < 0; pos = pos/2 )
10         heap[pos] = heap[pos/2];
11
12     heap[pos] = x;
13 }

Time complexity O(log n)

DeleteMin

1. Save last element value to root.

2. Decrease heap size by 1.

3. Restore the heap property.

Start from root, do follow steps in a loop until to the bottom:

Switch current (parent) value with smaller one of its two child.

Time complexity O(log n). Details can be checked here.

FindMin

Return first element.

In Java, PriorityQueue is based on a priority heap. The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queu construction time.

时间: 2024-08-08 13:56:25

Binary Heap的相关文章

二叉堆(binary heap)

堆(heap) 亦被称为:优先队列(priority queue),是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因而实际情况中某些时间较短的任务将等待很长时间才能结束,或者某些不短小,但具有重要性的作业,同样应当具有优先权.堆即为解决此类问题设计的一种数据结构. 本文地址:http://www.cnblogs.com/archimedes/p/binary-heap.html,转载请注明源地址. 逻辑定义 n个

C++之Binary Heap/Max Heap

1 #include <iostream> 2 #include <time.h> 3 #include <random> 4 5 using namespace std; 6 7 //Binary Heap; Max Heap; 8 9 class BinaryHeap 10 { 11 public: 12 BinaryHeap(); 13 BinaryHeap(int capacity); 14 ~BinaryHeap(); 15 16 int insert(int

堆(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)

9.STL简单binary heap的实现

我用VS2013写的程序(github ),queue版本的代码位于cghSTL/version/cghSTL-0.3.6.rar 所谓binary heap就是一种完全二叉树,也就是说,整颗binary tree除了对底层的叶节点外,是填满的,而最底层的叶节点由左至右不能有空隙. 完全二叉树内没有任何节点漏洞,这带来一个极大的好处:我们可以利用vector来存储所有节点.我们把vector的0号元素保留,那么当完全二叉树的某个节点位于vector的i处时,其左子树必然位于2i处,右子树必然位于

ADT - Binary Heap(二叉堆) &amp;&amp;

今天早上起来完成了一个完整的基于二叉堆实现的优先队列,其中包含最小优先和最大优先队列. 实际上上篇也说了优先队列的特性,通过建堆和堆排序操作,我们就已经看到了这种数据结构中的数据具有某种优先级别,要么非根节点大于他的子节点,要么就相反,在最大优先队列中最大优先级别就是指节点值最大的数据为根节点,每次出队时肯定是最大的先出去,反之则是最小优先队列,但要注意插入时的数据不一定是最大或最小的,优先队列会通过一点小技巧找到所有节点之间的关系并对应起来,重新使得你随意插入的数据满足优先队列的特性,因而这种

[email&#160;protected] Implementation of minimal heap

A binary heap is a heap data struc­ture cre­ated using a binary tree. binary tree has two rules - Binary Heap has to be com­plete binary tree at all lev­els except the last level. This is called shape prop­erty. All nodes are either greater than equa

Heap和Heapify

最近复习数据结构,又回去再看塞神的课件,看到PriorityQueue的实现.自己也根据塞神的代码写一写. 下面使用Binary Heap实现了一个简单的 Max-oriented PriorityQueue. 这里Binary Heap我们使用的是array represetation,数组形式. 第0个元素我们留空,从第一个元素开始存储, 第一个元素也将是PQ里最大的元素. 特点是假如父节点位置是 k, 那么两个子节点的位置就是 2 * k 和 2 * k + 1.这样很方便计算,知道父节点

c++ heap学习

heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制. 而这个实现机制中的max-heap实际上是以一个vector表现的完全二叉树(complete binary tree).二叉堆(binary heap)就是i一种完全二叉树.也即是.整棵二叉树除了最底层的叶节点以外,都是填满的,而最低层的叶子结点必须是从左到右不留空隙.至于max-heap和min-heap,前者的任何一个父亲结

Heap and HashHeap

Heap 堆(英语:Heap)是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因为实际情况中某些时间较短的任务将等待很长时间才能结束,或者某些不短小,但具有重要性的作业,同样应当具有优先权.堆即为解决此类问题设计的一种数据结构. 逻辑定义 n个元素序列{k1,k2...ki...kn},当且仅当满足下列关系时称之为堆:(ki <= k2i,ki <= k2i+1)或者(ki >= k2i,ki >=