Max Heap (Implementation)
Learn how to implement a max heap in C# by building essential functions such as getMax, insert, removeMax, percolateUp, and maxHeapify. Understand their time complexities and how to build a max heap efficiently using bottom-up heapify to optimize your coding interview skills.
We'll cover the following...
We'll cover the following...
Max heap Implementation
Start with some function declarations for the heap class.
Structure of our MaxHeap
Declaring the private elements
Implementing the constructor and size()
Implementing the getMax() function
This function returns the maximum value from the heap, which is the root, i.e., the first value in the list. It does not modify the heap itself. If the heap is empty, then this function returns -1. The time complexity of this function is in constant time, which is what makes heaps so special!
Implementing the removeMax() function
This function removes the maximum value from the heap. It first checks if the ...