Min Heap Binary Tree Introduction

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

A min heap is a binary tree data structure that satisfies the heap property, where the parent node is always less than or equal to its children. In a min heap, the root node is always the minimum value in the tree.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") algorithm/BasicAlgorithmsGroup -.-> algorithm/graphs_trees("`Graphs Trees`") algorithm/BasicAlgorithmsGroup -.-> algorithm/recursion_dynamic("`Recursion Dynamic`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-268833{{"`Min Heap Binary Tree Introduction`"}} algorithm/graphs_trees -.-> lab-268833{{"`Min Heap Binary Tree Introduction`"}} algorithm/recursion_dynamic -.-> lab-268833{{"`Min Heap Binary Tree Introduction`"}} python/conditional_statements -.-> lab-268833{{"`Min Heap Binary Tree Introduction`"}} python/lists -.-> lab-268833{{"`Min Heap Binary Tree Introduction`"}} python/tuples -.-> lab-268833{{"`Min Heap Binary Tree Introduction`"}} python/function_definition -.-> lab-268833{{"`Min Heap Binary Tree Introduction`"}} python/classes_objects -.-> lab-268833{{"`Min Heap Binary Tree Introduction`"}} python/constructor -.-> lab-268833{{"`Min Heap Binary Tree Introduction`"}} python/polymorphism -.-> lab-268833{{"`Min Heap Binary Tree Introduction`"}} python/encapsulation -.-> lab-268833{{"`Min Heap Binary Tree Introduction`"}} python/raising_exceptions -.-> lab-268833{{"`Min Heap Binary Tree Introduction`"}} python/build_in_functions -.-> lab-268833{{"`Min Heap Binary Tree Introduction`"}} end

Min Heap

Problem

Implement a min heap with the following methods:

  • extract_min(): removes and returns the minimum value in the heap
  • insert(value): inserts a new value into the heap while maintaining the heap property

Requirements

The implementation should meet the following requirements:

  • The inputs are integers
  • The implementation should fit in memory

Example Usage

Consider the following min heap:

          _5_
        /     \
       20     15
      / \    /  \
     22  40 25
  • extract_min(): removes and returns the minimum value in the heap, which is 5. The resulting heap is:
          _15_
        /      \
       20      25
      / \     /  \
     22  40
  • insert(2): inserts the value 2 into the heap while maintaining the heap property. The resulting heap is:
          _2_
        /     \
       20      5
      / \     / \
     22  40  25  15

Summary

In summary, a min heap is a binary tree data structure that maintains the heap property, where the parent node is always less than or equal to its children. The implementation of a min heap should include the extract_min() and insert(value) methods, and meet the requirements of integer inputs and fitting in memory.

Other Algorithm Tutorials you may like