{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/binaryHeap","result":{"data":{"blog":{"frontmatter":{"title":"BINARY HEAP","thumbnail":"blog41","date":"January 16, 2021","dsaCppCodeFile":"https://drive.google.com/file/d/1oUrVBqxZ0oEJ0wD_3qfj6huU_8y3h7Wr/view?usp=sharing"},"excerpt":"<div class=\"my-2 p-2\">\n              <p>\n                In this blog post will study what is a binary heap, types,\n                applications, operations and C++ code implementation for Binary\n                Heap.\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Definition</h4>\n              <div class=\"m-2\">\n                <p>\n                  A heap is a tree with some special properties. The basic\n                  requirement of a heap is that the value of a node must be\n                  greater (or lesser) than the values of its children. This is\n                  called heap property.\n                </p>\n                <p>\n                  A heap also has the additional property that all leaves should\n                  be at h or h – 1 levels (where h is the height of the tree)\n                  for some h > 0. That means heap should form a complete binary\n                  tree.\n                </p>\n                <p>\n                  A heap with atmost two child nodes is called a Binary Heap.\n                  That is, a binary tree which has heap property is a binary\n                  heap.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Types</h4>\n              <ol class=\"pl-4\">\n                <li>\n                  <strong>Min Heap: </strong>The value of the parent node must\n                  be less than or equal to the values of child nodes.\n                </li>\n                <li>\n                  <strong>Max Heap: </strong>The value of the parent node must\n                  be greater than or equal to the values of child nodes.\n                </li>\n              </ol>\n              <p class=\"text-muted\">\n                The root node returns the min/max value in the heap.\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Storage Represention</h4>\n              <div class=\"m-2\">\n                <p>\n                  To represent a heap, we can use arrays instead of linked\n                  lists(as we have seen BST) because heaps will form a complete\n                  binary tree and there will be no wastage of memory.\n                </p>\n                <p>\n                  The new nodes will be added to the heap(tree) from left leaf\n                  node to the right ones and hence we can use arrays which\n                  stores the elements in contiguous memory location.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Applications</h4>\n              <ul class=\"pl-4\">\n                <li>To implement Priority Queues efficiently.</li>\n                <li>One of the sorting algorithms i.e. Heapsort.</li>\n                <li>Data compression: Huffman Coding algorithm.</li>\n                <li>Selection problem: Finding kth- smallest element.</li>\n                <li>Shortest path algorithms: Dijkstra’s algorithm.</li>\n                <li>Minimum spanning tree algorithms: Prim’s algorithm.</li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Terms</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  <strong>Heapify: </strong>After performing insert/delete\n                  operation in heap, it may not satisfy the heap property. In\n                  that case we need to adjust the locations of the heap to make\n                  it heap again. This process is called heapifying. There are\n                  two ways to heapify:\n                  <ol class=\"pl-2\">\n                    <li>\n                      Percolate Down: Process of heapifying from top(may be root\n                      or any other node) to bottom.\n                    </li>\n                    <li>\n                      Percolate Up: Process of heapifying from bottom to\n                      top(root).\n                    </li>\n                  </ol>\n                </li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Standard Operations</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following thoery.\n              </p>\n              <p>\n                We shall implement binary max heap. Also I have used the word\n                node as it is actually representing a tree but do note that the\n                storage used is an array of data type integer and not a\n                structure.\n              </p>\n              <ol class=\"pl-4\">\n                <li>\n                  <strong>Finding Parent Node: </strong>For a node at i<sup\n                    >th</sup\n                  >\n                  location, it's parent will be at the location (i-1)/2.\n                </li>\n                <li>\n                  <strong>Finding Child Node: </strong>For a node at i<sup\n                    >th</sup\n                  >\n                  location, it's children will be at the location (2*i) [Left\n                  child] & (2*i)+1 [Right child].\n                </li>\n                <li>\n                  <strong>Percolate Down: </strong>\n                  Get the left & right child indices and find the maximum value\n                  among the two child. <br />\n                  If the maximum value is not the parent node then swap the\n                  parent node and the child node which is maximum. Percolate\n                  down again from the index which is maximum(either left or\n                  right child index).\n                  <br />\n                  If the parent node itself is maximum then no need of swapping\n                  and percolating again.\n                </li>\n                <li>\n                  <strong>Insertion: </strong>\n                  Loop from the last element untill the new node's value is\n                  greater than the parent node's value. Swap the parent and it's\n                  either of child if this condition is true.<br />\n                  Add the new node to the index where the above condition was\n                  false and percolate down from root(index 0).\n                </li>\n                <li>\n                  <strong>Deletion: </strong> The maximum element i.e the root\n                  node will be deleted and returned from the heap. <br />\n                  Store the first element(root) in a temporary variable, assign\n                  last element to the root and percolate down from the\n                  root(index 0). <br />\n                  Finally return the temporary variable(the deleted value).\n                </li>\n                <li>\n                  <strong>Display: </strong>The traversal method used is breath\n                  first traversal and hence the elements in the array can be\n                  displayed using index starting from 0.\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/tree\">Tree Data Structure</a> |\n              <a href=\"/blogs/binaryTree\">Binary Trees</a> |\n              <a href=\"/blogs/binarySearchTree\">Binary Search Trees</a>\n            </div>\n","html":"<div class=\"my-2 p-2\">\n              <p>\n                In this blog post will study what is a binary heap, types,\n                applications, operations and C++ code implementation for Binary\n                Heap.\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Definition</h4>\n              <div class=\"m-2\">\n                <p>\n                  A heap is a tree with some special properties. The basic\n                  requirement of a heap is that the value of a node must be\n                  greater (or lesser) than the values of its children. This is\n                  called heap property.\n                </p>\n                <p>\n                  A heap also has the additional property that all leaves should\n                  be at h or h – 1 levels (where h is the height of the tree)\n                  for some h > 0. That means heap should form a complete binary\n                  tree.\n                </p>\n                <p>\n                  A heap with atmost two child nodes is called a Binary Heap.\n                  That is, a binary tree which has heap property is a binary\n                  heap.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Types</h4>\n              <ol class=\"pl-4\">\n                <li>\n                  <strong>Min Heap: </strong>The value of the parent node must\n                  be less than or equal to the values of child nodes.\n                </li>\n                <li>\n                  <strong>Max Heap: </strong>The value of the parent node must\n                  be greater than or equal to the values of child nodes.\n                </li>\n              </ol>\n              <p class=\"text-muted\">\n                The root node returns the min/max value in the heap.\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Storage Represention</h4>\n              <div class=\"m-2\">\n                <p>\n                  To represent a heap, we can use arrays instead of linked\n                  lists(as we have seen BST) because heaps will form a complete\n                  binary tree and there will be no wastage of memory.\n                </p>\n                <p>\n                  The new nodes will be added to the heap(tree) from left leaf\n                  node to the right ones and hence we can use arrays which\n                  stores the elements in contiguous memory location.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Applications</h4>\n              <ul class=\"pl-4\">\n                <li>To implement Priority Queues efficiently.</li>\n                <li>One of the sorting algorithms i.e. Heapsort.</li>\n                <li>Data compression: Huffman Coding algorithm.</li>\n                <li>Selection problem: Finding kth- smallest element.</li>\n                <li>Shortest path algorithms: Dijkstra’s algorithm.</li>\n                <li>Minimum spanning tree algorithms: Prim’s algorithm.</li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Terms</h4>\n              <ul class=\"pl-4\">\n                <li>\n                  <strong>Heapify: </strong>After performing insert/delete\n                  operation in heap, it may not satisfy the heap property. In\n                  that case we need to adjust the locations of the heap to make\n                  it heap again. This process is called heapifying. There are\n                  two ways to heapify:\n                  <ol class=\"pl-2\">\n                    <li>\n                      Percolate Down: Process of heapifying from top(may be root\n                      or any other node) to bottom.\n                    </li>\n                    <li>\n                      Percolate Up: Process of heapifying from bottom to\n                      top(root).\n                    </li>\n                  </ol>\n                </li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Standard Operations</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following thoery.\n              </p>\n              <p>\n                We shall implement binary max heap. Also I have used the word\n                node as it is actually representing a tree but do note that the\n                storage used is an array of data type integer and not a\n                structure.\n              </p>\n              <ol class=\"pl-4\">\n                <li>\n                  <strong>Finding Parent Node: </strong>For a node at i<sup\n                    >th</sup\n                  >\n                  location, it's parent will be at the location (i-1)/2.\n                </li>\n                <li>\n                  <strong>Finding Child Node: </strong>For a node at i<sup\n                    >th</sup\n                  >\n                  location, it's children will be at the location (2*i) [Left\n                  child] & (2*i)+1 [Right child].\n                </li>\n                <li>\n                  <strong>Percolate Down: </strong>\n                  Get the left & right child indices and find the maximum value\n                  among the two child. <br />\n                  If the maximum value is not the parent node then swap the\n                  parent node and the child node which is maximum. Percolate\n                  down again from the index which is maximum(either left or\n                  right child index).\n                  <br />\n                  If the parent node itself is maximum then no need of swapping\n                  and percolating again.\n                </li>\n                <li>\n                  <strong>Insertion: </strong>\n                  Loop from the last element untill the new node's value is\n                  greater than the parent node's value. Swap the parent and it's\n                  either of child if this condition is true.<br />\n                  Add the new node to the index where the above condition was\n                  false and percolate down from root(index 0).\n                </li>\n                <li>\n                  <strong>Deletion: </strong> The maximum element i.e the root\n                  node will be deleted and returned from the heap. <br />\n                  Store the first element(root) in a temporary variable, assign\n                  last element to the root and percolate down from the\n                  root(index 0). <br />\n                  Finally return the temporary variable(the deleted value).\n                </li>\n                <li>\n                  <strong>Display: </strong>The traversal method used is breath\n                  first traversal and hence the elements in the array can be\n                  displayed using index starting from 0.\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/tree\">Tree Data Structure</a> |\n              <a href=\"/blogs/binaryTree\">Binary Trees</a> |\n              <a href=\"/blogs/binarySearchTree\">Binary Search Trees</a>\n            </div>"},"thumbnail":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='225'%20viewBox='0%200%20400%20225'%20preserveAspectRatio='none'%3e%3cpath%20d='M38%2092l-1%2010v8h11v-9l2%204c1%204%202%205%204%205s3-1%203-10v-9h-2c-3%200-3%200-3%203v4l-1-4-4-3c-2%200-3%201-3%203-1%203-1%203-1%200%200-4%200-4-2-4s-3%201-3%202m27-1c-2%202-7%2014-7%2017%200%201%201%202%203%202l2-1%201-2%202-1c2%200%202%200%202%202%200%201%201%202%203%202s3-1%201-11c-2-8-4-10-7-8m14%200h-4v6c-2%2011-1%2013%201%2013s3-1%203-3c0-4%201-4%203%200%201%203%202%204%204%201v-4c-1-1-1-3%201-5%203-5%200-9-8-8m34%201l-1%2010c0%207%200%208%202%208%203%200%203%200%203-4%201-5%204-6%204%200%200%203%200%204%202%204s3-1%203-10V90h-2c-3%200-3%201-3%204l-2%204c-2%200-2%200-2-4%201-4-3-6-4-2m19-1l-4%201-1%209v9h4c6%200%209-1%209-3s-1-2-4-2c-5%200-5-2-1-3%204%200%204-4%200-4s-4-2%201-3c3%200%204-1%204-3s0-2-8-1m16%200c-1%201-7%2014-7%2018%200%202%204%201%206-1%203-3%204-3%204%200%200%202%201%202%206%202s5%200%205-3c0-2%201-3%203-4%207-2%208-11%201-12s-8%200-9%209v9l-2-7-3-11h-4M24%2094l-1%2010-1%206h5c7%200%2010-2%209-7v-6c2-4%200-6-6-6s-6%200-6%203m65-2l2%207%202%208c0%204%205%204%205%200l2-9c4-8%204-7%201-7-2%200-3%201-4%203l-1%204-2-4c-1-3-5-4-5-2m179%2010c-3%203-5%2013-3%2017%201%202%204%203%205%201%201-1-1-9-3-10-4-1%202-9%208-9%207%200%209%205%206%2011-2%205-2%208%200%209%204%201%207-8%204-9l-1-4c0-8-10-12-16-6m31%204l-1%208v7h17l-5%205a66%2066%200%2000-19%2037l-2%2010v1l6-7c7-9%2017-15%2017-10%200%202%201%203%203%203l2-2-1-3h3c3%200%202%201%200%204l-2%204v3h-17v7c0%206%200%207-2%208-3%202-3%203%200%203%202%200%203%204%201%206l-6%201c-9%200-11-4-7-9l2-5c0-3-8%206-8%2010-1%203-1%203-10%204-8%200-8%200-8%202s1%203%205%203c5%200%206%200%204%205-1%202%201%202%2048%202h49l1-4c1-2%202-3%205-3%205-1%205-4%200-5h-3v-14c0-14-1-21-4-21l-2-2-1-2c-7-1-7-4-3-12%207-13%207-17%200-22-4-3-5-4-5-8l-1-4c-1-1-1%201-1%204%200%205%200%205-2%204-3-1-19%200-23%201l-3%201v-10l-14-1-13%201m2%208v6h22v-13h-22v7m-31%2027l1%209h12v-18h-13v9m55%2016c0%201-1%202-3%202s-2%201-2%204v3h6c9%200%209%200%208%207l1%208v2l-1%203c0%204%200%204%205%204%207%201%208%200%208-5l-1-6-2-4-2-5-1-3h-1v-2l-3-5c-1-2-3-3-7-4l-5-2v3m-52%207l-1%205c0%203%200%204-1%203%200-4-2-3-3%200l2%204%202%203%203%201c2%200%202%200%202-3%200-2%200-3%202-3s2-1%202-4l-1-4-1%203c0%204-2%203-2-2-1-3-1-4-4-3'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1.7699115044247788,"src":"/static/b1aebc1b76ca08dfc8cd81472de5e1a5/ee604/blog41.png","srcSet":"/static/b1aebc1b76ca08dfc8cd81472de5e1a5/69585/blog41.png 200w,\n/static/b1aebc1b76ca08dfc8cd81472de5e1a5/497c6/blog41.png 400w,\n/static/b1aebc1b76ca08dfc8cd81472de5e1a5/ee604/blog41.png 800w,\n/static/b1aebc1b76ca08dfc8cd81472de5e1a5/f3583/blog41.png 1200w,\n/static/b1aebc1b76ca08dfc8cd81472de5e1a5/e4d72/blog41.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"binaryHeap","thumbnail":"thumbnails/blog41.png"}},"staticQueryHashes":["2987289216","63159454"]}