Implement A Binary Heap - An Efficient Implementation of The Priority Queue ADT (Abstract Data Type)

preview_player
Показать описание
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions

Question: Implement a binary heap (a complete binary tree which satisfies the heap ordering property). It can be either a min or a max heap.

Priority Queue ADT (Abstract Data Type)

The ADT's Fundamental API

isEmpty()
insertWithPriority()
pullHighestPriorityElement()
peek() (which is basically findMax() or findMin()) is O(1) in time complexity and this is crucial.

A heap is one maximally efficient implementation of a priority queue.

We can have:
-) a min heap: min element can be peeked in constant time.
-) or a max heap: max element can be peeked in constant time.

The name of the heap indicates what we can peek in O(1) time.

It does not matter how we implement this as long as we support the expected behaviors of the ADT thus giving our caller what they want from our heap data structure.

A binary heap is a complete binary tree with a total ordering property hence making it a heap with O(1) peek time to the min or max element.

We can implement the heap with actual nodes or we can just use an array and use arithmetic to know who is a parent or left or right child of a specific index.

Insertion into a binary heap:

We insert the item to the "end" of the complete binary tree (bottom right of the tree).

We "bubble up" the item to restore the heap. We keep bubbling up while there is a parent to compare against and that parent is greater than (in the case of a min heap) or less than (in the case of a max heap) the item. In those cases, the item we are bubbling up dominates its parent.

Removal from a binary heap:

We give the caller the item at the top of the heap and place the item at the "end" of the heap at the very "top".

We then "bubble the item down" to restore the heap property.

Min Heap: We keep swapping the value with the smallest child if the smallest child is less than the value we are bubbling down.

Max Heap: We keep swapping the value with the largest child if the largest child is greater than the value we are bubbling down.

In this manner, we restore the heap ordering property.

The critical thing is that we can have O(1) knowledge of the min or max of a set of data, whenever you see a problem dealing with sizes think of a min or max heap.

++++++++++++++++++++++++++++++++++++++++++++++++++

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

Table of Contents:

Plugging The Channel 0:00 - 0:36
The Problem Introduction 0:36 - 1:13
A Heap Is Not An ADT 1:13 - 1:43
The ADT Priority Queue 1:43 - 3:39
The Min and Max Heap 3:39 - 5:50
Insertions & Removals On A Min Heap 5:50 - 8:57
Our First Removal 8:57 - 11:16
Continue Insertions 11:16 - 13:20
Our Second Removal 13:20 - 15:21
Our Third Removal 15:21 - 15:54
Continue Insertions 15:54 - 17:47
Notice Our Heaps Contents 17:47 - 18:32
Time Complexity For Insertion & Removal 18:32 - 19:35
Wrap Up 19:35 - 20:00

The code is in the description fully commented for teaching purposes.

BackToBackSWE
Автор

This man explains better than my prof who has a PhD in Comp Sci lmao One of the best channels to understand Data Structures!

superparth
Автор

i really appreciate the fact you edit out the insane amount of erasing and rewriting youre doing in all of these. each of these videos must take so long to record and edit O_O thanks mate

brandonhui
Автор

I admire your dedication and treasure it a lot! It is an honour to have someone as skilled as you taking the time to explain these concepts for beginners like us!!!

mariasandru
Автор

This channel is by far the best at breaking down and explaining DSA concepts that I've ever seen (including university). Thanks for your awesome work!

panterra
Автор

2 minutes into the video, this guy answered questions I had to ask my professors for weeks to understand. You might not understand everything he is saying, but this is the same stuff that I'm being taught at a private, super expensive, well ranked engineering school. Thanks for the help!

markrosenthal
Автор

The way he explains, just goes into the mind..

VinothiniAnabayan
Автор

One of the best explanations I've seen for this topic, is complete, clear, with graphics, and examples. Thank you so much.

sacheras
Автор

Omg, this video was posted 5 years back and is still the best video I've ever seen. Your explanation makes it so simple! Glad I found your channel.. it's a gem 💎❤

lavanyam
Автор

Very helpful! I feel like I’m in an actual lecture, but with an awesome instructor. Thanks for the video! <3

minerbytrade
Автор

Thank you for the awesome explanation. Looking forward to more of your tutorial videos

sim
Автор

I like how he asks the question 'Do you see xxxx ?'. It just gives me a feeling that I'm actually sitting in front of him, and it makes me more engaged into the video.

Gzzzzzz
Автор

literally you explain better than any teacher I've ever had, keep it up, greetings from Colombia

DantCJT
Автор

Hello Sir!!!
I am from Myanmar student who is learning Software Engineering.
Your videos are so effective for me and you can explain very clearly.
Thanks for sharing your knowledge.

thihathantsin
Автор

you are an increadible human being for puttin this content out there.

FernandoRodriguez-etqj
Автор

This is "THE BEST CHANNEL" for Love it

adrijasamanta
Автор

This is literally a legendary video lol. I am learning BST and Heap in my data strucutres class and i understand majority of the BST stuff, but the heap was like, easy to understand but weird as hell to understand that its no longer an ADT etc. You did a great job explaining all this.

tannerbarcelos
Автор

this channel is the reason i'll pass my data structures class )': thanks so much man please keep making these videos!! you really explain stuff so in-depth.

fablesfables
Автор

youtube takes time but is rewarding, keep the tempo going

tombrady
Автор

nobody:
ben: "we're missing one child, that's fine"

cobrafriends