Posts

Showing posts with the label Binary Heap

Binary Heap

Image
A Binary Heap is a complete binary tree, which satisfies either the min-heap property or the max-heap property .  Min-Heap property - Root is element with minimum value. - Value at each node is greater than or equal to the value at its parent node. Max-Heap property - Root is element with maximum value. - Value at each node is less than or equal to the value at its parent node. Binary Heap is partially ordered. The ordering amongst elements at the same level doesn't matter. It must satisfy only the requirements of being a binary heap. Binary Heaps can be stored in arrays as they are complete binary trees. Some applications of binary heaps include : -Priority Queues -Order Statistics -> Kth largest element, merge K sorted arrays, ... Binary Heap can be implemented using arrays. In an array starting from index 0, to access elements corresponding to a node with index n : Current Node -> n Parent of Current Node -> (n-1)/2 Left Chi...