# 3. The Heap Data Structure

<span class="ng-star-inserted">Now that we understand the concept of a binary tree, we can define a Heap. A </span>**<span class="ng-star-inserted">Heap</span>**<span class="ng-star-inserted"> is a specialized tree-based data structure that satisfies two specific properties.</span>

#### **<span class="ng-star-inserted">The Two Defining Properties of a Heap</span>**

<span class="ng-star-inserted">For a binary tree to be considered a Heap, it must adhere to the following rules:</span>

1. **<span class="ng-star-inserted">Structure Property: It must be an </span><span class="ng-star-inserted">Essentially Complete Binary Tree</span><span class="ng-star-inserted">.</span>**  
    <span class="ng-star-inserted">This means that the tree is completely filled on all levels, with the possible exception of the last level, which must be filled from </span>**<span class="ng-star-inserted">left to right</span>**<span class="ng-star-inserted"> without any gaps. This "no gaps" property is crucial, as it allows a heap to be stored efficiently in an array.</span>
2. **<span class="ng-star-inserted">Heap Property (Order Property):</span>**<span class="ng-star-inserted"> The nodes must be ordered in a specific way relative to their children. This ordering defines the type of heap.</span>

#### **<span class="ng-star-inserted">Types of Heaps</span>**

<span class="ng-star-inserted">There are two main types of heaps, distinguished by the Heap Property they enforce:</span>

- **<span class="ng-star-inserted">Max-Heap:</span>**<span class="ng-star-inserted"> The value of every node is </span>**<span class="ng-star-inserted">greater than or equal to</span>**<span class="ng-star-inserted"> the value of each of its children.</span>
    
    
    - **<span class="ng-star-inserted">Implication:</span>**<span class="ng-star-inserted"> In a Max-Heap, the </span>**<span class="ng-star-inserted">largest element</span>**<span class="ng-star-inserted"> in the entire collection is always located at the </span>**<span class="ng-star-inserted">root</span>**<span class="ng-star-inserted"> of the tree. This is the type of heap used for Heap Sort.</span>
    - <span class="ng-star-inserted">Example: A tree with root 10 and children 8 and 9 is a valid Max-Heap at the top level.</span>
- **<span class="ng-star-inserted">Min-Heap:</span>**<span class="ng-star-inserted"> The value of every node is </span>**<span class="ng-star-inserted">less than or equal to</span>**<span class="ng-star-inserted"> the value of each of its children.</span>
    
    
    - **<span class="ng-star-inserted">Implication:</span>**<span class="ng-star-inserted"> In a Min-Heap, the </span>**<span class="ng-star-inserted">smallest element</span>**<span class="ng-star-inserted"> is always located at the </span>**<span class="ng-star-inserted">root</span>**<span class="ng-star-inserted">. This structure is commonly used to implement Priority Queues.</span>
    - <span class="ng-star-inserted">Example: A tree with root 2 and children 5 and 3 is a valid Min-Heap at the top level.</span>

#### **<span class="ng-star-inserted">The Array Representation</span>**

<span class="ng-star-inserted">The </span><span class="ng-star-inserted">complete binary tree</span><span class="ng-star-inserted"> property is precisely what makes an array a perfect and highly efficient way to store a heap. The hierarchical relationships are not stored with pointers, but are instead calculated mathematically based on an element's index.</span>

<span class="ng-star-inserted">For an element at a </span>**<span class="ng-star-inserted">zero-based index </span><span class="inline-code ng-star-inserted">i</span>**<span class="ng-star-inserted"> in an array representing a heap:</span>

- <span class="ng-star-inserted">The index of its </span>**<span class="ng-star-inserted">parent</span>**<span class="ng-star-inserted"> is calculated as: </span><span class="inline-code ng-star-inserted">floor((i - 1) / 2)</span>
- <span class="ng-star-inserted">The index of its </span>**<span class="ng-star-inserted">left child</span>**<span class="ng-star-inserted"> is calculated as: </span><span class="inline-code ng-star-inserted">2 \* i + 1</span>
- <span class="ng-star-inserted">The index of its </span>**<span class="ng-star-inserted">right child</span>**<span class="ng-star-inserted"> is calculated as: </span><span class="inline-code ng-star-inserted">2 \* i + 2</span>

<span class="ng-star-inserted">For example, for the node at index <span class="inline-code ng-star-inserted">i = 3</span>:</span>

- <span class="ng-star-inserted">Its parent is at index <span class="inline-code ng-star-inserted">floor((3 - 1) / 2) = floor(1) = 1</span>.</span>
- <span class="ng-star-inserted">Its left child would be at index <span class="inline-code ng-star-inserted">2 \* 3 + 1 = 7</span>.</span>