Heap Data Structure
A Heap is a complete binary tree data structure that satisfies the heap property: for every node, the value of its children is less than or equal to its own value. Heaps are usually used to implement priority queues, where the smallest (or largest) element is always at the root of the tree.
Table of Content
- What is Heap Data Structure?
- Types of Heaps
- Heap Operations
- Heap Data Structure Applications
- Basics of Heap Data Structure
- Other Types of Heap Data Structure
- Easy Problems on Heap Data Structure
- Medium Problems on Heap Data Structure
- Hard Problems on Heap Data Structure
What is Heap Data Structure?
A heap is a binary tree-based data structure that satisfies the heap property: the value of each node is greater than or equal to the value of its children. This property makes sure that the root node contains the maximum or minimum value (depending on the type of heap), and the values decrease or increase as you move down the tree.
Types of Heaps
There are two main types of heaps:
- Max Heap: The root node contains the maximum value, and the values decrease as you move down the tree.
- Min Heap: The root node contains the minimum value, and the values increase as you move down the tree.
Heap Operations
Common heap operations are:
- Insert: Adds a new element to the heap while maintaining the heap property.
- Extract Max/Min: Removes the maximum or minimum element from the heap and returns it.
- Heapify: Converts an arbitrary binary tree into a heap.
Heap Data Structure Applications
Heaps have various applications, like:
- Heaps are commonly used to implement priority queues, where elements are retrieved based on their priority (maximum or minimum value).
- Heapsort is a sorting algorithm that uses a heap to sort an array in ascending or descending order.
- Heaps are used in graph algorithms like Dijkstra’s algorithm and Prim’s algorithm for finding the shortest paths and minimum spanning trees.
Basics of Heap Data Structure
Other Types of Heap Data Structure
Easy Problems on Heap Data Structure
- Heap Sort
- Check if a given Binary Tree is Heap
- How to check if a given array represents a Binary Heap?
- Iterative Heap Sort
- K’th Largest Element in an array
- K’th Smallest/Largest Element in Unsorted Array | Set 1
- Height of a complete binary tree (or Heap) with N nodes
- Heap Sort for decreasing order using min heap
Medium Problems on Heap Data Structure
- Sort an almost sorted array
- Print all nodes less than a value x in a Min Heap.
- Tournament Tree (Winner Tree) and Binary Heap
- Connect n ropes with minimum cost
- Maximum distinct elements after removing k elements
- K maximum sum combinations from two arrays
- Median of Stream of Running Integers using STL
- Median in a stream of integers (running integers)
- K’th largest element in a stream
- Largest triplet product in a stream
- Find k numbers with most occurrences in the given array
- Convert min Heap to max Heap
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
Hard Problems on Heap Data Structure
- Design an efficient data structure for given operations
- Merge k sorted arrays | Set 1
- Sort numbers stored on different machines
- Smallest Derangement of Sequence
- Largest Derangement of a Sequence
- Maximum difference between two subsets of m elements
- Convert BST to Min Heap
- Merge two binary Max Heaps
- K-th Largest Sum Contiguous Subarray
- Minimum product of k integers in an array of positive Integers
- Leaf starting point in a Binary Heap data structure
- Rearrange characters in a string such that no two adjacent are same
- Sum of all elements between k1’th and k2’th smallest elements
- Minimum sum of two numbers formed from digits of an array
Quick Links:
Recommended:
Contact Us