Implementing Min Heap in JavaScript

Moving Nodes and Building Order - Day 213 Today Today I learned how to actually build a Min Heap from scratch using JavaScript. Yesterday was about the structure but today was about the movement of data. When I insert a new value I always put it at the very end of the array. Then I compare it with its parent. If the new value is smaller I swap them. I keep doing this until the value finds its right place. This is called heapify up or bubbling up. Deleting the smallest value is different. The smallest value is always at the top root. I take it out and move the last value of the array to the root. Since this value is probably too large I move it down by comparing it with its children. I always swap with the smaller child to keep the min heap property. This is called heapify down. I used a JavaScript class to manage this. I used simple math formulas to find the connections between parents and children in the array. This way I do not need complex pointers. Today was about logic and writing clean helper functions to make the heap work. LeetCode logic and structures solved today: Manual implementation of Min Heap class Logic for Heapify Up insertion Logic for Heapify Down deletion Array index mapping for binary trees #DSAinJavaScript #365daysOfCoding #JavaScriptLogic #LeetCode #DataStructures #MinHeap #AlgorithmBuilding #LogicDevelopment #ProgrammingDaily #SoftwareEngineer #CodingLogic #JavaScriptDeveloper #Heapify #ArrayLogic #ProblemSolving #CodePractice #TechLearning #StructureAndLogic #BuildingBlocks #WebDevLearning

To view or add a comment, sign in

Explore content categories