Javatpoint Logo
Javatpoint Logo

How do you check if a given array represents a heap?

Binary heaps are a useful data structure that can be implemented using arrays. They provide efficient access to the minimum or maximum element in a dataset. Binary heaps are commonly used to implement priority queues and graph algorithms like Dijkstra's algorithm.

It's important to validate that a given array accurately represents the binary heap structure to utilize a binary heap. This article will explore an O(N) algorithm to check if an array corresponds to a valid binary max or min heap. This involves verifying two key properties - the complete binary tree structure and the heap ordering property.

A binary heap is typically represented as an array that mirrors the structure of a complete binary tree. This provides an efficient way to store the elements while maintaining the relationship between parent and child nodes. Before using an array as a binary heap, it is useful to validate that it represents a valid heap. This article will walk through an O(N) algorithm to check that an array satisfies the required properties to function as a binary max or min heap. We will look at how to check for the complete binary tree structure and validate the heap ordering property.

How do you check if a given array represents a heap

What are Arrays?

An array is a data structure that stores a collection of elements sequentially in memory. Each element can be accessed directly by its index, which makes accessing and modifying elements efficient.

Arrays have fixed sizes set when they are created. The elements in an array can belong to any data type, like integers, strings, objects, etc. All elements in the same array must be of the same data type.

Arrays are useful for storing and accessing sequential data, looking up elements by index, and looping through the elements efficiently. However, adding or removing elements from anywhere but the end requires shifting other elements in memory.

Binary heaps are commonly implemented using arrays because the array indices align with the tree structure of the heap. The parent-child relationships can be derived mathematically from the indices. This allows efficient traversal, swapping, and maintenance of the heap structure.

What are Heaps?

A heap is a specialized tree-based data structure that satisfies the "heap property" - the value of each node is greater than or equal to (max heap) or less than or equal to (min-heap) the values of its child nodes.

Heaps efficiently provide access to the min or max element, allowing retrieval of the extreme value in constant time. Heaps are commonly implemented using arrays.

The heap property enables efficient insertion and removal of elements while maintaining order. Elements can be inserted at the end and bubbled up or down to satisfy the heap property. Removal always takes from the root node.

Heaps are useful when repeatedly finding the minimum or maximum value is needed, such as with priority queues. They provide efficient O(log n) insertion and removal while keeping the max/min value accessible.

Binary heaps have at most two child nodes per parent and are a specific type of heap structure. Binary heaps are often used to implement priority queues and graph algorithms.

Properties of Heaps:

  • Heaps are complete binary trees - all levels except the last are filled, and nodes are added from left to right.
  • Heaps satisfy the heap property - each node is greater than or equal to (max heap) or less than or equal to (min-heap) its child nodes.
  • Insertion and removal of elements maintain the heap property.
  • The root node contains the maximum or minimum value.
  • The max/min element can be accessed in constant O(1) time.

Properties of Arrays:

  • Arrays consist of contiguous memory locations to store elements
  • Elements are stored sequentially in memory in an array
  • Arrays have fixed sizes set on the creation
  • Elements can be accessed directly by their index in constant O(1) time
  • All elements in an array must be of the same data type
  • Adding/removing elements other than at the end is inefficient as it requires shifting

By leveraging these properties of arrays, binary heaps can be efficiently represented as arrays. The indices map to the tree structure. This allows easy traversal and maintenance of the heap structure.

Steps to check

To check if an array represents a binary heap, we need to validate two key properties:

1. The array must represent a complete binary tree structure

A complete binary tree fills all levels except the last, and nodes are filled from left to right. We can traverse the array and check the tree structure using these rules:

  • The root node is at index 0
  • For any node at index i:
    • Its left child is at index 2*i + 1
    • Its right child is at index 2*i + 2

We iterate through the array, using the formulas to compute each node's children indices. The tree structure is invalid if an expected child is missing or the indices don't align.

For example, for the node at index 1, its left child should be at 21 + 1 = 3 and its right child at 21 + 2 = 4. The structure is not a complete binary tree if any expected child is out of bounds or absent.

We complete one traversal to validate the tree shape. If any violation is found, we can conclude the array does not represent a binary heap.

2. The heap property must hold for every node.

The heap property means each node is greater than or equal to each of its children for a max heap or less than or equal to a min-heap.

We traverse the array again, comparing each node to its child nodes. We access the array at the computed child indices using the formulas above. The property is violated if the node value is smaller than any child node (for a max heap).

For example, if array[2] < array[5] (first child) or array[2] < array[6] (second child), we have a violation.

The array cannot represent a valid binary heap if any node is found where the heap property does not hold.

We must traverse the entire array, checking the heap property at each node. If both traversals are complete with no violations found, the array fulfils all requirements and represents a valid binary max or min heap.

So, in summary, we traverse once, checking the structure, then traverse again, validating the heap property. This algorithm verifies an array as a binary heap in O(N) time.

Python Program

Output:

How do you check if a given array represents a heap

Explanation

  1. Define the isMaxHeap() function, which takes the array (arr) and number of elements (n) as input.
  2. Calculate the index of the last internal node as int((n-2)/2). It is where we will loop till since a heap's property must hold for all internal nodes.
  3. Start a for loop from the root node index 0 to the last internal node index.
  4. Calculate the index of the left and right child of current node i using:
    • Left child index = 2*i + 1
    • Right child index = 2*i + 2
  1. Check if the left child is greater than the node using the:
  2. if arr[2*i + 1] > arr[i]
  3. If yes, the property is violated, so return False.
  4. Check if the right child exists (2i + 2 < n) and if it is greater than the node:
  5. if (2i + 2 < n and arr[2*i + 2] > arr[i])
  6. If yes, property violated, so return False.
  7. If the loop is completed without returning False, the max-heap property is held for all internal nodes. Return True.
  8. Call isMaxHeap() in the driver code on the input array and print the return value.

So, in summary, we traverse from the root to the last internal node, using formulas to access child nodes and validate the heap ordering property at each step. Returning False immediately if any violation is found.

The time complexity is O(N) as we visit each node exactly once.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news