Peeking the top item in the heap
The peek operation gets the maximum value from a max heap. The implementation is encapsulated in the peek function that copies the root node's value in the binary tree to the passed reference. Let us look at the algorithm and implementation of the peek operation on a max heap implemented as an array.
Algorithm
The algorithm for getting the maximum value in a max-heap is very simple. We return the value stored at the root node of the tree. Since we do not modify the tree, the resulting binary tree still follows the heap property and remains a heap.
Liking the course? Check our discounted plans to continue learning.