Heap Data Structure
Heap Data Structure
Heap Data Structure
In data structures,
• Heap is a specialized data structure.
• It has special characteristics.
• A heap may be implemented using a n-ary tree.
Binary Heap-
A binary heap is a Binary Tree with the following two properties-
Ordering Property
• Structural Property
1. Ordering Property-
By this property,
• Elements in the heap tree are arranged in specific order.
• This gives rise to two types of heaps- min heap and max heap.
2. Structural Property-
By this property,
• Binary heap is an almost complete binary tree.
• It has all its levels completely filled except possibly the last level.
• The last level is strictly filled from left to right.
1. Max Heap-
Max Heap conforms to the above properties of heap.
• In max heap, every node contains greater or equal value element than its child
nodes.
• Thus, root node contains the largest value element.
Example-
2. Min Heap-
Example-
Consider the following example of min heap-
Important Notes-
Note-01:
• Level order traversal technique may be used to achieve the array representation
of a heap tree.
• Array representation of a heap never contains any empty indices in between.
• However, array representation of a binary tree may contain some empty indices
in between.
Note-02:
Solutions-
Part-01: 25, 14, 16, 13, 10, 8, 12-
The given array representation may be converted into the following structure-
Clearly,
• It is a complete binary tree.
• Every node contains a greater value element than its child nodes.
Clearly,
• It is a complete binary tree.
• Every node does not contain a greater value element than its child nodes. (Node
12)
• So, it is not a max heap.
• Every node does not contain a smaller value element than its child nodes.
• So, it is not a min heap.
Thus, the given array does not represents a heap.
Part-03: 25, 14, 12, 13, 10, 8, 16-
The given array representation may be converted into the following structure-
Clearly,
• It is a complete binary tree.
• Every node does not contain a greater value element than its child nodes. (Node
12)
• So, it is not a max heap.
• Every node does not contain a smaller value element than its child nodes.
• So, it is not a min heap.
Thus, the given array does not represents a heap.
Part-04: 25, 14, 13, 16, 10, 8, 12-
The given array representation may be converted into the following structure-
Clearly,
• It is a complete binary tree.
• Every node does not contain a greater value element than its child nodes. (Node
14)
• So, it is not a max heap.
• Every node does not contain a smaller value element than its child nodes.
• So, it is not a min heap.
• Thus, the given array does not represents a heap.
Heap Operations-
1. Search Operation
2. Insertion Operation
3. Deletion Operation
Here, we will discuss how these operations are performed on a max heap.
Max Heap-
• In max heap, every node contains greater or equal value element than its child
nodes.
• Thus, root node contains the largest value element.
Example-
The following heap is an example of a max heap-
Max Heap Operations-
We will discuss the construction of a max heap and how following operations are
performed on a max heap-
• Finding Maximum Operation
• Insertion Operation
• Deletion Operation
• In max heap, the root node always contains the maximum value element.
• So, we directly display the root node value as maximum value in max heap.
Insertion Operation-
Step-01:
Insert the new element as a next leaf node from left to right.
Step-02:
Deletion Operation-
Deletion Operation is performed to delete a particular element from the heap tree.
When it comes to deleting a node from the heap tree, following two cases are possible-
Case-01: Deletion Of Last Node-
Step-01:
Step-02:
Problem-01:
Step-01:
We convert the given array of elements into an almost complete binary tree-
Step-02:
Step-03:
• Node 5 contains greater element in its right child node.
• So, we swap node 5 and node 12.
Step-04:
Step-05:
This is the required max heap for the given array of elements.
Problem-02:
Solution-
Step-01:
We insert the new element 60 as a next leaf node from left to right.
The resulting tree is-
Step-03:
Step-05:
This is the required max heap after inserting the node with value 60.
Problem-03:
Solution-
Step-01: