No video

Everything you need to know about Heaps (Min Heap / Max Heap Data Structures)

  Рет қаралды 58,645

Greg Hogg

Greg Hogg

Күн бұрын

FAANG Coding Interviews / Data Structures and Algorithms / Leetcode

Пікірлер: 65
@GregHogg
@GregHogg 5 ай бұрын
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@KevinWillowbee
@KevinWillowbee 5 ай бұрын
I like these tutorials better than the leetcode problems. We learn more by these explanations. Subscribed! 🎉🎉
@GregHogg
@GregHogg 5 ай бұрын
Glad to hear it :)
@nark4837
@nark4837 15 күн бұрын
I think there's benefit to both, these tutorials are vital, but then you should start doing heap problems on LC after you know how a heap works for example, I just got to the heap section on LC 75, but I forgot the heap operations so came back to this as a refresher, then will now do the LC 75 problems
@Himanshusingh-su6bj
@Himanshusingh-su6bj 5 ай бұрын
Thank you for posting these informative shorts
@GregHogg
@GregHogg 5 ай бұрын
You're very welcome!
@PandaVlademirBerlin
@PandaVlademirBerlin 5 ай бұрын
I like your voice man, it has some mini explosions from time to time.. And they sound so satisfying
@GregHogg
@GregHogg 5 ай бұрын
Lolol lol thank you I think 😂
@PandaVlademirBerlin
@PandaVlademirBerlin 5 ай бұрын
@@GregHoggthose are called voice cracks
@vamsikolachana8343
@vamsikolachana8343 5 ай бұрын
You’re an inspiration. Please continue doing these videos
@GregHogg
@GregHogg 5 ай бұрын
Thank you greatly, and of course I will :)
@dariofagotto4047
@dariofagotto4047 5 ай бұрын
Give this man a medal for O(n) sorting 😂, wonder how many people fail interviews for these things
@nobrainnogain7255
@nobrainnogain7255 5 ай бұрын
The fact that heapify is done in O(n) does not imply you can sort an array in O(n).
@dariofagotto4047
@dariofagotto4047 5 ай бұрын
​@@nobrainnogain7255Idk what function are you talking precisely, I'm talking about him telling heapify gives you the sorted array in O(n)
@Anthony-oz1jc
@Anthony-oz1jc 5 ай бұрын
????????? what is your point exactly, heapify is a bigO(n) operation for bottom-up and log n for top-down.
@polycrystallinecandy
@polycrystallinecandy 5 ай бұрын
They're saying he incorrectly said that heapify sorts the array
@jonispatented
@jonispatented 5 ай бұрын
​@Anthony-oz1jc yes, heapify is O(n), but in the video, he said heapify sorts the array. It does not.
@abdullahsaid181
@abdullahsaid181 29 күн бұрын
So to sort it's O(n) - heapify- * o(logn) - heapify down or up- => o(nlogn) Which the same time as sort function in python 😊
@CSRookie22
@CSRookie22 5 ай бұрын
Thank you for your video
@GregHogg
@GregHogg 5 ай бұрын
My pleasure!
@reeceslade4145
@reeceslade4145 5 ай бұрын
U the man
@RobertPodosek
@RobertPodosek 5 ай бұрын
Can you make a follow-up on how to use python's heapq module for min/max heaps? Love these!
@GregHogg
@GregHogg 5 ай бұрын
Was actually considering doing that, thank you for the suggestion :)
@RobertPodosek
@RobertPodosek 5 ай бұрын
You da man@@GregHogg
@Darkev77
@Darkev77 5 ай бұрын
This was good
@Krokodil986
@Krokodil986 5 ай бұрын
Wait why is heapify O of n when it calls the push method n times? I would have thought it would be O of nlogn
@Bluedragon2513
@Bluedragon2513 5 ай бұрын
Assuming we have an array of N integers, heapify goes through half of elements and checks whether a value X at index K is greater than the values at indices 2K and 2K+1. This starts at index N/2 because the latter half is already a heap of 1 element. This iterates starting at N/2 and works its way to index 0. Thus, each check is O(1) × N/2 times, which is O(N) time. If we had a tree object to represent the heap, heapify does a postorder traversal where it calls heapify on its children before heapifying itself compared to its children. In this, we do 2 comparisons per call after recursing, and we do N/2 recurses. This gets us to check N/2×2 recursive checks which is O(N) time. The reason heapify is not O(NlogN) is because we do not call push every time. We simply do in-place heapify foe maximum efficiency.
@Krokodil986
@Krokodil986 5 ай бұрын
@@Bluedragon2513 so for each iteration starting at N/2 going to 0, the current element X is compared to only one other element? I didn't know this was the case. And yes, in place heapify would make sense to not call push every time. Could you please reply with some structured English on the main concepts of how heapify works? Id appreciate it
@Bluedragon2513
@Bluedragon2513 5 ай бұрын
​@@Krokodil986 So, for each iteration starting at index N/2 going to 0 which we will call i the current index with its element e = array[i], we will compare e with elements e1 and e2, which lie at indices i*2 and i*2+1. We are doing N/2 iterations. Each iteration, we are checking 2 things: e vs. e1, and e vs e2. In total, we will be doing O(2) checks per iteration * O(N/2) iterations: we arrive at O(N) iteration checks for heapify. ~~ No, we are not doing only 1 comparison, we will be doing 2 comparisons. However, O(2) == O(1) because big-Oh decides the polynomial degree. If this didn't make sense, I will try with better English
@Krokodil986
@Krokodil986 5 ай бұрын
@@Bluedragon2513 thank you, I understand what you said By "structured English" I meant pseudocode which is vague and non-technical. In other words: what are the main basic steps that heapify executes? After you compare e to e1 and e2, what do you do after this?
@Bluedragon2513
@Bluedragon2513 5 ай бұрын
​@@Krokodil986 (assuming we are doing a max-heap) After comparing e with e1 and e with e2, you will select the greatest value. If e is greater than e1 and e2, then do nothing. If e is greater than e1 and less than e2, then swap e and e2 If e is less than e1 and greater than e2, then swap e and e1 If e is less than e1 and e2, then swap e with the higher of the two. Technically, I was wrong when I said you only do 2 comparisons as you'll technically be doing 4-5 comparisons given some nested code. Here's some pseudocode for the whole heapify process: for i from N/2 to 0: e = arr[i] e1 = arr[2*i] e2 = arr[2*i+1] if e > e1 and e > e2: continue else if e > e1 and e < e2: swap(arr,i, 2*i+1) else if e < e1 and e > e2: swap(arr,i,2*i) else if e < e1 and e < e2: if e1 > e2: swap(arr,i,2*i) else: swap(arr,i,2*i+1) end for The reason why we cannot just compare e with e1, swap if needed, then compare e with e2 is because it is not necessarily the case that e1 when transferred over to e2's spot. However, it is guaranteed to be the max when transferring e1 or e2 to e1's spot.
@Nainalarenukadevi9196-dh8rz
@Nainalarenukadevi9196-dh8rz 2 ай бұрын
Please make videos on heapss
@tkrassowski
@tkrassowski 5 ай бұрын
Heapify doesn't sort the array
@GregHogg
@GregHogg 5 ай бұрын
Yeah, this is very true. I was kind of annoyed at my phrasing during editing this. I meant sort in a loose term that it organized them to follow the heap structure, but I agree it was confusing and made it sound like it can actually sort properly
@HR-pz7ts
@HR-pz7ts 5 ай бұрын
How is it any different from a normal tree which is sorted
@dmitriyps22
@dmitriyps22 5 ай бұрын
👍
@Codisrocks
@Codisrocks 5 ай бұрын
"Everything you need to know" doesn't mention a single thing about how to create one.
@GregHogg
@GregHogg 5 ай бұрын
Literally in the video I have text for "how to make a heap" and then precisely tell you. Honest question, did you watch the video before writing this?
@Codisrocks
@Codisrocks 5 ай бұрын
@@GregHogg"How to make a heap" is a lie. It says nothing about how to do that. All it says is how to use one already made for you.
@shanky6343
@shanky6343 5 ай бұрын
Nice vid, whats written on top of your hat?
@baadrqaaba9529
@baadrqaaba9529 5 ай бұрын
Devin is gonna know
@ramalshebl60
@ramalshebl60 3 ай бұрын
and how is it different from a binary tree?
@GregHogg
@GregHogg 3 ай бұрын
It is often visualized as a binary tree :)
@ramalshebl60
@ramalshebl60 3 ай бұрын
@@GregHogg i understand, but how is it different?
@JCel
@JCel 2 ай бұрын
​@@ramalshebl60The rules to how those trees are build. Don't quote me on those time complexities tho. The binary tree doesn't really have a rule except that each parent has a max of 2 children. No sorting whatsoever, not recommended if you want to sort or search for values later on, as it's not better than the list it started with. Adding, deleting and searching in O(n). Generally just not worth it. A search tree is a binary tree with the added rule that each element of the left subtree of a node is smaller/eq than the root and right subtree larger. Unbalanced: adding, deleting and searching in O(n). Balanced: everything in O(log n) A heap only says that all elements of the next level must be smaller (max heap) or bigger (min heap) and that a heap musst be complete aka. The tree is filled to the max, except the last "row" (standard binary heap, there are others). Time complexities as shown in the video. Sorting in O(n log n).
@ramalshebl60
@ramalshebl60 2 ай бұрын
@@JCel thanks for posting a reply even though my comment was a month ago; your reply certainly helped
@chrisdonadio2537
@chrisdonadio2537 5 ай бұрын
This sounds awfully like a stack, whats the difference?
@okta25
@okta25 5 ай бұрын
Does this really sort an array in O(n) ? 😅
@JakeJJKs
@JakeJJKs 5 ай бұрын
It is true, that heapifying an array is O(n), but the resulting array is not necessarily sorted.
@Anthony-oz1jc
@Anthony-oz1jc 5 ай бұрын
a heap is not an array it's a binary tree
@rony86
@rony86 5 ай бұрын
it does...it might look like nlogn but the leaf nodes are not being compared which are half and if you calculate like that you can prove that not every node needs log n comparisons thus n
@RedHair651
@RedHair651 5 ай бұрын
Turns out this is most likely not what most people wanted to know about the heap. It surely wasn't for me.
@GregHogg
@GregHogg 5 ай бұрын
Well that's all there is to know about it so I guess you really don't care about them lol
@bastianp.8164
@bastianp.8164 5 ай бұрын
Well I still don't know wtf a heap is but hey cool vid ig
@emirmustafic786
@emirmustafic786 5 ай бұрын
To whomever it may be of concern, Bear in mind that the O(n) complexity for the heapify process does not include the beforehand sorting of the array.
@memelord4639
@memelord4639 5 ай бұрын
you can heapify in O(n) time without sorting iirc
@emirmustafic786
@emirmustafic786 5 ай бұрын
@@memelord4639 yes, but not max/min heaps
@memelord4639
@memelord4639 5 ай бұрын
no you can build both min and max heaps in O(n) time complexity@@emirmustafic786
@therealraymondjones
@therealraymondjones 5 ай бұрын
Rip people who watch this and think heaps fully sort arrays
@GregHogg
@GregHogg 5 ай бұрын
Yeah, I know my phrasing was a little misleading. It partially sorts enough to force it into a heap
@SKrandy
@SKrandy 5 ай бұрын
Lol Ive read it as неар
@dantkillmyvibe
@dantkillmyvibe 5 ай бұрын
Why is it log n to do an insert ?
Most Common Concepts for Coding Interviews
6:08
NeetCode
Рет қаралды 306 М.
А ВЫ УМЕЕТЕ ПЛАВАТЬ?? #shorts
00:21
Паша Осадчий
Рет қаралды 2,2 МЛН
艾莎撒娇得到王子的原谅#艾莎
00:24
在逃的公主
Рет қаралды 54 МЛН
SPONGEBOB POWER-UPS IN BRAWL STARS!!!
08:35
Brawl Stars
Рет қаралды 23 МЛН
3 Types of Algorithms Every Programmer Needs to Know
13:12
ForrestKnight
Рет қаралды 467 М.
Why I focus on patterns instead of technologies
7:55
NeetCodeIO
Рет қаралды 220 М.
Why you should not be a data scientist
12:33
Tina Huang
Рет қаралды 759 М.
Golang pointers explained, once and for all
13:49
JamieGo
Рет қаралды 5 М.
My Brain after 569 Leetcode Problems
7:50
NeetCode
Рет қаралды 2,5 МЛН
Top 7 Data Structures for Interviews Explained SIMPLY
13:02
Codebagel
Рет қаралды 172 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 189 М.
COMPUTER SCIENCE explained in 17 Minutes
16:49
Wacky Science
Рет қаралды 927 М.
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 364 М.
А ВЫ УМЕЕТЕ ПЛАВАТЬ?? #shorts
00:21
Паша Осадчий
Рет қаралды 2,2 МЛН