A max-heap is a complete binary tree in which the value in each internal node is greater than or equal to the values in the children of that node.
Mapping the elements of a heap into an array is trivial: if a node is stored a index k, then its left child is stored at index 2k+1 and its right child at index 2k+2.
Example of Max Heap:
How is Max Heap represented?
A Max Heap is a Complete Binary Tree. A Max heap is typically represented as an array. The root element will be at Arr. Below table shows indexes of other nodes for the ith node, i.e., Arr[i]:
Arr[(i-1)/2] Returns the parent node.
Arr[(2*i)+1] Returns the left child node.
Arr[(2*i)+2] Returns the right child node.
Operations on Max Heap:
1) getMax(): It returns the root element of Max Heap. Time Complexity of this operation is O(1).
2) extractMax(): Removes the maximum element from MaxHeap. Time Complexity of this Operation is O(Log n) as this operation needs to maintain the heap property (by calling heapify()) after removing root.
4) insert(): Inserting a new key takes O(Log n) time. We add a new key at the end of the tree. If new key is smaller than its parent, then we don’t need to do anything. Otherwise, we need to traverse up to fix the violated heap property.
Note : In below implementation, we do indexing from index 1 to simplify the implementation.
The Max Heap is PARENT : 84 LEFT CHILD : 22 RIGHT CHILD :19 PARENT : 22 LEFT CHILD : 17 RIGHT CHILD :10 PARENT : 19 LEFT CHILD : 5 RIGHT CHILD :6 PARENT : 17 LEFT CHILD : 3 RIGHT CHILD :9 The max val is 84
Head value using peek function:400 The queue elements: 400 30 20 10 After removing an element with poll function: 30 10 20 after removing 30 with remove function: 20 10 Priority queue contains 20 or not?: true Value in array: Value: 20 Value: 10
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Convert min Heap to max Heap
- Heap Sort for decreasing order using min heap
- Convert BST to Max Heap
- Minimum element in a max heap
- K-th Greatest Element in a Max-Heap
- Max Heap in Python
- Java Program for Heap Sort
- Min Heap in Java
- What is Heap Pollution in Java and how to resolve it?
- k largest(or smallest) elements in an array | added Min Heap method
- Applications of Heap Data Structure
- Tournament Tree (Winner Tree) and Binary Heap
- Time Complexity of building a heap
- Binomial Heap
- Why is Binary Heap Preferred over BST for Priority Queue?
- Fibonacci Heap | Set 1 (Introduction)
- Check if a given Binary Tree is Heap
- Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
- K-ary Heap
- Heap in C++ STL | make_heap(), push_heap(), pop_heap(), sort_heap(), is_heap, is_heap_until()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : gowtham_yuvaraj