Published on

Heap

Authors
  • avatar
    Name
    Simon Kurbiel
    Twitter

Introduction

A heap is a data-structure that implements a priority queue, by using a complete-binary- tree. Each node of the tree contains one element, and they are organized by their key values, such that

key of every node ≤ keys of its left and right children.\text{key of every node ≤ keys of its left and right children}.

Therefore, the root has the smallest key value. Below is an example heap with 6 nodes. The values inside the nodes are the key values. heap

Finally, a heap of nn nodes is represented efficiently by an array A[1:n]A[1: n], where A[i]A[i] represents node ii. For example, the array representation of the above heap is as shown below.

i123456
A[i]123459