<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AVL tree rotation Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/avl-tree-rotation/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/avl-tree-rotation/</link>
	<description></description>
	<lastBuildDate>Mon, 08 Jun 2026 03:26:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>AVL tree rotation Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/avl-tree-rotation/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Perform AVL Tree Rotations &#8211; Step By Step Example</title>
		<link>https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/</link>
					<comments>https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 08 Jun 2026 03:26:17 +0000</pubDate>
				<category><![CDATA[AVL Trees]]></category>
		<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[AVL tree]]></category>
		<category><![CDATA[avl tree example]]></category>
		<category><![CDATA[AVL tree rotation]]></category>
		<category><![CDATA[balance factor]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[double rotation]]></category>
		<category><![CDATA[self balancing tree]]></category>
		<category><![CDATA[tree rotation]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=387</guid>

					<description><![CDATA[<p>Learn how to rotate an AVL tree in this detailed example. We identify imbalance using balance factors, locate X Y Z nodes, and apply a double right rotation to restore the self-balancing property of the binary search tree.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/">How to Perform AVL Tree Rotations &#8211; Step By Step Example</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="How to Perform AVL Tree Rotations - Step By Step Example" width="1380" height="776" src="https://www.youtube.com/embed/StHS7CQa6BQ?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">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&#8217;ve seen the basics of binary search trees and AVL trees.</p>



<p class="wp-block-paragraph">Watch as we turn an invalid AVL tree into a perfectly balanced one with clear step-by-step instructions.</p>



<p class="wp-block-paragraph">If you&#8217;re learning data structures and algorithms, this practical example will help you understand when and how to rotate.</p>



<p class="wp-block-paragraph">00:00 Introduction to AVL Tree Rotation<br>00:14 Prerequisites and Previous Videos<br>00:36 AVL Trees Overview<br>00:40 Types of Rotations and Balance Factors<br>01:01 Examining the Example Tree<br>01:24 Confirming Binary Search Tree Properties<br>01:27 Computing Balance Factors<br>01:39 Identifying Imbalance at Node 65<br>02:01 Locating the Z Node<br>02:54 Finding Y and X Nodes<br>04:08 Assigning X Y Z Values<br>04:33 Creating ABC In-Order Representation<br>04:58 Drawing the Target Rotation Pattern<br>05:34 Updating Node Values in Pattern<br>06:04 Checking Unaccounted Children<br>07:00 Reattaching Nodes and Performing Rotation<br>07:56 Recomputing Balance Factors<br>08:24 Updating Root Balance Factor<br>08:41 Resulting Perfectly Balanced Tree<br>08:48 Identifying Double Right Rotation<br>09:06 Explaining the Rotation Process<br>10:16 Conclusion and Next Video Teaser<br>11:56 Channel Promotion and Outro<br>15:48 Final Hello and Recap</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p class="wp-block-paragraph">Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to the Blog: https://www.NeuralLantern.com</li>



<li>Watch the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hey there! Let&#8217;s maintain and rotate an AVL tree which is a self-balancing binary search tree.</p>



<p class="wp-block-paragraph">Okay. Alright, so hopefully by now you&#8217;ve seen my other videos. If you haven&#8217;t,</p>



<p class="wp-block-paragraph">you probably won&#8217;t understand this unless you&#8217;ve seen other videos elsewhere.</p>



<p class="wp-block-paragraph">But in my previous videos we talked about what is a binary search tree, how to make one, how to</p>



<p class="wp-block-paragraph">binary search tree, how to make one, how to define one, how to know you&#8217;re looking at one, how to build one, search,</p>



<p class="wp-block-paragraph">insert, remove all the operations and time complexities.</p>



<p class="wp-block-paragraph">And then we talked about AVL trees, which are a self-balancing binary search tree.</p>



<p class="wp-block-paragraph">And we talked about what are the different types of rotations you can do? Why would you rotate?</p>



<p class="wp-block-paragraph">How do you compute balance factors? How do you know when it&#8217;s time to rotate?</p>



<p class="wp-block-paragraph">How do you do the rotation? Stuff like that. So this is just going to be one</p>



<p class="wp-block-paragraph">example tree, and we&#8217;re just going to try to burn through it. If you want to learn more about</p>



<p class="wp-block-paragraph">see my previous videos. Okay, so supposing now that you have a tree that looks like this, let&#8217;s</p>



<p class="wp-block-paragraph">confirm that this is a valid AVL tree. This seems to be a tree. It&#8217;s a connected graph. It&#8217;s got no</p>



<p class="wp-block-paragraph">cycles. It&#8217;s a rooted tree. It&#8217;s a binary tree. There are no more than two children per every</p>



<p class="wp-block-paragraph">single node. And the data is in order. So 14, 22, 36, 41, 48, 55, 65. So this is a valid binary search</p>



<p class="wp-block-paragraph">Now to determine if it&#8217;s a valid AVL tree, we just have to compute balance factors.</p>



<p class="wp-block-paragraph">So in my previous videos, we talked about doing this.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to try to burn through this quickly.</p>



<p class="wp-block-paragraph">I want to put zeros on all the leaves because that&#8217;s pretty easy.</p>



<p class="wp-block-paragraph">This 48 gets a 1.</p>



<p class="wp-block-paragraph">The 65 actually gets a 2.</p>



<p class="wp-block-paragraph">So at this point already, we know there&#8217;s something wrong with our tree.</p>



<p class="wp-block-paragraph">We need to rotate it.</p>



<p class="wp-block-paragraph">This is not a valid AVL tree because we see a balance factor that is worse than, you know,</p>



<p class="wp-block-paragraph">plus or minus 1.</p>



<p class="wp-block-paragraph">61 is going to have a balance factor of, let&#8217;s see, just a 1.</p>



<p class="wp-block-paragraph">And so really the only thing that we need to rotate is going to be this 65 node.</p>



<p class="wp-block-paragraph">This is the node that&#8217;s out of whack.</p>



<p class="wp-block-paragraph">Sometimes you see, you know, the bad balance factor in other areas of the tree,</p>



<p class="wp-block-paragraph">maybe like a little bit higher and also a little bit lower.</p>



<p class="wp-block-paragraph">You want to rotate as low as possible first because sometimes when you rotate low,</p>



<p class="wp-block-paragraph">it fixes stuff that&#8217;s higher.</p>



<p class="wp-block-paragraph">This is actually only true if the thing that you&#8217;re rotating is a direct descendant,</p>



<p class="wp-block-paragraph">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</p>



<p class="wp-block-paragraph">unrelated in terms of</p>



<p class="wp-block-paragraph">You know, they&#8217;re not direct descendants or ancestors of one another. They&#8217;re sort of like siblings or cousins</p>



<p class="wp-block-paragraph">Then it doesn&#8217;t really matter so much which one you rotate first because they&#8217;re not really going to affect each other</p>



<p class="wp-block-paragraph">but</p>



<p class="wp-block-paragraph">In this case if we rotate, you know starting with the 65 there&#8217;s a chance that the 41 will get fixed up a little bit</p>



<p class="wp-block-paragraph">I haven&#8217;t tried this yet, so I don&#8217;t know for sure, but I have a feeling it might.</p>



<p class="wp-block-paragraph">Let&#8217;s see what happens.</p>



<p class="wp-block-paragraph">So we know that the first node that is out of balance we&#8217;ll call the Z node.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to put a Z here.</p>



<p class="wp-block-paragraph">Let me just duplicate this and we&#8217;ll say Z.</p>



<p class="wp-block-paragraph">Hang on a second.</p>



<p class="wp-block-paragraph">I got some dashes in my pen.</p>



<p class="wp-block-paragraph">There we go.</p>



<p class="wp-block-paragraph">We&#8217;ll make this the Z node.</p>



<p class="wp-block-paragraph">And then we have to find X and Y.</p>



<p class="wp-block-paragraph">We&#8217;ll do that backwards.</p>



<p class="wp-block-paragraph">We&#8217;ll find the Y first and then the X.</p>



<p class="wp-block-paragraph">them by going down one level at a time so the Y node will be a child of Z and</p>



<p class="wp-block-paragraph">we&#8217;ll have to find the child that has the taller subtree in this case there&#8217;s</p>



<p class="wp-block-paragraph">no choice because there&#8217;s no subtree on the right side of 65 so we can only go</p>



<p class="wp-block-paragraph">left but just keep in mind if there was a subtree let&#8217;s say like that then we</p>



<p class="wp-block-paragraph">would still go to the left and select that 48 as the Y node let me just write</p>



<p class="wp-block-paragraph">down Y node here because it would have a taller subtree notice the left subtree</p>



<p class="wp-block-paragraph">in this little example has a height of 2 and the right subtree has a height of 1</p>



<p class="wp-block-paragraph">That subtree has a height of one.</p>



<p class="wp-block-paragraph">So let&#8217;s just get rid of that little extra thing.</p>



<p class="wp-block-paragraph">Same thing for the X.</p>



<p class="wp-block-paragraph">We want to find a child of Y and we&#8217;ll always take the taller subtree.</p>



<p class="wp-block-paragraph">So you know, if there was a subtree over here and maybe like another like node down there,</p>



<p class="wp-block-paragraph">then we would still go to the right to get the 55 because it would have the taller subtree.</p>



<p class="wp-block-paragraph">In this case, there&#8217;s only one choice.</p>



<p class="wp-block-paragraph">So the X node is just going to be this 55 and that&#8217;s it.</p>



<p class="wp-block-paragraph">So let me just put an X right here.</p>



<p class="wp-block-paragraph">are x and y and z i&#8217;m just going to write that down real fast x equals 55 i&#8217;m going to probably</p>



<p class="wp-block-paragraph">change the color here y is equal to 48 uh z is equal to 65 um and then we want to make an in</p>



<p class="wp-block-paragraph">order representation of xyz and just call it abc so i&#8217;m going to say a is equal to something</p>



<p class="wp-block-paragraph">and so is c so a is going to be the least value the value that belongs on the left because again</p>



<p class="wp-block-paragraph">these are supposed to have ordered data like binary search trees so i&#8217;m going to put 48 here</p>



<p class="wp-block-paragraph">and 55 there and then 65 here now abc is the in order representation of xyz now that we&#8217;ve done</p>



<p class="wp-block-paragraph">that i&#8217;m going to draw our target pattern here so let&#8217;s see i&#8217;m going to do i&#8217;m going to do a</p>



<p class="wp-block-paragraph">to connect with some blue lines because that&#8217;s just like such a cool connection.</p>



<p class="wp-block-paragraph">Hang on a second.</p>



<p class="wp-block-paragraph">I put that a little bit too far down.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So we&#8217;ll do that.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to do another connected in line.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">I had to make a little edit there.</p>



<p class="wp-block-paragraph">So I&#8217;m going to just continue making this blue line.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do this.</p>



<p class="wp-block-paragraph">And I have to update the values now.</p>



<p class="wp-block-paragraph">the values now. So this 14 on the left, that should be the A node. So we&#8217;re going to see 48,</p>



<p class="wp-block-paragraph">and then the new parent is going to be 55, and then the right child is going to be 65.</p>



<p class="wp-block-paragraph">Always after you fill in these values, ask yourself, have I actually drawn a valid binary</p>



<p class="wp-block-paragraph">search tree? If you haven&#8217;t, meaning if the data is out of order in some way, then you haven&#8217;t</p>



<p class="wp-block-paragraph">drawn a valid binary search tree, and you probably need to try again. So we&#8217;ve got 48 and 55 and 60.</p>



<p class="wp-block-paragraph">is are there any children that are unaccounted for in terms of the input nodes to the pattern</p>



<p class="wp-block-paragraph">that we&#8217;ve just drawn down here, the output pattern.</p>



<p class="wp-block-paragraph">So let me just kind of remind you that there could have been, you know, potentially some</p>



<p class="wp-block-paragraph">nodes, right?</p>



<p class="wp-block-paragraph">Like we could have had a right child of 65, we could have had a left child of 48, and</p>



<p class="wp-block-paragraph">we could have had two children of 55.</p>



<p class="wp-block-paragraph">So there&#8217;s always a potential for four nodes that are unaccounted for.</p>



<p class="wp-block-paragraph">We&#8217;ll just check them one by one real fast here.</p>



<p class="wp-block-paragraph">Look at the 48 first. The 48 had a right child of 55, but that&#8217;s already taken care of in the output pattern, so we don&#8217;t have to worry about that.</p>



<p class="wp-block-paragraph">Now we look at the 55. We could go XYZ if we wanted to, but I like looking at the output pattern.</p>



<p class="wp-block-paragraph">55 had no children, so that&#8217;s fine. And then we look at the 65. The 65 had a left child of 48, but that&#8217;s already in the output pattern.</p>



<p class="wp-block-paragraph">So at this point, all children are accounted for. We don&#8217;t really need to do that much.</p>



<p class="wp-block-paragraph">that were unaccounted for they would need to be attached under the 48 and 65 somewhere and each</p>



<p class="wp-block-paragraph">child would only go in one place without an there&#8217;s only one place that the child could go</p>



<p class="wp-block-paragraph">without invalidating the binary search tree but that&#8217;ll be i&#8217;m going to make a harder example in</p>



<p class="wp-block-paragraph">the next video so now that we&#8217;ve done this we&#8217;re ready to reattach all the nodes are accounted for</p>



<p class="wp-block-paragraph">that we&#8217;re about to remove from the diagram we&#8217;re not actually going to be deleting nodes or creating</p>



<p class="wp-block-paragraph">creating nodes or anything like that in the code we&#8217;re really just disconnecting</p>



<p class="wp-block-paragraph">pointers and reattaching them so I&#8217;m gonna just erase this from the diagram</p>



<p class="wp-block-paragraph">but keeping in mind that the nodes are not really being recreated and I&#8217;ll just</p>



<p class="wp-block-paragraph">put the 55 over here where the old trinode subtree was and then I&#8217;m gonna</p>



<p class="wp-block-paragraph">recompute the balance factors for all of the nodes involved so you know I just</p>



<p class="wp-block-paragraph">the output pattern someone put a 0 48 also 0 because their leaves the 55 it&#8217;s a 0 it&#8217;s perfectly</p>



<p class="wp-block-paragraph">balanced that&#8217;s pretty sweet and now that we&#8217;re done with the nodes that we touched we have to</p>



<p class="wp-block-paragraph">work our way up to the root node we don&#8217;t have to worry about anything over here on the left</p>



<p class="wp-block-paragraph">because those nodes couldn&#8217;t possibly be affected by uh by the rotation we just did because only</p>



<p class="wp-block-paragraph">nodes that are above the rotation will be affected so uh the last one we you know looked at was this</p>



<p class="wp-block-paragraph">basically going to say all right 55 let&#8217;s look up one more parent it&#8217;s going to be the root node</p>



<p class="wp-block-paragraph">the 41 so what&#8217;s the new balance factor of the 41 node it&#8217;s actually going to be better it&#8217;s going</p>



<p class="wp-block-paragraph">to be a zero so with that in mind we&#8217;ve actually created a perfectly balanced binary search tree</p>



<p class="wp-block-paragraph">this is definitely a log time tree super super fast and super super cool and what type of rotation</p>



<p class="wp-block-paragraph">So we had XYZ. If you don&#8217;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.</p>



<p class="wp-block-paragraph">So I&#8217;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.</p>



<p class="wp-block-paragraph">And so does the 48. So the 48 and the 65 both are going to be rotated into position.</p>



<p class="wp-block-paragraph">That means two nodes are rotated, which means it&#8217;s a double rotation, not a single rotation.</p>



<p class="wp-block-paragraph">Which node was rotated first? I always rotate the Z node first, you know, the node on the very top.</p>



<p class="wp-block-paragraph">And so I&#8217;ll just use the rotation direction of that Z node.</p>



<p class="wp-block-paragraph">So that&#8217;s a regular clockwise rotation.</p>



<p class="wp-block-paragraph">And that&#8217;s a right rotation, so this is a double right rotation.</p>



<p class="wp-block-paragraph">you can do to remember is if you go from y to z you&#8217;re going up into the right so that&#8217;s a right</p>



<p class="wp-block-paragraph">rotation you could also imagine that there is some negative space kind of sitting here on the right</p>



<p class="wp-block-paragraph">side a little right pocket so that&#8217;s a right rotation and i&#8217;m sure there were some other ways</p>



<p class="wp-block-paragraph">that i added in the other video but i can no longer remember them so i&#8217;ll just move on we&#8217;ll</p>



<p class="wp-block-paragraph">say we did a double right rotation in order to get this uh this perfectly balanced binary search tree</p>



<p class="wp-block-paragraph">research tree so I&#8217;m gonna cut the video here in the next video we&#8217;re going to I</p>



<p class="wp-block-paragraph">think probably rotate a linear tree you can kind of see I&#8217;ve already started to</p>



<p class="wp-block-paragraph">draw one here in the next video we&#8217;re gonna look at this like giant gnarly</p>



<p class="wp-block-paragraph">linear tree and we&#8217;re gonna pretend that we have the ABL rotating disabled for a</p>



<p class="wp-block-paragraph">while while we built this tree and then all at once we&#8217;re gonna turn it on and</p>



<p class="wp-block-paragraph">do like a whole bunch of rotations and see what the tree is gonna end up</p>



<p class="wp-block-paragraph">looking like it&#8217;s gonna be pretty ugly and fun anyway thank you so much for</p>



<p class="wp-block-paragraph">Anyway, thank you so much for watching this video.</p>



<p class="wp-block-paragraph">I hope you learned a little bit of stuff and you had a little bit of fun.</p>



<p class="wp-block-paragraph">I&#8217;ll see you in a moment, hopefully.</p>



<p class="wp-block-paragraph">Hey everybody, thanks for watching this video again from the bottom of my heart.</p>



<p class="wp-block-paragraph">I really appreciate it.</p>



<p class="wp-block-paragraph">I do hope you did learn something and have some fun.</p>



<p class="wp-block-paragraph">If you could do me a please, a small little favor, could you please subscribe and follow</p>



<p class="wp-block-paragraph">as you do on the current social media website that you&#8217;re looking at right now.</p>



<p class="wp-block-paragraph">It would really mean the world to me and it&#8217;ll help make more videos and grow this community.</p>



<p class="wp-block-paragraph">So we&#8217;ll be able to do more videos, longer videos, better videos, or just I&#8217;ll be able</p>



<p class="wp-block-paragraph">to keep making videos in general.</p>



<p class="wp-block-paragraph">So please do me a kindness and subscribe.</p>



<p class="wp-block-paragraph">You know, sometimes I&#8217;m sleeping in the middle of the night and I just wake up because I</p>



<p class="wp-block-paragraph">know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy.</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">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</p>



<p class="wp-block-paragraph">And I just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen</p>



<p class="wp-block-paragraph">Also, uh, if you look at the middle of the screen right now</p>



<p class="wp-block-paragraph">You should see a QR code which you can scan in order to go to the website</p>



<p class="wp-block-paragraph">Which I think is also named somewhere at the bottom of this video</p>



<p class="wp-block-paragraph">And it&#8217;ll take you to my main website where you can just kind of like see</p>



<p class="wp-block-paragraph">All the videos I published and the services and tutorials and things that I offer and all that good stuff and</p>



<p class="wp-block-paragraph">and uh if you have a suggestion for uh uh clarifications or errata or just future videos</p>



<p class="wp-block-paragraph">that you want to see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on</p>



<p class="wp-block-paragraph">you know just send me a comment whatever i also wake up for those in the middle of the night i</p>



<p class="wp-block-paragraph">get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to me i</p>



<p class="wp-block-paragraph">would really appreciate it so again thank you so much for watching this video and um enjoy the cool</p>



<p class="wp-block-paragraph">Enjoy the cool music as I fade into the darkness which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>



<p class="wp-block-paragraph">Hello there.</p>



<p class="wp-block-paragraph">Hi there.</p>



<p class="wp-block-paragraph">Hi there. Let&#8217;s maintain a self-balancing AVL tree,</p>



<p class="wp-block-paragraph">which is recursive. Hello there. Let&#8217;s,</p>
<p>The post <a href="https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/">How to Perform AVL Tree Rotations &#8211; Step By Step Example</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AVL Tree Rotations Tutorial: Fixing Imbalance After Adding a Node</title>
		<link>https://www.NeuralLantern.com/avl-tree-rotations-tutorial-fixing-imbalance-after-adding-a-node/</link>
					<comments>https://www.NeuralLantern.com/avl-tree-rotations-tutorial-fixing-imbalance-after-adding-a-node/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 01 Jun 2026 05:46:56 +0000</pubDate>
				<category><![CDATA[AVL Trees]]></category>
		<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[avl insertion]]></category>
		<category><![CDATA[avl rotations]]></category>
		<category><![CDATA[AVL tree]]></category>
		<category><![CDATA[avl tree example]]></category>
		<category><![CDATA[AVL tree rotation]]></category>
		<category><![CDATA[balance factor]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[coding interview]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures tutorial]]></category>
		<category><![CDATA[double right rotation]]></category>
		<category><![CDATA[self balancing tree]]></category>
		<category><![CDATA[tree rotation]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=383</guid>

					<description><![CDATA[<p>Learn how to maintain balance in AVL trees through rotations. This tutorial shows inserting a new node into an AVL tree, recalculating balance factors up the path to the root, identifying the first imbalanced node, selecting X Y Z nodes based on subtree heights, and executing a double right rotation to restore the AVL property while preserving binary search tree ordering.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-rotations-tutorial-fixing-imbalance-after-adding-a-node/">AVL Tree Rotations Tutorial: Fixing Imbalance After Adding a Node</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="AVL Tree Rotations Tutorial: Fixing Imbalance After Adding a Node" width="1380" height="776" src="https://www.youtube.com/embed/ekoXDRjGHQ4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">In this video we walk through a complete example of maintaining an AVL tree. Starting with a valid AVL tree, we insert a new node that breaks the balance rules, then recompute balance factors up the tree until we find the imbalance.</p>



<p class="wp-block-paragraph">Watch as we identify the X, Y, and Z nodes and perform a double right rotation to restore the AVL property while keeping it a valid binary search tree.</p>



<p class="wp-block-paragraph">Perfect for students learning data structures, computer science fundamentals, or anyone preparing for coding interviews.</p>



<p class="wp-block-paragraph">Previous videos cover binary search trees, AVL basics, rotations, and balance factors.</p>



<p class="wp-block-paragraph">00:00 Intro to AVL Tree Rotation<br>00:20 Prerequisites and Previous Videos<br>01:06 Initial AVL Tree Example<br>01:18 Computing Balance Factors<br>03:44 Adding Node 54<br>04:31 Recomputing Balance Factors<br>06:08 Detecting Imbalance at Node 78<br>07:20 Identifying X Y Z Nodes<br>08:36 Labeling X Y Z and Trinode Pattern<br>10:09 Output Pattern and BST Ordering<br>11:03 Handling Extra Children Nodes<br>14:50 Reattaching Rotated Subtree<br>15:24 Recalculating Balance Factors<br>17:05 Double Right Rotation Explained<br>19:18 Final Verification and Outro</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p class="wp-block-paragraph">Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to the Blog: https://www.NeuralLantern.com</li>



<li>Watch the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hello there! Let&#8217;s maintain an AVL tree by rotating some nodes after we add a new node that messes up the whole tree.</p>



<p class="wp-block-paragraph">Okay, I hope that before you watch this video you&#8217;ve seen my other videos where we talk about what is a binary search tree, how to define it, terminology, how to build a tree, search through the tree, all the stuff for the tree.</p>



<p class="wp-block-paragraph">the tree all the stuff for the tree the binary search tree and then my other videos where we</p>



<p class="wp-block-paragraph">talked about what is an avl tree what is a rotation what are the different types of rotations why would</p>



<p class="wp-block-paragraph">you want to rotate how do you do the balance factors all that stuff happens in previous videos</p>



<p class="wp-block-paragraph">so uh search the video history anyway so for now i&#8217;m going to assume that you know all that because</p>



<p class="wp-block-paragraph">you watch the videos and now we&#8217;re looking at an a we&#8217;re looking at an avl tree maybe first we&#8217;re</p>



<p class="wp-block-paragraph">a valid AVL tree and then we will add a new node which will probably mess the whole thing up</p>



<p class="wp-block-paragraph">and force us to do a rotation to get the tree back in balance.</p>



<p class="wp-block-paragraph">Okay so the first thing I&#8217;m going to do is just this is an example tree, it&#8217;s got some nodes</p>



<p class="wp-block-paragraph">and I&#8217;m going to compute the balance factors for every single node just to double check.</p>



<p class="wp-block-paragraph">Remember the rule that we&#8217;re using is if an AVL tree has a balance factor of two</p>



<p class="wp-block-paragraph">for any node, then it&#8217;s considered invalid.</p>



<p class="wp-block-paragraph">The tree itself is just a binary search tree,</p>



<p class="wp-block-paragraph">not an AVL tree, until we fix the imbalance.</p>



<p class="wp-block-paragraph">So the 44 node, wait, hang on, that&#8217;s actually wrong.</p>



<p class="wp-block-paragraph">I don&#8217;t know why I even typed that number there.</p>



<p class="wp-block-paragraph">Oh, because I was saying two.</p>



<p class="wp-block-paragraph">First thing I want to do is do the balance factors</p>



<p class="wp-block-paragraph">for the leaves, because they&#8217;re the easiest.</p>



<p class="wp-block-paragraph">All the leaves have a balance factor of zero,</p>



<p class="wp-block-paragraph">because they have no difference in left subtree</p>



<p class="wp-block-paragraph">versus right subtree height.</p>



<p class="wp-block-paragraph">The 50 node is also kind of easy,</p>



<p class="wp-block-paragraph">because it&#8217;s left subtree and right subtree</p>



<p class="wp-block-paragraph">left subtree and right subtree have the same height so that&#8217;s just a zero 17 is a little bit</p>



<p class="wp-block-paragraph">more tricky it&#8217;s got one node hanging off the right side so its right subtree height is one</p>



<p class="wp-block-paragraph">its left subtree height is zero so absolute value of zero minus one is just going to be one</p>



<p class="wp-block-paragraph">so that&#8217;s not perfect but at the same time that&#8217;s acceptable we can just sort of move on remember</p>



<p class="wp-block-paragraph">only two or worse is going to make us stop we look at the 78 again let me just uh or for the first</p>



<p class="wp-block-paragraph">or for the first time we look at the 78 let me just do this the hard way to make sure everybody&#8217;s</p>



<p class="wp-block-paragraph">on the same page uh for the for the balance factor of the 78 node we look at the height of the left</p>



<p class="wp-block-paragraph">subtree uh which is two and then we look at the height of the right subtree which is one</p>



<p class="wp-block-paragraph">and so if you take the absolute value of two minus one that&#8217;s going to be one</p>



<p class="wp-block-paragraph">so actually the balance factor of the 78 node is okay this is maybe a good time to point out the</p>



<p class="wp-block-paragraph">We don&#8217;t care that there are more nodes on the left. We just care about the height of the left subtree</p>



<p class="wp-block-paragraph">So I&#8217;m going to duplicate this slide to continue so far so good every node seems to be okay</p>



<p class="wp-block-paragraph">We&#8217;ll look at the 44 node. You can probably eyeball it because</p>



<p class="wp-block-paragraph">Well, I mean for me when I want to eyeball a balance factors</p>



<p class="wp-block-paragraph">I just look at the left subtree versus the right subtree notice how there&#8217;s like one level of difference</p>



<p class="wp-block-paragraph">in height we could do it the hard way if we wanted to and</p>



<p class="wp-block-paragraph">do it the hard way if we wanted to um so like the left subtree has a height of two right subtree has</p>



<p class="wp-block-paragraph">a height of three so two minus three absolute value that&#8217;s going to be one or just that one</p>



<p class="wp-block-paragraph">line that i drew before which was the the difference so that means this whole tree is</p>



<p class="wp-block-paragraph">actually okay it is a valid avl tree nice video over i&#8217;m just kidding uh let&#8217;s add a node that</p>



<p class="wp-block-paragraph">So I&#8217;m going to add the number 54.</p>



<p class="wp-block-paragraph">Let me just try to select an existing node here, duplicate it.</p>



<p class="wp-block-paragraph">I&#8217;ll change this to a 54.</p>



<p class="wp-block-paragraph">Then we&#8217;ll figure out where the 54 node actually belongs.</p>



<p class="wp-block-paragraph">Hopefully you&#8217;ve already become an expert at binary search trees.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to do it real quick.</p>



<p class="wp-block-paragraph">We show up top here, 54 belongs on the right.</p>



<p class="wp-block-paragraph">We look at the 78, it belongs on the left.</p>



<p class="wp-block-paragraph">We look at the 50, it belongs on the right.</p>



<p class="wp-block-paragraph">We look at the 62, it belongs on the left.</p>



<p class="wp-block-paragraph">right there as the left child of the 62 node. So I&#8217;m going to go ahead and do my little connecting</p>



<p class="wp-block-paragraph">line indicating that the nodes are now pointing to each other. The 54 thinks its parent is 62.</p>



<p class="wp-block-paragraph">The 62 thinks its left child is 54. And now we have to recompute the balance factors.</p>



<p class="wp-block-paragraph">So obviously the node we just added, it doesn&#8217;t have a balance factor, so we have to recompute</p>



<p class="wp-block-paragraph">way up the tree until we hit the root node, recomputing all balance factors along the way.</p>



<p class="wp-block-paragraph">We don&#8217;t actually, I&#8217;m going to do marks here to make sure that I don&#8217;t forget.</p>



<p class="wp-block-paragraph">We don&#8217;t actually have to recompute, you know, the 17 or the 32 or the 88 because those nodes</p>



<p class="wp-block-paragraph">are not possibly going to, or actually even the 48, because those nodes are not possibly going</p>



<p class="wp-block-paragraph">to be affected by the 54 node because balance factors are only affected by nodes that are</p>



<p class="wp-block-paragraph">or even nodes below wouldn&#8217;t be affected. So we&#8217;re only going to compute the 62, the 50,</p>



<p class="wp-block-paragraph">the 78, and the 44, working our way up to the root node. Soon as we hit root, then we&#8217;re done</p>



<p class="wp-block-paragraph">recomputing. So the balance factor for the 62, I&#8217;m going to eyeball that. That&#8217;s going to be a one</p>



<p class="wp-block-paragraph">because left subtree height is one, right subtree height is zero. So far, so good. As soon as we</p>



<p class="wp-block-paragraph">away. Again as I said in a previous video we probably will see that the root</p>



<p class="wp-block-paragraph">nodes balance factor is bad but if we if we compute the whole tree and then just</p>



<p class="wp-block-paragraph">decide I don&#8217;t know like the root node is going to be rotated we&#8217;ll probably</p>



<p class="wp-block-paragraph">rotate incorrectly you want to rotate as low as possible first because rotating</p>



<p class="wp-block-paragraph">lower in a tree is probably going to fix nodes that are higher. So recomputing the</p>



<p class="wp-block-paragraph">balance factor for the 50 that&#8217;s going to be a 1 because left subtree height of</p>



<p class="wp-block-paragraph">right subtree height of 2, take the absolute value of the difference there.</p>



<p class="wp-block-paragraph">We go up to the 78 node and this one is going to be bad because you can see that the left subtree</p>



<p class="wp-block-paragraph">has a height of 3 and the right subtree has a height of 1 so the bounce factor is going to be</p>



<p class="wp-block-paragraph">a 2. It&#8217;s at this point since we just started by inserting the 54 and worked our way up that I</p>



<p class="wp-block-paragraph">would stop and do a rotation but just for the purposes of this tutorial I&#8217;m going to compute</p>



<p class="wp-block-paragraph">just to show you what&#8217;s going on.</p>



<p class="wp-block-paragraph">The height of the left subtree off the root node is two,</p>



<p class="wp-block-paragraph">the height of the right subtree is one, two, three, four.</p>



<p class="wp-block-paragraph">So that means the balance factor of the 44 node</p>



<p class="wp-block-paragraph">also has a two.</p>



<p class="wp-block-paragraph">So at this point, it might be a little confusing</p>



<p class="wp-block-paragraph">if you don&#8217;t remember that you kind of have to stop</p>



<p class="wp-block-paragraph">as you&#8217;re working your way up</p>



<p class="wp-block-paragraph">or at least rotate the lowest possible node first.</p>



<p class="wp-block-paragraph">You might be confused, you know, which one do I rotate?</p>



<p class="wp-block-paragraph">Do I rotate the 78 first or do they rotate the 44?</p>



<p class="wp-block-paragraph">If we were to rotate the 44 first, the root node, you know, we could do it.</p>



<p class="wp-block-paragraph">But at the same time, there&#8217;s a chance that it won&#8217;t actually fix the 78 node.</p>



<p class="wp-block-paragraph">And there will be other stuff underneath that is bad.</p>



<p class="wp-block-paragraph">And so it&#8217;ll cost us more work to do it that way.</p>



<p class="wp-block-paragraph">So instead, we&#8217;re going to stop at the 78 and do our rotation.</p>



<p class="wp-block-paragraph">So remember we said in a previous video that we&#8217;ll choose the first node that is out of whack as the Z node.</p>



<p class="wp-block-paragraph">We&#8217;re looking for X, Y, and Z.</p>



<p class="wp-block-paragraph">I&#8217;m going to write X, Y, and Z here.</p>



<p class="wp-block-paragraph">X, Y, and Z.</p>



<p class="wp-block-paragraph">Now, how do we choose Y?</p>



<p class="wp-block-paragraph">We go down to a child of Z,</p>



<p class="wp-block-paragraph">and it has to be the child that has the taller subtree.</p>



<p class="wp-block-paragraph">Notice how the left subtree of the Z node has a height of 3,</p>



<p class="wp-block-paragraph">and the right subtree has a height of 1,</p>



<p class="wp-block-paragraph">which means we definitely need to go left to find the Y node.</p>



<p class="wp-block-paragraph">So I&#8217;m going to put Y right here.</p>



<p class="wp-block-paragraph">If the left and the right subtree had the same height,</p>



<p class="wp-block-paragraph">then it doesn&#8217;t really matter which one you choose.</p>



<p class="wp-block-paragraph">matter which one you choose. Maybe try to be consistent, but definitely we have to go left</p>



<p class="wp-block-paragraph">this time because that&#8217;s the taller subtree. Again, look how tall it is. Okay, now we need to look for</p>



<p class="wp-block-paragraph">the X node. Again, we have to take the left or the right child of the Y node, or just like a grand</p>



<p class="wp-block-paragraph">child of the Z node. That is definitely a child of the Y node. We have to choose the taller subtree,</p>



<p class="wp-block-paragraph">so we could not put X as 48. We can&#8217;t do that because that&#8217;s not the taller subtree. Instead,</p>



<p class="wp-block-paragraph">Instead, the X is going to be the 62 node because that&#8217;s the taller subtree.</p>



<p class="wp-block-paragraph">Now we have chosen our X, Y, and Z node,</p>



<p class="wp-block-paragraph">which means I&#8217;m going to just type some stuff up here real fast.</p>



<p class="wp-block-paragraph">I&#8217;m going to do X is equal to 62, Y is equal to 50,</p>



<p class="wp-block-paragraph">and Z is equal to 78, just as a reminder.</p>



<p class="wp-block-paragraph">And this is kind of similar to what you would do in the code.</p>



<p class="wp-block-paragraph">I mean, on these diagrams, us humans,</p>



<p class="wp-block-paragraph">we have to like rearrange things and name the types of rotations and so forth in the code it&#8217;s</p>



<p class="wp-block-paragraph">actually really easy uh in comparison because all you have to do is produce an in-order representation</p>



<p class="wp-block-paragraph">of the three node pointers that you received x y and z let&#8217;s say we have a function called rotate</p>



<p class="wp-block-paragraph">we don&#8217;t even have to remember the types of rotations you just grab xyz and then you order</p>



<p class="wp-block-paragraph">them and call them a b and c let&#8217;s do a equals something b equals something c equals something</p>



<p class="wp-block-paragraph">So A is going to be the least value because again, this has to be a binary search tree</p>



<p class="wp-block-paragraph">before it can be an AVL tree.</p>



<p class="wp-block-paragraph">So 50 is going to go on the left.</p>



<p class="wp-block-paragraph">That&#8217;s the only place that it could go.</p>



<p class="wp-block-paragraph">And then 62 in the middle and then 78 on the right side.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to just, maybe I&#8217;ll copy and paste this here.</p>



<p class="wp-block-paragraph">This little trinode subtree.</p>



<p class="wp-block-paragraph">Was that, will that work?</p>



<p class="wp-block-paragraph">Yeah.</p>



<p class="wp-block-paragraph">So I don&#8217;t have to draw this again.</p>



<p class="wp-block-paragraph">I&#8217;m going to erase some of this stuff.</p>



<p class="wp-block-paragraph">Oh, come on, man.</p>



<p class="wp-block-paragraph">I&#8217;m going to erase some of this stuff just to make it a little more neat.</p>



<p class="wp-block-paragraph">just to make it a little more neat.</p>



<p class="wp-block-paragraph">Then I&#8217;m going to replace the values in these nodes with just X, Y, and Z.</p>



<p class="wp-block-paragraph">So, sorry, ABC.</p>



<p class="wp-block-paragraph">So A is going to be 50.</p>



<p class="wp-block-paragraph">That&#8217;s the leftmost node or the least value.</p>



<p class="wp-block-paragraph">And then B is going to be 62.</p>



<p class="wp-block-paragraph">And then C is 78.</p>



<p class="wp-block-paragraph">Before you continue, make sure you&#8217;ve actually drawn a valid binary search tree,</p>



<p class="wp-block-paragraph">because if you haven&#8217;t, then your AVL tree is not going to end up being valid either.</p>



<p class="wp-block-paragraph">not going to end up being valid either it always has to be a valid binary search tree so the order</p>



<p class="wp-block-paragraph">has to be correct so if you were thinking about well how can we can&#8217;t put the 78 in the middle</p>



<p class="wp-block-paragraph">or how come we can&#8217;t put the 62 on the left what would have happened well it would have been invalid</p>



<p class="wp-block-paragraph">or you would have ended up with like a straight line or something that wasn&#8217;t the target pattern</p>



<p class="wp-block-paragraph">for a rotation which is supposed to be a perfectly balanced trinode subtree okay so now we&#8217;ve got x y</p>



<p class="wp-block-paragraph">here which is going to basically take away from these three nodes the next thing we need to do is</p>



<p class="wp-block-paragraph">take into account any nodes that come underneath nodes from the output pattern so our xyz i guess</p>



<p class="wp-block-paragraph">you could say input pattern too let&#8217;s look at uh you know x for example so like the 62. did it have</p>



<p class="wp-block-paragraph">any children that are not accounted for in the output pattern actually yes it has a 54 node so</p>



<p class="wp-block-paragraph">this 54 needs to go somewhere we can&#8217;t just like erase it from the tree so i&#8217;m going to move it</p>



<p class="wp-block-paragraph">to move it down here just so I don&#8217;t forget it. Then we look at the Y node. What children did the</p>



<p class="wp-block-paragraph">Y node have? It had a 48 as a child and a 62 as a child. The 62 is actually already handled in the</p>



<p class="wp-block-paragraph">output pattern so we don&#8217;t need to worry about that but the 48 was not handled so we got to put</p>



<p class="wp-block-paragraph">that somewhere too. So I&#8217;m going to move that down here. Now we look at the Z node. The Z node</p>



<p class="wp-block-paragraph">had 50 as a child, as a left child. That&#8217;s already handled in the output pattern so we don&#8217;t need to</p>



<p class="wp-block-paragraph">don&#8217;t need to worry about that it also had 88 and that is not in the output pattern so we have to</p>



<p class="wp-block-paragraph">do something with 88 so i&#8217;m going to move that down here too i should also point out um i think</p>



<p class="wp-block-paragraph">in a future video i&#8217;m going to do a more complicated example where some of these nodes that are</p>



<p class="wp-block-paragraph">children of the uh the output pattern they might have children of their own or descendants of their</p>



<p class="wp-block-paragraph">own if they do don&#8217;t rearrange any of the descendants so anything that was below 54 just</p>



<p class="wp-block-paragraph">hanging exactly where it was already off of the 54.</p>



<p class="wp-block-paragraph">Same thing for the 48.</p>



<p class="wp-block-paragraph">If it had any children of its own,</p>



<p class="wp-block-paragraph">just leave them in place.</p>



<p class="wp-block-paragraph">Don&#8217;t touch them.</p>



<p class="wp-block-paragraph">Don&#8217;t touch any descendants.</p>



<p class="wp-block-paragraph">So now that we&#8217;ve done this, whoops, dang it.</p>



<p class="wp-block-paragraph">Let me copy this in a good way instead of a bad way.</p>



<p class="wp-block-paragraph">Okay, let me erase this real fast.</p>



<p class="wp-block-paragraph">X, Y, and Z.</p>



<p class="wp-block-paragraph">Nope, that&#8217;s going to screw me up.</p>



<p class="wp-block-paragraph">I&#8217;m going to forget.</p>



<p class="wp-block-paragraph">Okay, now we have to place these nodes</p>



<p class="wp-block-paragraph">nodes of the output pattern in their appropriate position like where do they</p>



<p class="wp-block-paragraph">actually belong well remember this is supposed to be a binary search tree so</p>



<p class="wp-block-paragraph">there&#8217;s actually only one place that the 54 node could ever go and still give us</p>



<p class="wp-block-paragraph">a valid binary search tree so I mean think about it for a second could it go</p>



<p class="wp-block-paragraph">on the left of 50 no could it go on the right of 62 no it has to go in between</p>



<p class="wp-block-paragraph">the 50 and the 62 otherwise we wouldn&#8217;t have a valid binary search tree so I&#8217;m</p>



<p class="wp-block-paragraph">there maybe a little bit lower to make this a more beautiful diagram then I&#8217;m going to do my</p>



<p class="wp-block-paragraph">connecting line real fast so I&#8217;m going to do this so it&#8217;s the right child of the 50 now notice how</p>



<p class="wp-block-paragraph">before it was the left child of the 62 so then you know again we&#8217;re just really disconnecting</p>



<p class="wp-block-paragraph">pointers and then rearranging them so this 48 it can&#8217;t be on the right side of 62 it has to be on</p>



<p class="wp-block-paragraph">the left side it can&#8217;t be on the right side of the 50 it has to be on the left side so the 48 must be</p>



<p class="wp-block-paragraph">side so the 48 must be the left child of the 50. again you must end up with a valid binary search</p>



<p class="wp-block-paragraph">tree or you&#8217;ve done something wrong so i&#8217;m going to just kind of do this here</p>



<p class="wp-block-paragraph">um and then same thing for the 88 node there&#8217;s only one place this could go it can&#8217;t go on the</p>



<p class="wp-block-paragraph">left of 62 it has to go on the right of the 78 it couldn&#8217;t go on the left of the 78 that would be bad</p>



<p class="wp-block-paragraph">news so i&#8217;m just going to place this here and then uh just make a little connecting line okay</p>



<p class="wp-block-paragraph">Okay, now I&#8217;m going to double check to make sure that I didn&#8217;t forget any other nodes</p>



<p class="wp-block-paragraph">because when you&#8217;re working with a diagram, you might forget a couple of nodes and just</p>



<p class="wp-block-paragraph">sort of like erase too many nodes without putting them in the output pattern.</p>



<p class="wp-block-paragraph">If you&#8217;re rearranging pointers, it&#8217;s fine.</p>



<p class="wp-block-paragraph">You wouldn&#8217;t have to worry about any nodes that came underneath these nodes because they</p>



<p class="wp-block-paragraph">would just remain attached in place.</p>



<p class="wp-block-paragraph">But I just want to double check real fast.</p>



<p class="wp-block-paragraph">So we got X, Y, and Z.</p>



<p class="wp-block-paragraph">So that&#8217;s three nodes and then four, five, six.</p>



<p class="wp-block-paragraph">So we&#8217;ve got six nodes total if we include the XYZ nodes and all nodes that are direct children of those and even underneath.</p>



<p class="wp-block-paragraph">So how many do I have in the output pattern?</p>



<p class="wp-block-paragraph">1, 2, 3, 4, 5, 6.</p>



<p class="wp-block-paragraph">So I&#8217;ve got the same number of nodes.</p>



<p class="wp-block-paragraph">I&#8217;m ready to erase all this stuff up top and reattach.</p>



<p class="wp-block-paragraph">1, 2, 3, 4, 5, 6.</p>



<p class="wp-block-paragraph">Yeah, reattach this into the diagram.</p>



<p class="wp-block-paragraph">So I&#8217;m literally just going to erase all this stuff right here.</p>



<p class="wp-block-paragraph">Then I&#8217;m going to select all the nodes in the output pattern.</p>



<p class="wp-block-paragraph">the output pattern and i&#8217;m going to move them up to be the new right child of the 44 because that&#8217;s</p>



<p class="wp-block-paragraph">where originally the z was so i&#8217;m just going to maybe move these up here real fast and then get</p>



<p class="wp-block-paragraph">that 32 back down where it belongs dang you 32 uh actually it&#8217;s a little bit too low get that</p>



<p class="wp-block-paragraph">So the last step is we have to recalculate the balance factors to make sure that we have a valid AVL tree.</p>



<p class="wp-block-paragraph">Maybe we have to do another rotation, maybe not, I don&#8217;t know.</p>



<p class="wp-block-paragraph">Kind of looks like maybe not, but I&#8217;m not going to take any chances.</p>



<p class="wp-block-paragraph">So let&#8217;s do the leaves first.</p>



<p class="wp-block-paragraph">Remember, we have to recompute the balance factors for every node that we just moved.</p>



<p class="wp-block-paragraph">So all the nodes in the output pattern and their immediate children, not anything lower.</p>



<p class="wp-block-paragraph">all the leaves are just going to have a balance factor of zero that&#8217;s pretty easy.</p>



<p class="wp-block-paragraph">The 50 is perfectly balanced nice so it&#8217;s going to have a balance factor of zero.</p>



<p class="wp-block-paragraph">The 78 has one node hanging off the right and nothing on the left so it&#8217;s got a one there.</p>



<p class="wp-block-paragraph">The 62 again we&#8217;re not looking at mass we&#8217;re not looking at weight we&#8217;re looking at the height of</p>



<p class="wp-block-paragraph">the left subtree versus the height of the right subtree so this is actually perfectly balanced</p>



<p class="wp-block-paragraph">because the height of the left subtree is 2 and so is the height of the right subtree.</p>



<p class="wp-block-paragraph">we&#8217;re done with all the nodes that we just moved we should recursively or just kind of like walk</p>



<p class="wp-block-paragraph">our way up the tree and recompute all balance factors until we find the root node. Okay so</p>



<p class="wp-block-paragraph">there&#8217;s the root node only one more thing left to do. The balance factor of the root node is let&#8217;s</p>



<p class="wp-block-paragraph">see left subtree has a height of two right subtree has a height of three so the balance factor for</p>



<p class="wp-block-paragraph">the root node is going to be a number one. Notice how the 44 the root node got fixed because we</p>



<p class="wp-block-paragraph">we rotate it a little bit lower.</p>



<p class="wp-block-paragraph">And this is not the fastest tree in the world,</p>



<p class="wp-block-paragraph">but it is considered a log tree</p>



<p class="wp-block-paragraph">because the imbalance is only scaling</p>



<p class="wp-block-paragraph">with a constant factor because we&#8217;re limiting</p>



<p class="wp-block-paragraph">the balance factors to just the number one basically</p>



<p class="wp-block-paragraph">before we rotate.</p>



<p class="wp-block-paragraph">So what type of rotation did we actually perform?</p>



<p class="wp-block-paragraph">If you looked at my previous video,</p>



<p class="wp-block-paragraph">you should know this already,</p>



<p class="wp-block-paragraph">but maybe I&#8217;ll duplicate this here</p>



<p class="wp-block-paragraph">sort of start to maybe annotate it. I&#8217;m going to do…</p>



<p class="wp-block-paragraph">Okay, so what is the rotation we did? Well, if we look at the ZYX here, the XYZ,</p>



<p class="wp-block-paragraph">there&#8217;s a little bit of a bounce. Actually, let me do orange. There&#8217;s a little bit of a bounce,</p>



<p class="wp-block-paragraph">right? So that&#8217;s going to be a double rotation. Or you can imagine that we had to move two nodes</p>



<p class="wp-block-paragraph">and the 50 on the left and the 78 on the right. Notice how the 78 had to be rotated first.</p>



<p class="wp-block-paragraph">So it&#8217;s like under the 62. And then the 50 was rotated second underneath. I mean,</p>



<p class="wp-block-paragraph">we snipped off that 54 elsewhere. But long story short, there are two nodes that need to be rotated</p>



<p class="wp-block-paragraph">into position for output pattern. So that&#8217;s a double rotation. So diagonal bounce or two nodes</p>



<p class="wp-block-paragraph">means a double rotation and then is it a left or a right rotation well if you</p>



<p class="wp-block-paragraph">think about the first node that we rotated it was the 78 what&#8217;s going on my</p>



<p class="wp-block-paragraph">computer the first node that we rotated was 78 the Z node so what direction did</p>



<p class="wp-block-paragraph">we rotate that that was clockwise so that&#8217;s a right rotation so a double</p>



<p class="wp-block-paragraph">right rotation another way of looking at it is if we only look at the input</p>



<p class="wp-block-paragraph">look at the input pattern we just look at you know these nodes right here there was kind of a</p>



<p class="wp-block-paragraph">pocket of empty space on the right side wasn&#8217;t there so that&#8217;s why it&#8217;s a double right rotation</p>



<p class="wp-block-paragraph">another way to think of it is I&#8217;m running out of colors if we started at the y node and worked our</p>



<p class="wp-block-paragraph">way up to the z node we went up into the right so that&#8217;s a double right rotation also either way you</p>



<p class="wp-block-paragraph">cut it we performed a rotation and now we have a pretty good tree okay let me just double check</p>



<p class="wp-block-paragraph">Okay, let me just double check.</p>



<p class="wp-block-paragraph">This is a valid BST.</p>



<p class="wp-block-paragraph">Doop, doop, doop, doop, doop, doop, doop, doop, doop.</p>



<p class="wp-block-paragraph">And it&#8217;s a valid AVL tree</p>



<p class="wp-block-paragraph">because all the balance factors are okay.</p>



<p class="wp-block-paragraph">Yeah, and in future videos,</p>



<p class="wp-block-paragraph">we&#8217;re going to do more rotations.</p>



<p class="wp-block-paragraph">I&#8217;m probably going to do at least like,</p>



<p class="wp-block-paragraph">you know, two or three more videos</p>



<p class="wp-block-paragraph">where we just simply rotate, you know, some tree</p>



<p class="wp-block-paragraph">and explain it and work our way through it.</p>



<p class="wp-block-paragraph">If you have an idea for a really gross tree</p>



<p class="wp-block-paragraph">that you want me to rotate in front of you,</p>



<p class="wp-block-paragraph">leave a comment.</p>



<p class="wp-block-paragraph">leave a comment if i get enough comments maybe i&#8217;ll do it um otherwise thanks for watching this</p>



<p class="wp-block-paragraph">video i&#8217;ll see you in the next one i hope you learned a little bit of stuff and had a little</p>



<p class="wp-block-paragraph">bit of fun i&#8217;m outie i&#8217;m just simply outie hey everybody thanks for watching this video again</p>



<p class="wp-block-paragraph">from the bottom of my heart i really appreciate it i do hope you did learn something and have some</p>



<p class="wp-block-paragraph">please a small little favor could you please subscribe and follow this channel or these videos</p>



<p class="wp-block-paragraph">or whatever it is you do on the current social media website that you&#8217;re looking at right now</p>



<p class="wp-block-paragraph">it would really mean the world to me and it&#8217;ll help make more videos and grow this community so</p>



<p class="wp-block-paragraph">we&#8217;ll be able to do more videos longer videos better videos or just i&#8217;ll be able to keep making</p>



<p class="wp-block-paragraph">videos in general so please do do me a kindness and uh and subscribe you know sometimes i&#8217;m</p>



<p class="wp-block-paragraph">And I just wake up because I know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy.</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me or you could troll me if you want to just wake me up in the middle of the night.</p>



<p class="wp-block-paragraph">Just subscribe and then I&#8217;ll just wake up.</p>



<p class="wp-block-paragraph">I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now, you should see a QR code,</p>



<p class="wp-block-paragraph">which you can scan in order to go to the website, which I think is also named somewhere at the bottom of this video.</p>



<p class="wp-block-paragraph">where you can just kind of like see all the videos I published and the services</p>



<p class="wp-block-paragraph">and tutorials and things that I offer and all that good stuff and if you have</p>



<p class="wp-block-paragraph">a suggestion for clarifications or errata or just future videos that you</p>



<p class="wp-block-paragraph">want to see please leave a comment or if you just want to say hey what&#8217;s up</p>



<p class="wp-block-paragraph">what&#8217;s going on you know just send me a comment whatever I also wake up for</p>



<p class="wp-block-paragraph">those in the middle of the night I get I wake up in a cold sweat I&#8217;m like this</p>



<p class="wp-block-paragraph">It would really mean the world to me.</p>



<p class="wp-block-paragraph">I would really appreciate it.</p>



<p class="wp-block-paragraph">So again, thank you so much for watching this video.</p>



<p class="wp-block-paragraph">And enjoy the cool music as I fade into the darkness, which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>



<p class="wp-block-paragraph">Hey there, let&#8217;s uh…</p>



<p class="wp-block-paragraph">Bart on ourselves.</p>



<p class="wp-block-paragraph">Hello there, let&#8217;s rotate an AVL tree because it&#8217;s frickin invalid.</p>



<p class="wp-block-paragraph">No.</p>



<p class="wp-block-paragraph">Hey there, let&#8217;s uh maintain an AVL tree.</p>



<p class="wp-block-paragraph">and eh.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-rotations-tutorial-fixing-imbalance-after-adding-a-node/">AVL Tree Rotations Tutorial: Fixing Imbalance After Adding a Node</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/avl-tree-rotations-tutorial-fixing-imbalance-after-adding-a-node/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AVL Tree Tutorial: Balance Factors and Why They Fix Slow BSTs</title>
		<link>https://www.NeuralLantern.com/avl-tree-tutorial-balance-factors-and-why-they-fix-slow-bsts/</link>
					<comments>https://www.NeuralLantern.com/avl-tree-tutorial-balance-factors-and-why-they-fix-slow-bsts/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 18 May 2026 06:09:32 +0000</pubDate>
				<category><![CDATA[AVL Trees]]></category>
		<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[AVL tree]]></category>
		<category><![CDATA[AVL tree rotation]]></category>
		<category><![CDATA[AVL trees]]></category>
		<category><![CDATA[balance factor]]></category>
		<category><![CDATA[balanced binary tree]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[self balancing binary search tree]]></category>
		<category><![CDATA[tree rotation]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=371</guid>

					<description><![CDATA[<p>AVL trees are self-balancing binary search trees that prevent the tree from becoming unbalanced. We compute balance factors as the absolute value of left subtree height minus right subtree height. If any node has a balance factor of 2 or worse, we rebalance using rotations on trinode subtrees. This keeps search, insert, and other operations efficient at logarithmic time.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-tutorial-balance-factors-and-why-they-fix-slow-bsts/">AVL Tree Tutorial: Balance Factors and Why They Fix Slow BSTs</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="AVL Tree Tutorial: Balance Factors and Why They Fix Slow BSTs" width="1380" height="776" src="https://www.youtube.com/embed/xfMFNMpGVz0?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Learn AVL trees in this beginner-friendly introduction. We cover balance factors, why regular BSTs get slow, and how AVL trees stay balanced with rotations. Great for coding interviews and data structure understanding.</p>



<p class="wp-block-paragraph">00:00 AVL Trees Introduction<br>00:00:28 Problems with Regular BSTs<br>00:00:56 AVL Tree Balance Rule<br>00:02:10 Balance Factor Explained<br>00:02:48 Computing Balance Factors<br>00:03:11 Example Tree Analysis<br>00:04:40 Imbalance at 65 Node<br>00:05:08 Invalid AVL Tree<br>00:06:18 Linear Tree Problem<br>00:06:50 Trinode Subtree<br>00:07:40 Selecting Z Y X Nodes<br>00:09:20 Rotation Overview<br>00:10:03 Next Videos Preview<br>00:11:09 Thank You and Subscribe</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p class="wp-block-paragraph">Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to the Blog: https://www.NeuralLantern.com</li>



<li>Watch the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hello there. Let&#8217;s talk about a type of self-balancing binary search tree called an AVL tree.</p>



<p class="wp-block-paragraph">Okay, so by now I hope you&#8217;ve seen my other videos that I&#8217;ve posted</p>



<p class="wp-block-paragraph">previously where we talked about binary search trees, how to define them,</p>



<p class="wp-block-paragraph">terminology, how to know you&#8217;re actually looking at one, how to build them, insert,</p>



<p class="wp-block-paragraph">If you finish those videos already with me, then you know that binary search trees can actually get slow sometimes because if we have bad data, regular binary search trees don&#8217;t really know how to rearrange themselves.</p>



<p class="wp-block-paragraph">They just might become slow depending on how good or bad the data is.</p>



<p class="wp-block-paragraph">The data is totally random.</p>



<p class="wp-block-paragraph">Then on average, it&#8217;ll, you know, the trees will be log time, but that&#8217;s not always the case.</p>



<p class="wp-block-paragraph">Maybe sometimes you have bad data, sorted data, poison data, whatever kind of data.</p>



<p class="wp-block-paragraph">The secret to AVL trees is we&#8217;re going to start with the binary search tree.</p>



<p class="wp-block-paragraph">And so first it has to satisfy all the rules of the binary search tree.</p>



<p class="wp-block-paragraph">And then we add on another rule.</p>



<p class="wp-block-paragraph">The rule is going to be that we don&#8217;t have a valid AVL tree.</p>



<p class="wp-block-paragraph">If the balance factor for any of the nodes, we&#8217;ll talk about balance factors in a second.</p>



<p class="wp-block-paragraph">If the balance factor for any node is two or worse.</p>



<p class="wp-block-paragraph">Meaning if we think the tree is too imbalanced at any node, it&#8217;s not a valid AVL tree.</p>



<p class="wp-block-paragraph">and therefore we must rebalance the tree with something called a rotation.</p>



<p class="wp-block-paragraph">After we do enough rotations and we see that the tree is balanced again, then it&#8217;ll be valid.</p>



<p class="wp-block-paragraph">And so AVL trees sometimes are invalid in some intermediate state.</p>



<p class="wp-block-paragraph">Like if you have an AVL tree and you add some data into it,</p>



<p class="wp-block-paragraph">for a moment it might be an invalid AVL tree and then internally it&#8217;s sort of just like rotating itself and rebalancing itself.</p>



<p class="wp-block-paragraph">Then when the tree is done, you&#8217;ll have a valid AVL tree again, if that makes sense.</p>



<p class="wp-block-paragraph">AVL tree again if that makes sense. So suppose this tree here we can tell this is a valid binary</p>



<p class="wp-block-paragraph">search tree because it follows all the rules that we talked about before. So let&#8217;s start implementing</p>



<p class="wp-block-paragraph">the first rule of an AVL tree which is let&#8217;s compute the balance factors for every single node.</p>



<p class="wp-block-paragraph">How do we compute balance factors? The basic idea is I want to say BF for balance factor</p>



<p class="wp-block-paragraph">the left subtree height minus the right subtree height.</p>



<p class="wp-block-paragraph">If you don&#8217;t know what a subtree or height is,</p>



<p class="wp-block-paragraph">you should probably check out my other videos right now.</p>



<p class="wp-block-paragraph">But basically, we&#8217;re just going to take the absolute value</p>



<p class="wp-block-paragraph">just to get the difference in the height of the left</p>



<p class="wp-block-paragraph">and the height of the right.</p>



<p class="wp-block-paragraph">So if you know binary search trees already,</p>



<p class="wp-block-paragraph">then you know that the leaves, they don&#8217;t have any subtrees.</p>



<p class="wp-block-paragraph">So their balance factor is going to be pretty easy to compute.</p>



<p class="wp-block-paragraph">It&#8217;s just going to be a zero.</p>



<p class="wp-block-paragraph">I should also point out that some other tutorials out there use positive and negative numbers</p>



<p class="wp-block-paragraph">for the balance factor.</p>



<p class="wp-block-paragraph">That&#8217;s okay.</p>



<p class="wp-block-paragraph">I&#8217;m just going to use absolute values here because it&#8217;s a little bit more simple.</p>



<p class="wp-block-paragraph">So all the leaves get a balance factor of zero, which is fine.</p>



<p class="wp-block-paragraph">We&#8217;re looking to see if we can find a balance factor of two or worse to indicate to us that</p>



<p class="wp-block-paragraph">we have an invalid AVL tree.</p>



<p class="wp-block-paragraph">So far, so good.</p>



<p class="wp-block-paragraph">Let&#8217;s look at the 22 node.</p>



<p class="wp-block-paragraph">we&#8217;ll compute the height of the left subtree versus the height of the right subtree.</p>



<p class="wp-block-paragraph">So, you know, I&#8217;m going to highlight its left subtree.</p>



<p class="wp-block-paragraph">That&#8217;s just the 14 node that has a height of 1.</p>



<p class="wp-block-paragraph">And then its right subtree is just the 36 node that also has a height of 1,</p>



<p class="wp-block-paragraph">which means the balance factor for the 22 node is the absolute value of 1 minus 1 is 0.</p>



<p class="wp-block-paragraph">So actually the 22 node is perfectly balanced.</p>



<p class="wp-block-paragraph">If you think about it, that makes sense.</p>



<p class="wp-block-paragraph">Left and right subtrees have the same height.</p>



<p class="wp-block-paragraph">No problem.</p>



<p class="wp-block-paragraph">have the same height no problem so I&#8217;m going to move on let&#8217;s compute the balance factor for the</p>



<p class="wp-block-paragraph">48 node this is a little trickier notice how the right subtree has a height of one and there is no</p>



<p class="wp-block-paragraph">left subtree which means its height is zero so for the 48 node it&#8217;s actually going to be</p>



<p class="wp-block-paragraph">the absolute value of zero minus one or just one so we find our first node that&#8217;s a little bit out</p>



<p class="wp-block-paragraph">imbalance here but avl trees tolerate imbalances of one they don&#8217;t really care we sort of try to</p>



<p class="wp-block-paragraph">make a trade-off between constantly always rotating every single time anything happens</p>



<p class="wp-block-paragraph">which would perhaps burn a little too much cpu versus letting the tree just be imbalanced to a</p>



<p class="wp-block-paragraph">reasonable amount so we could still call this a log tree in the end so we consider this to be okay</p>



<p class="wp-block-paragraph">65 node and compute the balance factor so 65 node has no right subtree so the height is zero there</p>



<p class="wp-block-paragraph">and its left subtree is those two nodes on the left so that has a height of two which means the</p>



<p class="wp-block-paragraph">balance factor for the 65 node is going to be the absolute value of two minus zero is two at this</p>



<p class="wp-block-paragraph">point we are already certain that this is not a valid avl tree and it would need to be rebalanced</p>



<p class="wp-block-paragraph">you know, consider it a valid AVL tree in the future.</p>



<p class="wp-block-paragraph">So invalid AVL tree, still a valid binary search tree.</p>



<p class="wp-block-paragraph">Something has to be done.</p>



<p class="wp-block-paragraph">Normally what I would say is if you find some nodes</p>



<p class="wp-block-paragraph">way down lower in the tree that are imbalanced,</p>



<p class="wp-block-paragraph">then just go ahead and perform a rebalancing</p>



<p class="wp-block-paragraph">or a rotation immediately,</p>



<p class="wp-block-paragraph">because sometimes when you rotate lower in the tree,</p>



<p class="wp-block-paragraph">you&#8217;ll end up fixing nodes that are a little higher.</p>



<p class="wp-block-paragraph">But since we started with this tree</p>



<p class="wp-block-paragraph">and we&#8217;re not kind of, you know,</p>



<p class="wp-block-paragraph">building a tree step-by-step,</p>



<p class="wp-block-paragraph">I just want to compute the balance factor for all of the nodes at the same time.</p>



<p class="wp-block-paragraph">So we&#8217;ll do the same thing for the 41 node, the root node.</p>



<p class="wp-block-paragraph">Its left subtree has a height of two and its right subtree has a height of three.</p>



<p class="wp-block-paragraph">So its balance factor is the absolute value of two minus three is one.</p>



<p class="wp-block-paragraph">So actually the root node is okay.</p>



<p class="wp-block-paragraph">If it was only up to the root node, we would say this is a valid AVL tree</p>



<p class="wp-block-paragraph">and we don&#8217;t really need to do anything.</p>



<p class="wp-block-paragraph">need to do anything however we&#8217;ve already seen that the 65 node is invalid so again uh we we</p>



<p class="wp-block-paragraph">need to do something here&#8217;s another quick example before we move on to actually identifying which</p>



<p class="wp-block-paragraph">nodes to to modify and rotate just real fast i want to show you you know that a binary search</p>



<p class="wp-block-paragraph">tree could actually end up being a linear tree if you had really really really bad data the binary</p>



<p class="wp-block-paragraph">This is slow because this is a linear tree.</p>



<p class="wp-block-paragraph">The time complexity of searching through this tree is linear time.</p>



<p class="wp-block-paragraph">It scales linearly with the number of nodes in your data set.</p>



<p class="wp-block-paragraph">That&#8217;s really far away from log time, which is supposed to be lightning fast.</p>



<p class="wp-block-paragraph">So an AVL tree will fix this kind of bad data.</p>



<p class="wp-block-paragraph">Let&#8217;s take a step back here and focus your attention for a second on these three nodes.</p>



<p class="wp-block-paragraph">We noticed that there is one node that&#8217;s actually out of whack.</p>



<p class="wp-block-paragraph">It&#8217;s the 65 node, right?</p>



<p class="wp-block-paragraph">What we&#8217;re going to do is we&#8217;re going to find a trinode subtree that starts with the first node or the lowest node we can find that&#8217;s out of whack.</p>



<p class="wp-block-paragraph">It&#8217;s the 65 node.</p>



<p class="wp-block-paragraph">And you can tell that if we just kind of go down a level and down another level to select the other two nodes,</p>



<p class="wp-block-paragraph">it&#8217;s just going to be this little, you know, subtree of three nodes or otherwise known as a trinode subtree.</p>



<p class="wp-block-paragraph">So here&#8217;s kind of a diagram for that.</p>



<p class="wp-block-paragraph">Whoops, forgot to delete that earlier.</p>



<p class="wp-block-paragraph">Let me get rid of that.</p>



<p class="wp-block-paragraph">So this is the trinode subtree that we selected, right?</p>



<p class="wp-block-paragraph">So we&#8217;ve got the 65 and the 48 and the 55.</p>



<p class="wp-block-paragraph">How did we actually select this?</p>



<p class="wp-block-paragraph">So what you kind of want to do is the first node that is out of whack,</p>



<p class="wp-block-paragraph">you want to call that Z.</p>



<p class="wp-block-paragraph">So I&#8217;m going to write a Z here.</p>



<p class="wp-block-paragraph">What we&#8217;re looking for is a trinode subtree,</p>



<p class="wp-block-paragraph">which we&#8217;ll first call X and Y and Z.</p>



<p class="wp-block-paragraph">And then we&#8217;ll end up reordering the nodes</p>



<p class="wp-block-paragraph">and then rearranging all the pointers to the nodes</p>



<p class="wp-block-paragraph">so that the trinode subtree is a little bit more in balance.</p>



<p class="wp-block-paragraph">more in balance. So we see the Z node. We have to find the Y node next. To find the Y node,</p>



<p class="wp-block-paragraph">what&#8217;s going on with my computer? Oh, there we go. We have the Z node. To find the Y node,</p>



<p class="wp-block-paragraph">really, we&#8217;re just going to take the child of the 65 node that has the taller subtree. So</p>



<p class="wp-block-paragraph">there&#8217;s only one subtree, you know, on the left, there&#8217;s no subtree on the right of the 65 node.</p>



<p class="wp-block-paragraph">So the choice is obvious. But just so you know, if there was, you know, another, you know, child</p>



<p class="wp-block-paragraph">another you know child hanging off of the right for some reason we would still choose the number</p>



<p class="wp-block-paragraph">48 node as the y node because it has the taller subtree notice how the subtree on the left is two</p>



<p class="wp-block-paragraph">and the subtree on the right has a height of one so always take the taller subtree when you&#8217;re</p>



<p class="wp-block-paragraph">looking for uh y and then x so then we do the same thing we go down to the grandchild of the z node</p>



<p class="wp-block-paragraph">and if we had a choice to go left or right again we would always choose the taller subtree in this</p>



<p class="wp-block-paragraph">taller subtree in this case there&#8217;s only one choice so that x node is going to be the 55</p>



<p class="wp-block-paragraph">now in this next slide i&#8217;ve kind of redrawn this in a little bit different of a way</p>



<p class="wp-block-paragraph">notice the trinode subtree that we just selected here the 65 48 55 subtree it has a height of three</p>



<p class="wp-block-paragraph">i mean that should be obvious right it&#8217;s just got a height of three</p>



<p class="wp-block-paragraph">or height. Notice on the left, these are the same three nodes. Exactly. 48, 55, and 65. That&#8217;s the</p>



<p class="wp-block-paragraph">same three nodes that we originally had. But notice how the height is too. So the basic idea for ABL</p>



<p class="wp-block-paragraph">trees is, you know, compute the balance factors. And as soon as you identify a node that&#8217;s out of</p>



<p class="wp-block-paragraph">whack, you grab a tri-node sub-tree starting at the node that&#8217;s out of whack, the Z node.</p>



<p class="wp-block-paragraph">And then we&#8217;ll perform, quote unquote, a rotation where we just kind of smoosh it to be a little</p>



<p class="wp-block-paragraph">be a little bit more balanced and a little bit more a little bit shorter so the height goes from</p>



<p class="wp-block-paragraph">three to two once we do that the rest of the tree it&#8217;s going to get a little bit better you&#8217;ll notice</p>



<p class="wp-block-paragraph">that this will end up sometimes completely but sometimes partially fixing bad balance factors</p>



<p class="wp-block-paragraph">in the tree so um this is the end of the intro i only wanted to spend a little time just kind of</p>



<p class="wp-block-paragraph">like warming you up to the idea in the next videos uh we&#8217;re going to actually look at performing</p>



<p class="wp-block-paragraph">identifying the four different types of rotations that you could see and like how to do them.</p>



<p class="wp-block-paragraph">We&#8217;ll talk more about the idea that we&#8217;re really just rearranging pointers here.</p>



<p class="wp-block-paragraph">If you&#8217;re thinking about code, you&#8217;re thinking about, oh, I&#8217;ve got some pointers between these</p>



<p class="wp-block-paragraph">nodes. We&#8217;re not going to delete any nodes or add any nodes. We&#8217;re just going to like</p>



<p class="wp-block-paragraph">rearrange who is a parent of who, who is a child of who. And then eventually we&#8217;ll deal with some</p>



<p class="wp-block-paragraph">really bad trees and we&#8217;ll become experts at just like maintaining ABL trees.</p>



<p class="wp-block-paragraph">talk about ways to visualize the rotations, which makes it a little bit easier to remember these</p>



<p class="wp-block-paragraph">patterns and stuff. Okay, so thank you for watching this video. I hope you learned a little bit of</p>



<p class="wp-block-paragraph">stuff and had a little bit of fun. In the next one, we&#8217;re going to get started. We&#8217;re going to</p>



<p class="wp-block-paragraph">go deep. I&#8217;m going to get my gear. Sorry.</p>



<p class="wp-block-paragraph">Hey, everybody. Thanks for watching this video again from the bottom of my heart. I really</p>



<p class="wp-block-paragraph">from the bottom of my heart. I really appreciate it. I do hope you did learn something and have</p>



<p class="wp-block-paragraph">some fun. If you could do me a please, a small little favor, could you please subscribe and</p>



<p class="wp-block-paragraph">follow this channel or these videos or whatever it is you do on the current social media website</p>



<p class="wp-block-paragraph">that you&#8217;re looking at right now? It would really mean the world to me and it&#8217;ll help make more</p>



<p class="wp-block-paragraph">videos and grow this community. So we&#8217;ll be able to do more videos, longer videos, better videos,</p>



<p class="wp-block-paragraph">Do me a kindness and subscribe.</p>



<p class="wp-block-paragraph">You know, sometimes I&#8217;m sleeping in the middle of the night and I just wake up because I know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy.</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me or you could troll me if you want to just wake me up in the middle of the night.</p>



<p class="wp-block-paragraph">Just subscribe and then I&#8217;ll just wake up.</p>



<p class="wp-block-paragraph">I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, 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.</p>



<p class="wp-block-paragraph">in order to go to the website which i think is also named somewhere at the bottom of this video</p>



<p class="wp-block-paragraph">and it&#8217;ll take you to my main website where you can just kind of like see all the videos i published</p>



<p class="wp-block-paragraph">and the services and tutorials and things that i offer and all that good stuff and uh</p>



<p class="wp-block-paragraph">if you have a suggestion for uh uh clarifications or errata or just future videos that you want to</p>



<p class="wp-block-paragraph">see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on you know</p>



<p class="wp-block-paragraph">I also wake up for those in the middle of the night. I get I wake up in a cold sweat and I&#8217;m like</p>



<p class="wp-block-paragraph">it would really it really mean the world to me. I would really appreciate it. So again thank you</p>



<p class="wp-block-paragraph">so much for watching this video and enjoy the cool music as I fade into the darkness</p>



<p class="wp-block-paragraph">which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-tutorial-balance-factors-and-why-they-fix-slow-bsts/">AVL Tree Tutorial: Balance Factors and Why They Fix Slow BSTs</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/avl-tree-tutorial-balance-factors-and-why-they-fix-slow-bsts/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
