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

  Рет қаралды 57,427

freeCodeCamp.org

freeCodeCamp.org

Күн бұрын

Пікірлер: 48
@ohmegatech666
@ohmegatech666 9 күн бұрын
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
@MichaelSalo
@MichaelSalo 4 жыл бұрын
I feel like this code could be clarified with some helper functions. Ex: getParent, getLeftChild, getRightChild
@SaranshDhingra
@SaranshDhingra 3 жыл бұрын
Funnily, when I was practicing, I did create those functions myself :D But, the logic explanation seems good.
@miguelpetrarca5540
@miguelpetrarca5540 3 жыл бұрын
for real lol. I hate seeing cryptic unreadable code, hurts my soul and eyes
@miguelpetrarca5540
@miguelpetrarca5540 3 жыл бұрын
in your helper method, I would assume you would pass the index of a node for these methods, also if they dont have a left child or right, would method return null
@convolutionalnn2582
@convolutionalnn2582 3 жыл бұрын
@@miguelpetrarca5540 I dont learn how to insert...How can i make Min Heap...I copy the code down...But i dont know how to insert etc etc
@TarasovFrontDev
@TarasovFrontDev 3 ай бұрын
The best video about the heap structure! Others one not even close. Keep up!
@carolinerobin4228
@carolinerobin4228 6 жыл бұрын
Super helpful, thanks Beau for all of your contributions. All of your Data Structure videos are fantastic. Been sharing them with my cohort!
@BeauCarnes
@BeauCarnes 6 жыл бұрын
Thanks!
@ebrahimmansour7606
@ebrahimmansour7606 4 жыл бұрын
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; }
@codingliteracy19
@codingliteracy19 2 жыл бұрын
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
@ironrose6
@ironrose6 3 жыл бұрын
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.
@_7__716
@_7__716 2 жыл бұрын
Destructuring syntax creates new arrays and increases space complexity.
@princeofexcess
@princeofexcess Жыл бұрын
it creates new arrays but it doesnt increase space complexity as space complexity only cares about n. O(n+2) = O(n) now this might still matter if youre coding for a chip but not on an interview.
@ranesh237
@ranesh237 6 жыл бұрын
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.
@Ford-ju9yr
@Ford-ju9yr 6 ай бұрын
Yes, but then you also will need to change line 43 to: If( (heap[left]
@allovince2257
@allovince2257 5 жыл бұрын
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
@41186pr
@41186pr 5 жыл бұрын
culprit is condition checking undefined for left and right in remove function. instead of OR we should have AND
@michaeljiang1002
@michaeljiang1002 5 жыл бұрын
lol havent run the program to check it, but yeah
@wayneli3890
@wayneli3890 5 жыл бұрын
if (heap[right] === undefined) { if (heap[i] >= heap[left]) { [heap[i], heap[left]] = [heap[left], heap[i]]; } break; } if (heap[left] === undefined) { break; }
@harrisonxm
@harrisonxm 7 жыл бұрын
Thanks for making this video!
@eywahbabam
@eywahbabam 4 жыл бұрын
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
@eywahbabam 4 жыл бұрын
Hence, I changed it this way: this.sort = function(){ let tempHeap = [ ...heap ] ; let result= [ ] ; while( heap.length > 1 ){ result.push(this.remove( )) ; } heap = tempHeap ; return result; }
@ctdr119
@ctdr119 10 ай бұрын
Super helpful
@Tyrowo
@Tyrowo 2 жыл бұрын
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?
@广东靓仔
@广东靓仔 6 жыл бұрын
"this._heap[left] === undefined || this.heap[right] === undefined" I think || sign should be && sign and other logic should be changed respectively
@ivantsybulin8328
@ivantsybulin8328 5 жыл бұрын
if (heap[right] === undefined || heap[left] > | < heap[right]) ..
@michaeljiang1002
@michaeljiang1002 5 жыл бұрын
we'll, we don't need to check that both is undefined, if we know that the left one is undefined then we know for sure that the right one is also empty. But if the left one is not undefined we can still run the look
@prakashavvaru6997
@prakashavvaru6997 7 жыл бұрын
what's the use of if(idx>=1) statement as checking is already done on while statement.(In min heap insert program)
@ebrahimmansour7606
@ebrahimmansour7606 4 жыл бұрын
There is no need to this condition, because after swapping, if the index if less than 2 the loop will break. consider the condition of (idx >= 1) is false, what will happen?? infinite looooop
@goadler8330
@goadler8330 Жыл бұрын
Yes exactly I was thinking the same. 😅
@JSDev776
@JSDev776 2 жыл бұрын
why would you write "heap.splice(heap.length - 1)" when you can do the same thing with heap.pop() ???
@dd88816
@dd88816 4 жыл бұрын
nice video!
@michaeljiang1002
@michaeljiang1002 5 жыл бұрын
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?
@mahmoudezzat4176
@mahmoudezzat4176 Жыл бұрын
messy remove function makes it very hard to understand
@muhammedsami3058
@muhammedsami3058 5 жыл бұрын
thank you for your help but your insert function does not work properly
@maxl.5607
@maxl.5607 4 жыл бұрын
My I ask what is the draw tool?
@gidmanone
@gidmanone 7 жыл бұрын
my head hurts watching this
@ndurocher85
@ndurocher85 6 жыл бұрын
How do we add to the heap?
@billwindsor4224
@billwindsor4224 6 жыл бұрын
+ndurocher85 You add a node to the heap by finding the first empty node at the bottom of the heap, and then ordering the heap nodes by successive comparisons to compare and swap nodes. See this tutorial with good animations: kzbin.info/www/bejne/qmGmommqi7OFeKM
@michaeljiang1002
@michaeljiang1002 5 жыл бұрын
Yo dude how should i run this? in node?
@snoudoubts1745
@snoudoubts1745 2 жыл бұрын
0:00 was enough to make me pause the video and search for another video
@StaceyWilsonlosangeles
@StaceyWilsonlosangeles 3 жыл бұрын
Although, this works... There is a better way to implement a min or max heap.
@travholt
@travholt 7 жыл бұрын
I'm sorry, but this video is not very helpful. There's not one word about WHY heaps are used, so I'm not given any incentive to learn HOW.
@BeauCarnes
@BeauCarnes 7 жыл бұрын
One of the main use cases is discussed later in the video. It is used in heapsort which is one of the fastest sorting algorithms. I guess I could have put that toward the beginning instead of the end. :)
@travholt
@travholt 7 жыл бұрын
Okay, a few words about WHY, then. :-) But yes, I definitely think mentioning that (or even better: showing a practical example) in the beginning would help.
@jtnix
@jtnix 6 жыл бұрын
pretty much any social site with a ranking system is using a max and/or min heap sort to manage their scoreboard rankings. think of stackoverflow's or reddit's contributor ranking systems, which likely use optimized, object store backed max heaps to manage the user ranks.
2.6.3 Heap - Heap Sort - Heapify - Priority Queues
51:08
Abdul Bari
Рет қаралды 2,1 МЛН
啊?就这么水灵灵的穿上了?
00:18
一航1
Рет қаралды 92 МЛН
黑的奸计得逞 #古风
00:24
Black and white double fury
Рет қаралды 28 МЛН
Это было очень близко...
00:10
Аришнев
Рет қаралды 6 МЛН
Yay, My Dad Is a Vending Machine! 🛍️😆 #funny #prank #comedy
00:17
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
Trie Data Structure - Beau teaches JavaScript
12:32
freeCodeCamp.org
Рет қаралды 37 М.
The Weird History of JavaScript
12:09
Fireship
Рет қаралды 1,2 МЛН
Heap Data Structure | Illustrated Data Structures
11:31
the roadmap
Рет қаралды 27 М.
Heap - Data Structures in Javascript
26:07
Questionable Coding
Рет қаралды 9 М.
Hash Tables and Hash Functions
13:56
Computer Science Lessons
Рет қаралды 1,6 МЛН
啊?就这么水灵灵的穿上了?
00:18
一航1
Рет қаралды 92 МЛН