Heap Data Structure (max and min)- Beau teaches JavaScript

preview_player
Показать описание
A binary heap is a partially ordered binary tree which satisfies the heap property. What is the heap property? Watch the video to find out! Also see how to implement a min heap in JavaScript.

Thanks to Sean Smith for the code!

⭐JavaScript Tutorials Playlists⭐

-
We're busy people who learn to code, then practice by building projects for nonprofits. Learn Full-stack JavaScript, build a portfolio, and get great references with our open source community.

Рекомендации по теме
Комментарии
Автор

The best video about the heap structure! Others one not even close. Keep up!

TarasovFrontDev
Автор

Super helpful, thanks Beau for all of your contributions. All of your Data Structure videos are fantastic. Been sharing them with my cohort!

carolinerobin
Автор

I feel like this code could be clarified with some helper functions. Ex: getParent, getLeftChild, getRightChild

MichaelSalo
Автор

This is wrong, because if there is an element in the left, then we need to check
if (this.heap[left] == undefined || this.heap[right] == undefined) {
break;
}

ebrahimmansour
Автор

Don't get confused by the left and right child index calculations here being different from a normal binary tree. Normally in a binary tree, left child index is parentIndex*2 + 1 and right child is parentIndex*2 +2. Here both of them are 1 less just because we're leaving index 0 blank so left becomes parentIndex*2, and right becomes parentIndex*2+1

ohmegatech
Автор

While I appreciate the info, why did he choose to show the diagram for a max heap and then walk through the code of a min heap? He even showed us that he has the code for a min heap already there, so why show one thing and explain how to build the opposite? Even more nonsensical, he leaves the diagram of the max heap side by side the whole time he's walking through the code of the min heap. Anyone who wants a visual representation of the min heap while he explains the code has to ignore the diagram displayed side by side with the code.

ironrose
Автор

at 10:38, in the remove function you use .splice(heap.length-1); to splice off the last element of our array..
is it faster to use splice instead of a .pop(); there?

Tyrowo
Автор

shouldn't we replace the || with and && here:
if(heap[left] == undefined || heap[right] == undefined){
break;
}

be this instead:

if(heap[left] == undefined && heap[right] == undefined){
break;
}

because we still want to swap even if the parent has only "one" child.

ranesh
Автор

Thank you Beau. My question is; when you sort the heap, you also remove everything in the actual heap. Is this expected? I mean, if I sort it and then insert another element, the heap will have only 2 elements; the null and the last thing I added.

eywahbabam
Автор

Why do you have to sacrifice index zero? Since most arrays always start at zero, we can then use the following formulas:

Left child: 2n+1
Right child: 2n+2

codingliteracy
Автор

what's the use of if(idx>=1) statement as checking is already done on while statement.(In min heap insert program)

prakashavvaru
Автор

demo code is wrong!
if heap is 1, 2, 3, 4, 5
run heap.remove() will get 2, 5, 3, 4
but correct should be 2, 4, 3, 5

allovince
Автор

if we cut off the last element on line 57, we would be left with just the array with one element in is which is null, then why are we cutting it off?

michaeljiang
Автор

thank you for your help but your insert function does not work properly

muhammedsami
Автор

why would you write "heap.splice(heap.length - 1)" when you can do the same thing with heap.pop() ???

JSDev
Автор

0:00 was enough to make me pause the video and search for another video

snoudoubts
Автор

Destructuring syntax creates new arrays and increases space complexity.

___
Автор

"this._heap[left] === undefined || this.heap[right] === undefined"
I think || sign should be && sign and other logic should be changed respectively

广东靓仔
Автор

messy remove function makes it very hard to understand

mahmoudezzat
Автор

Yo dude how should i run this? in node?

michaeljiang