Open In App

When building a Heap, is the structure of Heap unique?

Last Updated : 19 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

What is Heap?

A heap is a tree based data structure where the tree is a complete binary tree that maintains the property that either the children of a node are less than itself (max heap) or the children are greater than the node (min heap).

Properties of Heap:

Structural Property: This property states that it should be A Complete Binary Tree

For example:

Max Heap

Max Heap

Ordering Property: The heap should follow either Max-heap or Min-heap Property. 

  • If it is Min-heap, the parent node should be less than the child node and 
  • In the case of a Max-heap, the parent node should be greater than the child node. 

These rules should be followed at each level but the order of bottom child nodes can change we can understand it with an example(We are taking Max heap as an example):

Max Heap(Figure 1)

Max Heap(Figure 1)

Max Heap(Figure 2)

Max Heap(Figure 2)

Is the structure of a Heap unique?

From the above two examples, we can see that the heap follows the Max-heap property. And here we can also see that in the first example the child nodes at the bottom level are (5 and 4), (6 and 3) but in the second nodes the child node at the bottom level are (4 and 5), (3 and 6). The place is changed of nodes at the bottom of the heap but it satisfies the condition of Max-heap.

  • The order of child or leaf nodes is not fixed, So there will be 4! = 24 valid arrangements possible.
  • So which arrangement we get is dependent on the order of insertions and removals or insertion and removal algorithms. Or in the case of making a heap from an array(O(n) case), the order of the array when we start.

There are actually two common types of heaps: min heaps and max heaps.

In a min heap, the parent nodes have values that are less than or equal to the values of their children. In a max heap, the parent nodes have values that are greater than or equal to the values of their children.

The structure of a heap is not unique, because there can be multiple valid heap structures for a given set of values. However, the property of being a min heap or a max heap is unique for a given set of values.

For example, consider the following set of values: [5, 8, 9, 10, 11, 13]. This set of values could be arranged in a min heap as follows:

     5
    / \
  8  10
  / \    \
 9  11 13
This same set of values could also be arranged in a max heap as follows:

   13
    / \
  11 9
  / \  /
 5 8 10
Both of these structures are valid heap structures, but one is a min heap and the other is a max heap.

Heaps are complete binary trees, which means that they are filled in level-order, with the exception that the last level may not be completely filled. This means that the tree is always balanced, which is one of the key properties that makes heaps efficient.

Heaps can be implemented using arrays. In this case, the children of the element at index i are at indices 2i+1 and 2i+2, and the parent of the element at index i is at index (i-1)/2 (assuming that the array is 0-indexed).

Heaps have a number of useful properties that make them useful in a variety of algorithms. For example:

The minimum (or maximum) element can be found in a constant number of steps (this is the root of the heap).
The heap property can be restored by “bubbling up” or “sinking down” an element that has been added or removed.
Heapsort, a sorting algorithm, can be implemented using a heap.

Conclusion:

From the above images, we can conclude while building a heap, the structure of the heap is not Unique.


Previous Article
Next Article

Similar Reads

Check if a triplet of buildings can be selected such that the third building is taller than the first building and smaller than the second building
Given an array arr[] consisting of N integers, where each array element represents the height of a building situated on the X co-ordinates, the task is to check if it is possible to select 3 buildings, such that the third selected building is taller than the first selected building and shorter than the second selected building. Examples: Input: arr
12 min read
Difference between Binary Heap, Binomial Heap and Fibonacci Heap
Binary Heap:A Binary Heap is a Binary Tree with following properties. It’s a complete binary tree i.e., all levels are completely filled except possibly the last level and the last level has all keys as left as possible. This property of Binary Heap makes them suitable to be stored in an array. A Binary Heap is either Min Heap or Max Heap. In a Min
2 min read
Building Heap from Array
Given an array of N elements. The task is to build a Binary Heap from the given array. The heap can be either Max Heap or Min Heap. Examples: Input: arr[] = {4, 10, 3, 5, 1}Output: Corresponding Max-Heap: 10 / \ 5 3 / \4 1 Input: arr[] = {1, 3, 5, 4, 6, 13, 10, 9, 8, 15, 17}Output: Corresponding Max-Heap: 17 / \ 15 13 / \ / \ 9 6 5 10 / \ / \ 4 8 3
14 min read
Time Complexity of building a heap
Consider the following algorithm for building a Heap of an input array A. BUILD-HEAP(A) heapsize := size(A); for i := floor(heapsize/2) downto 1 do HEAPIFY(A, i); end for END A quick look over the above algorithm suggests that the running time is [Tex]O(n * lg(n)) [/Tex] since each call to Heapify costs [Tex]O(lg(n)) [/Tex]and Build-Heap makes [Tex
2 min read
Convert Min Heap to Max Heap
Given an array representation of min Heap, convert it to max Heap. Examples: Input: arr[] = {3, 5, 9, 6, 8, 20, 10, 12, 18, 9} 3 / \ 5 9 / \ / \ 6 8 20 10 / \ /12 18 9 Output: arr[] = {20, 18, 10, 12, 9, 9, 3, 5, 6, 8} 20 / \ 18 10 / \ / \ 12 9 9 3 / \ /5 6 8 Input: arr[] = {3, 4, 8, 11, 13}Output: arr[] = {13, 11, 8, 4, 3} Approach: To solve the p
10 min read
Heap Sort for decreasing order using min heap
Given an array of elements, sort the array in decreasing order using min heap. Examples: Input : arr[] = {5, 3, 10, 1} Output : arr[] = {10, 5, 3, 1} Input : arr[] = {1, 50, 100, 25} Output : arr[] = {100, 50, 25, 1} Prerequisite: Heap sort using min heap. Algorithm : Build a min heap from the input data. At this point, the smallest item is stored
13 min read
Difference between Min Heap and Max Heap
A Heap is a special Tree-based data structure in which the tree is a complete binary tree. Since a heap is a complete binary tree, a heap with N nodes has log N height. It is useful to remove the highest or lowest priority element. It is typically represented as an array. There are two types of Heaps in the data structure. Min-HeapIn a Min-Heap the
3 min read
What's the relationship between "a" heap and "the" heap?
A Heap: "A Heap" refers to the heap data structure where we can store data in a specific order. Heap is a Tree-based data structure where the tree is a complete binary tree. Heap is basically of two types: Max-Heap: The key at the Root node of the tree will be the greatest among all the keys present in that heap and the same property will be follow
15+ min read
Top 50 Problems on Heap Data Structure asked in SDE Interviews
A Heap is a special Tree-based Data Structure in which the tree is a complete binary tree. Generally, heaps are of two types: Max-Heap and Min-Heap. To know more about this Data Structure in-depth refer to the Tutorial on Heap Data-Structure. Given below are the most frequently asked interview questions on Heaps:  Easy Interview Questions on Heap D
2 min read
Stack Vs Heap Data Structure
What is Stack? A stack is a linear data structure where the last element entered exits first. The order of stack data structure might be LIFO, FILO: According to this technique, the piece that is in last will come out first. As an example, consider a stack of dishes stacked on top of each other. The plate we put last is on top, and because we take
3 min read
Practice Tags :