### Storing a heap using an array

• Common representation of heaps: uses an array !!!

• Common practice:

• The heap data structure is logically a binary tree

• But:

 The heap is often stored using an array !!!

• Case in point: a famous heap-based algorithm is

 The heap sort algorithm

The heap sort algorithm always sort the data stored in arrays !!!

• How to store an heap using an array

• How to store a heap using an array a[ ]:

• Store the root node in the array element a[1]

 The array element a[0] is not used !!!

• Store the level 1 nodes from left to right into the subsequent elements in the array

• Store the level 2 nodes from left to right into the subsequent elements in the array

• And so on....

• Example:

• Store the root node in the array element a[1]

• Store the level 1 nodes from left to right into the subsequent elements in the array

• Store the level 2 nodes from left to right into the subsequent elements in the array

• Final representation:

• Why do we store nodes of a heap in an array

• Fact:

 We can obtain the parent/child node relations easily in the array representation !!!

• How to find the children nodes of the node a[k]:

 The left child node of a[k] is a[2×k] The right child node of a[k] is a[2×k+1]

Examples:

• Children nodes of the node a[1] (a[2] and a[3]):

• Children nodes of the node a[2] (a[4] and a[5]):

• Child node of the node a[3] (only a left child a[4]):

• How to find the parent node of the node a[k]:

• The parent node of a[k] is a[k/2]

Note:

 a[1] (= the root node) has no parent node

Examples:

• The parent node of a[6] is a[3]:

• The parent node of a[5] is a[2]:

• The parent node of a[4] is a[2]:

• Defining the Heap structure in Java (using an array)

• Note:

 Just like the Binary Search Tree, I will store a double value in the nodes of the heap (We can easily change them to store different type of data. The algorithms will not change)

• Variables used to represent a Heap:

 ``` public class Heap { public double a[]; // Each array element a[i] represents a node // Like I said, I store a double in a node // So I define an array of double... public int NNodes; // This is the number of nodes in the heap ... methods still need to be written ... } ```

• Constructor for the Heap object:

• The constructor must initialize the variables in the class:

• NNodes
• a

 In order to initialize the array variable a, we must create an array using the new operator !!!

Here is is code of the constructor added to the class:

 ``` public class Heap { public double a[]; // Each array element a[i] represents a node // Like I said, I store a double in a node // So I define an array of double... public int NNodes; // This is the number of nodes in the heap /* ===================================================================== Constructor: construct a heap that can contain "size" number of nodes ===================================================================== */ public Heap( int size ) { a = new double[ size + 1 ]; // Because a[0] is not used.... NNodes = 0; // There are 0 nodes in the heap right now... } ... Other methods still need to be written ... } ```