In this video we walk through a real example of maintaining an AVL tree by performing a rotation. We start with an unbalanced tree, compute balance factors, identify the Z Y and X nodes, and apply a double right rotation to restore balance. Perfect follow-up if you’ve seen the basics of binary search trees and AVL trees.
Watch as we turn an invalid AVL tree into a perfectly balanced one with clear step-by-step instructions.
If you’re learning data structures and algorithms, this practical example will help you understand when and how to rotate.
00:00 Introduction to AVL Tree Rotation
00:14 Prerequisites and Previous Videos
00:36 AVL Trees Overview
00:40 Types of Rotations and Balance Factors
01:01 Examining the Example Tree
01:24 Confirming Binary Search Tree Properties
01:27 Computing Balance Factors
01:39 Identifying Imbalance at Node 65
02:01 Locating the Z Node
02:54 Finding Y and X Nodes
04:08 Assigning X Y Z Values
04:33 Creating ABC In-Order Representation
04:58 Drawing the Target Rotation Pattern
05:34 Updating Node Values in Pattern
06:04 Checking Unaccounted Children
07:00 Reattaching Nodes and Performing Rotation
07:56 Recomputing Balance Factors
08:24 Updating Root Balance Factor
08:41 Resulting Perfectly Balanced Tree
08:48 Identifying Double Right Rotation
09:06 Explaining the Rotation Process
10:16 Conclusion and Next Video Teaser
11:56 Channel Promotion and Outro
15:48 Final Hello and Recap
=-=-=-=-=-=-=-=-=
Thanks for watching!
Find us on other social media here:
- https://www.NeuralLantern.com/social
- Twitter / X: https://x.com/NeuralLantern
- Rumble: https://rumble.com/c/c-3696939
- BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt
- Daily Motion: https://www.dailymotion.com/neurallantern
- Minds: https://www.minds.com/neurallantern/
- Odysee: https://odysee.com/@NeuralLantern:5
Please show your support!
- Buy me a coffee: https://ko-fi.com/neurallantern
- Subscribe + Sharing on Social Media
- Leave a comment or suggestion
- Subscribe to the Blog: https://www.NeuralLantern.com
- Watch the main “pinned” video of this channel for offers and extras
Hey there! Let’s maintain and rotate an AVL tree which is a self-balancing binary search tree.
Okay. Alright, so hopefully by now you’ve seen my other videos. If you haven’t,
you probably won’t understand this unless you’ve seen other videos elsewhere.
But in my previous videos we talked about what is a binary search tree, how to make one, how to
binary search tree, how to make one, how to define one, how to know you’re looking at one, how to build one, search,
insert, remove all the operations and time complexities.
And then we talked about AVL trees, which are a self-balancing binary search tree.
And we talked about what are the different types of rotations you can do? Why would you rotate?
How do you compute balance factors? How do you know when it’s time to rotate?
How do you do the rotation? Stuff like that. So this is just going to be one
example tree, and we’re just going to try to burn through it. If you want to learn more about
see my previous videos. Okay, so supposing now that you have a tree that looks like this, let’s
confirm that this is a valid AVL tree. This seems to be a tree. It’s a connected graph. It’s got no
cycles. It’s a rooted tree. It’s a binary tree. There are no more than two children per every
single node. And the data is in order. So 14, 22, 36, 41, 48, 55, 65. So this is a valid binary search
Now to determine if it’s a valid AVL tree, we just have to compute balance factors.
So in my previous videos, we talked about doing this.
So I’m just going to try to burn through this quickly.
I want to put zeros on all the leaves because that’s pretty easy.
This 48 gets a 1.
The 65 actually gets a 2.
So at this point already, we know there’s something wrong with our tree.
We need to rotate it.
This is not a valid AVL tree because we see a balance factor that is worse than, you know,
plus or minus 1.
61 is going to have a balance factor of, let’s see, just a 1.
And so really the only thing that we need to rotate is going to be this 65 node.
This is the node that’s out of whack.
Sometimes you see, you know, the bad balance factor in other areas of the tree,
maybe like a little bit higher and also a little bit lower.
You want to rotate as low as possible first because sometimes when you rotate low,
it fixes stuff that’s higher.
This is actually only true if the thing that you’re rotating is a direct descendant,
Not necessarily a direct child, but just a descendant of the thing that you see that is higher if you have two different nodes that are
unrelated in terms of
You know, they’re not direct descendants or ancestors of one another. They’re sort of like siblings or cousins
Then it doesn’t really matter so much which one you rotate first because they’re not really going to affect each other
but
In this case if we rotate, you know starting with the 65 there’s a chance that the 41 will get fixed up a little bit
I haven’t tried this yet, so I don’t know for sure, but I have a feeling it might.
Let’s see what happens.
So we know that the first node that is out of balance we’ll call the Z node.
So I’m just going to put a Z here.
Let me just duplicate this and we’ll say Z.
Hang on a second.
I got some dashes in my pen.
There we go.
We’ll make this the Z node.
And then we have to find X and Y.
We’ll do that backwards.
We’ll find the Y first and then the X.
them by going down one level at a time so the Y node will be a child of Z and
we’ll have to find the child that has the taller subtree in this case there’s
no choice because there’s no subtree on the right side of 65 so we can only go
left but just keep in mind if there was a subtree let’s say like that then we
would still go to the left and select that 48 as the Y node let me just write
down Y node here because it would have a taller subtree notice the left subtree
in this little example has a height of 2 and the right subtree has a height of 1
That subtree has a height of one.
So let’s just get rid of that little extra thing.
Same thing for the X.
We want to find a child of Y and we’ll always take the taller subtree.
So you know, if there was a subtree over here and maybe like another like node down there,
then we would still go to the right to get the 55 because it would have the taller subtree.
In this case, there’s only one choice.
So the X node is just going to be this 55 and that’s it.
So let me just put an X right here.
are x and y and z i’m just going to write that down real fast x equals 55 i’m going to probably
change the color here y is equal to 48 uh z is equal to 65 um and then we want to make an in
order representation of xyz and just call it abc so i’m going to say a is equal to something
and so is c so a is going to be the least value the value that belongs on the left because again
these are supposed to have ordered data like binary search trees so i’m going to put 48 here
and 55 there and then 65 here now abc is the in order representation of xyz now that we’ve done
that i’m going to draw our target pattern here so let’s see i’m going to do i’m going to do a
to connect with some blue lines because that’s just like such a cool connection.
Hang on a second.
I put that a little bit too far down.
Okay.
So we’ll do that.
And then I’m going to do another connected in line.
Okay.
I had to make a little edit there.
So I’m going to just continue making this blue line.
So I’m going to do this.
And I have to update the values now.
the values now. So this 14 on the left, that should be the A node. So we’re going to see 48,
and then the new parent is going to be 55, and then the right child is going to be 65.
Always after you fill in these values, ask yourself, have I actually drawn a valid binary
search tree? If you haven’t, meaning if the data is out of order in some way, then you haven’t
drawn a valid binary search tree, and you probably need to try again. So we’ve got 48 and 55 and 60.
is are there any children that are unaccounted for in terms of the input nodes to the pattern
that we’ve just drawn down here, the output pattern.
So let me just kind of remind you that there could have been, you know, potentially some
nodes, right?
Like we could have had a right child of 65, we could have had a left child of 48, and
we could have had two children of 55.
So there’s always a potential for four nodes that are unaccounted for.
We’ll just check them one by one real fast here.
Look at the 48 first. The 48 had a right child of 55, but that’s already taken care of in the output pattern, so we don’t have to worry about that.
Now we look at the 55. We could go XYZ if we wanted to, but I like looking at the output pattern.
55 had no children, so that’s fine. And then we look at the 65. The 65 had a left child of 48, but that’s already in the output pattern.
So at this point, all children are accounted for. We don’t really need to do that much.
that were unaccounted for they would need to be attached under the 48 and 65 somewhere and each
child would only go in one place without an there’s only one place that the child could go
without invalidating the binary search tree but that’ll be i’m going to make a harder example in
the next video so now that we’ve done this we’re ready to reattach all the nodes are accounted for
that we’re about to remove from the diagram we’re not actually going to be deleting nodes or creating
creating nodes or anything like that in the code we’re really just disconnecting
pointers and reattaching them so I’m gonna just erase this from the diagram
but keeping in mind that the nodes are not really being recreated and I’ll just
put the 55 over here where the old trinode subtree was and then I’m gonna
recompute the balance factors for all of the nodes involved so you know I just
the output pattern someone put a 0 48 also 0 because their leaves the 55 it’s a 0 it’s perfectly
balanced that’s pretty sweet and now that we’re done with the nodes that we touched we have to
work our way up to the root node we don’t have to worry about anything over here on the left
because those nodes couldn’t possibly be affected by uh by the rotation we just did because only
nodes that are above the rotation will be affected so uh the last one we you know looked at was this
basically going to say all right 55 let’s look up one more parent it’s going to be the root node
the 41 so what’s the new balance factor of the 41 node it’s actually going to be better it’s going
to be a zero so with that in mind we’ve actually created a perfectly balanced binary search tree
this is definitely a log time tree super super fast and super super cool and what type of rotation
So we had XYZ. If you don’t have the rotations memorized, keep in mind my previous videos explain all of that with some fun ways to help you remember, hopefully. Maybe some unfun ways.
So I’ll just kind of like reiterate real fast here. There are two nodes that need to be moved into position. Notice how 55 is supposed to be the new parent node, which means the 65 needs to be rotated under the 55, and so does the 48.
And so does the 48. So the 48 and the 65 both are going to be rotated into position.
That means two nodes are rotated, which means it’s a double rotation, not a single rotation.
Which node was rotated first? I always rotate the Z node first, you know, the node on the very top.
And so I’ll just use the rotation direction of that Z node.
So that’s a regular clockwise rotation.
And that’s a right rotation, so this is a double right rotation.
you can do to remember is if you go from y to z you’re going up into the right so that’s a right
rotation you could also imagine that there is some negative space kind of sitting here on the right
side a little right pocket so that’s a right rotation and i’m sure there were some other ways
that i added in the other video but i can no longer remember them so i’ll just move on we’ll
say we did a double right rotation in order to get this uh this perfectly balanced binary search tree
research tree so I’m gonna cut the video here in the next video we’re going to I
think probably rotate a linear tree you can kind of see I’ve already started to
draw one here in the next video we’re gonna look at this like giant gnarly
linear tree and we’re gonna pretend that we have the ABL rotating disabled for a
while while we built this tree and then all at once we’re gonna turn it on and
do like a whole bunch of rotations and see what the tree is gonna end up
looking like it’s gonna be pretty ugly and fun anyway thank you so much for
Anyway, thank you so much for watching this video.
I hope you learned a little bit of stuff and you had a little bit of fun.
I’ll see you in a moment, hopefully.
Hey everybody, thanks for watching this video again from the bottom of my heart.
I really appreciate it.
I do hope you did learn something and have some fun.
If you could do me a please, a small little favor, could you please subscribe and follow
as you do on the current social media website that you’re looking at right now.
It would really mean the world to me and it’ll help make more videos and grow this community.
So we’ll be able to do more videos, longer videos, better videos, or just I’ll be able
to keep making videos in general.
So please do me a kindness and subscribe.
You know, sometimes I’m sleeping in the middle of the night and I just wake up because I
know somebody subscribed or followed.
It just wakes me up and I get filled with joy.
That’s exactly what happens every single time.
So you could do it as a nice favor to me or you could you could troll me if you want to just wake me up in the middle
And I just subscribe and then I’ll just wake up. I promise that’s what will happen
Also, uh, if you look at the middle of the screen right now
You should see a QR code which you can scan in order to go to the website
Which I think is also named somewhere at the bottom of this video
And it’ll take you to my main website where you can just kind of like see
All the videos I published and the services and tutorials and things that I offer and all that good stuff and
and uh if you have a suggestion for uh uh clarifications or errata or just future videos
that you want to see please leave a comment or if you just want to say hey what’s up what’s going on
you know just send me a comment whatever i also wake up for those in the middle of the night i
get i wake up in a cold sweat and i’m like it would really it really mean the world to me i
would really appreciate it so again thank you so much for watching this video and um enjoy the cool
Enjoy the cool music as I fade into the darkness which is coming for us all.
Thank you.
Hello there.
Hi there.
Hi there. Let’s maintain a self-balancing AVL tree,
which is recursive. Hello there. Let’s,
