C++ Max Heap Implementation

Поділитися
Вставка
  • Опубліковано 16 січ 2025

КОМЕНТАРІ • 72

  • @insofcury
    @insofcury 3 роки тому +4

    This is the best implementation of a priority queue ever. Thank you so much

  • @chalie42
    @chalie42 3 роки тому

    Man, Thank You for taking the time to explain what my Uni lecturers would not.

  • @SuperKing11100
    @SuperKing11100 4 роки тому +7

    By far one of the best explanations I've seen. Thank you so much for this video, I just started covering trees along with heaps and this was very helpful, I don't know how you don't have more views/subscribers. I am now subscribed, I look forward to your videos! I just had one question, this might be a dumb one but I was wondering why did you use ">>" & "

    • @CodingJesus
      @CodingJesus  4 роки тому +4

      Because bitshifting a number to the left multiplied it by 2, and bitshifting to the right divides it by 2.
      Thanks for your support!

    • @mallew32
      @mallew32 3 роки тому

      This is a great question. To follow up on it, @Coding Jesus, is it faster in c++ to bitshift than to multiply/divide?

    • @AmandeepSinghGini
      @AmandeepSinghGini 2 роки тому

      @@mallew32 Yes, usually they are faster. However certain machine do have their math processor, which contains special instructions for multiplication/division.

  • @jimmyshenmusic
    @jimmyshenmusic 4 роки тому +6

    It's such an awesome implementation. I implemented it by myself based on your video and I found for the shiftUp method, maybe we can optimize it a little bit by changing the logic as below:
    ```
    void MaxHeap::_shiftUp(int i) {
    if (i > _size) return;
    if (i == 1) return;
    if (vect[p(i)] < vect[i]) {
    swap(vect[p(i)], vect[i]);
    _shiftUp(p(i));
    }
    else return;
    }
    ```
    By doing this, we don't need to always go to index 1.

    • @CodingJesus
      @CodingJesus  4 роки тому

      Thanks for the kind words.

    • @jimmyshenmusic
      @jimmyshenmusic 4 роки тому

      @@CodingJesus Thanks for the nice post.

    • @iliasp4275
      @iliasp4275 4 роки тому

      yeah, i was about to comment the same thing but as a question, as i am not very familiar with these things

  • @aadarshktofficial
    @aadarshktofficial Рік тому

    I had a doubt in shifDown function when reading online article, but after watching your video doubt is cleared thanks.

  • @nachoman400
    @nachoman400 3 роки тому

    Let's ignore the explanation and assume it's a muted video; he can write good readable code.
    Good video.

  • @mr.mirror1213
    @mr.mirror1213 4 роки тому +8

    Damn bro , this is quality content holy shit

  • @jimmyshenmusic
    @jimmyshenmusic 4 роки тому +2

    This is awesome. 1 based indexing is exactly what I am looking for. Thanks.

    • @CodingJesus
      @CodingJesus  4 роки тому

      Cheers! Thanks for subscribing.

    • @jimmyshenmusic
      @jimmyshenmusic 4 роки тому

      @@CodingJesus Thanks for sharing. I am looking forward to your more posts such as LRU/LFU, red-black tree. It is very nice you also post hash table implementation.

    • @CodingJesus
      @CodingJesus  4 роки тому

      @@jimmyshenmusic Yeah I'm going to make a series on data structures. Thanks for those suggestions.

    • @jimmyshenmusic
      @jimmyshenmusic 4 роки тому

      @@CodingJesus That's great. I am preparing coding interviews and I found the LRU is very frequently asked.

  • @nazmicancalk2415
    @nazmicancalk2415 2 роки тому

    One of the best explanations I have seen, thanks!

  • @mariorios8827
    @mariorios8827 4 роки тому +2

    Thanks!! It was so clear. Hope you continue making such great videos.

  • @hendrykik7132
    @hendrykik7132 2 роки тому

    Thank you man, in my laungage "possałbym bym ci jajca za to".

  • @JRivero
    @JRivero 8 місяців тому

    Much appreciated. Very good explanation.
    cheers

  • @TonyAntonucci
    @TonyAntonucci 2 роки тому

    Why the semicolons after the closing curly brace on method bodies?

  • @wayne_logan
    @wayne_logan 2 роки тому

    there is a subtle off-by-one error in the r() and l() methods. Vector indices are always less by 1 than the actual index. So, l() should return i1 (to implement i*2 + 1 and i/2 respectively)

  • @chendadon7168
    @chendadon7168 4 роки тому

    nice to see such a clean code ! the 1 base idx was very clever idea

  • @ShimShammidy
    @ShimShammidy 3 роки тому

    Damn bro, 🔥 explanation. Instantly hit that subscribe button. This is good shit homie, quality content.

  • @nathansmith3974
    @nathansmith3974 4 роки тому

    Coding Jesus you are the man! Great explanation thank you. I hope to see more videos from you

    • @CodingJesus
      @CodingJesus  4 роки тому

      You can see more if you subscribe. :D

  • @shivamchauhan2673
    @shivamchauhan2673 4 роки тому +1

    What a great explanation bro, nailed it.

  • @eladon19153
    @eladon19153 11 місяців тому

    I think I did not understand or there is a problem in the code - shiftDown should have if (i >= _size), and not if(i > _size), and also, why didnt you just say in shiftUp that it should stop after it didnt replace the item?

  • @dayronpadilla2203
    @dayronpadilla2203 3 роки тому

    How can I get the code? Thanks

  • @raghavnandwani4890
    @raghavnandwani4890 4 роки тому

    Nice work man, it was really helpful... !!
    If you can, next video, implement graphs and implement BFS and DFS... !!

  • @kaslon13
    @kaslon13 4 роки тому

    Is there any specific reason that the first index in the heap needs to be a garbage value? Can't we just make index 0 the max instead of 1?

    • @CodingJesus
      @CodingJesus  4 роки тому

      0 times any number is always 0.

  • @AmandeepSinghGini
    @AmandeepSinghGini 2 роки тому

    really good explanation

  • @arsenalism
    @arsenalism 3 роки тому

    Great explanation. Keep it up

  • @ilovancristian
    @ilovancristian 3 роки тому

    nice video, great explanation

  • @ThatTURK1
    @ThatTURK1 3 роки тому

    I dont code so i have no idea whats going on but what would be the purpose of the implementation of this code?

    • @chris_burrows
      @chris_burrows 3 роки тому

      Mix and max heaps are very fast to sort and the lowest value is always guaranteed to be the root node which again is very quick to access.

  • @sk07shubham
    @sk07shubham 3 роки тому

    Can I get code?

  • @黃威凱-u9z
    @黃威凱-u9z 3 роки тому +1

    I think using vect.pop_back()(after swap) and size- - would be better since this will release the memory

  • @tusharjangid2492
    @tusharjangid2492 3 роки тому

    Great video, it was a real help, thanks man.

  • @abdelrhmanahmed1378
    @abdelrhmanahmed1378 4 роки тому

    i have a question what if we want to heap sort then we can delete all element and print the vector ,but what if
    we want to correct it again 40 30 20 15 10 , it will be 20 30 40 15 10 .which is not right ,so how we can fix that ?!

    • @CodingJesus
      @CodingJesus  4 роки тому

      I don't understand your question

  • @fahadabbas5252
    @fahadabbas5252 4 роки тому

    it is giving me error on 12 line where vector vect = {-1};
    [Error] converting to 'std::vector' from initializer list would use explicit constructor 'std::vector::vector(std::vector::size_type, const value_type&, const allocator_type&) [with _Tp = int; _Alloc = std::allocator; std::vector::size_type = long long unsigned int;

    • @CodingJesus
      @CodingJesus  4 роки тому

      Works just fine, the problem is somewhere else in your code.
      godbolt.org/z/b187f8

  • @SkuezTech
    @SkuezTech 3 роки тому

    hey man, just thought i'd say i wouldn't recommend putting stuff like i

  • @CaveDave
    @CaveDave 4 роки тому

    6:50 it is not the "smallest element", but the last added which gets swapped. - Take your time to make things clear. For me, you rushed way to fast through this.

  • @RanaKamel-z7v
    @RanaKamel-z7v Рік тому +1

    Please Give me all code

  • @sarthakbhatia1728
    @sarthakbhatia1728 4 роки тому

    what does _size{} mean
    ?
    Also,thanks :)

    • @CodingJesus
      @CodingJesus  4 роки тому

      _size = 0

    • @sarthakbhatia1728
      @sarthakbhatia1728 4 роки тому

      @@CodingJesus is it some C++ syntax or does it convey something. Like , what are those extra braces for?

    • @sarthakbhatia1728
      @sarthakbhatia1728 4 роки тому

      and also please explain this if condition in insert function

    • @CodingJesus
      @CodingJesus  4 роки тому +1

      @@sarthakbhatia1728 It's C++11 syntax, brace initialization. It initializes the declared variable to it's default value (in this case 0). Without it, it would contain a junk value.

    • @CodingJesus
      @CodingJesus  4 роки тому

      @@sarthakbhatia1728 If a vector has 10 elements, you cannot index into its 11th element. So because I can't index into the 11th element, I add a place holder (0), and both increment the size of the vector, and change the placeholders value to the value I'd like it to be.

  • @abhaykaushik5944
    @abhaykaushik5944 3 роки тому

    this is damn good explanation, thnku so much

  • @1264781
    @1264781 4 роки тому +1

    thx man!

  • @Mandeepsingh-jo5cf
    @Mandeepsingh-jo5cf 4 роки тому

    Thanks

  • @dipeshjha3528
    @dipeshjha3528 4 роки тому

    Ur keyboard making too much noise

  • @Diablo1313-
    @Diablo1313- 2 роки тому

    do sorts please

  • @akshatsuwalka5759
    @akshatsuwalka5759 3 роки тому

    #include
    using namespace std;
    class max_heap{
    private:
    vector arr = {-1};
    int _size{};
    int p(int indx) {return indx>>1;};
    int l(int indx) {return indx_size)
    return;

    int largest = indx;
    if (l(indx)InsertHeap(34);
    pq->InsertHeap(41);
    pq->InsertHeap(5);
    cout MaxHeap() ExtractHeap();
    cout MaxHeap() ExtractHeap();
    cout MaxHeap()

  • @Lordecai
    @Lordecai 4 роки тому

    Hey I made a pastebin from your code. For some reason I just get correct from the isEmpty, but nothing else from the vectors work.
    I can link the pastebin here or the code, whatever works
    pastebin: pastebin.com/4X7BTLYz

    • @Lordecai
      @Lordecai 4 роки тому

      //heap
      #include
      #include
      using namespace std;
      class MaxHeap
      {
      private:
      int _size{};
      vector vect = { -1 };
      int p(int i) {
      return(i >> 1);
      };
      int l(int i)
      {
      return i vect[p(i)])
      swap(vect[p(i)], vect[i]);
      shiftUp(_size);
      return;
      }
      void shiftDown(int i)
      {
      if (i > _size) return;
      int swapId = i;// want to track comparisons
      if (l(i) isEmpty())
      cout insertItem(120);
      PriorQ->insertItem(34);
      PriorQ->insertItem(41);
      PriorQ->insertItem(5);
      cout getMax() extractMax();
      cout getMax() isEmpty())
      {
      cout

    • @especialistaemnada718
      @especialistaemnada718 7 місяців тому

      thank you so much!!