Data Structures: Heaps

แชร์
ฝัง

ความคิดเห็น • 454

  • @harshitm6403
    @harshitm6403 4 ปีที่แล้ว +275

    Storing the heap in the form of an array just blew my mind...so effective

    • @theinverteddonkey2961
      @theinverteddonkey2961 2 ปีที่แล้ว +9

      it's really a tree in the form of a list of nodes

    • @typingcat
      @typingcat ปีที่แล้ว +16

      Damn, if that blows your mind, your mind most be blown multiple times a day.

    • @davidlee588
      @davidlee588 ปีที่แล้ว

      @@typingcat haha, i'm also been wondering why people easily got blown away by simply TH-cam videos, it must be like an ejaculation moment for them. 😂

    • @vectoralphaAI
      @vectoralphaAI ปีที่แล้ว +51

      @@typingcat mine is. There is so much to learn every day. My mind is blown on a daily basis. Its great because im never bored.

    • @hamzahkhan878
      @hamzahkhan878 ปีที่แล้ว +3

      what in the hell we were you thinking of if that blew your mind? lol

  • @cron93
    @cron93 5 ปีที่แล้ว +59

    If you're trying to write this code in Python, beware: You cannot simply assign items[0] = items[self.size - 1]. You must pop() the item at the end of the list to remove it: items[0] = items.pop() ... also be sure to use floor division in the parent calc if using Python 3: (index - 1) // 2

  • @WorklLife
    @WorklLife 6 ปีที่แล้ว +74

    This is one of Gayle Laakmann's best videos. She walks us through the code, array, and tree versions while speaking calmly in a pleasant voice. Thank you!

  • @Saztog1425
    @Saztog1425 3 ปีที่แล้ว +87

    3:22 "Aaand there we go, we've created Minecraft!"

    • @AlanD20
      @AlanD20 3 ปีที่แล้ว +5

      EXACTLYYYY 😂😂😂

  • @octamodius
    @octamodius 5 ปีที่แล้ว +27

    Clean implementation. Clean explanation. Wonderful video! Thank you very much for taking the time to make this. I really needed it!

  • @sherazdotnet
    @sherazdotnet 2 ปีที่แล้ว +110

    Just an FYI: At 3:01 timeframe, you are showing formulas and for pareint you have [Index -2) / 2]. This needs to be change dto index -1 * 2. On next screen where you are coding it, you have it right.

    • @Pazushh
      @Pazushh 2 ปีที่แล้ว +4

      I think it shouldv'e been (Index-1)/2. while "/" rounds to bottom

    • @calviethang7139
      @calviethang7139 2 ปีที่แล้ว

      You are right!

    • @SupunNimantha
      @SupunNimantha 2 ปีที่แล้ว

      Actually that equation is not for the immediate parent of any given node but it gives you the min node (top most node ). Instead of saying parent she should have told its the min. There she made a mistake. At the same time actually there is no need to have that equation because simply the 0th element is always the min.

    • @dennisfolz352
      @dennisfolz352 ปีที่แล้ว +1

      @@SupunNimantha no you are wrong. The formula (index-1)/2 returns the parent for any given node. And it is important, because you need the parent of any given node if you want to heapify up. ^^

    • @santjagocorkez
      @santjagocorkez 7 หลายเดือนก่อน +1

      @@SupunNimantha You could do the maths yourself: take the 9 at #3. Its parent is 4 at #1. Now let's compute: (3 - 2) / 2 = 0 (floor div). Oopsie, 9 at #3 has the root as its parent, while we know from the picture it's not.

  • @harshwardhankoushik8515
    @harshwardhankoushik8515 5 ปีที่แล้ว +40

    The explanation with the code is amazzing !! loved it....seems that would work for me! Please continue with the good work

  • @BeginningProgrammer
    @BeginningProgrammer 4 ปีที่แล้ว +3

    This is a really nice explanation of min heaps.... Very nice, very clear, very simple , concise and short enough to pick up in a jiffy. Thanks Gayle.

  • @johnkimura5585
    @johnkimura5585 6 ปีที่แล้ว +155

    Her keyboard clicks are the most satisfying sound

  • @PsyKosh
    @PsyKosh 7 ปีที่แล้ว +585

    Possible error around 2:52
    The diagram shows the parent as (index-2)/2, when it should be (index-1)/2

    • @g.o.4678
      @g.o.4678 7 ปีที่แล้ว +36

      I believe that calculation takes the ceiling (or whole integer value rounded up, depending on the programming language) of the operation. So, for instance, to get the parent of the node at index 7, we'd have: ceiling((7-2)/2) = ceiling(5/2) = ceiling(2.5) = 3, which is the appropriate index we're looking for.

    • @arvinsim
      @arvinsim 7 ปีที่แล้ว

      Gbenga Ojo

    • @DimaKurguzov
      @DimaKurguzov 7 ปีที่แล้ว +56

      Your are right. (index-2)/2 for parent index is a mistake. Look the code at 3:22 - here is the correct version (index-1)/2.

    • @quangthang10d4
      @quangthang10d4 7 ปีที่แล้ว +5

      Yes I was gonna say the same thing!

    • @josevillegas2721
      @josevillegas2721 7 ปีที่แล้ว +6

      In python 2, / is integer division and it truncates the result so 5/2 = truncate(2.5) = 2

  • @kiaksarshirvanimoghaddam4354
    @kiaksarshirvanimoghaddam4354 3 ปีที่แล้ว +4

    I always had problems understanding heaps, but you made it so clear. Thank you so much ...

  • @murnoth
    @murnoth ปีที่แล้ว

    I am translating these lessons for use in Unreal Engine Visual Blueprints, and Gayle delivers these lessons very cohesively. Thank You!

  • @guadalupevictoriamartinez4537
    @guadalupevictoriamartinez4537 3 ปีที่แล้ว

    I forgot this channel existed. It saved me once again

  • @LeaFox
    @LeaFox 7 ปีที่แล้ว +3

    I read about heaps online and first implemented it using a right and left Node. I felt array, though - spidey senses. I wish I would have seen it on my own. But, this video was a close second. Thank you so much for a clear description.

  • @technowey
    @technowey 3 ปีที่แล้ว +2

    Thanks you for this excellent video. It''s the best, most concise and straightforward, explanation of a heap that I've seen.

  • @akhiljain1695
    @akhiljain1695 4 ปีที่แล้ว +4

    I was searching for something just like this. Awesome explanation of implementation of heap.

  • @MrJakson112
    @MrJakson112 2 ปีที่แล้ว

    Having that visual next to the code is such a godsent, thank you for saving my bachelors degree

  • @JOJOSHgaming7514
    @JOJOSHgaming7514 3 ปีที่แล้ว

    Thanks a lot, Madam. I've been burning out myself scrolling numerous websites not getting how a heap actually works and how it's implemented, and now finally implemented successfully in C#.

  • @CamdenBloke
    @CamdenBloke 5 ปีที่แล้ว +17

    Pro tip: if your array is indexed at 1 (like with Fortran) the pointers are parent: (index-1)/2, left child:2*index, right child:2*index +1

    • @xMercuryx56
      @xMercuryx56 2 ปีที่แล้ว +7

      that's not pro, that's just math ... lmfao

  • @AlexXPandian
    @AlexXPandian 3 ปีที่แล้ว +1

    Best video explanation with code walkthrough showing how to answer the ubiquitous lazy interviewer question "Implement a Heap data structure".

  • @m2rafik
    @m2rafik 5 ปีที่แล้ว +82

    Most of coders strugles to use complex abstract data structures like heaps or graphs because they dont learn it from a concrete use case.

    • @sarvasvarora
      @sarvasvarora 3 ปีที่แล้ว +5

      +1 for this. Doing an intro to CS course at uni rn and def if it wasn't for the coding assignments involving practical usr cases, I would've never appreciated such data structures...
      It's certainly very important to actually implement them in some use case in order to grasp them well.

    • @stas4985
      @stas4985 3 ปีที่แล้ว

      why the hell graphs or heaps complex???

    • @axea4554
      @axea4554 3 ปีที่แล้ว

      @@stas4985 because they are more complex than a simple non-resizable array or a linked list

  • @NathanSowatskey
    @NathanSowatskey 5 หลายเดือนก่อน +1

    The calculation shown in the cartoon diagram to get the parent of the node is shown as (index-2)/2. In the code the calculation is (index-1)/2.

  • @eddiet279
    @eddiet279 7 ปีที่แล้ว

    Very clear. Even more clear than the book actually.

  • @alicianieto2822
    @alicianieto2822 4 ปีที่แล้ว +6

    The video is awesome and what I needed to know. Thank you!

  • @programmercouple
    @programmercouple 3 ปีที่แล้ว +1

    This is the best explanation of Heap.
    Thanks 🙌🏻

  • @Thunder117
    @Thunder117 3 ปีที่แล้ว

    This is the most helpful code video i have ever seen

  • @ManuelRochaCR
    @ManuelRochaCR 7 ปีที่แล้ว

    I completely ignored about heaps. Nice explanation. Thanks!

  • @enkhboldochirbat3578
    @enkhboldochirbat3578 4 ปีที่แล้ว

    This is best explanation of BST on basic concepts.

  • @principlesoflife172
    @principlesoflife172 7 ปีที่แล้ว +2

    This is awesome explanation and you are awesome.

  • @finn5571
    @finn5571 7 ปีที่แล้ว

    For deleting a node, is there any issue with just not moving the last node up and bubbling up the smaller child of the empty node until there are no children, and then moving the remaining indices left by 1? Is it less efficient, does it cause any problems, or is it just that we want to heapify down since we already have that method for other purposes anyway?

  • @paoloparker8991
    @paoloparker8991 7 ปีที่แล้ว +2

    Thank you very much miss! Awesome lesson!

  • @AshfaqAhmed05
    @AshfaqAhmed05 2 ปีที่แล้ว

    such an amazing explanation with clean code. Loved it!!!

  • @lolipopscandy62
    @lolipopscandy62 7 ปีที่แล้ว +48

    How does this not have more views?? What a simple, and amazing explanation. Subscribed!!!

    • @palanisamymadakkannu4350
      @palanisamymadakkannu4350 6 ปีที่แล้ว +3

      only entertainment videos ll get more views.. useful videos wont get..😊

    • @intrepidsouls
      @intrepidsouls 5 ปีที่แล้ว +2

      Agree with you. I watched quite a lot of her videos and it seems like she doesn't quite understand what she is talking about either.

    • @blasttrash
      @blasttrash 5 ปีที่แล้ว

      @@intrepidsouls I agree too. Her book is good though.

    • @thatoneuser8600
      @thatoneuser8600 3 ปีที่แล้ว +1

      Because it doesn't answer why heaps are used or when one should use them.
      It doesn't give a perfect concrete use-case where a heap would always be beneficial if used.

  • @esa2236
    @esa2236 5 ปีที่แล้ว

    So in this array, the entire time the last subscript will be empty? I ask because when you add a new value to the heap you first put it in the last space in the array then you increment right after.

  • @ontimegrad7069
    @ontimegrad7069 2 ปีที่แล้ว +1

    Thanks for the video! But I am a bit confused about the smallerChirld at around 10 min. Should the left child always be the smaller one?

  • @beingyourself9824
    @beingyourself9824 5 ปีที่แล้ว +1

    Today I actually understand how coders actually codes and how to actually maintain the semantics of variables name fabulous explanation I sub you within 1 minutes of this video

  • @DominicVictoria
    @DominicVictoria 5 ปีที่แล้ว +1

    What overhead will you get from an array of class?

  • @moelayo
    @moelayo 7 ปีที่แล้ว

    In the heapifyUp() function why do you have to reassign index = getParentIndex(index) when the swap function does that for you

  • @tritrinh568
    @tritrinh568 7 ปีที่แล้ว +1

    Didn't know HackerRank has itself a TH-cam channel. Subscribed! :)

  • @persianhenry2897
    @persianhenry2897 5 ปีที่แล้ว

    How can I insert String objects to the Heap if it is an Array? Or should I use and ArrayList

  • @anwarshaikh6023
    @anwarshaikh6023 7 ปีที่แล้ว +62

    Very nice explanation. Though including big O complexity of the operations would be great.

    • @BryanTidwell2
      @BryanTidwell2 7 ปีที่แล้ว +12

      Anwar Shaikh insertion and removal should be logarithmic. of course poll is constant and search is linear but you wouldn't want to use the structure for search

    • @damnstupidoldidiot8776
      @damnstupidoldidiot8776 5 ปีที่แล้ว

      It should be O(nlog(n)).

    • @uusoft
      @uusoft 4 ปีที่แล้ว

      time complexity O(nlog(n))
      space complexity O(1)

    • @drophy
      @drophy 3 ปีที่แล้ว +2

      Insertion and removal have a time complexity of O(log(n)), where 'n' is the number of nodes in the heap. This is because for example, during insertion, in the worst case scenario, you'll need to move the inserted element from the bottom all the way up. Therefore, the max number of swaps is the height of the tree, which is log2(n) approximately (note that this is just true if the tree is balanced, but they always are for heaps).
      For example, her tree had 10 nodes at some point, a height of 3 (or 4, depending on how you define 'height') and log2(10) = 3.32. The max number of swaps you might need when inserting is 3. The same idea applies for removal, since the element you place at the top might need to go all the way down.
      The space complexity of the structure is O(n), of course, since you need an array of size 'n'. The space complexity of the 2 operations, however, is indeed O(1), since they don't need additional space.

  • @wong7642
    @wong7642 4 ปีที่แล้ว

    may i ask, is it true that the array index will always start from 1 for the root in the heap? but your video said it will start from 0? Thank you !

  • @lilypad5182
    @lilypad5182 2 ปีที่แล้ว +1

    For heapingDown, what if instead of the left child being swapped, the right child was being swapped, and the new node would get bubbled down to a place that exceeds the size? then the heap will no longer be compact and there would be empty spots, no? So we'd need additional implementations to take care of this case

  • @kimberlycaritas
    @kimberlycaritas 5 ปีที่แล้ว +1

    SO helpful - thank you so much!

  • @heldermelendez61
    @heldermelendez61 2 ปีที่แล้ว

    Well done, Gayle. Thank you.

  • @VideosOfEarth
    @VideosOfEarth 5 ปีที่แล้ว +1

    I didn't know until now the God of programming is on youtube! Thank you ma'am! 🙏

  • @dvvdsvsd
    @dvvdsvsd 7 ปีที่แล้ว +32

    I have final exam tomorrow, after this explanation, if this will be my pick, I know I'm safe. Amazing videos!

    • @utkarsh_108
      @utkarsh_108 4 ปีที่แล้ว +1

      Best of luck

    • @ShermanSitter
      @ShermanSitter 3 ปีที่แล้ว

      I don't have an exam, but i found it useful as well! I don't know why, but heaps were so confusing...until now! :)

    • @ShermanSitter
      @ShermanSitter 3 ปีที่แล้ว

      @Chris Chu Learns ah shucks. thank you!

    • @kenansari
      @kenansari 3 ปีที่แล้ว +2

      how it was?

    • @Itskfx
      @Itskfx ปีที่แล้ว

      Same, I'm terrible at heaps. These vids help a lot!

  • @rock53355
    @rock53355 3 ปีที่แล้ว

    I've basically watched every one of her videos before starting the chapter in my book on the topic

  • @varunajaygupta
    @varunajaygupta 5 ปีที่แล้ว

    I am getting 404 when I am clicking on the link given in the description. I have tried to find the Cracking The Coding Interview Tutorial on hacker rank on the website, no luck there as well. Can anyone help? Thanks

  • @adelinghanayem2369
    @adelinghanayem2369 6 ปีที่แล้ว

    For the sake of curiosity, how can we implement a heap with with left and right nodes ?

  • @Amolang991
    @Amolang991 2 ปีที่แล้ว +1

    After you poll(), shouldn't you remove the element at `items[size - 1]`?

  • @yourManLan
    @yourManLan 5 ปีที่แล้ว

    Wouldn't the equation for the parent node @2:55 be incorrect? For example, Index 9 with the value 13. if you take (9-2)/2 you get 3. Index 3 is parent of 15 and 20, not 13. Node 7 at index 4 is parent to Node 13 at index 9, so the equation was wrong? Am I missing something? Thanks

  • @Amolang991
    @Amolang991 2 ปีที่แล้ว

    what is the reason you set the helper methods as private and others for public?

  • @ophir1982
    @ophir1982 7 ปีที่แล้ว +12

    Possible error at 1:54 the algorithm is said to be swapping with the smallest of the 2 child elements (when bubbling down) So 20 is swapped with 4, then the pointer is swapped with 9 (left child) while the right child is 7 - smaller.

    • @nopenope8409
      @nopenope8409 5 ปีที่แล้ว +3

      1 year later but that is not correct because what you see there is already swapped so there was 4 before the swap and 20 took the place of 4 and then took the place of 9. there isn't an error

  • @manojkanduri1823
    @manojkanduri1823 5 ปีที่แล้ว

    Curious how rightChild, leftChild hasParent english syllabuls used here are actually implemented when we are dealing with arrays :) May be doable but will turn brain teaser. I guess one would prefer to use classes at that point. In any case this video is worthwhile and very relevant. Thank you Gayle.

  • @johndubchak
    @johndubchak 5 ปีที่แล้ว +1

    Thank you, Gayle. I enjoyed your explanation and found the visual and code helpful.

  • @owinophil5777
    @owinophil5777 ปีที่แล้ว

    why are we swapping indices instead of elements in the heapifyUp method?

  • @gauravmaithani4707
    @gauravmaithani4707 3 ปีที่แล้ว +1

    best vid ever... thanks McDowell.

  • @maheshnampoothirikv5080
    @maheshnampoothirikv5080 6 ปีที่แล้ว +4

    We need to have one more line in the "poll()" method, correct me if I am wrong. I used the starting example (after inserting 3 as new value to heap) to test the same.
    int item = items[0];
    items[0] = items[size - 1];
    items[size - 1] = 0;//We need to make the last element zero explicitly as the last element will stay otherwise.
    size--;

    • @dishantsheth9592
      @dishantsheth9592 5 ปีที่แล้ว +3

      The "size" variable maintains the boundary of the heap in the array and so there isn't a necessity to take care of elements with index >= size in the array.
      Also, "items[size-1] = 0" doesn't achieve the same result as assigning a dynamic node in a tree to null. Here, it simply gets assigned a value of 0.
      To help with understanding, consider the pop operation in the static implementation of a stack. The popped values remain in memory after pop but not in the stack because of the "TOP" pointer there. Similarly here, size keeps track of the boundary of the heap to help with add and poll operations.

  • @AlexandruRepede
    @AlexandruRepede 7 ปีที่แล้ว +89

    this is useful for priority queues

    • @tamoorhamid519
      @tamoorhamid519 4 ปีที่แล้ว +1

      That's one application.

    • @jadanabil8044
      @jadanabil8044 3 ปีที่แล้ว +1

      @@tamoorhamid519 what are the other applications?

    • @tamoorhamid519
      @tamoorhamid519 3 ปีที่แล้ว

      @@jadanabil8044 They can be used to efficiently find the largest or smallest value in an array.

  • @listigertrex8923
    @listigertrex8923 ปีที่แล้ว

    Can someone explain me please why 50% of the internet says parent is (n-1) / 2 and the other 50% says its (n-2) / 2 in a Min Heap? For me when I do the calc (n-1) / 2 should be right.

  • @xXmayank.kumarXx
    @xXmayank.kumarXx หลายเดือนก่อน

    Heaps can be thought of as a binary tree. Peek takes O(1) while other operations take O(log n).
    For min heap:
    1) Insert new node at last, then heapify (ie swap with parent until parent > child)
    2) Delete the root node, replace last element at root then heapify (ie swap down)

  • @nyahhbinghi
    @nyahhbinghi ปีที่แล้ว

    Not sure if she explains this clearly but keeping the array operations to O(1) is probably accomplished via using swaps, where the indices to be used by the swap are found in O(1) by using parent/left/right references?

  • @AAZinvicto
    @AAZinvicto 7 ปีที่แล้ว

    What are the key differences between a min heap and a binary search tree?

  • @Itskfx
    @Itskfx ปีที่แล้ว

    Can anyone how'd it have to change in order for it to function as max heap?
    Currently I assume I'd have to use heapifyDown in the add function instead of heapifyUp?

  • @acymiranda
    @acymiranda 4 ปีที่แล้ว +2

    I don't know, but is getParent(index) correct?
    If I get the final heap of the example, like: [10, 15, 20, 17, 25] and I add an element in the end, for example, 32 and it would be below 17, so 17 is 32 parent.
    32 index is 5. 17 index is 3. If we use getParent(5), I would have: (5 - 1) / 2 => 4 / 2 = 2
    But index 2 is not 17, but 20...
    Am I missing something here?

    • @dmitrybahtiarov3555
      @dmitrybahtiarov3555 2 ปีที่แล้ว

      There is error at 2:56, parent is (index - 1 ) / 2 and not (index - 2) / 2

  • @chaptersdiversified2940
    @chaptersdiversified2940 2 ปีที่แล้ว

    This is the best explanation I've seen :) ty!

  • @KelleyNielsenSalticidSoft
    @KelleyNielsenSalticidSoft 7 ปีที่แล้ว +5

    At 9:03, she moves the updating of index to outside the else block. I'm thinking you could move both lines outside it and get rid of the else branch. Anyone disagree?

    • @RathodDharmin
      @RathodDharmin 7 ปีที่แล้ว +2

      It's more readable and less code, but "else" gives you an idea of flow.

    • @mogomotsiseiphemo1681
      @mogomotsiseiphemo1681 5 ปีที่แล้ว

      You would have to re-write the code as follows :
      if( items[index] >= items[smallerChildIndex] ) //Notice the change in the binary operator from < to >=
      {
      swap(index, smallerChildIndex);
      }
      index = smallerChildIndex;

  • @mariezhang6818
    @mariezhang6818 4 ปีที่แล้ว

    Should didn't heapifyUp() compare both item[index] vs parent and item[index] vs parent.anotherChild to make sure the smallest become the new parent?

  • @vadzimmikhalenak7389
    @vadzimmikhalenak7389 7 ปีที่แล้ว +1

    is it book in russian on the table behind Gayle? :)

  • @cron93
    @cron93 5 ปีที่แล้ว

    Also, what happens if you pass 0 as an index into parent()? You'll get back -1 from getParentIndex since (0-1)//2 == -1, and then you'll get an "index out of bounds error" in some languages or worse, you'll get the last item in the list in python!

  • @lifewear.reseller
    @lifewear.reseller 2 ปีที่แล้ว

    Can someone explain me why don’t we just add 25 at the end of heap instead of deleting 10, because we just wanted to add new one not deleting the min one...?

  • @SavageScientist
    @SavageScientist 2 ปีที่แล้ว

    Bringing back memories of my Data Structures course Shini book it was actually good

  • @pranavnyavanandi9710
    @pranavnyavanandi9710 ปีที่แล้ว

    Why do the heapify() methods need to be public ? Any reason a user might want to access them?

  • @mvcds92
    @mvcds92 7 ปีที่แล้ว +166

    The video feels incomplete because it never explains what a heap is used for, though the data structure very well.

    • @jimmithfarrel8986
      @jimmithfarrel8986 6 ปีที่แล้ว +99

      A heap is used as a queue where the min (or max if max heap) is always accessed in O(1) time. If the min (which is always at index 0 is popped off, then the next smallest takes its place. Remember its stored linearly yet indexed logarithmically. Therefore its a "priority" queue.

    • @mvcds92
      @mvcds92 6 ปีที่แล้ว +28

      Yeah, I've googled it afterward, though it's kind of you helping me here, thanks!

    • @musicprod8366
      @musicprod8366 6 ปีที่แล้ว +3

      Thank you : )

    • @xNajda
      @xNajda 6 ปีที่แล้ว +3

      What's the difference then between a heap data set and just a normal ordered data set using a binary search for the placing of each new element?

    • @sumitbhattacharya1720
      @sumitbhattacharya1720 6 ปีที่แล้ว

      go read a book then.

  • @Memes_uploader
    @Memes_uploader 2 ปีที่แล้ว

    are there in java inself heapifyDon and Up methods?

  • @yonascalender1219
    @yonascalender1219 5 ปีที่แล้ว

    Is there any particular reason why you didn't use an ArrayList?

  • @NickeManarin
    @NickeManarin 2 ปีที่แล้ว

    At @2:49, isn't parent = (index - 1) / 2 like the code uses ?

  • @supportteam2007
    @supportteam2007 7 ปีที่แล้ว +1

    Correct me if I am wrong but I think that adding 1000 (or any number greater than 7) and then adding 5 (or 6 or 7 as well) to the heap example at 3:00 would result in an error if the heapfyUp() code provided further in the video is used. Namely, the top node would be the second number added (5 or 6 or 7) which would be greater than the left hand side child.

    • @bigbangind
      @bigbangind 7 ปีที่แล้ว

      I don't think so

    • @supportteam2007
      @supportteam2007 7 ปีที่แล้ว

      Me neither. haha
      I guess I wasn't paying too much attention.

  • @saurabhvaidya4228
    @saurabhvaidya4228 3 ปีที่แล้ว

    is there anywhere that lists the pros and cons of implementing heaps using arrays and Linked Lists
    Arrays do work amazing tho!

  • @devin6977
    @devin6977 3 ปีที่แล้ว

    Why would you want the heapify methods to be public? Is there a need for them to be accessible outside of the class?

  • @DanielSincAlexandru
    @DanielSincAlexandru 4 ปีที่แล้ว

    I didn't figure it out how main should look like. Could you give me some tips? Thank you! Keep up the good work!

  • @julieh4488
    @julieh4488 6 ปีที่แล้ว

    Great explanation of heaps

  • @IMC5776
    @IMC5776 2 ปีที่แล้ว

    Whats the intuition behind choosing the right child over the left child? Why do we choose the right child if it is less than the left child? A heap is not necessarily a binary search tree is it?

  • @satyam_dey
    @satyam_dey 2 ปีที่แล้ว

    When I clicked on this video I had no idea I'd be learning from the legend herself. Damn.

  • @michaeldang8189
    @michaeldang8189 5 ปีที่แล้ว +13

    hasParent method should be simplified to:
    hasParent(int index) {return index > 0}

  • @yourfriend988
    @yourfriend988 3 ปีที่แล้ว

    Can some tell me please What font is used in the video code?

  • @SpiritVector
    @SpiritVector ปีที่แล้ว

    My brain just short circuited, I think I might be stupid. So if to get to a child of an element you have to scale the index by two and then add one of two choices {1, 2}... but to do the reverse you have to (index -2) then divide that by 2?
    But what if the index is 1?

  • @DanielAzevedo94
    @DanielAzevedo94 4 ปีที่แล้ว

    Awesome explanation. Healped me a lot. Thank you.

  • @illiaharkusha9859
    @illiaharkusha9859 2 ปีที่แล้ว

    Could someone tell the name of the software, that is used in 6:23 to paint on your screen while doing something else, in this case coding?

  • @SicariusVIII
    @SicariusVIII 5 ปีที่แล้ว +2

    Really have to appreciate the readability of the code a variables.

  • @sonntagshans
    @sonntagshans 2 ปีที่แล้ว

    Wouldn't it be more logical to swap the 20 with the 17 at height 4? So you always look if the left node is smaller and if not you compare with the right one. Until you no longer have a smaller child to swap with. Or do I understand something wrong?

  • @annEngr
    @annEngr 3 ปีที่แล้ว

    I'm trying to use your example code in Hacker Rank and it fails two test cases. I can't figure out why or how to fix it. :(

  • @ibrahimkassem7505
    @ibrahimkassem7505 8 หลายเดือนก่อน

    There is an error in the code of getting the parent index, it must be "childIndex / 2 - 1" other wise it will be stuck in the root. or we can put in the while loop (hasParent(index) && index != 0

  • @anirudhreddybasani3555
    @anirudhreddybasani3555 5 ปีที่แล้ว +4

    In getParentIndex() function if childIndex=0 then (childIndex-1)/2 would evaluate to 0 as integer division will be performed...
    so hasParent(int index) function would return true even if index=0 as getParentIndex() function would return 0...So I think hasParent() function is not working when index=0....Is it correct????

    • @imochurad
      @imochurad 5 ปีที่แล้ว

      It seems that your assumption is correct, however, you'll still gonna break from the while loop in heapifyUp() method b/c root is not bigger than itself.
      As a proper fix, one should check if the index passed into hasParent() method is 0. If it is -- return false.

    • @anujpancholi8293
      @anujpancholi8293 4 ปีที่แล้ว

      hasParent or getParentIndex will not, and should not, work for index=0, since the root element itself is positioned at index 0, and by definition, it has no parent. You can include a check for this, but for the sake of simplicity, that check was not included here.

  • @mateuspimentel2753
    @mateuspimentel2753 ปีที่แล้ว

    There is an animation on 1:49 which not reflects the actual logic of the heap sink down ( accordingnaly to the idea of swaping with smallest child node when bubbling down )

  • @mantistoboggan537
    @mantistoboggan537 6 ปีที่แล้ว

    So, is the relationship between heap and tree similar to the relationship between stack/array or stack/linked list? I.e. it's a heap at the higher level of abstraction, and the implementation behind the scenes is another data structure?

    • @BalagardashBashirov
      @BalagardashBashirov 6 ปีที่แล้ว

      yes. But heap is not usually implemented as a tree (but it can be)

  • @stevenmarsh4051
    @stevenmarsh4051 5 ปีที่แล้ว

    What software is she using to show the handwritten aids?

  • @AyanSengupta17
    @AyanSengupta17 5 ปีที่แล้ว +6

    It should be (index-1)/2 for the parent and not (index-2)/2. Please correct it.

  • @adityachawla2163
    @adityachawla2163 4 ปีที่แล้ว +1

    At 2:51 How do you know that the element at the top will still be minimum after insertion?

    • @shawnjackson7568
      @shawnjackson7568 4 ปีที่แล้ว

      Aditya Chawla what do you mean? The algorithm for addition doesn’t change based on the underlying storage tool