Lecture 6: AVL Trees, AVL Sort
HTML-код
- Опубликовано: 13 янв 2013
- MIT 6.006 Introduction to Algorithms, Fall 2011
View the complete course: ocw.mit.edu/6-006F11
Instructor: Erik Demaine
License: Creative Commons BY-NC-SA
More information at ocw.mit.edu/terms
More courses at ocw.mit.edu
02:50 height of a tree
06:40 height of a node
10:40 AVL tree
14:40 AVL tree balanced
29:00 AVL insert example
32:45 rotation
40:05 general AVL insert
48:42 AVL sort
@45:00- it should be LeftRotate(x), not Right
Yeah, exactly. I am surprised none of the students pointed that out, even though it's MIT.
I went back and forth from 45:00 to the part where he did the left-rotate(x) originally and was sure I'd missed something. Thanks for clarifying. MIT's RUclips videos are some of the few with comments that are actually useful :-).
This needs to be upvoted to the top of the comments section.
RR means you have to left rotate, its used RR notation because you add a node to the RIGHT twice.
so is everything else he says after right? with reference to the trees he drew
all those beautiful mornings ruined for stupid classes whilst i could have listened to this guy at my warm bed with a cup of coffee at my hand. smh
Mesele dersi dinlemek değil aynı dersi aynı şekilde başka yerde de dinleyebilirsin
Dude, I'm feeling it right now during the class, my professor is kinda bad
I am thinking the same blud lol
05:42 that jump though
#secret_teaching_skills
@44:57 its called a left rotate of x. Not right rotate. He goofed up.
CEngineer2010 This caused me so much confusion! I agree that he reversed them.
+CEngineer2010 Yes. That's a left rotate of x!
+CEngineer2010 Thanks man! I was so confused about that!
+CEngineer2010 super upvote. this is really important lolol
+CEngineer2010
when I was a student, I often found mistakes in notes which I made at lectures. And I used to think: "o, the professors are so silly, even me, ordinary student, find stupid mistakes in what they teach".
Everything reversed when I started to teach. I cannot explain this exactly, but something like magic happens. When you stand near the board and teach, you sometimes forget trivial things, and do such silly and stupid mistakes that even you cannot understand how that happens.
It is not that easy to stand near the board and explain something to other people.
Thank you, Erik Demaine. Your courses are always so clear and intuitive!
It blows my mind that people can essentially get a free MIT education from these lectures. Thanks for all the CS lectures & the rest of the online courses! I look forward to absorbing as much knowledge as I can from them!
Thank you for this video, MIT! It helps me a lot to understand the material. And thank you to Mr. Demaine for the explanation. Your explanation is understandable.
i swear this is the best algorithm lecture you could ever find on internet!! I tried paid courses from other top universities and they are not nearly half as clear as MIT!!!! great MIX of concept and image rather than plain formulas and calculations!!
I love this guys, so enthusiastic about what he's teaching!
He’s a child prodigy for no reason
Unlike indian profs 🔥🔥
Thanks a lot! Way better then my prof in germany... Amazing how much better motivated ppl. can teach stuff! Keep up the good work!
Ja, ich glaub die haben da auch ein ganz anderes Verhältnis als in Deutschland. Die Studenten dürfen ihn mit Vornamen anreden (es noch weitere Videos, aus dieser Vorlesungsreihe, wo er mehrfach mit "Erik" angesprochen wird", er redet mit denen Studenten, versucht einen lebendigen Vortrag zu entwickeln etc. Kein blödes Rumgedrücke mit Folien und Laserpointer, wie es in Deutschland gelehrt wird, sondern richtige Herleitungen Schritt für Schritt. Massachusist müsste man sein^^
I love how the professor is kinda dancing while cleaning the board @ 26:05
Oh my god I wish my instructor was like this. I swear my csc instructor just pulled rotations out of his mitochondria and did not explain a lick of how they work.
At least you learn about the avl tree in your university...
same dude...same...At least your teacher speaks
at least you went to university...
Haha this is why I believe this video has so many views :[
@@Retrosenescent "Bad english is the language of science"
Thanks MIT and prof. Erik for the fantastic lecture on AVL trees
It is a enjoyment to watch Erik's lecture! Greetings from Dalhousie!
At 45:00 isn't it LEFT ROTATION of X instead of RIGHT ROTATION of X?
UPDATE: Already pointed out by DaxXx988 in the comments below. Thanks!
Dude I thought I was the only one. The teacher, towards the end, was tired and as confused as the rest of us.
SO good to see those teachers would take time to encourage students to pause and think what just happened
Thanks a lot professor for solving my doubt of AVL Tree and also thanks to MIT
Very strong lecture! Clearly understandable and Smooth
Great lecture! Really helped to understand AVL trees for my exam
Hey, how do we do to define which node is ''x'' oder ''y''
And in double rotation who is ''x'', ''y'' or ''z'' please? thanks!
Nice lecture I got clear image in my mind. Thank you so much sir
Beautifully explained
thanks MIT & professor for this video
@49:16, the instructor mentions the insertion time for AVL try is nlg(n), is it because we have to talk about the worst case when we are talking about insertion time? Since it seems it assumes all the insertions are going to the leaf node?
@34:55, I don't understand how he checked formally. "B contains all the nodes between x and y" -what does this mean?
Could someone please help me?
A balanced binary tree, also referred to as a height-balanced binary tree, is defined as a binary tree in which the height of the left and right subtree of any node differ by not more than 1.
Log (n) = height for balanced trees. On the example on 5:20, there are 15 nodes, height is 4, but Log(15) = 3.90689059. Wouldn't it be height = Log (n+1) ? What am I missing? Or "+1" is considered a constant, so it is not inserted into the equation?
31.57 : "This guy is doubly left heavy.
Hmm, Bad News."
Cracked me up.
Does the abstract data type have the feature 'search' that BST provides as well?
Erik's explanations are great! Thanks to MIT.
if node height = max height of the left + max height of the right + 1 shouldn't the root of the tree at 9:35 = 4 and not 3?
@6:42 how is the height of the node equal to the level of the node? Isn’t the Level the depth..? Can anyone help here if I misunderstood levels?
Why in the lecture note on page 6 the running time for findmin of AVL tree is constant?
Have a doubt about BST in this lecture, where professor writes >=x and < =x that shows nodes in left subtree are equal or less than its parent...same on another side. Isn't BST supposes to have no duplicate nodes, which means nodes in left subtree must be less than its parent not equal or less, and nodes in right subtree must be greater than its parent not equal or greater?
For some reason the Frisbee cushions bring me such joy. xD
Those tree nodes are so cute
I'm a Maths student from Italy and my exam is tomorrow! thank you so much for sharing these!! they're so useful!!
But why did you leave it to the last moment? :)
I had been studying for a month, i was revising. Passed it.
g
Great lecture, thank you very much!
If we could listen to the students remarks and questions, that would make this video in my opinion mush more beneficial.
You can turn on the subtitles, it shows what the students are saying
There must be some sort of translations in the course's website
At 27:30 ,
how did he get N(h) > 2*N(h-2) to N(h) > theta(2 ^ (h/2) )
did you get the answer for it ?
N(h) > 2 * N(h - 2) > 2 * (2 * N(h - 4)) > 2 * (2 * (2 * N(h - 6))) > ... > 2^k * N(h - 2k) for some non-negative integer k and recurrence ends when N(h - 2k) reaches base case N(0) for k = h/2.
24:50 a random n appears in the reasoning whereas we were always talking in terms of N_h, h, and phi. He meant phi^h / sqrt(n) < N_h
i got confused for a while, maybe thats what reviews are for
i'm very impressed by the way u taught :)
25:39 "Now if you don't know fibonacci numbers, I pull a 'rabbit' out of a hat and..."
I see what you did here Eric.
At 46:55, and was noted at another point earlier that there may be an imbalance in the higher levels of a tree if an imbalanced node is balanced at a lower level of the tree. Is that really true in a properly coded AVL tree? A properly coded AVL tree is balanced at the moment of insertion. After insertion, node X is imbalanced because of, let's say, right-heavy right subtree Y. After rotation(s), Y is now at the previous location of X, and Y has the same height as X before the insertion. This is because Y's height increased by 1 as a result of insertion, and now has the same height as X before insertion. Moving Y to replace X in the rotation means the height at the NODE OF ROTATION does not change. Therefore, the only way that there could be further imbalance up the tree is if the tree was already imbalanced before insertion.
Amazing! Greetings from Mexico.
i think that 45:13 it is a left rotate not a right rotation. can someone help me pls? since he's moving the x to the left thus it is a left rotation
Another reason to give NULLPTR a value of negative one is that you have to move upward once to get to a genuine leaf.
At 46.00 is a left rotate, not a right rotate
RR means you have to left rotate, its used RR notation because you add a node to the RIGHT twice.
how to check if the node is even, left heavy or right heavy???????????????
At 44:58 shouldn't it be left rotate of x insyead of right?
Damn good prof. ... Need more videos from him !
27:32 "... I'll just write theta to avoid the base case"
Does anyone know what that means?
this guy is a magician with chalk
He sometimes switches between 'depth' and 'height', but the depth or level of a node is it's distance going down from the root, right?
Also I've seen different ideas about whether the root of a tree is at level/depth 0 or - 1. Is there a widely accepted consensus on which should be used or is it just a matter of consistency with the formula/algorithm in the same way that null nodes were defined to have height - 1 here?
The notation and terminology around this topic is a tad confusing...
I think level and depth are same but height and depth are different.That -1 theory for null nodes is widely accepted and about that root being at level -1,I've never come across any algorithm which assumes the level/ depth of root of a tree as -1.
MIT &Erik you owe me for this!!
Don't judge by edit sign on comment i misspelled Erik Lol😁
Can someone explain 26:09?I don't get why he changed N(h-1) to N(h-2) and removed the 1. Also I don't get how 2N(h-2) = (2^(h/2)
Yes,but how?
Thanks! By the way, why h is decreasing by 2? Why the second line should be N(h-4) instead of N(h-3)??
it is more convenient to use log, I guess. If you use N(h-3), then you get N(h) > 2*N(h-3), nothing helpful
Becase N(h) > 2N(h-2) and h replaced by h-2, the righthand side becomes N(h-4). Actually you can write this process as N(h) > 2N(h-2) > 2*2*N(h-4) until N(0)
Sagar Theurkar Thanks a lot! I had a same question and little trouble to derive the last from the first. It was not only me who have the same.
This teacher is awesome
at 44:58 he says do a right rotate, but isn't what he does a left rotate?
It's so cool that these top schools release courses like this one online free of charge. I may not get a chance to go to MIT
yeah no shit
I love this prof
Literally a genius.
great teacher
Thank you MIT.
He really loves that board
Can anybody give an example when you need more than two rotations? After one or two rotations height of an unbalanced subtree remains the same it was before insertion, doesn't it? So I don't understand why we might need more than two rotations.
maybe when you are given a task of Converting BST to AVL
I study at UNIMELB and this is so much better...and this is in 2011.
@31:30 "now all the heights change... and it's annoying to draw what the heights are... but... I'll do it"
Yes I also watched the video
are the hard seats made on purpouse? lol
Tree rotation is magic.
At 11:38 . Why not define the empty left or right tree as height = -1 and not as you mention deept = -1.
Thanks a lot Sir.
he also blows glass. like a badass
in at around 39:00 he is rotated the tree. ok but it is not exactly same as the previous. means he rotated the previous ---- axbyc --- and rotated the result same. but at if her double rotation at 65 . it has to be 55 -50- 65 but the result is 50-55-65. not the same as the previous.
Is Erik out there? or could someone help me?
I have a question.
At 45:20, RR(x) should be changed to LR(x) or RR(y)? it violates 35:21
Thanks.
It should be LR(x)
RR(y) would be something completely different but you are correct
Why didn't he start with a balanced tree. Is it not true that before he inserted the 23 the tree was already off balance. The tree was left heavy.
I am still just starting the study of this structure, but it would seem that the tree would have to left rotate at 20 so 29 becomes it's parent and 26 joins it. Then the left subchild of the left subtree is heavy so it would right rotate so 29 becomes the head.
Maybe it is because he knew he was going to add the 55 and didn't care that he didn't start balanced. If he did that rotation first the tree would be a perfect tree after adding the 55.
Perhaps I'm confused.
It would seem that you could improve the algorithm simply by looking beyond the max{HR,HL}. Could you not calculate optimal tree height for N and if max height after walking down to insert in the BST fashion is worse, then start rotations on the max height side.
First step towards that side being heaviest in that side (left heavy where left is the issue/right heavy where right is the issue), then balance the head node by rotating away from the heaviness. Is there another name for that tree balancing algorithm.
Eric Belrose The tree at start point is not complete balanced, but is indeed `AVL balanced` which means it obeys the constraint of AVL tree, so it is a possible real world case you'll dealing with.
the tree was left heavy but only by a factor of 1 over the right side. The idea is to keep the lack of balance either 0 or 1 to either side, but not larger than that 1. Otherwise if you forced a perfect balance of 0 at all times, you would be entirely unable to modify the tree, since you can't add two nodes at the same time, and if you did, you'd have to then add four nodes at the same time the next time and so on. If you allow it to be left or right heavy by at least a factor of 1 then you can add nodes as you go and then rebalance the tree if it's necessary, because you allow it to have empty spots. If that wasn't the case, say you have only the root node, and you want to add a child to it. You just can't, because that'd already make it either right or left heavy from the start, and so that would break the 0 balance factor.
I think he may have made a mistake. He defines N(h) as the minimum number of nodes in an AVL tree of height h. In my opinion N(h) = 2^(h - 1) + 1 (I think a student suggested that but he never really elaborated on why that was wrong). As I see it, you take the perfectly balanced tree of height h - 1, and you add only 1 node to increase the height by 1.
@11:13 - Would like some confirmation on this, did he mean "height" to be -1 when he said "depths"?
yes. he meant "height"
keep the height order log n to keep the left n right tree balanced
здравствуйте, объясните пожалуйста, как этот бизнес растает.
AVL Insert algorithm is incorrectly described as possibly requiring more than 1 or 2 rotations and having to travel up to the root. After 1 or 2 rotations retracing is done. I skimmed the video waiting for delete() and it isn't there, delete rotations can introduce further imbalances up the tree. For an example of the insert see: refactoringlightly.wordpress.com/2017/11/05/rank-balanced-avl-trees-on-github/
Yeah, you're right about the amount of rotations required after inserting a new element. I was confused when he said about needing to traverse the AVL up in order to find any new imbalances so I was looking for a comment like yours to confirm my doubt xD
Earlier i was sad :( now m happy :) thanks Erik
Lol, good to see your tree got balanced
How come N suffix (h-1) + N suffix (h-2) got converted into 2*N suffix (h-2) ?
Timestamp : 28:31
We know that N(h-1) > N(h-2) as the latter has one less layer/height from the former. Therefore adding N(h-2) on both sides, N(h-1) + N(h-2) > 2 N(h-2). Hence if something > N(h-1) + N(h-2), it is also > 2N(h-2)
no if you look at the maximum height of the children it is 2(maximum) since the last node has a height of 0. ok?
@46:08, why is it k-3? The triangle is connected to the X? similarly, the B triangle is connected to Y which is k-1 and C is k-2, but B is k-3?
because the earlier assumption is "suppose x is the lowest node VIOLATING AVL", if x is k-2, then x will be AVL, thus contradicts the assumption.
at 37:25 he says that the 65 node is doubly left heavy, how is this? Can somebody please explain this?
exactly, how it is doubly left heavy? it seems like a balanced tree to me. Am I missing something?
40:47 - not sure it's correct that "we might have to do more than 2 rotations"
after a violation fix, the tree should become re-balanced again
+korvix He explains that just after what you quoted. By adding a node you make all of the (left or right) subtrees of nodes on path from root to the added node larger. This one violation fix fixes only one of these subtrees. Others might have to be fixed too.
Otherwise each operation would have a constant complexity. That'd be lovely, but unfortunately it's not true :(
yes you are correct: when a node is balanced after an insert, it will have the same height as before the insert, so after doing a post-insertion rebalancing of the first unbalanced node, you can terminate. in contrast, with deletion, you need to rebalance all ancestors.
BST has not a same Node...X-node can't equal X-Root in this case
Very nice your lecture
Thanks for explaining this. I can't believe how those students just wonder in late ,, given the opportunity they have
because it gets filmed and put online.
@@TheMasterfulcreator ?
@@jonsmith1271 i'm saying i probably wouldn't worry too much about seeing the entire lecture if it all gets recorded and put online anyway
at 42:50 didn't he mean that the left sub tree is -1 and the right one is -1 and that's why it comes out -2? If it was 0 like he says then the node would be balanced
also at 44:20 it seems like if you're AVL tree is good, it will never happen that node x is unbalanced and at the same time x's right child will be balanced.
Good stuff, thanks bro
The lecture is AWESOME. but yeah, the LR thing and the heights of that tree in the question.. that isn't (h-3)... else the right child would be left heavy.. (lesser the height, lower it is..).... just my point of view.... I love these lectures :D
cushions, everytime they get a right answer they get a cushion because the seats they sit on are hard seats.
@4:45 when writing the inset operation Erik got the direction wrong. It's a left rotation
guy walking in at 1:50 is a treat!
after 7 years , still excellent
AVL FULL CODE C/C++
ruclips.net/video/dpc8Wl09hOw/видео.html
Yes it is
Viel besser als unsere Algorithmentheorie-Vorlesungen
And the people said that you have to become a nerd to be smart and here we have Eric :P
I can tell that the students are pretty smart, just comparing it to my class at University of Waterloo. Although I can't tell if its the same students answering the question. I know that I would be clueless for half these questions.
Waterloo is a pretty damn good school.