Professional Documents
Culture Documents
Week 9
Heap and Deap
1. Definition of HEAP
HEAP is a specialized tree-based data structure that satisfies the heap property: if “B” is
a child node of “A”, then key(“A”) ≤ key(“B”).
This implies that an element with the smallest key is always in the root node, and so such
a heap is sometimes called a min-heap. (Alternatively, if the comparison is reversed, the
greatest element is always in the root node, which results in a max-heap.)
The several variants of heaps are the prototypical most efficient implementations of the
abstract data type priority queues. Besides that, a heap is a complete binary tree, which
leads to the idea of storing it using an array. By utilizing array-based representation, we
can reduce memory costs while tree navigation remains quite simple.
A heap is stored in array level by level. The topmost level contains root only. It is mapped
to the first element of an array (with index 0).
Root's children are mapped to the second and third elements and so on. A heap is a
complete binary tree, which guarantees, that heap's nodes take up places in the array
compactly, making the mapping quite efficient.
Assume that the data inputted in Heap are 1, 5, 10, 13, 15, 18 and 11. Then, the heap will
look like
Left(i) = 2 * n + 1 Right(i) = 2 * n + 2
for the example : 2*0 + 1 = 1 for the example : 2*0 + 2 = 2
value in index 1 is 5 value in index 2 is 10
Parent(i) = (n - 1) / 2
1. Reema Thareja,. 2011. Data structures using C. OXFOR. New Delhi. ISBN:978-0-19-
806544-9
2.