From 3184ae086ff604a9eab51a48252f7fd88cdf43c1 Mon Sep 17 00:00:00 2001 From: Nikita Kapoor <62166533+nikitakapoor1919@users.noreply.github.com> Date: Sat, 9 Oct 2021 14:18:07 +0530 Subject: [PATCH] Add Readme.md for Heap Data Structure (#2440) --- DataStructures/Heaps/Readme.md | 128 +++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 DataStructures/Heaps/Readme.md diff --git a/DataStructures/Heaps/Readme.md b/DataStructures/Heaps/Readme.md new file mode 100644 index 00000000..5e77c68d --- /dev/null +++ b/DataStructures/Heaps/Readme.md @@ -0,0 +1,128 @@ +

HEAP DATA STRUCTURE

+

A Heap is a special Tree-based data structure in which the tree is a complete binary tree. + +##

Complete Binary Tree

+

A complete binary tree is a binary tree +in which all the levels except the last level, i.e., leaf node should be completely filled, and all the nodes should be left-justified.

+ + +``` + 10 + / \ + 20 30 + / \ + 40 50 + + COMPLETE BINARY TREE +``` + + +##

Types of Heap

+

Generally, Heaps can be of two types: +
+Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys present at all of it’s children. The same property must be recursively true for all sub-trees in that Binary Tree. +
+Min-Heap: In a Min-Heap the key present at the root node must be minimum among the keys present at all of it’s children. The same property must be recursively true for all sub-trees in that Binary Tree. +

+ + + +``` + 10 + / \ + 20 30 + / \ / \ + 40 50 60 70 + + MIN HEAP +``` + +``` + 70 + / \ + 50 60 + / \ / \ + 40 30 10 20 + + MAX HEAP +``` + +##

Min Heap Construction Algorithm

+``` +Step 1 − Create a new node at the end of heap. +Step 2 − Assign new value to the node. +Step 3 − Compare the value of this child node with its parent. +Step 4 − If value of parent is more than child, then swap them. +Step 5 − Repeat step 3 & 4 until Heap property holds. +``` + +``` +Add 15 + + 10 10 10 + / \ / \ / \ + 20 30 ------> 20 30 ------> 20 15 + / \ / \ / / \ / + 40 50 40 50 15 40 50 30 + + +``` + +##

Min Heap Deletion Algorithm

+``` +Step 1 − Remove root node. +Step 2 − Move the last element of last level to root. +Step 3 − Compare the value of this child node with its parent. +Step 4 − If value of parent is more than child, then swap them. +Step 5 − Repeat step 3 & 4 until Heap property holds. +``` + +``` +Delete 10 + + 10 50 20 20 + / \ / \ / \ / \ + 20 30 ------> 20 30 ------> 50 30 ------> 40 30 + / \ / / / + 40 50 40 40 50 + + +``` + +##

Time Complexity (Min Heap)

+ + + + + + + + + + + + + + + + + + + + + + + + + +
OperationsSorted ArrayUnSorted ArrayHeap
AddO(N)O(1)O(logN)
Delete MinimumO(N)O(N)O(logN)
Get MinimumO(1)O(N)O(1)
+ +##

Applications of Heap Data Structure

+ +

+Heapsort: Heapsort algorithm has limited uses because Quicksort is better in practice. Nevertheless, the Heap data structure itself is enormously used. + +Priority Queues: Priority queues can be efficiently implemented using Binary Heap because it supports insert(), delete() and extractmax(), decreaseKey() operations in O(logn) time. Binomoial Heap and Fibonacci Heap are variations of Binary Heap. These variations perform union also in O(logn) time which is a O(n) operation in Binary Heap. Heap Implemented priority queues are used in Graph algorithms like Prim’s Algorithm and Dijkstra’s algorithm. + +Order statistics: The Heap data structure can be used to efficiently find the kth smallest (or largest) element in an array. +