<?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 rotation example Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/avl-rotation-example/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/avl-rotation-example/</link>
	<description></description>
	<lastBuildDate>Sat, 27 Jun 2026 21:17:00 +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 rotation example Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/avl-rotation-example/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>AVL Trees: Adding Linear Data and Performing Rotations</title>
		<link>https://www.NeuralLantern.com/avl-trees-adding-linear-data-and-performing-rotations/</link>
					<comments>https://www.NeuralLantern.com/avl-trees-adding-linear-data-and-performing-rotations/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sat, 27 Jun 2026 21:16:58 +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[AVL rotation example]]></category>
		<category><![CDATA[AVL tree]]></category>
		<category><![CDATA[AVL trees]]></category>
		<category><![CDATA[balance factor]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[binary search tree insertion]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[data structures tutorial]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[self balancing tree]]></category>
		<category><![CDATA[tree rotations]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=396</guid>

					<description><![CDATA[<p>In this AVL tree example we add linear data starting with 12 then 21 30 38 42 and 55. After each insertion we update balance factors and when we hit imbalance we select the XYZ trinode and perform rotations to restore the AVL property. The tree stays balanced despite the bad input data.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-trees-adding-linear-data-and-performing-rotations/">AVL Trees: Adding Linear Data and Performing Rotations</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 Trees: Adding Linear Data and Performing Rotations" width="1380" height="776" src="https://www.youtube.com/embed/6kasiB918fE?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">Watch me build an AVL tree step by step with linear data that would normally create a terrible unbalanced BST. See insertions, balance factor calculations, and rotations in action.</p>



<p class="wp-block-paragraph">Perfect follow-up to my BST and AVL intro videos.</p>



<p class="wp-block-paragraph">00:00 Introduction to AVL Tree with Bad Data<br>00:14 Previous Videos on BST and AVL Trees<br>00:28 Practice Example Building Step by Step<br>00:50 Adding First Node 12<br>01:11 Adding Node 21<br>02:10 Adding Node 30 and First Rotation<br>04:03 Recomputing Balance Factors<br>05:08 Adding Node 38<br>08:03 Adding Node 42 and Second Rotation<br>12:51 Recomputing Balance After Rotation<br>14:09 Adding Node 55 and Third Rotation<br>18:55 Placing Unaccounted Nodes<br>19:21 Final Tree and Balance Factors<br>20:07 Conclusion and Thanks</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 build an AVL self-balancing binary search tree with some really bad data.</p>



<p class="wp-block-paragraph">It&#8217;s all going to work out, trust me.</p>



<p class="wp-block-paragraph">Okay, so hopefully by now you&#8217;ve seen my other videos where we talked about how to define binary</p>



<p class="wp-block-paragraph">search trees, how to build them, search through them, add, delete, all that stuff. And also AVL</p>



<p class="wp-block-paragraph">trees. What are rotations? Why do we rotate? How does the AVL tree maintain itself? And so forth.</p>



<p class="wp-block-paragraph">just going to be a practice example. In the previous video that I that I</p>



<p class="wp-block-paragraph">recorded regarding AVL trees, I started off with a really long and gnarly linear</p>



<p class="wp-block-paragraph">tree and then I suddenly turned on AVLness and we rotated the crap out of</p>



<p class="wp-block-paragraph">it for about 30 minutes until it was really really good. So now I&#8217;m just</p>



<p class="wp-block-paragraph">going to build a tree one step at a time. Suppose for the sake of argument that</p>



<p class="wp-block-paragraph">you have some really bad data. You know a long time ago, I love telling this story.</p>



<p class="wp-block-paragraph">time ago you know my grandma before she passed on she was kind of bitter and she had some enemies in</p>



<p class="wp-block-paragraph">the neighborhood she used to call the cops on her neighbors for gossip um she had a neighbor who</p>



<p class="wp-block-paragraph">kind of feuded with her and at one point according to my grandma anyway uh my neighbor like didn&#8217;t</p>



<p class="wp-block-paragraph">like my grandma&#8217;s tree and so one day when my grandma was looking out into the backyard</p>



<p class="wp-block-paragraph">through the back window because she likes to yell at ducks who are swimming in her pool</p>



<p class="wp-block-paragraph">notice that the other little old lady&#8217;s arm just kind of went over my grandma&#8217;s fence</p>



<p class="wp-block-paragraph">with a spray bottle and then sprayed her tree. And then my grandma&#8217;s tree died. So this kind of</p>



<p class="wp-block-paragraph">thing I like to call, you know, tree poisoning or data poisoning or bad data, it can affect a binary</p>



<p class="wp-block-paragraph">search tree and make it really, really, really slow. But in AVL trees, if we have bad data,</p>



<p class="wp-block-paragraph">it should actually end up being fine. So I&#8217;m just going to say we&#8217;re poisoning our grandma&#8217;s tree</p>



<p class="wp-block-paragraph">grandma&#8217;s tree with some bad data. I&#8217;ll just do maybe maybe five pieces of data. We could go</p>



<p class="wp-block-paragraph">further than that but it&#8217;s it&#8217;s going to be a super long video if we do. So we&#8217;re going to add</p>



<p class="wp-block-paragraph">these pieces of data one by one. So that means the first thing I&#8217;m going to do remember we are kind</p>



<p class="wp-block-paragraph">of just regularly you know building a regular binary search tree. It&#8217;s just that the differences</p>



<p class="wp-block-paragraph">after we modify the topology in some way we&#8217;ll just check to see that it satisfies the rules of</p>



<p class="wp-block-paragraph">then we&#8217;re going to start performing rotations to make sure that it&#8217;s self-balancing.</p>



<p class="wp-block-paragraph">So we&#8217;ll add the 12 first.</p>



<p class="wp-block-paragraph">So let me see if I can draw a little arrow just indicating,</p>



<p class="wp-block-paragraph">yeah, we&#8217;re going to add this number right now.</p>



<p class="wp-block-paragraph">I&#8217;m going to do an ellipse for a node.</p>



<p class="wp-block-paragraph">And you know, like for binary, what is that?</p>



<p class="wp-block-paragraph">Flying saucer for binary search trees.</p>



<p class="wp-block-paragraph">It&#8217;s like, well, if there is no data already, if the tree is empty,</p>



<p class="wp-block-paragraph">first piece of data you add is going to end up being the root node. So that&#8217;s it. We have our</p>



<p class="wp-block-paragraph">root node here. See my other videos if you want more practice on building trees. So we&#8217;re just</p>



<p class="wp-block-paragraph">going to call that the root node. And then we&#8217;re going to compute its balance factor, which is</p>



<p class="wp-block-paragraph">obviously just going to be a zero. So that was super easy. Now we&#8217;re going to move on to adding</p>



<p class="wp-block-paragraph">the next piece of data. Things are getting a little bit worse. This is going to be a linear tree.</p>



<p class="wp-block-paragraph">tree if it weren&#8217;t for our trusty I guess AVL functionality so the 21 I&#8217;m</p>



<p class="wp-block-paragraph">gonna maybe just duplicate this node right now and set that to a 21 and then</p>



<p class="wp-block-paragraph">we&#8217;ll figure out where does the 21 go well it can&#8217;t be the root because the</p>



<p class="wp-block-paragraph">root is already occupied 21 is larger than 12 so it&#8217;s got to be on the right</p>



<p class="wp-block-paragraph">side so the 21 is going to be the right child of the root node so I&#8217;m just going</p>



<p class="wp-block-paragraph">Connecting line right here.</p>



<p class="wp-block-paragraph">And then we have to recompute balance factors.</p>



<p class="wp-block-paragraph">So the 21 is the thing we just added.</p>



<p class="wp-block-paragraph">We have to compute that.</p>



<p class="wp-block-paragraph">Then we have to work our way up the tree until we hit root.</p>



<p class="wp-block-paragraph">So we see the root node.</p>



<p class="wp-block-paragraph">It now has a balance factor of 1.</p>



<p class="wp-block-paragraph">So it got a little bit worse.</p>



<p class="wp-block-paragraph">But we don&#8217;t see 2 or worse.</p>



<p class="wp-block-paragraph">So that means this is currently a valid AVL tree.</p>



<p class="wp-block-paragraph">And we don&#8217;t have to do any rotations.</p>



<p class="wp-block-paragraph">So now it&#8217;s time to add the 30.</p>



<p class="wp-block-paragraph">just realized I could just upload like a hundred videos like this and I think you&#8217;d all be pretty</p>



<p class="wp-block-paragraph">happy or some people would be pretty happy um I don&#8217;t know if I want to do that to myself maybe</p>



<p class="wp-block-paragraph">I will so we&#8217;re going to add the 30 node uh so first I&#8217;m just going to duplicate some node</p>



<p class="wp-block-paragraph">and I&#8217;m going to call it 30 then we have to figure out where the 30 node belongs uh it&#8217;s not going</p>



<p class="wp-block-paragraph">to be the root node that&#8217;s already occupied 30 belongs on the right side of 12 but we can&#8217;t use</p>



<p class="wp-block-paragraph">of 12 but we can&#8217;t use that right child it&#8217;s occupied 30 belongs on the right side of 21</p>



<p class="wp-block-paragraph">so you know it makes sense we called this linear data that would make a linear tree so everything</p>



<p class="wp-block-paragraph">ended up being on the right side so far it&#8217;s just getting worse and worse but now the ablness is</p>



<p class="wp-block-paragraph">going to kind of come to the rescue we have to recompute the balance factors so the 30 has a 0</p>



<p class="wp-block-paragraph">the 21 ends up having a 1 again if you want practice computing balance factors see my other</p>



<p class="wp-block-paragraph">The 12 has a balance factor of 2 and now we know right away this is not a valid ABL tree anymore</p>



<p class="wp-block-paragraph">because we have a balance factor that is bad. It&#8217;s too bad.</p>



<p class="wp-block-paragraph">So we have to now select an XYZ trinode subtree so we can perform a rotation.</p>



<p class="wp-block-paragraph">I&#8217;m sure you can imagine it&#8217;s just going to end up being three nodes that are perfectly balanced like that.</p>



<p class="wp-block-paragraph">But let&#8217;s do it one step at a time.</p>



<p class="wp-block-paragraph">So the Z node is going to be the 12.</p>



<p class="wp-block-paragraph">That was the first node that we noticed that was out of whack or like I guess the highest node.</p>



<p class="wp-block-paragraph">or like I guess the highest node of the lowest nodes.</p>



<p class="wp-block-paragraph">And then we have to find Y by taking a child of Z.</p>



<p class="wp-block-paragraph">There&#8217;s only one possible child we could choose, so that&#8217;s going to be Y.</p>



<p class="wp-block-paragraph">And then we find X by taking a child of Y.</p>



<p class="wp-block-paragraph">There&#8217;s only one choice again, so it&#8217;s just going to be there.</p>



<p class="wp-block-paragraph">Now we have X, Y, and Z, and I&#8217;m going to write it down real fast.</p>



<p class="wp-block-paragraph">Let me just put this in black ink.</p>



<p class="wp-block-paragraph">So X is 30, and Y is 21, and Z is 12.</p>



<ol start="12" class="wp-block-list">
<li>Then we&#8217;re going to do A and B and C, which are going to be just in order representations of</li>
</ol>



<p class="wp-block-paragraph">XYZ. So again, think in your code, you would be doing pointers, you&#8217;d probably want to make three</p>



<p class="wp-block-paragraph">more pointers, call them ABC. If you had a rotation function, the rotation function,</p>



<p class="wp-block-paragraph">after you selected three nodes to rotate, it would receive XYZ. And then you say, let&#8217;s reorder,</p>



<p class="wp-block-paragraph">you know, XYZ and call them ABC. So A is going to be 12, and B is going to be 21,</p>



<p class="wp-block-paragraph">it should you know ascend from left to right if we&#8217;re not supporting duplicates in our tree</p>



<p class="wp-block-paragraph">and then i&#8217;m going to um just you know make some make some nodes for our target pattern here</p>



<p class="wp-block-paragraph">our output pattern so i&#8217;m going to like make some nodes here kind of place them uh in the perfect</p>



<p class="wp-block-paragraph">avl tree output pattern a perfectly balanced trinode subtree i&#8217;m just going to reconnect this</p>



<p class="wp-block-paragraph">The output pattern is always the same.</p>



<p class="wp-block-paragraph">You know, it always starts this way because this is better</p>



<p class="wp-block-paragraph">than the linear pattern that we have above.</p>



<p class="wp-block-paragraph">And then I just have to update the numbers.</p>



<p class="wp-block-paragraph">So the left child is going to be 12.</p>



<p class="wp-block-paragraph">The right child is going to be 21.</p>



<p class="wp-block-paragraph">Sorry, the center is going to be 21, the new parent.</p>



<p class="wp-block-paragraph">And the right child is going to be 30.</p>



<p class="wp-block-paragraph">Okay, now we look for any nodes that are children of the input nodes</p>



<p class="wp-block-paragraph">that have been unaccounted for.</p>



<p class="wp-block-paragraph">You can tell easily that all nodes are being used.</p>



<p class="wp-block-paragraph">So we don&#8217;t even have to worry about that step.</p>



<p class="wp-block-paragraph">So now that we have our trinode subtree which is perfectly balanced, we are ready to just sort of override it into the diagram.</p>



<p class="wp-block-paragraph">And since the diagram only has the three nodes to begin with, I could pretty much just like erase everything, call it a day, and say this is the new tree, the new rotated tree.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to maybe put this in the middle I guess.</p>



<p class="wp-block-paragraph">I&#8217;m going to delete this over here and recompute the balance factors.</p>



<p class="wp-block-paragraph">a zero, get zeros, and this 21 also gets a zero because it&#8217;s perfectly balanced.</p>



<p class="wp-block-paragraph">Next thing is let&#8217;s move on to adding the number 38. So we&#8217;ll add the 38 here and</p>



<p class="wp-block-paragraph">same thing that we do before. Let&#8217;s make a copy of one of the nodes for our diagram.</p>



<p class="wp-block-paragraph">Let&#8217;s call it the 38 and then let&#8217;s figure out where it belongs. It&#8217;s not going to be the new</p>



<p class="wp-block-paragraph">root node because that&#8217;s occupied. It belongs on the right side. It&#8217;s not going to be the right</p>



<p class="wp-block-paragraph">It&#8217;s not going to be the right child because that&#8217;s occupied.</p>



<p class="wp-block-paragraph">It belongs on the right side of the 30.</p>



<p class="wp-block-paragraph">It couldn&#8217;t go on the left.</p>



<p class="wp-block-paragraph">That would be an invalid binary search tree.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to stick it right there and do a connection.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to update the balance factors.</p>



<p class="wp-block-paragraph">So the 38 is a leaf.</p>



<p class="wp-block-paragraph">It gets 0.</p>



<p class="wp-block-paragraph">The 30 gets a 1.</p>



<p class="wp-block-paragraph">And the 21 also gets a 1 because its left subtree is a 1 and its right subtree is 2,</p>



<p class="wp-block-paragraph">or height, and the difference is just 1.</p>



<p class="wp-block-paragraph">is just one. Remember we don&#8217;t really need to update the 12 or any nodes that are not</p>



<p class="wp-block-paragraph">directly above the node that we just added or removed. So if you look at this tree there&#8217;s</p>



<p class="wp-block-paragraph">a little bit of imbalance but nothing is two or worse so we don&#8217;t actually need to perform a</p>



<p class="wp-block-paragraph">rotation. This is already a valid AVL tree. So I&#8217;m going to move on to adding the next number.</p>



<p class="wp-block-paragraph">Let&#8217;s add a 42. Same thing we&#8217;re going to duplicate a node for our diagram and we&#8217;re going to call it</p>



<p class="wp-block-paragraph">going to call it the 42 where does it belong well we start by looking at root that&#8217;s occupied 42</p>



<p class="wp-block-paragraph">belongs on the right side of 21 the right child is occupied 42 belongs on the right side belongs on</p>



<p class="wp-block-paragraph">the right side i mean it&#8217;s always going to be on the very right of the of the diagram because that&#8217;s</p>



<p class="wp-block-paragraph">the poison data that we received uh so i&#8217;m going to put it over there and then connect um the child</p>



<p class="wp-block-paragraph">child relationship and then let&#8217;s update the the balance factor so 42 gets a 0</p>



<p class="wp-block-paragraph">38 gets a 1 and 30 gets a 2 now because it&#8217;s got 2 height of subtree on its</p>



<p class="wp-block-paragraph">right side and no subtree on its left side the 21 we should update left subtree</p>



<p class="wp-block-paragraph">is a 1 right subtree is a 3 oh it&#8217;s still gonna it&#8217;s actually gonna be 2 so</p>



<p class="wp-block-paragraph">we have two instances of the number 2 and again which one do we actually want</p>



<p class="wp-block-paragraph">by the way here&#8217;s here&#8217;s how I like to eyeball it you can kind of see oh there&#8217;s two levels of</p>



<p class="wp-block-paragraph">difference in terms of the root node which one do we rotate first well as low as possible because</p>



<p class="wp-block-paragraph">if we rotate the 30 it might end up fixing the 21 so we don&#8217;t want to do any more more work than we</p>



<p class="wp-block-paragraph">have to so I&#8217;m just going to maybe move this over here a little bit and select our xyz um hang on</p>



<p class="wp-block-paragraph">a second I think I just like didn&#8217;t do a snap to grid when I was doing that okay hang on</p>



<p class="wp-block-paragraph">doing that okay hang on okay so we got to choose our x y and z uh we&#8217;re going to say that the 30</p>



<p class="wp-block-paragraph">is z the lowest node possible that is out of whack and then we hop down to a child of z to find y</p>



<p class="wp-block-paragraph">we hop down to a child of y to find x and so now we have our xyz i&#8217;m just going to write them down</p>



<p class="wp-block-paragraph">I&#8217;m going to change the ink real fast and then we have to get ABC so A is the least value B is the</p>



<p class="wp-block-paragraph">one that belongs in the middle and C is 42 now we have our ABC we&#8217;re ready to draw our output pattern</p>



<p class="wp-block-paragraph">or target pattern so I&#8217;m just going to put like some nodes up here duplicate that a couple times</p>



<p class="wp-block-paragraph">all right not the prettiest but I&#8217;ll take it I&#8217;m gonna do my connecting lines</p>



<p class="wp-block-paragraph">and then I have to update the numbers so a is 30 and then b is 38 and c is 42 still so we&#8217;ll just</p>



<p class="wp-block-paragraph">leave that as is now we have to look for children of any of the output pattern nodes that are</p>



<p class="wp-block-paragraph">for so we&#8217;ll first look at the z node here the 30 it had a right child of 38 that&#8217;s already handled</p>



<p class="wp-block-paragraph">as the y node so we don&#8217;t need to worry about the 38 okay next we look at the uh y node it had a</p>



<p class="wp-block-paragraph">right child of 42 that&#8217;s also handled in the output pattern so we don&#8217;t have to worry about that uh</p>



<p class="wp-block-paragraph">then the 42 had no children so we don&#8217;t need to really do anything this is going to be super easy</p>



<p class="wp-block-paragraph">Just as a reminder, the Z could have had a left child, the Y could have had a left child,</p>



<p class="wp-block-paragraph">and the X could have had two children, at least in this particular pattern.</p>



<p class="wp-block-paragraph">Oh, I think in my previous video, I didn&#8217;t name off all the rotation types.</p>



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



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



<p class="wp-block-paragraph">So now I&#8217;m ready to just kind of like move this into the diagram.</p>



<p class="wp-block-paragraph">So the 30 and 38 and 42, we can just kind of get rid of that</p>



<p class="wp-block-paragraph">move the rotated output pattern into the diagram like this.</p>



<p class="wp-block-paragraph">We&#8217;ll make the 38 the right child of 21.</p>



<p class="wp-block-paragraph">I want to make it go a little bit more to the right</p>



<p class="wp-block-paragraph">so that we have a nicely drawn diagram</p>



<p class="wp-block-paragraph">so that all the nodes have space so it&#8217;s easy to debug visually.</p>



<p class="wp-block-paragraph">It&#8217;s easy to just kind of eyeball it.</p>



<p class="wp-block-paragraph">Then I&#8217;m going to update the balance factors.</p>



<p class="wp-block-paragraph">So the 42 has 0, the 30 has 0, the 38 has 0.</p>



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



<p class="wp-block-paragraph">The 21, hopefully that improved.</p>



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



<p class="wp-block-paragraph">of one the right subtree has a height of two so actually the balance factor of the 21 node improved</p>



<p class="wp-block-paragraph">to one we are now done uh rotating and we have a valid avl tree and uh we could have had you know</p>



<p class="wp-block-paragraph">a tree that was just totally linear all the way down to the right it could have worked out that</p>



<p class="wp-block-paragraph">way if we weren&#8217;t using avl trees uh and we would have had a really slow binary search tree but with</p>



<p class="wp-block-paragraph">avl trees this is like you know a lot a lot faster reasonably and you can imagine if i added some</p>



<p class="wp-block-paragraph">And you can imagine if I added some more data, let&#8217;s see, how long is this video?</p>



<p class="wp-block-paragraph">Oh, it&#8217;s this video.</p>



<p class="wp-block-paragraph">It&#8217;s already 15 minutes.</p>



<p class="wp-block-paragraph">Let&#8217;s add one more piece of data just for fun, Zars.</p>



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



<p class="wp-block-paragraph">Anybody remember that old song, I Can&#8217;t Drive, 55?</p>



<p class="wp-block-paragraph">So we&#8217;re going to add the 55.</p>



<p class="wp-block-paragraph">I&#8217;m going to say, here it is.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to do just like a new node duplicate.</p>



<p class="wp-block-paragraph">node duplicate. I&#8217;m going to stick a 55 in there. And then where does the 55 belong? Well,</p>



<p class="wp-block-paragraph">it&#8217;s not going to go with the root node belongs on the right side. Not going to go there belongs</p>



<p class="wp-block-paragraph">on the right side, not going to go there belongs on the right side, we just end up going all the</p>



<p class="wp-block-paragraph">way to the right. Pretty much. Okay, so I&#8217;m going to say it belongs there. And I&#8217;m going to connect</p>



<p class="wp-block-paragraph">it. And then we update the balance factors. So zero here, and then the 42 becomes a one and the</p>



<p class="wp-block-paragraph">42 becomes a 1 and the 38 becomes a 1 and the 21 becomes a 1 on the left and 3 on the right</p>



<p class="wp-block-paragraph">which means we have a balance factor of 2 so now the 21 node is imbalanced this is no longer a</p>



<p class="wp-block-paragraph">valid abl tree we got to rotate to make it better so once again we look at the first node that&#8217;s out</p>



<p class="wp-block-paragraph">of whack or the lowest node that&#8217;s out of whack it&#8217;s going to be the 21 we call it our z and then</p>



<p class="wp-block-paragraph">we find a child of z with the tallest subtree so it you know is it going to be the 12 or the 38</p>



<p class="wp-block-paragraph">the 12 or the 38 it&#8217;s definitely going to be the 38 because 38 is the taller subtree same thing to</p>



<p class="wp-block-paragraph">find x it&#8217;s got to be a child of y should we go left or should we go right we have to go right</p>



<p class="wp-block-paragraph">because that&#8217;s the taller subtree so now we have our x y and z um i think i&#8217;ve been forgetting to</p>



<p class="wp-block-paragraph">is 38, Z is 21, X, Y, and Z, and then we do A and B and C. So A is the least value, 21, B is the one</p>



<p class="wp-block-paragraph">that belongs in the middle, C is the one that belongs on the right side. Now we&#8217;re ready to</p>



<p class="wp-block-paragraph">draw our target output pattern. So I&#8217;m going to do this, a little duplication.</p>



<p class="wp-block-paragraph">Do you like when I say, oh, we&#8217;re going to do this, we&#8217;re going to do this, we&#8217;re going to do this?</p>



<p class="wp-block-paragraph">Because sometimes I just go, I don&#8217;t like it.</p>



<p class="wp-block-paragraph">And I don&#8217;t think people like it when I say that.</p>



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



<p class="wp-block-paragraph">I&#8217;m going to connect these real fast and then change the numbers to match.</p>



<p class="wp-block-paragraph">I think I made that one.</p>



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



<p class="wp-block-paragraph">I can never tell if it&#8217;s perfectly symmetrical.</p>



<p class="wp-block-paragraph">I can&#8217;t do it.</p>



<p class="wp-block-paragraph">It&#8217;s perfectly symmetric.</p>



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



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



<p class="wp-block-paragraph">Alright, so A is going to be on the left, that&#8217;s 21.</p>



<p class="wp-block-paragraph">B is in the middle, that&#8217;s 38.</p>



<p class="wp-block-paragraph">C is on the right, that&#8217;s 42.</p>



<p class="wp-block-paragraph">Then we have to look for children of the output pattern that are unaccounted for.</p>



<p class="wp-block-paragraph">So we&#8217;re going to do, look at the Z node.</p>



<p class="wp-block-paragraph">The Z node had 12, which is unaccounted for as a left child.</p>



<p class="wp-block-paragraph">So I&#8217;m going to copy the 12 somewhere.</p>



<p class="wp-block-paragraph">We&#8217;ll deal with it in a second.</p>



<p class="wp-block-paragraph">Then we look for the right child of 21, that&#8217;s a 38.</p>



<p class="wp-block-paragraph">that&#8217;s a 38 that&#8217;s already in the output pattern so we don&#8217;t have to worry about that anymore</p>



<p class="wp-block-paragraph">and then we look at the y node so y had left child was 30 that&#8217;s not accounted for so i&#8217;m</p>



<p class="wp-block-paragraph">going to copy that put it somewhere else and then its right child was 42 that&#8217;s already</p>



<p class="wp-block-paragraph">handled in the output pattern so we&#8217;re done there i&#8217;m going to duplicate this and then we look at</p>



<p class="wp-block-paragraph">the uh we look at the x node the 42 it had a right child of 55 that also was not accounted for so i&#8217;m</p>



<p class="wp-block-paragraph">for so I&#8217;m gonna just copy it over here and we&#8217;ll do something with it okay we&#8217;re done looking for</p>



<p class="wp-block-paragraph">unaccounted for children again if you are just rearranging pointers you don&#8217;t really have to</p>



<p class="wp-block-paragraph">worry about this in your code but since we&#8217;re doing a diagram we have to double check that</p>



<p class="wp-block-paragraph">none of these nodes at the bottom here the unaccounted for children have children of their</p>



<p class="wp-block-paragraph">own if they did the children would have to come with them so 55 uh no children 12 no children 30</p>



<p class="wp-block-paragraph">So those are fine. If there were any children underneath, they would just stay on those nodes.</p>



<p class="wp-block-paragraph">So now we have to figure out who goes where. So the 12 is the least value. That means it&#8217;s going</p>



<p class="wp-block-paragraph">to go probably in the leftmost position compared to the other ones. But if we figure out like,</p>



<p class="wp-block-paragraph">where does this belong in the binary search tree? It belongs right here as the left child of 21.</p>



<p class="wp-block-paragraph">The 30 node, same thing. Where does it belong? It belongs between the 21 and 38. That&#8217;s the only</p>



<p class="wp-block-paragraph">And then the 55, it belongs on the right side of 42 for the same reason.</p>



<p class="wp-block-paragraph">Okay, I&#8217;m going to do my connecting lines real fast.</p>



<p class="wp-block-paragraph">If I don&#8217;t do my connecting lines, then I don&#8217;t like it.</p>



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



<p class="wp-block-paragraph">We have one, two, three, four, five, six.</p>



<p class="wp-block-paragraph">there&#8217;s also just six which means the final tree after rotation is just everything that we&#8217;ve</p>



<p class="wp-block-paragraph">written on the right side so i&#8217;m just going to erase all of this stuff get rid of that too</p>



<p class="wp-block-paragraph">and then move all these nodes over to the left call that the tree this is now the tree and then</p>



<p class="wp-block-paragraph">i just have to recompute the balance factors to make sure that everything is okay so the leaves</p>



<p class="wp-block-paragraph">0, 0, the 42 gets a 1, the 21 is perfectly balanced so that gets a 0.</p>



<p class="wp-block-paragraph">The 38 is also perfectly balanced.</p>



<p class="wp-block-paragraph">Again, it&#8217;s not about weight or mass even though there are more nodes on the left subtree,</p>



<p class="wp-block-paragraph">it&#8217;s all about the height.</p>



<p class="wp-block-paragraph">The height of the left subtree is 2, the height of the right subtree is 2, so 38 is actually</p>



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



<p class="wp-block-paragraph">Because there are no nodes that have a balance factor of 2 or worse, we are done rotating</p>



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



<p class="wp-block-paragraph">This is a valid AVL tree and it&#8217;s pretty fast.</p>



<p class="wp-block-paragraph">So I think that&#8217;s pretty much it.</p>



<p class="wp-block-paragraph">That&#8217;s all I wanted to go through today for this video.</p>



<p class="wp-block-paragraph">Thank you so much for following along.</p>



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



<p class="wp-block-paragraph">And I will see you in the next video.</p>



<p class="wp-block-paragraph">Have a great night and or day and or week and or weekend.</p>



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



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



<p class="wp-block-paragraph">subscribe and follow this channel or these videos or whatever it is you do on</p>



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



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



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



<p class="wp-block-paragraph">just I&#8217;ll be able to keep making videos in general so please do do me a</p>



<p class="wp-block-paragraph">do me a kindness and uh and subscribe you know sometimes i&#8217;m sleeping in the middle of the night</p>



<p class="wp-block-paragraph">and i just wake up because i know somebody subscribed or followed it just wakes me up and</p>



<p class="wp-block-paragraph">i get filled with joy that&#8217;s exactly what happens every single time so you could do it as a nice</p>



<p class="wp-block-paragraph">favor to me or you could you could troll me if you want to just wake me up in the middle of the</p>



<p class="wp-block-paragraph">night just subscribe and then i&#8217;ll i&#8217;ll just wake up i promise that&#8217;s what will happen also uh if</p>



<p class="wp-block-paragraph">you look at the middle of the screen right now you should see a qr code which you can scan</p>



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



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



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



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



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



<p class="wp-block-paragraph">It should all work out according to plan, trust me.</p>



<p class="wp-block-paragraph">Anyway, um, so…</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-trees-adding-linear-data-and-performing-rotations/">AVL Trees: Adding Linear Data and Performing Rotations</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/avl-trees-adding-linear-data-and-performing-rotations/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
