<?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 example Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/avl-tree-example/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/avl-tree-example/</link>
	<description></description>
	<lastBuildDate>Mon, 01 Jun 2026 05:46:58 +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 example Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/avl-tree-example/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
	</channel>
</rss>
