Category Archives: Heap

Heap using STL C++

Heap data structure can be implemented in a range using STL which allows faster input into heap and retrieval of a number always results in the largest number i.e. largest number of the remaining numbers is popped out each time. Other numbers of the heap are arranged depending upon the implementation. Operations on heap :… Read More »

Convert min Heap to max Heap

Given array representation of min Heap, convert it to max Heap in O(n) time. Input: arr[] = [3 5 9 6 8 20 10 12 18 9] 3 / \ 5 9 / \ / \ 6 8 20 10 / \ / 12 18 9 Output: arr[] = [20 18 10 12 9 9… Read More »

K-ary Heap

Prerequisite – Binary Heap K-ary heaps are a generalization of binary heap(K=2) in which each node have K children instead of 2. Just like binary heap, it follows two properties: 1) Nearly complete binary tree, with all levels having maximum number of nodes except the last, which is filled in left to right manner. 2)… Read More »

Check if a given Binary Tree is Heap

Given a binary tree we need to check it has heap property or not, Binary tree need to fulfill following two conditions for being a heap – It should be a complete tree (i.e. all levels except last should be full). Every node’s value should be greater than or equal to its child node (considering… Read More »

Fibonacci Heap | Set 1 (Introduction)

Heaps are mainly used for implementing priority queue. We have discussed below heaps in previous posts. Binary Heap Binomial Heap In terms of Time Complexity, Fibonacci Heap beats both Binary and Binomial Heaps. Below are amortized time complexities of Fibonacci Heap. 1) Find Min: ?(1) [Same as both Binary and Binomial] 2) Delete Min: O(Log… Read More »

Why is Binary Heap Preferred over BST for Priority Queue?

A typical Priority Queue requires following operations to be efficient. Get Top Priority Element (Get minimum or maximum) Insert an element Remove top priority element Decrease Key A Binary Heap supports above operations with following time complexities: O(1) O(Logn) O(Logn) O(Logn) A Self Balancing Binary Search Tree like AVL Tree, Red-Black Tree, etc can also… Read More »