<?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 trees Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/avl-trees/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/avl-trees/</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 trees Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/avl-trees/</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>
		<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>
