Heap: Structure and Applications, Introduction to Min-Heap and Max-Heap

A heap is a special type of complete binary tree, characterized by the size relationship between parent and child nodes (parent ≤ child for a min-heap, parent ≥ child for a max-heap). It efficiently retrieves extreme values (with the top element being the minimum or maximum), similar to a priority queue. The underlying structure is a complete binary tree, where each level is filled as much as possible, and the last level is filled from left to right. When stored in an array, the left child index is 2i+1, the right child index is 2i+2, and the parent index is (i-1)//2. Basic operations include insertion (appending to the end and then "bubbling up") and deletion (replacing the top element with the last element and then "bubbling down"), both with a time complexity of O(log n). Heaps are widely used in priority queues (e.g., task scheduling), finding the k-th largest element, and Huffman coding. They are a critical structure for efficiently handling extreme value problems.

Read More