<?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>computer science Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/computer-science/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/computer-science/</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>computer science Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/computer-science/</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>How to Perform AVL Tree Rotations &#8211; Step By Step Example</title>
		<link>https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/</link>
					<comments>https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 08 Jun 2026 03:26:17 +0000</pubDate>
				<category><![CDATA[AVL Trees]]></category>
		<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[AVL tree]]></category>
		<category><![CDATA[avl tree example]]></category>
		<category><![CDATA[AVL tree rotation]]></category>
		<category><![CDATA[balance factor]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[double rotation]]></category>
		<category><![CDATA[self balancing tree]]></category>
		<category><![CDATA[tree rotation]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=387</guid>

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



<p class="wp-block-paragraph">In this video we walk through a real example of maintaining an AVL tree by performing a rotation. We start with an unbalanced tree, compute balance factors, identify the Z Y and X nodes, and apply a double right rotation to restore balance. Perfect follow-up if you&#8217;ve seen the basics of binary search trees and AVL trees.</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">Not necessarily a direct child, but just a descendant of the thing that you see that is higher if you have two different nodes that are</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">So we had XYZ. If you don&#8217;t have the rotations memorized, keep in mind my previous videos explain all of that with some fun ways to help you remember, hopefully. Maybe some unfun ways.</p>



<p class="wp-block-paragraph">So I&#8217;ll just kind of like reiterate real fast here. There are two nodes that need to be moved into position. Notice how 55 is supposed to be the new parent node, which means the 65 needs to be rotated under the 55, and so does the 48.</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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

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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">and eh.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-rotations-tutorial-fixing-imbalance-after-adding-a-node/">AVL Tree Rotations Tutorial: Fixing Imbalance After Adding a Node</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/avl-tree-rotations-tutorial-fixing-imbalance-after-adding-a-node/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AVL Tree Rotation Types Explained for Self-Balancing Binary Search Trees</title>
		<link>https://www.NeuralLantern.com/avl-tree-rotation-types-explained-for-self-balancing-binary-search-trees/</link>
					<comments>https://www.NeuralLantern.com/avl-tree-rotation-types-explained-for-self-balancing-binary-search-trees/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 25 May 2026 20:03:52 +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 rotations]]></category>
		<category><![CDATA[AVL tree]]></category>
		<category><![CDATA[avl tree tutorial]]></category>
		<category><![CDATA[balance factor]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[coding interview]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[double left rotation]]></category>
		<category><![CDATA[double right rotation]]></category>
		<category><![CDATA[self balancing binary search tree]]></category>
		<category><![CDATA[single left rotation]]></category>
		<category><![CDATA[single right rotation]]></category>
		<category><![CDATA[tree rotations]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=377</guid>

					<description><![CDATA[<p>In AVL trees, when we find a node with a balance factor of 2 or worse, we perform rotations on the trinode subtree. There are four input patterns that all resolve to the same balanced output pattern through single or double rotations. Single rotations handle straight line imbalances while double rotations address the zigzag cases. Each rotation rearranges parent-child pointers to reduce the height from 3 to 2, helping keep the overall tree balanced for log time operations.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-rotation-types-explained-for-self-balancing-binary-search-trees/">AVL Tree Rotation Types Explained for Self-Balancing Binary Search Trees</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 loading="lazy" title="AVL Tree Rotation Types Explained for Self-Balancing Binary Search Trees" width="1380" height="776" src="https://www.youtube.com/embed/zY8ItrR-j1U?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">Hey everyone, in this video we break down the four types of rotations you need to know for AVL trees &#8211; self-balancing binary search trees.</p>



<p class="wp-block-paragraph">We cover single right rotations, single left rotations, double right rotations, and double left rotations with clear diagrams and explanations of the input patterns and how they become the balanced output pattern.</p>



<p class="wp-block-paragraph">If you&#8217;ve been learning about binary search trees and want to understand how AVL trees maintain their balance through rotations, this is the perfect next step. We look at the trinode subtrees, balance factors, and exactly how to rearrange the pointers for each case.</p>



<p class="wp-block-paragraph">Great for computer science students, coding interviews, or anyone building their data structures knowledge.</p>



<p class="wp-block-paragraph">Watch the full series on binary search trees and AVL trees for more.</p>



<p class="wp-block-paragraph">00:00 Introduction to AVL Tree Rotations<br>00:14 Previous Videos Overview<br>00:28 Balance Factors and Trinode Subtrees<br>00:50 Identifying Imbalance<br>01:56 When to Rotate<br>02:20 Four Input Patterns<br>02:41 Target Balanced Pattern<br>04:18 Single Right Rotation<br>04:33 Single Left Rotation<br>08:44 Double Rotations<br>11:51 Remembering Rotation Types<br>13:04 Wrapping Up Rotations<br>13:27 Thanks and Outro</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">Hey there, let&#8217;s talk about rotation types in an AVL tree, which is a self-balancing binary search tree.</p>



<p class="wp-block-paragraph">Okay, so hopefully you&#8217;ve watched my previous videos. If you have not, you probably want to check them out first,</p>



<p class="wp-block-paragraph">where we talk about what is a binary search tree, how to build one, how to define one,</p>



<p class="wp-block-paragraph">terminology, searching through it, all that stuff. And then we talked about an introduction to AVL trees,</p>



<p class="wp-block-paragraph">trees which are self-balancing binary search trees this video is going to be</p>



<p class="wp-block-paragraph">about the types of rotations that you can perform in an AVL tree what do I</p>



<p class="wp-block-paragraph">mean by rotation well the basic idea that we talked about in the last video</p>



<p class="wp-block-paragraph">was that throughout your tree you&#8217;ll notice that different nodes have</p>



<p class="wp-block-paragraph">different balance factors which means how imbalanced or balanced they are when</p>



<p class="wp-block-paragraph">we identify a node that has a balance factor that&#8217;s too bad then we consider</p>



<p class="wp-block-paragraph">AVL tree and we have to stop and rotate a couple of nodes you know kind of rearrange them to make</p>



<p class="wp-block-paragraph">them better before we have a valid AVL tree so we have four different input patterns that we can</p>



<p class="wp-block-paragraph">have so imagine you know this little trinode subtree up on the top left that could be you know</p>



<p class="wp-block-paragraph">three nodes that we found somewhere in our tree that had a bad balance if you just quickly compute</p>



<p class="wp-block-paragraph">the balance factor for this trinode subtree you&#8217;ll notice actually let me just do it real fast for fun</p>



<p class="wp-block-paragraph">that the 65 node has a balance factor of 2 because its left subtree has a height of 2</p>



<p class="wp-block-paragraph">and there is no right subtree. The 55 has a balance factor of 1 and 48 being a leaf, at</p>



<p class="wp-block-paragraph">least in terms of this trinode subtree, has a balance factor of 0. All of these trinode</p>



<p class="wp-block-paragraph">subtrees are going to have the same situation. The one on top is going to be 2 and so forth.</p>



<p class="wp-block-paragraph">So you can imagine that you were going through the whole tree, computing balance factors like</p>



<p class="wp-block-paragraph">And you found a two, which remember we said in our last video, two or worse, meaning two or higher,</p>



<p class="wp-block-paragraph">if we&#8217;re using balance factors that are absolute or negative two and also positive two or worse,</p>



<p class="wp-block-paragraph">if we&#8217;re not using absolute balance factors, means you got to stop and try to rebalance that trinode subtree.</p>



<p class="wp-block-paragraph">So we found a node that had a balance factor of two and we realized we have to stop and rotate.</p>



<p class="wp-block-paragraph">The pattern of the trino subtree before we perform the rotation could be one of these four things.</p>



<p class="wp-block-paragraph">It could be the thing on the left on top, the thing on the right on top, the thing on the bottom left, and the thing on the bottom right.</p>



<p class="wp-block-paragraph">And all four of these patterns should end up becoming, after we do our rotation, they should end up becoming the pattern in the middle.</p>



<p class="wp-block-paragraph">So this is always going to be the target pattern.</p>



<p class="wp-block-paragraph">Let me draw some arrows real fast.</p>



<p class="wp-block-paragraph">pattern is going to end up looking like the output pattern in the middle.</p>



<p class="wp-block-paragraph">Why is that?</p>



<p class="wp-block-paragraph">How is that possible?</p>



<p class="wp-block-paragraph">Well, we only have three nodes.</p>



<p class="wp-block-paragraph">If you look at all the input patterns, they&#8217;re all the same numbers.</p>



<p class="wp-block-paragraph">What we&#8217;re really doing is disconnecting all the pointers, disconnecting the parent child</p>



<p class="wp-block-paragraph">relationships and then reconnecting them in the best possible way to make, you know,</p>



<p class="wp-block-paragraph">the most balanced trinode subtree.</p>



<p class="wp-block-paragraph">Notice how the height of all of these input trees is always three, right?</p>



<p class="wp-block-paragraph">because it&#8217;s a trinode subtree that&#8217;s really really bad and out of whack so we&#8217;ll you know</p>



<p class="wp-block-paragraph">like one two three one two three right height of three we&#8217;ll do a rotation rearrange the pointers</p>



<p class="wp-block-paragraph">rearrange the parent-child relationships so that the final height ends up being actually just two</p>



<p class="wp-block-paragraph">and if you think about reducing the height we&#8217;re making the tree better we&#8217;re making it more</p>



<p class="wp-block-paragraph">balanced we&#8217;re making it faster to search through and add and remove things from so we&#8217;re making a</p>



<p class="wp-block-paragraph">So we&#8217;re making a better tree by reducing the height a little bit.</p>



<p class="wp-block-paragraph">If you had a really, really, really bad tree and you just went around to all the bad nodes</p>



<p class="wp-block-paragraph">and just started rotating them all, you&#8217;re reducing the height by one for each rotation.</p>



<p class="wp-block-paragraph">Eventually, when you get up to the top of the tree, you&#8217;ll have an overall AVL tree,</p>



<p class="wp-block-paragraph">which is pretty fast.</p>



<p class="wp-block-paragraph">We should say now that AVL trees are always going to be log time trees because the imbalance</p>



<p class="wp-block-paragraph">factor is limited by a constant number.</p>



<p class="wp-block-paragraph">We&#8217;ll say that again in a future video.</p>



<p class="wp-block-paragraph">in a in a future video so anyway i&#8217;m going to label these trees now</p>



<p class="wp-block-paragraph">i&#8217;m going to say that the one on the top left is a single right rotation</p>



<p class="wp-block-paragraph">and the uh the tree on the top right you can probably imagine is a single left rotation</p>



<p class="wp-block-paragraph">let me just duplicate this real fast so i can come on man can i duplicate this yeah so i can</p>



<p class="wp-block-paragraph">edit the text real fast. This is going to be a single left rotation. And then the trees at the</p>



<p class="wp-block-paragraph">bottom, those are going to be double rotations. Let me just write over here, maybe next to that</p>



<p class="wp-block-paragraph">one, double right rotation. And then I&#8217;ll duplicate this. And this one over here is going to be a</p>



<p class="wp-block-paragraph">these on different sides than I usually do because I was trying to make it look a little more tricky</p>



<p class="wp-block-paragraph">but it&#8217;s really not that bad. So how do we know that the top pattern is a single right rotation</p>



<p class="wp-block-paragraph">and not a double right rotation or it&#8217;s a single right rotation and not a single left rotation?</p>



<p class="wp-block-paragraph">The first thing to try to understand is what&#8217;s going to happen between the input pattern and</p>



<p class="wp-block-paragraph">the output pattern. So if you look at the input pattern here we&#8217;ve got like a straight line and</p>



<p class="wp-block-paragraph">And the output pattern is like a perfectly balanced trinode subtree.</p>



<p class="wp-block-paragraph">Notice how the 55 is the new parent.</p>



<p class="wp-block-paragraph">And it&#8217;s also in the middle for the output pattern.</p>



<p class="wp-block-paragraph">And then for the input pattern, the 55 is also in the middle, but it&#8217;s not the new parent.</p>



<p class="wp-block-paragraph">But it is the parent already of the 48 node.</p>



<p class="wp-block-paragraph">Notice how it&#8217;s the parent already of one node, the 48 node.</p>



<p class="wp-block-paragraph">So that means its relationship to the 48 node, the left child, doesn&#8217;t actually change.</p>



<p class="wp-block-paragraph">bit more you&#8217;ll realize that only one node actually needs to be moved into position so we&#8217;ll call this</p>



<p class="wp-block-paragraph">a rotation try to imagine visualize maybe like your point your left pointer finger being placed</p>



<p class="wp-block-paragraph">on the 55 node and your right pointer finger being placed on the 65 node and then just rotate</p>



<p class="wp-block-paragraph">the 65 node downward we rotate that clockwise and the physics people out there love to say that</p>



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



<p class="wp-block-paragraph">At least as far as I remember.</p>



<p class="wp-block-paragraph">Not totally sure about that,</p>



<p class="wp-block-paragraph">but I&#8217;m just going to say it anyway.</p>



<p class="wp-block-paragraph">So notice how the 65 needs to be rotated</p>



<p class="wp-block-paragraph">to the right or clockwise.</p>



<p class="wp-block-paragraph">And only one node actually needs to be rotated</p>



<p class="wp-block-paragraph">to produce that final output pattern.</p>



<p class="wp-block-paragraph">That&#8217;s why we call this a single right rotation</p>



<p class="wp-block-paragraph">because there&#8217;s only one node to rotate.</p>



<p class="wp-block-paragraph">The next thing I&#8217;ll do is just try to give you</p>



<p class="wp-block-paragraph">a few extra ways of being able to remember</p>



<p class="wp-block-paragraph">being able to remember whether it&#8217;s a left or a right so for starters one node needs to be rotated</p>



<p class="wp-block-paragraph">so it&#8217;s a single rotation and we&#8217;re rotating clockwise so it&#8217;s a right rotation but you can</p>



<p class="wp-block-paragraph">also do this you can think of it like can i get rid of that real fast you can think of it like</p>



<p class="wp-block-paragraph">well there is a bunch of empty space kind of sitting here on the right side so that could be</p>



<p class="wp-block-paragraph">another way to remember that this is a right rotation also if you were to start at the node</p>



<p class="wp-block-paragraph">and work your way to the node at the top,</p>



<p class="wp-block-paragraph">you know, the Z node,</p>



<p class="wp-block-paragraph">then we&#8217;re kind of going up and to the right.</p>



<p class="wp-block-paragraph">So that&#8217;s another way to remember</p>



<p class="wp-block-paragraph">that this is a right rotation.</p>



<p class="wp-block-paragraph">Single, you can remember that also</p>



<p class="wp-block-paragraph">by just the fact that this is a straight line.</p>



<p class="wp-block-paragraph">Like notice how the nodes are kind of in a straight line.</p>



<p class="wp-block-paragraph">They don&#8217;t do a zigzag or a bounce.</p>



<p class="wp-block-paragraph">So single rotations are rotations that look straight at first,</p>



<p class="wp-block-paragraph">or I guess input patterns that look straight</p>



<p class="wp-block-paragraph">and only one node actually needs to be rotated.</p>



<p class="wp-block-paragraph">um right versus left is there&#8217;s a pocket of empty space on the right side and you go up and to the</p>



<p class="wp-block-paragraph">right for the right rotations similarly you can probably already figure out by now that</p>



<p class="wp-block-paragraph">the left rotation just means well we&#8217;re going let&#8217;s see we&#8217;re going up and to the left to get</p>



<p class="wp-block-paragraph">to that top node or the only node that has to be rotated is that 48 node and it&#8217;s going to be</p>



<p class="wp-block-paragraph">rotated counterclockwise which is left so again only one node needs to be rotated and we&#8217;re going</p>



<p class="wp-block-paragraph">to be rotated and we&#8217;re going to rotate it counterclockwise which is a left rotation so</p>



<p class="wp-block-paragraph">one node rotated single counterclockwise left or just like a pocket of empty space sitting on the</p>



<p class="wp-block-paragraph">left side okay so then for the double rotations the first thing you can look at is the idea that</p>



<p class="wp-block-paragraph">there&#8217;s a little bit of a bounce you know there&#8217;s like a angle bounce there if you see a bounce or</p>



<p class="wp-block-paragraph">sick a couple weeks ago then that&#8217;s a double rotation another way you can try to remember it</p>



<p class="wp-block-paragraph">as two nodes need to be moved into position so if I maybe I should copy this to another slide real</p>



<p class="wp-block-paragraph">fast let me copy this and we&#8217;ll do another slide okay so you know which nodes need to be moved into</p>



<p class="wp-block-paragraph">position let me copy also the output pattern just as a quick reminder if we wanted to make that</p>



<p class="wp-block-paragraph">If we wanted to make that output pattern, let me put it up here maybe,</p>



<p class="wp-block-paragraph">then obviously the 55 still has to be in the middle</p>



<p class="wp-block-paragraph">because it was already in the middle before we rotated.</p>



<p class="wp-block-paragraph">That means both the 48 and the 65 need to be rotated underneath the 55.</p>



<p class="wp-block-paragraph">So that means we&#8217;re going to do the 65, we&#8217;re going to rotate it clockwise underneath the 55.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do like a little circle here, a connector, and then I&#8217;ll do like 65 there.</p>



<p class="wp-block-paragraph">65 there and then after we do that rotation we have to rotate the 48</p>



<p class="wp-block-paragraph">underneath the 55 so that&#8217;s going to be a rotation that is counterclockwise so</p>



<p class="wp-block-paragraph">I&#8217;m going to do this circle that there and then do a 48 notice how when we&#8217;re</p>



<p class="wp-block-paragraph">done doing the rotations we have a valid binary search tree you should always</p>



<p class="wp-block-paragraph">check your rotated pattern your output pattern to make sure it&#8217;s actually in</p>



<p class="wp-block-paragraph">binary search tree because if it&#8217;s not let&#8217;s say the numbers are out of order for some reason from</p>



<p class="wp-block-paragraph">left to right then you have rotated incorrectly and you have to double check yourself so two nodes</p>



<p class="wp-block-paragraph">need to be rotated into position therefore it&#8217;s a double rotation or you see a diagonal input pattern</p>



<p class="wp-block-paragraph">double rotation how do you remember the difference between left and right well um if you start at the</p>



<p class="wp-block-paragraph">node in the middle just like we did in the last slides we start at the node in the middle and go</p>



<p class="wp-block-paragraph">and go to the node at the top, the Z node,</p>



<p class="wp-block-paragraph">we&#8217;re going up and to the right.</p>



<p class="wp-block-paragraph">And so this is a right rotation.</p>



<p class="wp-block-paragraph">So it&#8217;s a double right rotation.</p>



<p class="wp-block-paragraph">Another way to remember it is you should probably try to rotate the Z node first.</p>



<p class="wp-block-paragraph">The node that&#8217;s on very top is the 65.</p>



<p class="wp-block-paragraph">Rotate that first and then call that the direction of the rotation.</p>



<p class="wp-block-paragraph">So, you know, we rotate the 65 first.</p>



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



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



<p class="wp-block-paragraph">Then just ignore the direction of the other node that you rotated.</p>



<p class="wp-block-paragraph">you can also kind of imagine taking the original node if you don&#8217;t if you don&#8217;t</p>



<p class="wp-block-paragraph">want to think of like actually rotating let me just kind of erase this real fast</p>



<p class="wp-block-paragraph">you can kind of imagine that you are you&#8217;re sort of taking the new parent</p>



<p class="wp-block-paragraph">that&#8217;s supposed to be the new parent and just sort of pulling it up</p>



<p class="wp-block-paragraph">I draw a picture of Batman and he shoots a batarang at it and he sounds like Christian</p>



<p class="wp-block-paragraph">Bale and he&#8217;s like, where is the rotation?</p>



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



<p class="wp-block-paragraph">Maybe if there are enough comments, I&#8217;ll do a special video just for my really, really</p>



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



<p class="wp-block-paragraph">But imagine the 55 is getting pulled up and it&#8217;s getting pulled up past two other nodes.</p>



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



<p class="wp-block-paragraph">Another way to remember.</p>



<p class="wp-block-paragraph">The 55 would be the mob boss that&#8217;s hanging by his ankle.</p>



<p class="wp-block-paragraph">anyway so uh we we kind of know how we can remember the difference between uh single and</p>



<p class="wp-block-paragraph">double rotations uh we&#8217;ve got a single right rotation a single left a double right a double</p>



<p class="wp-block-paragraph">left there&#8217;s only going to be four possible input patterns and they&#8217;re all going to look like this</p>



<p class="wp-block-paragraph">one output pattern because really what we&#8217;re doing is we&#8217;re just we&#8217;re not you know creating</p>



<p class="wp-block-paragraph">any nodes or removing any nodes we&#8217;re just disconnecting pointers we&#8217;re disconnecting</p>



<p class="wp-block-paragraph">just get this over here we&#8217;re saying okay input pattern I disconnect the pointers I&#8217;m saying the</p>



<p class="wp-block-paragraph">65 no longer has a left child the 48 no longer has a parent the 55 no longer has a parent and</p>



<p class="wp-block-paragraph">the 48 no longer has a right child and then when we&#8217;re done with that we just kind of like reconnect</p>



<p class="wp-block-paragraph">the relationships we&#8217;re like okay you know what just kidding you all have parents and children</p>



<p class="wp-block-paragraph">again we&#8217;re just rearranging them okay let me check out my thing real fast here</p>



<p class="wp-block-paragraph">I&#8217;m going to cut the video here because this is the basic idea for the different types of rotations that you could use.</p>



<p class="wp-block-paragraph">In the next videos, we&#8217;re going to actually start rotating bad trees to make them work.</p>



<p class="wp-block-paragraph">So thanks for watching this video.</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">I&#8217;ll see you in the next video.</p>



<p class="wp-block-paragraph">Okay.</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">it. I do hope you did learn something and have some fun. If you could do me a please, a small</p>



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



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



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



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



<p class="wp-block-paragraph">and subscribe. You know sometimes I&#8217;m sleeping in the middle of the night and</p>



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



<p class="wp-block-paragraph">up and I get filled with joy. That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me or you could you could troll me if you</p>



<p class="wp-block-paragraph">want to just wake me up in the middle of the night just subscribe and then I&#8217;ll</p>



<p class="wp-block-paragraph">just wake up. I promise that&#8217;s what will happen. Also if you look at the middle of</p>



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



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



<p class="wp-block-paragraph">is also named somewhere at the bottom of this video and it&#8217;ll take you to my main website where</p>



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



<p class="wp-block-paragraph">things that i offer and all that good stuff and uh if you have a suggestion for uh uh clarifications</p>



<p class="wp-block-paragraph">or errata or just future videos that you want to see please leave a comment or if you just want to</p>



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



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



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



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



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



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



<p class="wp-block-paragraph">Let&#8217;s talk about rotation types in an AVL self-balancing binary search tree.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-rotation-types-explained-for-self-balancing-binary-search-trees/">AVL Tree Rotation Types Explained for Self-Balancing Binary Search Trees</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/avl-tree-rotation-types-explained-for-self-balancing-binary-search-trees/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 loading="lazy" 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>
		<item>
		<title>How to Search in a Binary Search Tree (BST) &#8211; Step by Step Explanation with Examples</title>
		<link>https://www.NeuralLantern.com/how-to-search-in-a-binary-search-tree-bst-step-by-step-explanation-with-examples/</link>
					<comments>https://www.NeuralLantern.com/how-to-search-in-a-binary-search-tree-bst-step-by-step-explanation-with-examples/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sat, 02 May 2026 23:54:59 +0000</pubDate>
				<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[binary search tree]]></category>
		<category><![CDATA[binary search tree tutorial]]></category>
		<category><![CDATA[binary tree search]]></category>
		<category><![CDATA[BST explained]]></category>
		<category><![CDATA[BST search]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[data structures and algorithms]]></category>
		<category><![CDATA[log n time]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[search in BST]]></category>
		<category><![CDATA[tree traversal]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=336</guid>

					<description><![CDATA[<p>Learn how to search in a Binary Search Tree. We demonstrate searching for existing and non-existing values, explain why BST search is O(log n) on average, and show how poor data ordering can turn your tree into a slow linear structure similar to a linked list. Includes discussion of tree height and balanced vs unbalanced BSTs.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-to-search-in-a-binary-search-tree-bst-step-by-step-explanation-with-examples/">How to Search in a Binary Search Tree (BST) &#8211; Step by Step Explanation with Examples</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 loading="lazy" title="How to Search in a Binary Search Tree (BST) - Step by Step Explanation with Examples" width="1380" height="776" src="https://www.youtube.com/embed/FCz9IbLywI4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">In this video we walk through how to perform searches in a Binary Search Tree. Starting with a perfectly balanced BST, we cover the search process step by step, including finding existing nodes like 73 and determining that numbers like 44 do not exist in the tree.</p>



<p class="wp-block-paragraph">We discuss time complexity for searches in BSTs &#8211; O(log n) on average for balanced trees and O(n) in the worst case when the tree becomes skewed like a linked list due to sorted or bad data. Learn why each comparison eliminates half the remaining data and how height affects performance.</p>



<p class="wp-block-paragraph">Perfect for students learning data structures and algorithms. If you&#8217;re studying BSTs, this clear explanation will help you understand searching, insertion paths, and why self-balancing trees matter.</p>



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



<p class="wp-block-paragraph">00:00 Introduction to BST Search<br>00:31 BST Not Self-Balancing<br>00:40 Average Case Log Time<br>00:53 Linear Time in Worst Case<br>01:13 How BST Search Works<br>01:18 Search Path Example<br>02:08 Searching for Existing Node<br>02:35 Searching for Non-Existent Node<br>03:19 Tree Size and Height<br>03:30 Time Complexity O(h)<br>05:40 Bad Data Example<br>08:23 Skewed Tree Like Linked List<br>09:53 Linear Time in Worst Case<br>11:58 Why Log Time<br>12:36 Halving the Search Space<br>14:14 O(log n) Summary<br>14:28 Conclusion</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 perform searches in a binary search tree</p>



<p class="wp-block-paragraph">okay if you watch my previous videos you know how to define a binary search tree by now</p>



<p class="wp-block-paragraph">you know some terminology you know how to construct a tree and so now let&#8217;s just take</p>



<p class="wp-block-paragraph">a tree that&#8217;s already been constructed and let&#8217;s search through it first thing i want to say is</p>



<p class="wp-block-paragraph">first thing I want to say is this is a binary search tree this is not a self</p>



<p class="wp-block-paragraph">balancing tree so in the future this tree could get lopsided and messed up</p>



<p class="wp-block-paragraph">depending on what kind of data we add we can always expect that binary search</p>



<p class="wp-block-paragraph">trees have a search time or an insert time or removal time of log log base 2</p>



<p class="wp-block-paragraph">of n on average assuming that the data is random or in the worst possible case</p>



<p class="wp-block-paragraph">scenario linear time so these could get slower right now you can see this tree</p>



<p class="wp-block-paragraph">Right now you can see this tree is perfectly balanced.</p>



<p class="wp-block-paragraph">So if we say that our tree is currently perfectly balanced and it&#8217;s always going to be perfectly balanced,</p>



<p class="wp-block-paragraph">which is a fantasy, but let&#8217;s just say that it is,</p>



<p class="wp-block-paragraph">then we could just say it&#8217;s a log tree.</p>



<p class="wp-block-paragraph">It&#8217;s going to take log time to search through.</p>



<p class="wp-block-paragraph">So how do we perform searches?</p>



<p class="wp-block-paragraph">Again, if you saw my previous videos, you should know that it&#8217;s really easy to find out where a node belongs.</p>



<p class="wp-block-paragraph">Like for example, if I was going to add the number 14,</p>



<p class="wp-block-paragraph">I would first look at the root node and I&#8217;d say, okay, the root node is occupied.</p>



<p class="wp-block-paragraph">and I&#8217;d say, okay, the root node is occupied.</p>



<p class="wp-block-paragraph">And so where would 14 belong?</p>



<p class="wp-block-paragraph">Well, it would belong on the left of 37</p>



<p class="wp-block-paragraph">because it&#8217;s less than 37.</p>



<p class="wp-block-paragraph">It would belong on the left of 16</p>



<p class="wp-block-paragraph">because it&#8217;s less than 16.</p>



<p class="wp-block-paragraph">It would belong on the right of 9</p>



<p class="wp-block-paragraph">because it&#8217;s greater than 9.</p>



<p class="wp-block-paragraph">It would belong on the right of 11</p>



<p class="wp-block-paragraph">because it&#8217;s greater than 11.</p>



<p class="wp-block-paragraph">And so the 14 node that we just wanted to add,</p>



<p class="wp-block-paragraph">is that what I just said, 14?</p>



<p class="wp-block-paragraph">Would end up going there.</p>



<p class="wp-block-paragraph">So when you&#8217;re searching through a binary search tree,</p>



<p class="wp-block-paragraph">you really just have to go find</p>



<p class="wp-block-paragraph">have to go find where the node would belong as if you&#8217;re performing insert or you could think of it</p>



<p class="wp-block-paragraph">the other way around like insert is really thinking of a search first but basically we&#8217;re just going</p>



<p class="wp-block-paragraph">to start by saying you know pick a number and we&#8217;ll see if it exists so uh let&#8217;s pick the number</p>



<p class="wp-block-paragraph">67 or actually let&#8217;s pick the number 73 just to see if it exists in the tree so 73 i&#8217;m going to</p>



<p class="wp-block-paragraph">do like you know seven three with like a question mark we check the root node 73 belongs on the</p>



<p class="wp-block-paragraph">side we look at the 59 it belongs on the right side and we found our 73 so 73 exists in the tree</p>



<p class="wp-block-paragraph">and it only took us three examinations only three examinations let&#8217;s look for a number that doesn&#8217;t</p>



<p class="wp-block-paragraph">actually exist in the tree so maybe oh my thing is freezing what&#8217;s going on okay so maybe uh let&#8217;s</p>



<p class="wp-block-paragraph">The 37 here, 44 belongs on the right of 37.</p>



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



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



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



<p class="wp-block-paragraph">The 44 would have been there, but it&#8217;s not because that right child pointer off the 43</p>



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



<p class="wp-block-paragraph">So we only had to really examine one, two, three, four nodes in order to determine that</p>



<p class="wp-block-paragraph">our number 44 didn&#8217;t exist in the tree.</p>



<p class="wp-block-paragraph">Notice how we&#8217;re not doing a lot of examinations compared to the total size of the tree.</p>



<p class="wp-block-paragraph">compared to the total size of the tree.</p>



<p class="wp-block-paragraph">So let me just write this up here.</p>



<p class="wp-block-paragraph">How many nodes do we have?</p>



<p class="wp-block-paragraph">1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15.</p>



<p class="wp-block-paragraph">We got 15 nodes up here.</p>



<p class="wp-block-paragraph">Someone say N is equal to 15.</p>



<p class="wp-block-paragraph">And what&#8217;s the height of the tree?</p>



<p class="wp-block-paragraph">Remember, the height of the tree is the number of nodes you must touch</p>



<p class="wp-block-paragraph">as you make your way down towards the deepest node.</p>



<p class="wp-block-paragraph">Or if you&#8217;ve already found the deepest node,</p>



<p class="wp-block-paragraph">you take its depth and you just add 1, assuming that depths start at 0.</p>



<p class="wp-block-paragraph">start at zero. So I&#8217;ll just say that the depth of the root node is zero and the 59 and the 16 are</p>



<p class="wp-block-paragraph">one. And then that third row is two. And that last row is three. And then I just add one to the</p>



<p class="wp-block-paragraph">deepest node that I can find, which is just going to have a depth of three. All of these on the</p>



<p class="wp-block-paragraph">bottom row have a depth of three. So the height of this tree is actually four, because we would</p>



<p class="wp-block-paragraph">so like 37 59 48 43 that&#8217;s four touches um or the or the depth of the deepest node plus one okay um</p>



<p class="wp-block-paragraph">all trees all binary search trees whether they&#8217;re actually avl trees or regular binary search trees</p>



<p class="wp-block-paragraph">or whether they&#8217;re balanced or imbalanced they&#8217;re always going to have a time complexity to search</p>



<p class="wp-block-paragraph">through of o of h meaning in the worst case scenario you&#8217;ll have to touch h number of nodes</p>



<p class="wp-block-paragraph">order to find a node by search query or to add a new node or even to delete a new node.</p>



<p class="wp-block-paragraph">So how does that compare to this tree in reality?</p>



<p class="wp-block-paragraph">Let me just open up a little calculator real fast.</p>



<p class="wp-block-paragraph">I just want to show you.</p>



<p class="wp-block-paragraph">So if we do log base two, because we have a binary search tree, not a trinary search</p>



<p class="wp-block-paragraph">nodes 15 it tells us that it should take us no more than four examinations to find the node that</p>



<p class="wp-block-paragraph">we&#8217;re looking for if the tree was perfectly balanced and if you if you if you notice that&#8217;s</p>



<p class="wp-block-paragraph">exactly what the height is telling us here if the tree was more imbalanced the height would get</p>



<p class="wp-block-paragraph">really bad compared to the number of nodes and it would no longer be considered a log tree it would</p>



<p class="wp-block-paragraph">just be you know somewhere in between linear time and log time um let&#8217;s see what else can i show you</p>



<p class="wp-block-paragraph">let&#8217;s do well we did that computation let&#8217;s do a search through the tree that</p>



<p class="wp-block-paragraph">through a tree that&#8217;s actually really really bad let&#8217;s pretend that we have a</p>



<p class="wp-block-paragraph">bunch of nodes here and you know I&#8217;ve got an old story that I love to tell</p>



<p class="wp-block-paragraph">about this when like when my grandma was alive she was kind of bitter towards the</p>



<p class="wp-block-paragraph">end you know I loved her but she used to call the cops on her neighbors for</p>



<p class="wp-block-paragraph">gossip and at some point in time she got into a feud with her neighbor who was</p>



<p class="wp-block-paragraph">with her neighbor who was also a bitter old lady.</p>



<p class="wp-block-paragraph">You know, towards the end, it kind of happens sometimes.</p>



<p class="wp-block-paragraph">It sucks, but it happened.</p>



<p class="wp-block-paragraph">And I guess the neighbor didn&#8217;t like my grandma&#8217;s tree</p>



<p class="wp-block-paragraph">that she had in her backyard.</p>



<p class="wp-block-paragraph">So they fought over it, they argued over it,</p>



<p class="wp-block-paragraph">and then one day my grandma said</p>



<p class="wp-block-paragraph">she was looking out the backyard window</p>



<p class="wp-block-paragraph">to yell at ducks who were jumping into her pool</p>



<p class="wp-block-paragraph">because she just hated when the ducks went into her pool.</p>



<p class="wp-block-paragraph">She was waiting for them to show up.</p>



<p class="wp-block-paragraph">She wasn&#8217;t even looking at ducks.</p>



<p class="wp-block-paragraph">even looking at ducks she was just waiting and she saw the other little old lady reach her arm over</p>



<p class="wp-block-paragraph">the fence and spray my grandma&#8217;s tree with some sort of poison then my grandma&#8217;s tree died and uh</p>



<p class="wp-block-paragraph">i guess you know my grandma never never got over it i always thought it was like a funny story it</p>



<p class="wp-block-paragraph">was a little sad but um she poisoned my grandma&#8217;s tree so we can kind of do the same thing with the</p>



<p class="wp-block-paragraph">binary search tree we can give bad data let me show you what happens here if i use this data set</p>



<p class="wp-block-paragraph">search tree. So we&#8217;re going to add this node right here. It&#8217;s going to be the 14.</p>



<p class="wp-block-paragraph">And then when we add the 19 node, I&#8217;m just going to duplicate it. The 19 is supposed to be on the</p>



<p class="wp-block-paragraph">right side of 14 because it&#8217;s greater than 14. So I&#8217;m just going to like, you know, draw a 19 right</p>



<p class="wp-block-paragraph">here and a little connecting line. Okay. Then when we add the 24, I&#8217;m going to duplicate this node</p>



<p class="wp-block-paragraph">the 24 go belongs on the right side of the 14 and also belongs on the right side of the 19</p>



<p class="wp-block-paragraph">because it&#8217;s greater than 19 so again we have a node that shows up on the right side let me</p>



<p class="wp-block-paragraph">remember to actually fix the 19 so it looks like 24. then when we add the uh 29 it&#8217;s gonna end up</p>



<p class="wp-block-paragraph">being the same thing right the 29 is gonna end up showing on the right the right the right it&#8217;s</p>



<p class="wp-block-paragraph">If we have bad data, we can end up with a tree that kind of looks like a straight line.</p>



<p class="wp-block-paragraph">And if you are watching this video from the future,</p>



<p class="wp-block-paragraph">after I&#8217;ve posted all of my data structures videos that I have,</p>



<p class="wp-block-paragraph">you might notice that this looks like another data structure.</p>



<p class="wp-block-paragraph">If you&#8217;re watching this right away, I haven&#8217;t posted those videos just yet.</p>



<p class="wp-block-paragraph">But just tilt your head to the right a little bit</p>



<p class="wp-block-paragraph">and tell me if that looks like something that you recognize.</p>



<p class="wp-block-paragraph">It looks like a linked list.</p>



<p class="wp-block-paragraph">looks like a linked list just like a straight line or even i guess you could kind of say maybe an array</p>



<p class="wp-block-paragraph">um just like a straight line of data there&#8217;s no uh you know there&#8217;s no splitting of the data</p>



<p class="wp-block-paragraph">so we have the 29 and then the 35 and then uh maybe i&#8217;ll just do one more node only and i&#8217;ll</p>



<p class="wp-block-paragraph">get rid of that 54 because that&#8217;s too many nodes so what happens is if you have you know bad data</p>



<p class="wp-block-paragraph">tree doesn&#8217;t actually search or add insert or remove or anything like that in log time anymore</p>



<p class="wp-block-paragraph">instead it operates in linear time just like a linked list would meaning we would have to scan</p>



<p class="wp-block-paragraph">through every single node in the entire tree to be sure that some value did not exist well</p>



<p class="wp-block-paragraph">not exactly but in the worst case scenario we&#8217;d have to scan through the whole entire tree</p>



<p class="wp-block-paragraph">for example let&#8217;s just look at the the number of nodes real fast we&#8217;ll say the</p>



<p class="wp-block-paragraph">number of nodes here is one two three four oh I put two 24s in there oh dearie</p>



<p class="wp-block-paragraph">let me get that says 14 19 24 29 and then I guess I got to make that a 35 oh</p>



<p class="wp-block-paragraph">okay and then there&#8217;s a 49 I guess I did add every single node okay so we added</p>



<p class="wp-block-paragraph">so there&#8217;s like seven uh items in the tree so i&#8217;m going to say n is equal to seven the height also</p>



<p class="wp-block-paragraph">is seven though so the height is seven notice how if we say that it&#8217;s always true that a binary</p>



<p class="wp-block-paragraph">search tree has a time complexity in the worst case of o of h meaning you&#8217;d have to go down</p>



<p class="wp-block-paragraph">in the worst case the number of levels that is equal to the whole entire height of the entire</p>



<p class="wp-block-paragraph">tree that&#8217;s also the number of nodes in the tree which means uh if i wanted to add let&#8217;s say the</p>



<p class="wp-block-paragraph">I don&#8217;t know, if you wanted to add the number 60,</p>



<p class="wp-block-paragraph">60 would end up going down here.</p>



<p class="wp-block-paragraph">And in order to find where 60 would go,</p>



<p class="wp-block-paragraph">we&#8217;d have to look at the 14, that&#8217;s 1, 19, 24.</p>



<p class="wp-block-paragraph">We&#8217;d have to look at all these nodes.</p>



<p class="wp-block-paragraph">That is seven nodes.</p>



<p class="wp-block-paragraph">We&#8217;d have to examine seven nodes</p>



<p class="wp-block-paragraph">just to figure out where the 60 would go.</p>



<p class="wp-block-paragraph">Or if we were searching for 60,</p>



<p class="wp-block-paragraph">then we&#8217;d have to go down that far</p>



<p class="wp-block-paragraph">to find out that 60 didn&#8217;t exist.</p>



<p class="wp-block-paragraph">So this is nowhere near a log tree.</p>



<p class="wp-block-paragraph">This is a linear tree, a linear time tree.</p>



<p class="wp-block-paragraph">year time tree and just to drive the point further if we did log base two of the number of nodes in</p>



<p class="wp-block-paragraph">this case seven notice how it&#8217;s telling us that we should if it was a log tree if it was perfectly</p>



<p class="wp-block-paragraph">balanced or a self-balancing tree we should have to touch no more than three nodes in order to find</p>



<p class="wp-block-paragraph">what we&#8217;re looking for to search through the tree or to insert a new node or even to delete a node</p>



<p class="wp-block-paragraph">but that&#8217;s not the case here because the tree is really really slow compared to a perfectly balanced</p>



<p class="wp-block-paragraph">slow compared to a perfectly balanced tree.</p>



<p class="wp-block-paragraph">So we call this a linear tree.</p>



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



<p class="wp-block-paragraph">You want to try to avoid that.</p>



<p class="wp-block-paragraph">You don&#8217;t want your data to be poisoned.</p>



<p class="wp-block-paragraph">On average, your trees won&#8217;t really look like this if you just have totally</p>



<p class="wp-block-paragraph">random data, but if you think there&#8217;s a chance that your data might be skewed</p>



<p class="wp-block-paragraph">in some way, it&#8217;s probably a good idea to upgrade to a self-balancing tree,</p>



<p class="wp-block-paragraph">which we&#8217;re going to talk about later.</p>



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



<p class="wp-block-paragraph">What else can I tell you real fast?</p>



<p class="wp-block-paragraph">So we talked about searching.</p>



<p class="wp-block-paragraph">why are these trees considered log time? I mean, they&#8217;re really, really fast, but why?</p>



<p class="wp-block-paragraph">Let&#8217;s just duplicate this slide here and I&#8217;ll move it down one level. I just want to kind of</p>



<p class="wp-block-paragraph">show you why this ends up being a log time tree. Okay. So why, why is this a log time tree?</p>



<p class="wp-block-paragraph">Suppose for the sake of argument, we&#8217;re searching for the number 52. So if we&#8217;re searching for the</p>



<p class="wp-block-paragraph">node the root node and decide where would the 52 belong let me actually write that down so i don&#8217;t</p>



<p class="wp-block-paragraph">forget 52 question mark where would the 52 belong it would belong on the right side of the 37 because</p>



<p class="wp-block-paragraph">it&#8217;s greater than 37 right which means it&#8217;s actually impossible that the 52 would be in any</p>



<p class="wp-block-paragraph">of these other nodes on the left side there&#8217;s no need to check them we&#8217;re not going to scan the</p>



<p class="wp-block-paragraph">whole tree we&#8217;re just going to go left or right and ignore the whole other side of the tree</p>



<p class="wp-block-paragraph">we just did is with one examination we eliminated half of the remaining data set or i guess of the</p>



<p class="wp-block-paragraph">initial data set if we&#8217;re looking at the root node so then the next thing that we do is we just kind</p>



<p class="wp-block-paragraph">of say okay it belongs on the right side so we look at the 59 node and we do the same thing we</p>



<p class="wp-block-paragraph">say where would 52 be would it be on the left side or the right side of the 59 node it would be on</p>



<p class="wp-block-paragraph">the left side which means we&#8217;re going to eliminate from consideration the entire right subtree of the</p>



<p class="wp-block-paragraph">So again, we&#8217;re eliminating half of the remaining data set.</p>



<p class="wp-block-paragraph">Then we kind of go down here and we look at the 48.</p>



<p class="wp-block-paragraph">We do the same thing.</p>



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



<p class="wp-block-paragraph">So we eliminate half of the remaining data set, you know, and then eventually we do find</p>



<p class="wp-block-paragraph">the 52 and then that&#8217;s it.</p>



<p class="wp-block-paragraph">So if we&#8217;re eliminating half of the data set with every single, just one examination at</p>



<p class="wp-block-paragraph">a time, that&#8217;s why it ends up being log base two.</p>



<p class="wp-block-paragraph">getting twice as close to your destination at every step you took if you&#8217;re like in a car like</p>



<p class="wp-block-paragraph">running maybe you&#8217;re on the moon or something then it would be a log time you know uh progress</p>



<p class="wp-block-paragraph">towards your goal or eliminating half of the data set at every single step sometimes people see</p>



<p class="wp-block-paragraph">for loops on exams and stuff when we talk about this um like for i equals zero uh i is less than</p>



<p class="wp-block-paragraph">it would be like i times equals two.</p>



<p class="wp-block-paragraph">So i&#8217;s jumping twice as far each time.</p>



<p class="wp-block-paragraph">That&#8217;s the sort of thing that makes it log.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to write down log here real fast</p>



<p class="wp-block-paragraph">to drive the point home a little bit more.</p>



<p class="wp-block-paragraph">O of log base two of n.</p>



<p class="wp-block-paragraph">Yeah, so I guess that&#8217;s all I wanted to tell you in this video.</p>



<p class="wp-block-paragraph">Hopefully you feel like an expert in searching now.</p>



<p class="wp-block-paragraph">If you want to see more searching videos,</p>



<p class="wp-block-paragraph">leave a comment or something like that.</p>



<p class="wp-block-paragraph">leave a comment or something like that. But yeah, thank you for watching. And I hope you</p>



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



<p class="wp-block-paragraph">I&#8217;m going to go find a blueberry muffin.</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">appreciate it. I do hope you did learn something and have some fun. If you could do me a please,</p>



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



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



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



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



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



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy. That&#8217;s exactly what happens every single time</p>



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



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



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



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



<p class="wp-block-paragraph">Which I think is also named somewhere at the bottom of this video and it&#8217;ll take you to my main website where you can just kind of like</p>



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



<p class="wp-block-paragraph">if you have a suggestion for</p>



<p class="wp-block-paragraph">Clarifications or errata or just future videos that you want to see please leave a comment or if you just want to say hey</p>



<p class="wp-block-paragraph">What&#8217;s up? What&#8217;s going on? You know, just send me a comment, whatever</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. I&#8217;m like this</p>



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



<p class="wp-block-paragraph">So again, thank you so much for watching this video and enjoy the cool music as I fade into the darkness, which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-to-search-in-a-binary-search-tree-bst-step-by-step-explanation-with-examples/">How to Search in a Binary Search Tree (BST) &#8211; Step by Step Explanation with Examples</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/how-to-search-in-a-binary-search-tree-bst-step-by-step-explanation-with-examples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Binary Search Tree Insertion Tutorial: Building a BST from Scratch Step by Step</title>
		<link>https://www.NeuralLantern.com/binary-search-tree-insertion-tutorial-building-a-bst-from-scratch-step-by-step/</link>
					<comments>https://www.NeuralLantern.com/binary-search-tree-insertion-tutorial-building-a-bst-from-scratch-step-by-step/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 27 Apr 2026 06:42:54 +0000</pubDate>
				<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[binary search tree]]></category>
		<category><![CDATA[binary search tree tutorial]]></category>
		<category><![CDATA[binary tree]]></category>
		<category><![CDATA[BST insertion]]></category>
		<category><![CDATA[BST insertion step by step]]></category>
		<category><![CDATA[build BST from scratch]]></category>
		<category><![CDATA[coding interview prep]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[inorder traversal]]></category>
		<category><![CDATA[self balancing trees]]></category>
		<category><![CDATA[time complexity BST]]></category>
		<category><![CDATA[tree height]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=333</guid>

					<description><![CDATA[<p>Learn how to build a binary search tree (BST) by inserting numbers one by one. Follow the insertion process for the sequence 88, 21, 3, 6, 72, 34, 11, 1, 90, 65, 55, 17, 23, 9. See left/right decisions, pointer updates, inorder verification for sorted order, tree height calculation (6), and discussion of O(h) time complexity versus log n for balanced trees. Clear explanations without skipping steps.</p>
<p>The post <a href="https://www.NeuralLantern.com/binary-search-tree-insertion-tutorial-building-a-bst-from-scratch-step-by-step/">Binary Search Tree Insertion Tutorial: Building a BST from Scratch Step by Step</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 loading="lazy" title="Binary Search Tree Insertion Tutorial: Building a BST from Scratch Step by Step" width="1380" height="776" src="https://www.youtube.com/embed/-vtcFOEkrEk?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">In this hands-on tutorial, we build a binary search tree (BST) from scratch by inserting numbers one by one: 88, 21, 3, 6, 72, 34, 11, 1, 90, 65, 55, 17, 23, and 9. Watch as each node finds its correct position following BST rules, with clear explanations of left and right child decisions, parent pointers, and why the tree ends up looking a bit wonky but is still valid.</p>



<p class="wp-block-paragraph">We cover the full insertion process, verify the tree is a proper BST by checking inorder traversal (sorted order), confirm it&#8217;s a connected acyclic rooted binary tree, and discuss tree height, time complexity (O(h) where h=6 for this tree), and why self-balancing trees matter for better performance.</p>



<p class="wp-block-paragraph">Great practice for computer science students, programmers learning data structures, or anyone prepping for coding interviews. No fancy animations &#8211; just real step-by-step drawing and reasoning.</p>



<p class="wp-block-paragraph">If you want more BST practice videos or specific insertion sequences, drop a comment below!</p>



<p class="wp-block-paragraph">00:00 Introduction to BST Practice<br>00:35 Insertion Sequence Overview<br>00:59 Rules for Building BST<br>01:30 Insert 88 as Root<br>02:20 Continue building the tree from input data<br>13:04 Finish building the tree<br>14:56 Clean Up Diagram<br>15:24 Verify Inorder Traversal<br>16:48 Confirm Tree Properties<br>19:23 Tree Height and Time Complexity<br>22:01 Search Example for 55<br>23:09 Log Time vs Actual Height<br>24:21 Conclusion and Future Topics</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 practice constructing a binary search tree from scratch.</p>



<p class="wp-block-paragraph">In my previous video, we actually did this already, and in other previous videos we defined</p>



<p class="wp-block-paragraph">what is a binary search tree and some terminology and stuff like that, but I thought it would</p>



<p class="wp-block-paragraph">be nice to have two practice videos up. I&#8217;m willing to put more up if people want to make</p>



<p class="wp-block-paragraph">comments about more trees that they want to see inserted. For now, I&#8217;m just going to say</p>



<p class="wp-block-paragraph">we will add the following sequence of numbers. So this is a little diagram from the last time we</p>



<p class="wp-block-paragraph">did this. Let me just, oh, I&#8217;m supposed to make that full screen so it can be bigger. Whoops.</p>



<p class="wp-block-paragraph">Let me go ahead and add a new slide here. And then I&#8217;m going to do,</p>



<p class="wp-block-paragraph">Okay, so we&#8217;re going to try to add the following sequence of numbers.</p>



<p class="wp-block-paragraph">Like I said in my other video, don&#8217;t try to make the tree prettier.</p>



<p class="wp-block-paragraph">Don&#8217;t get mad if a number looks like it&#8217;s too far to the left or something.</p>



<p class="wp-block-paragraph">Just follow the normal rules of letting a node drop wherever it is supposed to drop</p>



<p class="wp-block-paragraph">according to the rules of building a binary search tree.</p>



<p class="wp-block-paragraph">So the first thing that I&#8217;m going to do is maybe draw a little arrow here.</p>



<p class="wp-block-paragraph">I&#8217;m going to do like a pink arrow to indicate, you know,</p>



<p class="wp-block-paragraph">You know, well, maybe I&#8217;ll do like a red one to indicate which number we&#8217;re actually trying to add at the moment.</p>



<p class="wp-block-paragraph">So it&#8217;s going to be the 88 first.</p>



<p class="wp-block-paragraph">Let&#8217;s see, we&#8217;ll put that right there.</p>



<p class="wp-block-paragraph">And then now let&#8217;s say that the 88 is going to drop into our tree somewhere.</p>



<p class="wp-block-paragraph">First question we have to ask ourselves is do we have a root node?</p>



<p class="wp-block-paragraph">We don&#8217;t have a root node, which means the number we&#8217;re trying to add should be the root node.</p>



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



<p class="wp-block-paragraph">So I&#8217;m going to draw like a little circle here, a little node.</p>



<p class="wp-block-paragraph">and I&#8217;m going to stick the number 88 inside of it we&#8217;re using integer t types</p>



<p class="wp-block-paragraph">if you recall the last videos these nodes are supposed to be able to hold</p>



<p class="wp-block-paragraph">almost any data type that you want they don&#8217;t have to be numbers but I&#8217;m just</p>



<p class="wp-block-paragraph">going to use numbers for the sake of this example because it&#8217;s easier so I&#8217;m</p>



<p class="wp-block-paragraph">going to say this is our root node it&#8217;s at the very top of the tree and we&#8217;re</p>



<p class="wp-block-paragraph">done adding 88 and that&#8217;s it next step we&#8217;re going to add the 21 so I&#8217;m just</p>



<p class="wp-block-paragraph">forward a little bit here and the 21 same question do we have a root node we currently do so that</p>



<p class="wp-block-paragraph">means we can&#8217;t actually put the 21 there so we have to make a decision does the 21 belong on the</p>



<p class="wp-block-paragraph">left or the right of the 88 the 21 is less than 88 which means it belongs on the left side so i&#8217;m</p>



<p class="wp-block-paragraph">just going to duplicate this and try to make a pretty diagram by sort of splitting the difference</p>



<p class="wp-block-paragraph">in terms of the empty space on the left between the edge of the screen and the existing root node</p>



<p class="wp-block-paragraph">and make sure that it goes down, you know, a full rank, you know, at least the full height of a node,</p>



<p class="wp-block-paragraph">maybe a little bit more, but you want to keep all same depth nodes on the same Y coordinate.</p>



<p class="wp-block-paragraph">It&#8217;s just easier to debug your diagrams that way.</p>



<p class="wp-block-paragraph">This one&#8217;s actually probably going to get messy. I might have to readjust this a few times.</p>



<p class="wp-block-paragraph">I think I recalled these were, these might&#8217;ve been bad numbers. I can&#8217;t remember.</p>



<p class="wp-block-paragraph">Okay. So the 21 is going to be the left child of the 88 because it belongs on the left side.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to do my little connecting line here.</p>



<p class="wp-block-paragraph">connecting line here now 21 has a parent of 88 and 88 has a left child of 21 and we&#8217;re done adding</p>



<p class="wp-block-paragraph">the 21 so i&#8217;m going to move the arrow to the three we&#8217;re now trying to add the three node</p>



<p class="wp-block-paragraph">again we look at the root node we say okay that&#8217;s occupied so we can&#8217;t use that does three belong on</p>



<p class="wp-block-paragraph">the left or the right of the 88 well it&#8217;s less than 88 so it belongs on the left the left child</p>



<p class="wp-block-paragraph">again, does the three belong on the left or the right of the 21? It belongs on the left</p>



<p class="wp-block-paragraph">because it&#8217;s less than 21 and the left child of 21 is currently doesn&#8217;t exist. It&#8217;s null.</p>



<p class="wp-block-paragraph">So that means the three is going to be the new left child of the 21 node. Maybe just put it</p>



<p class="wp-block-paragraph">like right there, put the three there. And then I&#8217;m going to do the connecting line in your code.</p>



<p class="wp-block-paragraph">You can imagine that the left child pointer of the 21 node has now been set. So it&#8217;s pointing</p>



<p class="wp-block-paragraph">been set so it&#8217;s pointing to the brand new node we just made for the three and then inside of the</p>



<p class="wp-block-paragraph">three node its parent pointer is pointing to the 21 node okay so we&#8217;re done adding the three we</p>



<p class="wp-block-paragraph">move on to the six same thing that we did before we look at the root node uh the six belongs on</p>



<p class="wp-block-paragraph">the left so i&#8217;m gonna say go to the left the 21 node is it&#8217;s there it&#8217;s occupied so we have to</p>



<p class="wp-block-paragraph">left of 21 because it&#8217;s less than 21 we go down to the left then we look at the three that&#8217;s also</p>



<p class="wp-block-paragraph">occupied does the six belong on the left or right of three it belongs on the right side of three</p>



<p class="wp-block-paragraph">because it&#8217;s greater than three so that means the sixth node is going to be the right child</p>



<p class="wp-block-paragraph">of three again try to split the difference these diagrams get ugly fast i really</p>



<p class="wp-block-paragraph">i don&#8217;t i don&#8217;t want to rearrange the topology but i&#8217;m probably gonna have to</p>



<p class="wp-block-paragraph">I think I&#8217;m like crashing out here.</p>



<p class="wp-block-paragraph">Oh, no.</p>



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



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



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



<p class="wp-block-paragraph">Let me try one more time.</p>



<p class="wp-block-paragraph">These lines are killing me.</p>



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



<p class="wp-block-paragraph">I&#8217;m just going to update the three to a six and leave a little gap on that line.</p>



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



<p class="wp-block-paragraph">Now we&#8217;re ready to try to add the 72 node.</p>



<p class="wp-block-paragraph">We have a little relief here because this is not like a smaller and smaller number that we&#8217;re dealing with.</p>



<p class="wp-block-paragraph">So, well, we just look at the root node.</p>



<p class="wp-block-paragraph">72 belongs on the left side so I&#8217;m going to hop to the left here we then look at</p>



<p class="wp-block-paragraph">the 21 72 belongs on the right side because it&#8217;s greater than 21 so that</p>



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



<p class="wp-block-paragraph">make a duplicate try to keep the three and the six at the same you know y</p>



<p class="wp-block-paragraph">coordinate because they&#8217;re on the same descendancy rank they&#8217;re at the same</p>



<p class="wp-block-paragraph">you know generation whatever you want to call it so I&#8217;m going to put the 72 on</p>



<p class="wp-block-paragraph">And then I&#8217;m going to do our connecting line, indicating that both nodes now have pointers that refer to each other.</p>



<p class="wp-block-paragraph">Okay, we&#8217;re done adding the 72.</p>



<p class="wp-block-paragraph">I&#8217;m going to move this to the right a little bit.</p>



<p class="wp-block-paragraph">And then, oops, get rid of that little dot.</p>



<p class="wp-block-paragraph">Now we&#8217;re ready to add the 34.</p>



<p class="wp-block-paragraph">So the 34, we look at the 88 node occupied.</p>



<p class="wp-block-paragraph">We go left.</p>



<p class="wp-block-paragraph">We look at the 21 node.</p>



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



<p class="wp-block-paragraph">of 21 so then we look at the 72 34 belongs on the left side of 72 so that means 34 is going to be</p>



<p class="wp-block-paragraph">the left child of 72 so I&#8217;m going to select one of these nodes and duplicate it and say that</p>



<p class="wp-block-paragraph">you have a left child of 72 now and it&#8217;s going to be the 34 node do my pointer connection real fast</p>



<p class="wp-block-paragraph">I haven&#8217;t been duplicating the pages. Okay. So, uh, I guess that&#8217;s just for me.</p>



<p class="wp-block-paragraph">We&#8217;ll move on to adding the 11 node.</p>



<p class="wp-block-paragraph">So we add the 11 node by looking at the root node first.</p>



<p class="wp-block-paragraph">11 belongs on the left side. We then look at the 21,</p>



<p class="wp-block-paragraph">11 belongs on the left of 21. Go to the left here. We look at the three,</p>



<p class="wp-block-paragraph">11 belongs on the right of the three. We go to the right,</p>



<p class="wp-block-paragraph">11 belongs on the right of the six node.</p>



<p class="wp-block-paragraph">This is definitely a gross graph at this point.</p>



<p class="wp-block-paragraph">at this point. So I&#8217;m going to duplicate this node here and try to split the difference between</p>



<p class="wp-block-paragraph">the six and the 21 in terms of like the free space. So I&#8217;m just going to drag this down a</p>



<p class="wp-block-paragraph">little bit there and say that 11 goes as the new right child of the six node. So</p>



<p class="wp-block-paragraph">just do that. This is a gross diagram. I might want to redraw this later.</p>



<p class="wp-block-paragraph">Now we&#8217;re ready to try to add the one node.</p>



<p class="wp-block-paragraph">the one again we look at the root node of the tree and it&#8217;s occupied so we can&#8217;t put it there</p>



<p class="wp-block-paragraph">we go to the left we go to the left again and the one belongs on the left of the three nodes so we</p>



<p class="wp-block-paragraph">have a little bit more room there so that&#8217;s good i&#8217;m gonna copy this node and make it the new left</p>



<p class="wp-block-paragraph">child of the three i&#8217;m gonna like say that our one is right there and then i&#8217;m gonna do the connecting line</p>



<p class="wp-block-paragraph">okay all right we&#8217;re done adding the one now I&#8217;m gonna add the 90 node oh this is</p>



<p class="wp-block-paragraph">gonna be nice and easy because the 90 definitely belongs on the right side of</p>



<p class="wp-block-paragraph">the 80 node whoops what happened okay sorry for making that noise I&#8217;m gonna</p>



<p class="wp-block-paragraph">duplicate this node here and make it the right child of the 88 node so let&#8217;s see</p>



<p class="wp-block-paragraph">kind of even there. So we&#8217;re going to put 90 right there. And then I&#8217;m going to do a connecting line</p>



<p class="wp-block-paragraph">in blue. Now we&#8217;re done adding the 90.</p>



<p class="wp-block-paragraph">So then we&#8217;re going to add the 65. 65 belongs on the left of the 88 node.</p>



<p class="wp-block-paragraph">So we&#8217;re kind of in left territory now. It&#8217;s kind of sucky again.</p>



<p class="wp-block-paragraph">And bounce down to the left. 66 belongs on the right side of the 21 node. So we look at the 72.</p>



<p class="wp-block-paragraph">look at the 72 it belongs on the left of the 72 we go there it belongs on the right of the 34 so we</p>



<p class="wp-block-paragraph">go right here as the new right child and the diagram kind of sucks at this point it&#8217;s a little</p>



<p class="wp-block-paragraph">cramped but i&#8217;m going to try my best to split the difference between the 34 and the 32 visually to</p>



<p class="wp-block-paragraph">make a nice easy to debug graph so maybe like somewhere around there put 66 then i&#8217;m going to</p>



<p class="wp-block-paragraph">So the 66 is now the right child of 34.</p>



<p class="wp-block-paragraph">Okay, next number that we&#8217;re going to add is my computer&#8217;s crashing.</p>



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



<p class="wp-block-paragraph">What happened?</p>



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



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



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



<p class="wp-block-paragraph">Oh, God.</p>



<p class="wp-block-paragraph">Okay, let me see what&#8217;s going to happen here.</p>



<p class="wp-block-paragraph">This is awful.</p>



<p class="wp-block-paragraph">is awful i usually say you have to space these out enough so that none of these overlap but</p>



<p class="wp-block-paragraph">they&#8217;re about to overlap okay we&#8217;ll do go to the left because 55 is less than 88 go to the right</p>



<p class="wp-block-paragraph">because 55 is greater than 21 go to the left because it&#8217;s less than 72 go to the right because</p>



<p class="wp-block-paragraph">it&#8217;s greater than 34 then go to the left again because it&#8217;s less than 66 i hate this i hate this</p>



<p class="wp-block-paragraph">between 34 and 66, just to try to make it easier to debug.</p>



<p class="wp-block-paragraph">This is almost, I&#8217;m almost stretched</p>



<p class="wp-block-paragraph">to my mental limit right now.</p>



<p class="wp-block-paragraph">So I&#8217;m gonna make the left child pointer here</p>



<p class="wp-block-paragraph">and the new node we just added was 55.</p>



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



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



<p class="wp-block-paragraph">Let me just duplicate this one more time</p>



<p class="wp-block-paragraph">and get rid of those pink arrows.</p>



<p class="wp-block-paragraph">arrows let&#8217;s add the 17 next 17 right here um so once again we look at the root node it&#8217;s occupied</p>



<p class="wp-block-paragraph">go to the left because it&#8217;s less than we look at the 21 we go left because it&#8217;s less than 21</p>



<p class="wp-block-paragraph">we go to the right because it&#8217;s greater than three we look at the six we go to the right</p>



<p class="wp-block-paragraph">because it&#8217;s greater than six we go to the right because it&#8217;s greater than 11.</p>



<p class="wp-block-paragraph">So a super cramped diagram.</p>



<p class="wp-block-paragraph">I&#8217;m in hell.</p>



<p class="wp-block-paragraph">I&#8217;m just going to move this slightly to the right because this is just awful.</p>



<p class="wp-block-paragraph">And I&#8217;m going to try to make sure again that same depth nodes are at the same Y coordinate.</p>



<p class="wp-block-paragraph">Let me fix that number real fast before I forget.</p>



<p class="wp-block-paragraph">And put it as a 17.</p>



<p class="wp-block-paragraph">And then do my connecting line.</p>



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



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



<p class="wp-block-paragraph">I&#8217;m losing it.</p>



<p class="wp-block-paragraph">The, uh, whoops, the 23, where is this going to go?</p>



<p class="wp-block-paragraph">Is it 23?</p>



<p class="wp-block-paragraph">Um, Hmm.</p>



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



<p class="wp-block-paragraph">Oh, well, it&#8217;s going to be like left child of the 34 probably.</p>



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



<p class="wp-block-paragraph">Let me just, uh, look at the root node.</p>



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



<p class="wp-block-paragraph">23 belongs on the left of that.</p>



<p class="wp-block-paragraph">Look at the 21, 23 belongs on the right of that.</p>



<p class="wp-block-paragraph">Cause it&#8217;s greater than 21.</p>



<p class="wp-block-paragraph">Look at the 72.</p>



<p class="wp-block-paragraph">It belongs on the left.</p>



<p class="wp-block-paragraph">it belongs on the left look at the 34 belongs on the left and then that&#8217;s going to be the new left</p>



<p class="wp-block-paragraph">child okay so I&#8217;m going to duplicate one of these nodes and it&#8217;s going to be the new left child of</p>



<p class="wp-block-paragraph">the 34 node that is awful but I guess we barely have enough room let me change that to a 23 and</p>



<p class="wp-block-paragraph">then I&#8217;ll add my connecting line okay and now we&#8217;re ready to add the final value in our tree</p>



<p class="wp-block-paragraph">to be? It&#8217;s going to be like left child of 11 maybe. So we look at the root node. Whoops,</p>



<p class="wp-block-paragraph">forgot to move that arrow. We look at the root node. We can&#8217;t put it there. Nine belongs on the</p>



<p class="wp-block-paragraph">left side of the root. So we go like that. Nine belongs on the left side of the 21. Oh, did I just</p>



<p class="wp-block-paragraph">guess something wrong? No, no, no. I think I&#8217;m okay. Go to the left side of that. We look at</p>



<p class="wp-block-paragraph">right side of three we look at the six nine belongs on the right side of the six we look at</p>



<p class="wp-block-paragraph">the eleven nine belongs on the left side of eleven because it&#8217;s less than eleven so I&#8217;m just going to</p>



<p class="wp-block-paragraph">duplicate this and it&#8217;s going to end up being the left side whoops nope that&#8217;s wrong hang on a second</p>



<p class="wp-block-paragraph">let me oh come on man let me get this down it&#8217;s going to be the left child of eleven not the left</p>



<p class="wp-block-paragraph">there and the diagram is a little messed up maybe I should try to rearrange this in a second</p>



<p class="wp-block-paragraph">it the topology is correct but it&#8217;s harder to debug because you can see that one of the right</p>



<p class="wp-block-paragraph">descendants of the six node is actually physically on its left side and that just makes it extra hard</p>



<p class="wp-block-paragraph">to debug but for now we&#8217;ll say that we&#8217;re done adding stuff into the tree so maybe I can kind</p>



<p class="wp-block-paragraph">I&#8217;m not going to change the actual topology.</p>



<p class="wp-block-paragraph">I&#8217;m just going to like, you know, redraw this in a slightly nicer way.</p>



<p class="wp-block-paragraph">So I can probably take this whole subtree and move it over to the left a little bit to make it a little cleaner.</p>



<p class="wp-block-paragraph">Which will give me some room to move this subtree a little bit over to the right.</p>



<p class="wp-block-paragraph">Am I still wearing my microphone? Yeah.</p>



<p class="wp-block-paragraph">And then it&#8217;s a little bit cleaner.</p>



<p class="wp-block-paragraph">Then I just have to redraw this line.</p>



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



<p class="wp-block-paragraph">oops yeah just have to redraw this line and now i think we&#8217;re following the rules that i talked</p>



<p class="wp-block-paragraph">about before where a good diagram has all the left descendants physically on the left side of</p>



<p class="wp-block-paragraph">a given node so let&#8217;s see the six has everything on the right and everything i think we&#8217;re okay</p>



<p class="wp-block-paragraph">i&#8217;m going to duplicate this real fast and now let&#8217;s verify that our tree is actually correct</p>



<p class="wp-block-paragraph">So I said this in the last video when we built a tree, but you basically want to take like the input data set here and sort it.</p>



<p class="wp-block-paragraph">And whatever sorted list you end up with, that&#8217;s what you should see when you scan your eyes from left to right.</p>



<p class="wp-block-paragraph">Again, this is another good argument for drawing the diagram in a really good way.</p>



<p class="wp-block-paragraph">All left descendants are physically on the left. All right descendants are physically on the right.</p>



<p class="wp-block-paragraph">So another way that I could do this, besides I don&#8217;t really want to sort this list right now,</p>



<p class="wp-block-paragraph">Just scan from left to right and make sure that I see increasing numbers</p>



<p class="wp-block-paragraph">Or if you&#8217;ve decided to make a tree that supports duplicates, make sure that you see non-decreasing numbers</p>



<p class="wp-block-paragraph">But I&#8217;m looking for an ascending list</p>



<p class="wp-block-paragraph">Okay, so I&#8217;m gonna just like do little hashes here</p>



<p class="wp-block-paragraph">One is the lowest number. That&#8217;s good. Then if I go to the right just a little bit</p>



<p class="wp-block-paragraph">I see a three go to the right a little bit. I see a six that&#8217;s still increasing then I see a nine and then I see an 11</p>



<p class="wp-block-paragraph">still been increasing go to the right a little bit more it&#8217;s a 21 go to the right a little bit more</p>



<p class="wp-block-paragraph">it&#8217;s a 23 then it&#8217;s a 34 then it&#8217;s a 55 then it&#8217;s a 66 then it&#8217;s a 72 so far everything&#8217;s been</p>



<p class="wp-block-paragraph">increasing and then from 72 to 88 and from 88 to 90. so the ordering of the numbers are correct</p>



<p class="wp-block-paragraph">let me just make sure i actually have a binary search tree by the definition of a bst first it</p>



<p class="wp-block-paragraph">crazy lines going off in some direction or like different symbols this is a</p>



<p class="wp-block-paragraph">connected graph meaning I could probably find well I definitely could find a path</p>



<p class="wp-block-paragraph">from any node to any other node if I just you know trace the edges so can the</p>



<p class="wp-block-paragraph">9 find its way to the 90 yeah if we just kind of like go you know up this way and</p>



<p class="wp-block-paragraph">follow the edges up to the 88 and then back down to the 90 so every single node</p>



<p class="wp-block-paragraph">could do that it could find every single other node on the graph which means this</p>



<p class="wp-block-paragraph">every single other node on the graph which means this is a connected graph so that&#8217;s good the next</p>



<p class="wp-block-paragraph">thing is now that we have a connected graph do we have a tree so check for cycles uh can any start</p>



<p class="wp-block-paragraph">node find its way back to itself in a path that does not repeat edges i don&#8217;t see that anywhere</p>



<p class="wp-block-paragraph">in the tree so i think we have a valid tree let me get rid of that real fast so for example uh</p>



<p class="wp-block-paragraph">Could we find a path that goes away from the 9 node and then comes back without repeating any edges?</p>



<p class="wp-block-paragraph">I cannot because as soon as we go out to the 11,</p>



<p class="wp-block-paragraph">later on we would have to use that same edge to come back down to the 9 and that would be repeating an edge.</p>



<p class="wp-block-paragraph">So we just kind of check that for every single node.</p>



<p class="wp-block-paragraph">We can&#8217;t find a cycle anywhere in this entire graph.</p>



<p class="wp-block-paragraph">So this is an acyclic graph.</p>



<p class="wp-block-paragraph">Okay, that means it&#8217;s a tree.</p>



<p class="wp-block-paragraph">Is this a rooted tree?</p>



<p class="wp-block-paragraph">ancestor to all other nodes and it&#8217;s that 88 node that&#8217;s the root node of the</p>



<p class="wp-block-paragraph">entire tree every single node in the tree can say that 88 is its greatest</p>



<p class="wp-block-paragraph">ancestor there&#8217;s no you know same leveled nodes at the very very very top okay so</p>



<p class="wp-block-paragraph">this is a rooted tree now is it a binary tree yeah we look at every single node</p>



<p class="wp-block-paragraph">and we can see that none of the nodes have more than two children they&#8217;ve all</p>



<p class="wp-block-paragraph">this is a binary tree. And then when we, you know, we verified the numbers a second ago,</p>



<p class="wp-block-paragraph">that means this is a binary search tree. So it satisfies all the properties of a binary search</p>



<p class="wp-block-paragraph">tree. It doesn&#8217;t look very good. It&#8217;s kind of wonky, but it is valid. Remember this,</p>



<p class="wp-block-paragraph">you know, a binary search tree just by itself is not a self-balancing tree,</p>



<p class="wp-block-paragraph">which means you, you, you need to like not inject your human feelings into the tree while you&#8217;re</p>



<p class="wp-block-paragraph">building it and start rearranging it to make it prettier. It wouldn&#8217;t be a valid binary search</p>



<p class="wp-block-paragraph">if you tried to add special rules when you were creating it.</p>



<p class="wp-block-paragraph">I don&#8217;t know.</p>



<p class="wp-block-paragraph">If you imagine that maybe you, the human,</p>



<p class="wp-block-paragraph">were changing the input data,</p>



<p class="wp-block-paragraph">then I guess that&#8217;s valid.</p>



<p class="wp-block-paragraph">But don&#8217;t imagine that the tree</p>



<p class="wp-block-paragraph">is changing the data as you add it.</p>



<p class="wp-block-paragraph">Anyway, so how fast is this tree?</p>



<p class="wp-block-paragraph">Remember we said in a previous video</p>



<p class="wp-block-paragraph">that a perfect binary search tree,</p>



<p class="wp-block-paragraph">meaning perfectly balanced on every single node,</p>



<p class="wp-block-paragraph">would be a log time tree.</p>



<p class="wp-block-paragraph">It would be really, really fast to add and search</p>



<p class="wp-block-paragraph">to add and search and even remove but this tree is definitely lopsided so this</p>



<p class="wp-block-paragraph">is not a log tree the time complexity of a binary search tree actually is not</p>



<p class="wp-block-paragraph">really log because the tree could just grow in any direction so in the worst</p>



<p class="wp-block-paragraph">case scenario if you add the worst possible data you&#8217;d end up with a linear</p>



<p class="wp-block-paragraph">time tree self balancing trees will be log time trees because they always</p>



<p class="wp-block-paragraph">rearrange themselves to make sure that they&#8217;re balanced to a certain extent but</p>



<p class="wp-block-paragraph">But, you know, as a binary search tree scales, it could just get slower and slower and slower.</p>



<p class="wp-block-paragraph">So we wouldn&#8217;t be able to say that BSTs, binary search trees in general are log time.</p>



<p class="wp-block-paragraph">We could pull up a calculator though and we could say, well, you know, what&#8217;s the height of this particular tree?</p>



<p class="wp-block-paragraph">Let me just look at the depths real fast here.</p>



<p class="wp-block-paragraph">What&#8217;s the height of this tree?</p>



<p class="wp-block-paragraph">So the root node has a depth of zero and then one and then two and then three and then four.</p>



<p class="wp-block-paragraph">drawing your tree in a nice pretty way where all the ranks are the same physically.</p>



<p class="wp-block-paragraph">We just take the deepest node and we add one to it and we say that&#8217;s the height.</p>



<p class="wp-block-paragraph">So the height of this tree is six.</p>



<p class="wp-block-paragraph">Another way of looking at it is if we wanted to touch one of the deepest nodes,</p>



<p class="wp-block-paragraph">how many nodes would we have to touch as we travel down through the tree?</p>



<p class="wp-block-paragraph">So how many nodes would we have to touch to find, you know, the nine or the 17 or the 55?</p>



<p class="wp-block-paragraph">one node the 88 two and let&#8217;s go to the left three four five six we&#8217;d have to touch six nodes</p>



<p class="wp-block-paragraph">to get to the deepest node so that&#8217;s why we&#8217;re saying that&#8217;s another way that we&#8217;re justifying</p>



<p class="wp-block-paragraph">that we say the height is six so the height of this tree is six which means in the worst case</p>



<p class="wp-block-paragraph">scenario if we needed to search this tree again we&#8217;ll talk about actually searching in another</p>



<p class="wp-block-paragraph">video but for now i can just promise you that if we were going to search this tree we could do it</p>



<p class="wp-block-paragraph">going to search this tree we could do it in at most six uh examinations so even though we have</p>



<p class="wp-block-paragraph">a lot of nodes here how many nodes do we actually have let me just double check this we have n</p>



<p class="wp-block-paragraph">is equal to one two three four five six seven eight nine ten eleven twelve thirteen fourteen</p>



<p class="wp-block-paragraph">we have fourteen nodes but at most we&#8217;d have to examine six nodes because this is a binary search</p>



<p class="wp-block-paragraph">tree so it&#8217;s a lot faster than scanning the whole tree in linear time but it could be a lot faster</p>



<p class="wp-block-paragraph">It could be a lot faster if the tree was better balanced.</p>



<p class="wp-block-paragraph">That&#8217;s a topic for another day.</p>



<p class="wp-block-paragraph">So for example, if we wanted to search for the number 55, let&#8217;s say we were just going</p>



<p class="wp-block-paragraph">to search for 55, you know, we&#8217;d look at the 88.</p>



<p class="wp-block-paragraph">Basically searching is pretty much the same as adding a node.</p>



<p class="wp-block-paragraph">You just kind of go find where the node would belong.</p>



<p class="wp-block-paragraph">And then if it&#8217;s there, then you found it.</p>



<p class="wp-block-paragraph">And if not, then it&#8217;s not in there.</p>



<p class="wp-block-paragraph">55 belongs on the left of 88, belongs on the right of 21, it belongs on the left of 72.</p>



<p class="wp-block-paragraph">belongs on the left of 72, belongs on the right of 34, belongs on the left of 66, and there we</p>



<p class="wp-block-paragraph">found it. Notice how we only did one, two, three, four, five, six examinations. So I&#8217;m going to talk</p>



<p class="wp-block-paragraph">about big O and time complexities in another video, but long story short, the time complexity</p>



<p class="wp-block-paragraph">of this particular tree, or actually any binary search tree, is always going to be O of H, no</p>



<p class="wp-block-paragraph">imbalanced or perfectly balanced O of H so in the worst case scenario since H is 6 we&#8217;re going to have to touch H or 6 nodes</p>



<p class="wp-block-paragraph">To search or add something or whatever if the tree was perfect. It would look more like log time. Let&#8217;s pull up that calculator real fast</p>



<p class="wp-block-paragraph">Where the heck is my calculator? I think I like got sidetracked</p>



<p class="wp-block-paragraph">so if I pull up a calculator here</p>



<p class="wp-block-paragraph">and I typed in</p>



<p class="wp-block-paragraph">log base 2 it&#8217;s base 2 because we only allow two children per node at most log</p>



<p class="wp-block-paragraph">base 2 of the number of nodes in the tree so log base 2 of 14 if we get the</p>



<p class="wp-block-paragraph">answer to that it says 3.8 which means the calculator is telling us that in a</p>



<p class="wp-block-paragraph">log time tree we should not have to examine more than four nodes in order to</p>



<p class="wp-block-paragraph">search the tree or add a brand new node but this is not a log tree because it is</p>



<p class="wp-block-paragraph">totally imbalanced it&#8217;s kind of wonky it&#8217;s not it&#8217;s not perfect so it&#8217;s telling us in a perfect</p>



<p class="wp-block-paragraph">tree we&#8217;d have to touch four nodes only but we clearly know we might have to touch six nodes in</p>



<p class="wp-block-paragraph">the worst case so that i&#8217;m just trying to bring to your attention that the more imbalanced the tree is</p>



<p class="wp-block-paragraph">the further its uh performance drifts away from log time which is a great argument</p>



<p class="wp-block-paragraph">I think this is pretty much everything I wanted to talk about today.</p>



<p class="wp-block-paragraph">I&#8217;m going to take a break and like, you know, eat some cookies and stuff.</p>



<p class="wp-block-paragraph">But in a future video, we&#8217;re going to talk about inserting.</p>



<p class="wp-block-paragraph">No, we already just did that literally right now.</p>



<p class="wp-block-paragraph">We&#8217;re going to talk about searching through the tree.</p>



<p class="wp-block-paragraph">We&#8217;re going to talk about removing nodes from the tree.</p>



<p class="wp-block-paragraph">We&#8217;re going to talk about linear trees.</p>



<p class="wp-block-paragraph">We&#8217;re going to talk about all the time complexities.</p>



<p class="wp-block-paragraph">And then eventually we&#8217;ll move on to self-balancing trees.</p>



<p class="wp-block-paragraph">on to self-balancing trees. So I hope you had fun watching this video. I hope you learned a little</p>



<p class="wp-block-paragraph">bit of stuff and I&#8217;ll see you at a later date in time. I&#8217;m outie. Patowdy. Note to self, edit out</p>



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



<p class="wp-block-paragraph">hope you did learn something and have some fun if you could do me a please a small little favor</p>



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



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



<p class="wp-block-paragraph">and it&#8217;ll help make more videos and grow this community so we&#8217;ll be able to do more videos</p>



<p class="wp-block-paragraph">longer videos better videos or just i&#8217;ll be able to keep making videos in general so please</p>



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



<p class="wp-block-paragraph">because I know somebody subscribed or followed. 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. So you could do it as a nice favor to me or you</p>



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



<p class="wp-block-paragraph">and then I&#8217;ll, I&#8217;ll just wake up. I promise that&#8217;s what will happen. Also, uh, if you look at the</p>



<p class="wp-block-paragraph">middle of the screen right now, you should see a QR code, which you can scan in order to go to the</p>



<p class="wp-block-paragraph">we&#8217;re at the bottom of this video and it&#8217;ll take you to my main website where you can just kind of</p>



<p class="wp-block-paragraph">like see all the videos i published and the services and tutorials and things that i offer</p>



<p class="wp-block-paragraph">and all that good stuff and uh if you have a suggestion for uh uh clarifications or errata</p>



<p class="wp-block-paragraph">or just future videos that you want to see please leave a comment or if you just want to say hey</p>



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



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



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



<p class="wp-block-paragraph">as as i fade into the darkness which is coming for us all</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/binary-search-tree-insertion-tutorial-building-a-bst-from-scratch-step-by-step/">Binary Search Tree Insertion Tutorial: Building a BST from Scratch Step by Step</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/binary-search-tree-insertion-tutorial-building-a-bst-from-scratch-step-by-step/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Binary Search Tree Definition Explained Step by Step</title>
		<link>https://www.NeuralLantern.com/binary-search-tree-definition-explained-step-by-step/</link>
					<comments>https://www.NeuralLantern.com/binary-search-tree-definition-explained-step-by-step/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Tue, 31 Mar 2026 03:10:16 +0000</pubDate>
				<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[acyclic graph]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[binary search tree definition]]></category>
		<category><![CDATA[binary search tree explained]]></category>
		<category><![CDATA[binary tree vs binary search tree]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[bst for beginners]]></category>
		<category><![CDATA[bst ordering property]]></category>
		<category><![CDATA[bst rules]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[connected graph]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[left subtree right subtree]]></category>
		<category><![CDATA[rooted binary tree]]></category>
		<category><![CDATA[tree data structure]]></category>
		<category><![CDATA[what is a binary search tree]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=324</guid>

					<description><![CDATA[<p>A Binary Search Tree (BST) is a rooted binary tree where for every node, all values in its left subtree are less than the node's value, and all values in its right subtree are greater than the node's value. No duplicates in this simple version.</p>
<p>The post <a href="https://www.NeuralLantern.com/binary-search-tree-definition-explained-step-by-step/">Binary Search Tree Definition Explained Step by Step</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 loading="lazy" title="Binary Search Tree Definition Explained Step by Step" width="1380" height="776" src="https://www.youtube.com/embed/H1BU1UkFiDo?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">Clear step-by-step explanation of what defines a Binary Search Tree (BST). We build the definition rule-by-rule starting from graphs all the way to the BST ordering property (all left descendants, node, all right descendants). Great first video before learning insert, delete, search, and Big-O.</p>



<p class="wp-block-paragraph">What is a Binary Search Tree? 00:00<br>Intro to Graphs and Nodes 00:42<br>Connected Graph Requirement 03:16<br>Acyclic Graph &#8211; Removing Cycles 04:54<br>Turning Graph into Tree 07:05<br>Establishing a Root 07:09<br>Adding Hierarchy and Levels 09:05<br>Single Common Ancestor 11:18<br>Binary Tree Definition 17:30<br>BST Ordering Property 17:39<br>Left Subtree Less Than Node 18:29<br>Fixing Invalid BST Example 20:26<br>Valid BST Final Check 21:24<br>In-Order Ascending Order 24:11<br>Video Summary and Next Steps 25:02<br>Thanks and Call to Action 25:30</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 talk about binary search trees specifically in this video I want to help you</p>



<p class="wp-block-paragraph">define and identify what is a binary search tree all the rules that go into determining whether</p>



<p class="wp-block-paragraph">or not something is a binary search tree so you can you can tell that you&#8217;re looking at one or</p>



<p class="wp-block-paragraph">whether you&#8217;re not looking at one we&#8217;ll talk about some terminology but keep in mind I&#8217;m</p>



<p class="wp-block-paragraph">going to save searching and sorting and like you know deleting and adding items and a whole bunch</p>



<p class="wp-block-paragraph">extra stuff, especially the hard stuff for videos that&#8217;ll come right after this one.</p>



<p class="wp-block-paragraph">For now, we&#8217;re just going to talk about what is a binary search tree.</p>



<p class="wp-block-paragraph">So you can see on the screen right here, I hope that we have a binary search tree.</p>



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



<p class="wp-block-paragraph">Can you see that?</p>



<p class="wp-block-paragraph">Yeah, I think you probably can.</p>



<p class="wp-block-paragraph">Yeah, I think you can.</p>



<p class="wp-block-paragraph">This is not drawn quite as well as I would like to draw ours.</p>



<p class="wp-block-paragraph">I&#8217;ll tell you how to do that in a little while.</p>



<p class="wp-block-paragraph">to do that in a little while but when you draw a tree in a really really nice pretty aligned way</p>



<p class="wp-block-paragraph">it&#8217;s way easier to debug it&#8217;s way easier to tell that you&#8217;re looking at something that is sorted</p>



<p class="wp-block-paragraph">and so forth anyway okay so first let&#8217;s start off with a graph so this is not a video about graphs</p>



<p class="wp-block-paragraph">uh i guess i can probably expect that you already kind of know what a graph is at this point in time</p>



<p class="wp-block-paragraph">and edges. So what do I mean by nodes? Well just imagine there&#8217;s like a little circle here</p>



<p class="wp-block-paragraph">that contains some sort of data. If you know data structures already then</p>



<p class="wp-block-paragraph">there would be a t-type sitting inside the node. We&#8217;ll just call the t-type an integer</p>



<p class="wp-block-paragraph">and so for our nodes, for our trees, we&#8217;re just going to say that they hold integers even though</p>



<p class="wp-block-paragraph">you know technically speaking in a binary search tree or a graph you could have all sorts of</p>



<p class="wp-block-paragraph">Okay. So I&#8217;m just going to, you know, grab this and sort of like duplicate it and just duplicate it again.</p>



<p class="wp-block-paragraph">I&#8217;m just going to make a bunch of duplicates. Something is definitely wonky about my setup right now.</p>



<p class="wp-block-paragraph">I think I just upgraded the camera and the CPU is maxed out. I just upgraded the CPU on this computer too.</p>



<p class="wp-block-paragraph">Oh well. Okay. So we aren&#8217;t going to support duplicate values in our binary search tree,</p>



<p class="wp-block-paragraph">I&#8217;m going to say it doesn&#8217;t really matter for the graph. Later on, we&#8217;ll start making sure there&#8217;s</p>



<p class="wp-block-paragraph">no duplicate data. For now, I think I can probably do that somewhat quickly. Let&#8217;s see. So a graph,</p>



<p class="wp-block-paragraph">you know, for the for our purposes right now, a graph is just basically a collection of nodes and</p>



<p class="wp-block-paragraph">edges. It could be empty. It could be just nodes. It couldn&#8217;t just be edges, it would have an edge</p>



<p class="wp-block-paragraph">always has to have a node or two connected to it. So we just have like a bunch of nodes inside of</p>



<p class="wp-block-paragraph">data values this is a valid graph but I&#8217;m going to draw some edges randomly here so we&#8217;re going</p>



<p class="wp-block-paragraph">to do this it doesn&#8217;t really matter I&#8217;m just randomly drawing edges okay actually I should</p>



<p class="wp-block-paragraph">probably do maybe like one that&#8217;s kind of separated okay so this is a graph this is not a binary search</p>



<p class="wp-block-paragraph">tree so let&#8217;s add a bunch of rules let me just pull up my rules real fast here okay so what is</p>



<p class="wp-block-paragraph">First we&#8217;ll start off with the graph like we have here and then we&#8217;ll say that the graph must be connected.</p>



<p class="wp-block-paragraph">What is a connected graph? A connected graph is basically a graph where every single node can find every single other node</p>



<p class="wp-block-paragraph">or find a path to every single other node only following along edges and only by respecting the direction of edges.</p>



<p class="wp-block-paragraph">You can see this particular graph is undirected meaning the edges don&#8217;t really have a direction to them.</p>



<p class="wp-block-paragraph">So that means we could travel along in any direction from node to node.</p>



<p class="wp-block-paragraph">So just as a real quick, you know, I guess like tutorial, I&#8217;m going to say is 8 connected to every single other node?</p>



<p class="wp-block-paragraph">Well, it can find a path to 12 and it can find a path to 33 and it can find a path to 76 and 45.</p>



<p class="wp-block-paragraph">But it cannot find a path to 99, which means this is not a connected graph.</p>



<p class="wp-block-paragraph">So that&#8217;s the first rule that we have to implement here.</p>



<p class="wp-block-paragraph">We&#8217;re going to say this graph needs to be upgraded or modified so that it will be a connected graph.</p>



<p class="wp-block-paragraph">start randomly adding edges until this is a connected graph I&#8217;m gonna do maybe</p>



<p class="wp-block-paragraph">like an edge from 99 to 12 there and then now I think this is a connected</p>



<p class="wp-block-paragraph">graph but let&#8217;s double check real fast so connected graph you know what I&#8217;m</p>



<p class="wp-block-paragraph">gonna put graph connectedness in another video to keep this one short for now</p>



<p class="wp-block-paragraph">check out my other videos if you would like to see whether or not a graph is</p>



<p class="wp-block-paragraph">but long story short as I just said if every single node can find a path to</p>



<p class="wp-block-paragraph">every single other node without you know skipping where there is no edge then</p>



<p class="wp-block-paragraph">it&#8217;s connected okay so that&#8217;s the first thing the next thing that we need let me</p>



<p class="wp-block-paragraph">just put the words connected up here we need a connected graph the next thing</p>



<p class="wp-block-paragraph">that we need is we need an acyclic graph which basically means a graph with no</p>



<p class="wp-block-paragraph">A cycle is, can you find a path in this graph that starts at one node and ends at the same node without repeating any edges?</p>



<p class="wp-block-paragraph">So for example, if I was asking, you know, is 76 involved in any cycles?</p>



<p class="wp-block-paragraph">Well, we could go from 76 to 33 and then 12.</p>



<p class="wp-block-paragraph">And, you know, we could kind of take 8, 45, 12 again.</p>



<p class="wp-block-paragraph">But by the time we turn around and try to get back to the 33, we&#8217;ve already crossed this edge twice.</p>



<p class="wp-block-paragraph">so that means the 76 is not involved in any cycles because we cannot find our way back to 76</p>



<p class="wp-block-paragraph">without repeating an edge so 76 is okay i&#8217;m just going to maybe put like a little check box here</p>



<p class="wp-block-paragraph">and uh you can also tell that the 33 is not involved in any cycles because we go down to</p>



<p class="wp-block-paragraph">the 76 and then come back up to 33 we already repeated an edge so that&#8217;s not valid same thing</p>



<p class="wp-block-paragraph">again. Same thing for the 34, same thing for the 99. But if you look at the 12 here, we could</p>



<p class="wp-block-paragraph">actually find a cycle. We could go from the 12 to the 8, and then from the 8 to the 45, and from the</p>



<p class="wp-block-paragraph">45 to the 12. Now we&#8217;re back where we started. We&#8217;re at the 12 node again, and we did not repeat</p>



<p class="wp-block-paragraph">any edges. So this is not a cyclic. This is a cyclic graph. It&#8217;s got a cycle. So that&#8217;s the</p>



<p class="wp-block-paragraph">we want to make sure that our graph has no cycles. So, uh, the next rule I&#8217;m just going to say is,</p>



<p class="wp-block-paragraph">is going to be satisfied by me removing some edges at random. So I don&#8217;t know, let&#8217;s get rid of, uh,</p>



<p class="wp-block-paragraph">the eight to the 45 connection. See if that works. Um, let&#8217;s see. Well, wait a minute. What did I</p>



<p class="wp-block-paragraph">just do? Something happened here. I just accidentally, oh, I erased too many edges</p>



<p class="wp-block-paragraph">from the last slide. Okay. Let me add that one back in 33 to 76. That was supposed to be there.</p>



<p class="wp-block-paragraph">33 to 76 that was supposed to be there.</p>



<p class="wp-block-paragraph">So now we have an acyclic graph</p>



<p class="wp-block-paragraph">and we can consider this graph a tree.</p>



<p class="wp-block-paragraph">The next thing we need to do is add a hierarchy to this tree.</p>



<p class="wp-block-paragraph">So this is gonna make more sense in a little bit</p>



<p class="wp-block-paragraph">but for now I&#8217;m gonna say,</p>



<p class="wp-block-paragraph">we&#8217;ll imagine a family tree kind of,</p>



<p class="wp-block-paragraph">it&#8217;s not really exactly a family tree</p>



<p class="wp-block-paragraph">but you know imagine there are parents</p>



<p class="wp-block-paragraph">and children and grandchildren</p>



<p class="wp-block-paragraph">and we&#8217;ll just try to like,</p>



<p class="wp-block-paragraph">we&#8217;ll try to rank children according to</p>



<p class="wp-block-paragraph">according to you know when they were born or or how many parents or children</p>



<p class="wp-block-paragraph">they have you know their descendancy their lineage whatever you want to call</p>



<p class="wp-block-paragraph">it so I think I&#8217;m just gonna maybe move the eight up a little bit so that it&#8217;s</p>



<p class="wp-block-paragraph">on the same rank with the 12 and the 33 so I&#8217;m just gonna move these up a little</p>



<p class="wp-block-paragraph">bit and then redo their connecting lines so I&#8217;m gonna do this and that and then</p>



<p class="wp-block-paragraph">and then we&#8217;ll say that the 76 is still a child of the 33.</p>



<p class="wp-block-paragraph">So I&#8217;ll just kind of move it over here.</p>



<p class="wp-block-paragraph">Or sorry, we never said it was a child.</p>



<p class="wp-block-paragraph">Now we&#8217;re saying it&#8217;s a child</p>



<p class="wp-block-paragraph">because when you go from top to bottom,</p>



<p class="wp-block-paragraph">imagine like a family tree,</p>



<p class="wp-block-paragraph">we&#8217;ll say we have parents and children.</p>



<p class="wp-block-paragraph">The children or the descendants are lower.</p>



<p class="wp-block-paragraph">The ancestors or the parents or the grandparents are higher.</p>



<p class="wp-block-paragraph">Maybe I want to put this 45 over on the left</p>



<p class="wp-block-paragraph">so that it&#8217;s a child of the 8 node.</p>



<p class="wp-block-paragraph">And maybe I&#8217;ll say that the 99 node</p>



<p class="wp-block-paragraph">the 99 node is a child of the 12 node. Okay, so eventually this is going to look not exactly like</p>



<p class="wp-block-paragraph">a family tree because usually in the family tree we have two parents that go to one or more children.</p>



<p class="wp-block-paragraph">In this type of tree, in eventually our binary tree, we&#8217;re going to have one parent that has,</p>



<p class="wp-block-paragraph">you know, children just by itself, like asexual reproduction. We&#8217;re pretending to be bacteria</p>



<p class="wp-block-paragraph">What was that comedy skit a long time ago, Tiny Elvis?</p>



<p class="wp-block-paragraph">And he was like, hey, see that protozoa over there?</p>



<p class="wp-block-paragraph">The thing is huge.</p>



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



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



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



<p class="wp-block-paragraph">So the next rule we need is we must have a rooted tree.</p>



<p class="wp-block-paragraph">What is a rooted tree?</p>



<p class="wp-block-paragraph">Actually, let me clean up the lineage real fast, the heritage.</p>



<p class="wp-block-paragraph">I want to make nodes that have the same level with respect to their ancestors</p>



<p class="wp-block-paragraph">respect to their ancestors to have the same physical y coordinate the same</p>



<p class="wp-block-paragraph">physical level so notice how the 99 I just moved it down a little bit because</p>



<p class="wp-block-paragraph">I wanted it to look like it&#8217;s the same you know number of generations lower</p>



<p class="wp-block-paragraph">than the top row so like with aid it had a child 45 so that&#8217;s one level down 33</p>



<p class="wp-block-paragraph">had a child that&#8217;s one level down that&#8217;s a 76 and the 12 also one level down</p>



<p class="wp-block-paragraph">would have been 99 but the 99 was kind of too physically high this will help</p>



<p class="wp-block-paragraph">will help you debug your binary trees in the future anyway so i&#8217;m just going to clean this up right now</p>



<p class="wp-block-paragraph">and uh now that we have you know parent child relationships we need to look at the most common</p>



<p class="wp-block-paragraph">ancestor in the whole entire tree or if there is one so notice how uh i guess i&#8217;m just kind of using</p>



<p class="wp-block-paragraph">the word ancestor without explaining it so your ancestors are the people that you&#8217;re disappointing</p>



<p class="wp-block-paragraph">or whatever it is, ancestors came before us, right?</p>



<p class="wp-block-paragraph">Like my parents are my ancestors.</p>



<p class="wp-block-paragraph">My grandparents are also my ancestors.</p>



<p class="wp-block-paragraph">My great-grandparents are also my ancestors.</p>



<p class="wp-block-paragraph">Going down in the family tree are your descendants.</p>



<p class="wp-block-paragraph">You know, my children are my descendants.</p>



<p class="wp-block-paragraph">My grandchildren are my descendants.</p>



<p class="wp-block-paragraph">My great-grandchildren are my descendants and so forth.</p>



<p class="wp-block-paragraph">So if you look at the number 34 here,</p>



<p class="wp-block-paragraph">what is the greatest ancestor that it has?</p>



<p class="wp-block-paragraph">Well, if you just go up and up and up</p>



<p class="wp-block-paragraph">until you can&#8217;t go up anymore,</p>



<p class="wp-block-paragraph">up until you can&#8217;t go up anymore it&#8217;s the 12th so that means the greatest ancestor of at least in</p>



<p class="wp-block-paragraph">this tree of the 34 is the 12th the 45 the highest you can go is the 8 notice how 8 and 12 and 33 are</p>



<p class="wp-block-paragraph">siblings there&#8217;s not really like a parent-child relationship they&#8217;re connected sideways we&#8217;ll</p>



<p class="wp-block-paragraph">eventually get rid of the sideways connections for our binary search trees but for now we have</p>



<p class="wp-block-paragraph">some siblings and so we actually have three different greatest ancestors in this tree we&#8217;ve</p>



<p class="wp-block-paragraph">tree we&#8217;ve got 8 and 12 and 33 and that&#8217;s no good so the next rule that we need is we need</p>



<p class="wp-block-paragraph">one common ancestor for every single other node in the entire tree so I&#8217;m going to duplicate this</p>



<p class="wp-block-paragraph">slide right here and I&#8217;m going to say we need to clean this up so we got to choose 8 or 12 or 33</p>



<p class="wp-block-paragraph">I don&#8217;t know I&#8217;m just going to maybe say that 8 and it&#8217;s 45 are children of the 12 so I&#8217;m just</p>



<p class="wp-block-paragraph">line here arbitrarily so i&#8217;m gonna go like that notice how i&#8217;m trying to keep the 8 the 99 and</p>



<p class="wp-block-paragraph">the 76 on the same y coordinate the same level now we have two uh you know greatest ancestors</p>



<p class="wp-block-paragraph">we need to have only one so i think i&#8217;m probably going to say that the 33 and the 76 are going to</p>



<p class="wp-block-paragraph">become children of the 12 node just for simplicity so i&#8217;m going to put this over here i&#8217;m going to</p>



<p class="wp-block-paragraph">i&#8217;m gonna like move this over here this is a gross tree but we&#8217;ll fix it soon we&#8217;ll do that all right</p>



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



<p class="wp-block-paragraph">okay and then i have to get rid of that little sibling line and then i have to add another line</p>



<p class="wp-block-paragraph">to say that 33 is a child of 12. okay so now we have a rooted tree what we had before was an</p>



<p class="wp-block-paragraph">unrooted tree or just like not a rooted tree because there was no root there was no greatest</p>



<p class="wp-block-paragraph">greatest common ancestor to the entire rest of the tree. But now we can say that 12 is the root</p>



<p class="wp-block-paragraph">of the tree because it&#8217;s the ancestor for all other nodes. I guess even if 12 was by itself,</p>



<p class="wp-block-paragraph">we could call it a root of tree. It just wouldn&#8217;t be a very good example. So I&#8217;m going to actually</p>



<p class="wp-block-paragraph">get rid of the word acyclic here since we already handled that. And I&#8217;m going to do,</p>



<p class="wp-block-paragraph">I&#8217;m going to say that 34&#8217;s greatest ancestor is 12, 99&#8217;s greatest ancestor is 12, 33&#8217;s greatest</p>



<p class="wp-block-paragraph">all the nodes in the tree their greatest ancestor is 12 and it&#8217;s common to all of them so now we</p>



<p class="wp-block-paragraph">have a rooted tree maybe I should type in my doomed I really hope the screen I really hope</p>



<p class="wp-block-paragraph">the screen cast doesn&#8217;t freeze it probably will upgraded the CPU it&#8217;s 33 faster than it was before</p>



<p class="wp-block-paragraph">board so i am still using an old cpu even though it&#8217;s new to me dang it i&#8217;m gonna have to spend</p>



<p class="wp-block-paragraph">like 500 okay uh so the next rule is so we have a rooted tree and now let&#8217;s rearrange everything</p>



<p class="wp-block-paragraph">hierarchically which we already kind of did so uh there&#8217;s not really much to do here except</p>



<p class="wp-block-paragraph">we did hierarchical organization</p>



<p class="wp-block-paragraph">we probably just need to move on to our next rule which is to say that</p>



<p class="wp-block-paragraph">if our rooted tree is to become a binary tree</p>



<p class="wp-block-paragraph">then each node in the entire tree</p>



<p class="wp-block-paragraph">cannot have more than two children</p>



<p class="wp-block-paragraph">every single node in the entire tree</p>



<p class="wp-block-paragraph">can have zero or one or two children</p>



<p class="wp-block-paragraph">if there are three or more anywhere in the entire tree</p>



<p class="wp-block-paragraph">then it&#8217;s not a binary tree</p>



<p class="wp-block-paragraph">so okay that means you know the 45 is okay</p>



<p class="wp-block-paragraph">because it has no children</p>



<p class="wp-block-paragraph">because it has no children. All the rows, all the leaves at the bottom are okay. All these nodes</p>



<p class="wp-block-paragraph">over here are okay because they just have one child. But the 12 node, the root of the tree,</p>



<p class="wp-block-paragraph">it&#8217;s got three children and that violates the rule. So now we need to rearrange this.</p>



<p class="wp-block-paragraph">So I think I&#8217;m going to just move these numbers down here maybe. Whoops. Maybe move these nodes</p>



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



<p class="wp-block-paragraph">Okay. So cool. Now we have a binary tree and now we need to rearrange the drawing a little bit.</p>



<p class="wp-block-paragraph">This is not part of the definition, but this will make it easier to debug your trees. So what I want</p>



<p class="wp-block-paragraph">to do is make it so that every left descendant of a node appears physically on that node&#8217;s left</p>



<p class="wp-block-paragraph">every right descendant of a node should appear on its right side</p>



<p class="wp-block-paragraph">or like further to the right on the x-coordinate</p>



<p class="wp-block-paragraph">so sort of similar to you know like the y-coordinate ranking</p>



<p class="wp-block-paragraph">but notice how the 12 its left child is 8 and its right child is 33</p>



<p class="wp-block-paragraph">so that means all the nodes here on the left side you know that come underneath the 8</p>



<p class="wp-block-paragraph">are left descendants or you could say that they are in the left subtree of the 12.</p>



<p class="wp-block-paragraph">So, they&#8217;re okay because 8 is to the left of 12 and 45 is to the left of 12.</p>



<p class="wp-block-paragraph">But notice how 33 is a right child of 12 and it&#8217;s part of the right subtree.</p>



<p class="wp-block-paragraph">It&#8217;s part of the right descendancy, but it&#8217;s physically on the left of 12.</p>



<p class="wp-block-paragraph">That&#8217;s going to be really bad because it&#8217;ll be harder to debug our trees later.</p>



<p class="wp-block-paragraph">So, I&#8217;m just going to slide it over without rearranging the tree.</p>



<p class="wp-block-paragraph">And I usually try to split the difference here.</p>



<p class="wp-block-paragraph">and then their parent is kind of like physically in the middle that will also help you debug in</p>



<p class="wp-block-paragraph">the future so i&#8217;m going to do this wrong let me get the blue i&#8217;m going to do this make sure that</p>



<p class="wp-block-paragraph">my ranking is still okay not the best most neatest tree and so now i&#8217;m going to look at the eight</p>



<p class="wp-block-paragraph">it&#8217;s left subtree or it&#8217;s left uh descendancy uh all of those nodes which is just the 45 are</p>



<p class="wp-block-paragraph">it&#8217;s left child 76 is the only thing that&#8217;s on the left side in the left subtree so that&#8217;s all</p>



<p class="wp-block-paragraph">right probably the 33 I think it&#8217;s still a little lopsided I probably could have you know dragged</p>



<p class="wp-block-paragraph">it a little bit more to the right but I&#8217;m going to leave it so the 33 and the 76 are okay and then</p>



<p class="wp-block-paragraph">the 99 is a right descendant or it&#8217;s part of the right subtree of the 33 so it should be on the</p>



<p class="wp-block-paragraph">right side it is that&#8217;s good the 34 should be on the right side of the 33 because it&#8217;s it&#8217;s a it&#8217;s</p>



<p class="wp-block-paragraph">It&#8217;s part of the right subtree of 33.</p>



<p class="wp-block-paragraph">It&#8217;s part of the right descendancy.</p>



<p class="wp-block-paragraph">And it should also be on the left side of the 99</p>



<p class="wp-block-paragraph">because it&#8217;s part of the left subtree of the 99.</p>



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



<p class="wp-block-paragraph">This is not the best drawing.</p>



<p class="wp-block-paragraph">So now we have a binary tree</p>



<p class="wp-block-paragraph">and we have kind of drawn it in a slightly good way.</p>



<p class="wp-block-paragraph">Not super great.</p>



<p class="wp-block-paragraph">The last rule that we&#8217;re going to add here is,</p>



<p class="wp-block-paragraph">whoops, what happened there?</p>



<p class="wp-block-paragraph">I&#8217;m supposed to do that, yeah.</p>



<p class="wp-block-paragraph">The last rule we&#8217;re going to add is</p>



<p class="wp-block-paragraph">we must have ordering to our nodes, to the numbers in our nodes. So what do I mean by ordering? Well,</p>



<p class="wp-block-paragraph">the whole reason that we want a binary search tree, at least usually, is because it&#8217;s really,</p>



<p class="wp-block-paragraph">really fast to search through. This is going to be a tree that we can search through in log time,</p>



<p class="wp-block-paragraph">which we&#8217;ll talk more about in another video. We&#8217;ll do time complexities and searching and stuff. But</p>



<p class="wp-block-paragraph">in order for this to be a super fast tree to search through, we need to be able to</p>



<p class="wp-block-paragraph">very quickly make a decision. Do we go down and to the left? Do we go down and to the right?</p>



<p class="wp-block-paragraph">left do we go down into the right and every decision we make should help us eliminate half</p>



<p class="wp-block-paragraph">of the remaining data set half of the tree at a time every time we go down a level and that&#8217;s only</p>



<p class="wp-block-paragraph">possible if the nodes themselves are ordered so what i&#8217;m going to do is say every node that&#8217;s on</p>



<p class="wp-block-paragraph">the left side or let&#8217;s say every descendant that&#8217;s on the left side of a node should be of a lesser</p>



<p class="wp-block-paragraph">value than that particular node every node that is on the right side of a node meaning it&#8217;s a right</p>



<p class="wp-block-paragraph">descendant of that node or part of the right subtree of the node should be greater than that</p>



<p class="wp-block-paragraph">node. Please note that this would not allow us to support duplicate values in our tree.</p>



<p class="wp-block-paragraph">You could use something like less than or equal to versus greater than if you wanted to support</p>



<p class="wp-block-paragraph">duplicate values in your tree. You could also change the T type, you know, the template type</p>



<p class="wp-block-paragraph">to a node from the data you want it to store to a list. So every single node has a list inside of it</p>



<p class="wp-block-paragraph">and the items in the list are just, you know, the regular values that you wanted. There&#8217;s a whole</p>



<p class="wp-block-paragraph">values that you wanted there&#8217;s a whole bunch of different ways you could actually do that</p>



<p class="wp-block-paragraph">but for now we&#8217;re going to see our trees don&#8217;t support duplicate values</p>



<p class="wp-block-paragraph">so okay we don&#8217;t support duplicate values but we need you know from left to right</p>



<p class="wp-block-paragraph">the order needs to look sane and this is kind of why we&#8217;re drawing the tree in this special way</p>



<p class="wp-block-paragraph">we&#8217;re trying to make our tree really easy to debug visually so check this out the way we&#8217;ve</p>



<p class="wp-block-paragraph">drawn this every single node has its own x coordinate basically notice how the 12 there&#8217;s</p>



<p class="wp-block-paragraph">nothing underneath it at least not directly underneath it the 33 the 34 all the nodes you</p>



<p class="wp-block-paragraph">know you can draw a straight line down from the node and it shouldn&#8217;t cross over with any other</p>



<p class="wp-block-paragraph">nodes or edges so that means now that we&#8217;ve drawn it this way and we&#8217;ve made sure that all the left</p>



<p class="wp-block-paragraph">descendants are on the left and all the right descendants are on the right we could actually</p>



<p class="wp-block-paragraph">just scan our eyes from left to right and be sure that we are seeing an increasing order or</p>



<p class="wp-block-paragraph">Since duplicates aren&#8217;t allowed in this tree, we&#8217;ll say an increasing order or an ascending order.</p>



<p class="wp-block-paragraph">So if we start at 45 and then we go to the right, we should see a higher number.</p>



<p class="wp-block-paragraph">So if we look at 8, 8 is a lower number.</p>



<p class="wp-block-paragraph">So this violates the ordering rule.</p>



<p class="wp-block-paragraph">This is not a valid binary search tree.</p>



<p class="wp-block-paragraph">It&#8217;s only a valid binary tree.</p>



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



<p class="wp-block-paragraph">Okay, so I could, you know, swap the 45 and the 8 or I could just increase the value of the 8 itself.</p>



<p class="wp-block-paragraph">of the 8 itself, I think it&#8217;ll be faster for me to just increase the 8 value. So I&#8217;m going to make</p>



<p class="wp-block-paragraph">it 52 now. So we&#8217;ve got 45, 52. Then we go to the right a little bit more. It should also increase.</p>



<p class="wp-block-paragraph">So 52 should increase. I&#8217;m just going to randomly increase it to, you know, 59, let&#8217;s say. Then as</p>



<p class="wp-block-paragraph">we go to the right, we see another number here. It is 76. That already is increased from 59.</p>



<p class="wp-block-paragraph">So we don&#8217;t need, we don&#8217;t need to alter that number. Then we go to the right one more time</p>



<p class="wp-block-paragraph">that because it&#8217;s lower than 76 so I&#8217;m going to put 87 then when we go to the right one more time</p>



<p class="wp-block-paragraph">that&#8217;s the 34 down here that&#8217;s definitely a decrease so we have to increase it now so I&#8217;m</p>



<p class="wp-block-paragraph">going to say 99 how about if I just do 92 yeah so now we have a 92 down here and then if we go</p>



<p class="wp-block-paragraph">to the right a little bit more there&#8217;s the 99 and now we actually have a binary search tree let&#8217;s</p>



<p class="wp-block-paragraph">just double check all the rules first off is this a graph yeah there&#8217;s nodes and edges there&#8217;s nothing</p>



<p class="wp-block-paragraph">There&#8217;s nodes and edges.</p>



<p class="wp-block-paragraph">There&#8217;s nothing weird happening.</p>



<p class="wp-block-paragraph">There&#8217;s not like a edge floating in the middle of nowhere.</p>



<p class="wp-block-paragraph">Is this a connected graph?</p>



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



<p class="wp-block-paragraph">Because every single node, if you followed, you know, the edges, you know, could find</p>



<p class="wp-block-paragraph">every single other node.</p>



<p class="wp-block-paragraph">Like the 45, it could find, you know, the 92 node if it just followed all these edges.</p>



<p class="wp-block-paragraph">Notice again that these edges have no direction.</p>



<p class="wp-block-paragraph">There&#8217;s no arrows on them.</p>



<p class="wp-block-paragraph">So we could go up or down in any direction we want.</p>



<p class="wp-block-paragraph">So that means the 76 can find everything else.</p>



<p class="wp-block-paragraph">else and so this is a connected graph for sure okay now that we have a</p>



<p class="wp-block-paragraph">connected graph is this an acyclic graph I don&#8217;t know about you but I can&#8217;t find</p>



<p class="wp-block-paragraph">any cycles in the graph right like the 92 it goes out but it doesn&#8217;t come back in</p>



<p class="wp-block-paragraph">same thing for the 76 if we went from the 76 up to the 87 and then took a</p>



<p class="wp-block-paragraph">right turn we wouldn&#8217;t be able to come back towards it without repeating that</p>



<p class="wp-block-paragraph">same edge so this is definitely an acyclic graph all of the nodes follow</p>



<p class="wp-block-paragraph">they&#8217;re not involved in cycles is this a rooted tree yeah there&#8217;s only one common ancestor to the</p>



<p class="wp-block-paragraph">entire tree it&#8217;s the 59 it&#8217;s the highest node in the entire tree and it&#8217;s common to all other of</p>



<p class="wp-block-paragraph">all of its descendants um so let&#8217;s see i&#8217;m going to duplicate this real fast the next thing is</p>



<p class="wp-block-paragraph">we arranged it hierarchically and we added the terminology of like i think i kind of just like</p>



<p class="wp-block-paragraph">tried to sneak this one past you but we added the terminology of a left child and a right child</p>



<p class="wp-block-paragraph">and a right child.</p>



<p class="wp-block-paragraph">Right? We also added the terminology of a right subtree and a left subtree.</p>



<p class="wp-block-paragraph">Just to clarify what I mean, what do I mean by a right subtree and a left subtree.</p>



<p class="wp-block-paragraph">So imagine we&#8217;re considering the 59.</p>



<p class="wp-block-paragraph">If we consider the 59, then the 52 is the root node of the left subtree of the 59 node.</p>



<p class="wp-block-paragraph">If we consider the 59, then the 87 is the root node of the right subtree of the 59 node.</p>



<p class="wp-block-paragraph">so the right child is basically the root of the subtree uh in in you know the right child is the</p>



<p class="wp-block-paragraph">root of a subtree of the right subtree uh i think i&#8217;m getting muddled my words here okay</p>



<p class="wp-block-paragraph">anyway so we have a little bit of terminology uh the 59 is the root node that&#8217;s like the</p>



<p class="wp-block-paragraph">the only node in the entire tree without uh an ancestor or a parent so okay uh then we have a</p>



<p class="wp-block-paragraph">Note, and the entire tree doesn&#8217;t have more than two children.</p>



<p class="wp-block-paragraph">Notice how the 59 has two children.</p>



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



<p class="wp-block-paragraph">If it was three, that&#8217;s way too much.</p>



<p class="wp-block-paragraph">52 has one.</p>



<p class="wp-block-paragraph">87 has one.</p>



<p class="wp-block-paragraph">No, 87 has two children.</p>



<p class="wp-block-paragraph">99 has one child.</p>



<p class="wp-block-paragraph">92 has zero children.</p>



<p class="wp-block-paragraph">So, okay, we&#8217;re not violating that rule.</p>



<p class="wp-block-paragraph">So, we definitely have a binary tree.</p>



<p class="wp-block-paragraph">And then we, you know, kind of read from left to right.</p>



<p class="wp-block-paragraph">Let me write down the numbers this time for fun.</p>



<p class="wp-block-paragraph">physically without kind of like following edges or anything we just scan</p>



<p class="wp-block-paragraph">our eyes from left to right we can see that we have an ascending list if we</p>



<p class="wp-block-paragraph">supported duplicates it would be okay to just see a non decreasing list but we</p>



<p class="wp-block-paragraph">see an increasing list or an ascended list ascending list okay whoops this is</p>



<p class="wp-block-paragraph">left to right so this is actually a binary search tree and um i don&#8217;t know about you but i think i</p>



<p class="wp-block-paragraph">need to like you know get a snickers or something right now because uh this video has been going on</p>



<p class="wp-block-paragraph">for longer than i wanted it to go on so we&#8217;ve defined a binary search tree in future videos</p>



<p class="wp-block-paragraph">we&#8217;ll talk about how to add nodes into a binary search tree how to build the tree what&#8217;s the time</p>



<p class="wp-block-paragraph">complexity of the tree in various operations deleting nodes from a tree terminology like</p>



<p class="wp-block-paragraph">terminology like what are these nodes called things like that so anyway thank you so much</p>



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



<p class="wp-block-paragraph">i&#8217;ll see you at a later date in time</p>



<p class="wp-block-paragraph">just kidding</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">appreciate it i do hope you did learn something and have some fun uh if you could do me a please</p>



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



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



<p class="wp-block-paragraph">It&#8217;s a website that you&#8217;re looking at right now</p>



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



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



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



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



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



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



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



<p class="wp-block-paragraph">to see, please leave a comment.</p>



<p class="wp-block-paragraph">Or if you just want to say, Hey, what&#8217;s up?</p>



<p class="wp-block-paragraph">What&#8217;s going on?</p>



<p class="wp-block-paragraph">You know, just send me a comment, whatever.</p>



<p class="wp-block-paragraph">I also wake up for those in the middle of the night.</p>



<p class="wp-block-paragraph">I get, I wake up in a cold sweat and I&#8217;m like, it would really, it would really, it</p>



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



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



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



<p class="wp-block-paragraph">and enjoy the cool music</p>



<p class="wp-block-paragraph">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/binary-search-tree-definition-explained-step-by-step/">Binary Search Tree Definition Explained Step by Step</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/binary-search-tree-definition-explained-step-by-step/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Mastering System Services in x86-64 Assembly Programming</title>
		<link>https://www.NeuralLantern.com/mastering-system-services-in-x86-64-assembly-programming/</link>
					<comments>https://www.NeuralLantern.com/mastering-system-services-in-x86-64-assembly-programming/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 18 Aug 2025 14:42:23 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[coding tutorials]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[exit codes]]></category>
		<category><![CDATA[file descriptors]]></category>
		<category><![CDATA[file operations]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[standard output]]></category>
		<category><![CDATA[syscalls]]></category>
		<category><![CDATA[system calls]]></category>
		<category><![CDATA[system services]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=205</guid>

					<description><![CDATA[<p>Learn x86-64 assembly system services! Master syscalls, file descriptors, and program exits with clear examples in this coding tutorial. #AssemblyProgramming</p>
<p>The post <a href="https://www.NeuralLantern.com/mastering-system-services-in-x86-64-assembly-programming/">Mastering System Services in x86-64 Assembly Programming</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 loading="lazy" title="Mastering System Services in x86-64 Assembly Programming" width="1380" height="776" src="https://www.youtube.com/embed/-ukGDzRNTxU?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">Ready to master x86-64 assembly? This video breaks down system services, showing you how to use syscalls to print messages, handle file descriptors, and exit programs cleanly. We’ll walk through a real assembly program, explain key concepts like standard output and file handles, and share tips from a top book on the subject. Whether you’re new to assembly or sharpening your skills, this tutorial is packed with clear examples and practical advice. Subscribe for more coding deep dives, and check out our upcoming file I/O video! #AssemblyProgramming #SystemCalls #x86_64 #CodingTutorials</p>



<p class="wp-block-paragraph">Introduction to System Services 00:00:00<br>System Services in x86-64 Assembly 00:00:04<br>Recommended Book on Assembly 00:00:35<br>What is a System Service? 00:01:01<br>Example Assembly Program 00:01:21<br>Syscall Instruction Explanation 00:02:52<br>Standard Output and File Descriptors 00:04:01<br>Printing a String with Syscall 00:05:16<br>Exiting a Program with Syscall 00:07:21<br>Exit Codes and Program Success 00:08:02<br>Book Reference for System Services 00:09:51<br>Detailed System Write Service 00:11:01<br>Checking System Call Return Values 00:12:08<br>File Operations with System Calls 00:14:35<br>Closing Files and Best Practices 00:15:50<br>Other System Call Codes 00:16:43<br>File Permissions and Modes 00:17:20<br>Handling System Call Results 00:19:03<br>Future Video Plans and Wrap-Up 00:20:40<br>Call to Subscribe and Outro 00:21:28</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>
</ul>



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">Hey everybody in this video we&#8217;re going to talk a little bit about system services</p>



<p class="wp-block-paragraph">in an x86-64 machine while programming in assembly</p>



<p class="wp-block-paragraph">this video is going to focus on yasm assembly but this should also work for any other assembly</p>



<p class="wp-block-paragraph">language that you program as long as you have the right kind of cpu x86-64 is also known as</p>



<p class="wp-block-paragraph">that in mind okay so for starters let&#8217;s see here&#8217;s a wonderful book i love pushing this book i did</p>



<p class="wp-block-paragraph">not write it the person who wrote it is a genius it&#8217;s called x86 64 assembly language programming</p>



<p class="wp-block-paragraph">with the boot to it&#8217;s a free and open source book so anybody can go get it find his website</p>



<p class="wp-block-paragraph">and here&#8217;s the version i&#8217;m using and honestly a really old version of this book is still really</p>



<p class="wp-block-paragraph">good he just keeps you know making little improvements but it was it was good even a</p>



<p class="wp-block-paragraph">even a long time ago. Anyway, so what is a system service? For starters, before I go into this book</p>



<p class="wp-block-paragraph">a little bit more, what is the system service? Suppose for the sake of argument that I&#8217;ve</p>



<p class="wp-block-paragraph">already written an assembly program, a pure assembly program, not a hybrid module program</p>



<p class="wp-block-paragraph">or anything. If you want to learn assembly, see my other videos, but for now I&#8217;m just going to</p>



<p class="wp-block-paragraph">assume you kind of already know how, so I&#8217;m just going to open up a pre-made program here.</p>



<p class="wp-block-paragraph">I&#8217;m going to do nano so I can edit the assembly file just so you can see what&#8217;s inside of here.</p>



<p class="wp-block-paragraph">just so you can see what&#8217;s inside of here.</p>



<p class="wp-block-paragraph">Okay, so I hope the red is not too hard to see.</p>



<p class="wp-block-paragraph">Oh, the tabs got ruined on Nano.</p>



<p class="wp-block-paragraph">Hang on, let me see if I can just open this up in Genie.</p>



<p class="wp-block-paragraph">Should have done that to begin with, okay.</p>



<p class="wp-block-paragraph">Yeah, okay, I wrote it in Genie</p>



<p class="wp-block-paragraph">and I guess I don&#8217;t have the tabs set up in Nano very well.</p>



<p class="wp-block-paragraph">Okay, so here&#8217;s like my assembly program.</p>



<p class="wp-block-paragraph">You know, I just have a little string here</p>



<p class="wp-block-paragraph">string length and CRLF for like a line feed and some file descriptors and stuff</p>



<p class="wp-block-paragraph">that I talk about in other videos here&#8217;s the entry point of the program just like</p>



<p class="wp-block-paragraph">a little start entry point because it&#8217;s pure assembly and all I&#8217;m gonna do is</p>



<p class="wp-block-paragraph">I&#8217;m gonna print a message right I&#8217;m just gonna print a little hello message and</p>



<p class="wp-block-paragraph">then call on CRLF all CRLF does is just it just prints a new line so the program</p>



<p class="wp-block-paragraph">if i run it right now you&#8217;ll just see what happens if i say let&#8217;s see clear and make run</p>



<p class="wp-block-paragraph">then you can see there&#8217;s some stuff happening up here in the make file which i forgot to update</p>



<p class="wp-block-paragraph">the title ignore that basically the real action is this line right here hello i&#8217;m printing using</p>



<p class="wp-block-paragraph">a system service if you already know assembly then you have probably already seen this before</p>



<p class="wp-block-paragraph">and it seems a little boring that&#8217;s good but i just want you to know that we&#8217;re using a system</p>



<p class="wp-block-paragraph">system service to actually do the printing.</p>



<p class="wp-block-paragraph">Kind of take it for granted here.</p>



<p class="wp-block-paragraph">This little instruction called syscall.</p>



<p class="wp-block-paragraph">What is a syscall?</p>



<p class="wp-block-paragraph">Well, it&#8217;s just basically you set up some variables, some,</p>



<p class="wp-block-paragraph">some codes, some data, whatever in, in some special registers.</p>



<p class="wp-block-paragraph">One of the registers lets the system know what you want it to do.</p>



<p class="wp-block-paragraph">One of the registers is like, or I guess all the following registers</p>



<p class="wp-block-paragraph">if they&#8217;re needed, are just input to the system call that you&#8217;re trying to do.</p>



<p class="wp-block-paragraph">do so in this case rax is letting the system know i want you to do this this code i want you to do</p>



<p class="wp-block-paragraph">this certain thing i have a defined set up here called system right but if you look up in my</p>



<p class="wp-block-paragraph">little defines area it&#8217;s just a number one so that means if i set the number one in rax i&#8217;m letting</p>



<p class="wp-block-paragraph">the system know that i want it to write to some pipe to some file then you know depending on what</p>



<p class="wp-block-paragraph">code you&#8217;re using rdi rsi rdx and other things may be needed or maybe not be needed at all</p>



<p class="wp-block-paragraph">in this case when i&#8217;m writing somewhere it wants three arguments so i have to use rdi rsi and rdx</p>



<p class="wp-block-paragraph">because those are the three arguments the first argument that it wants is where to print</p>



<p class="wp-block-paragraph">see my other videos for a more in-depth explanation of file descriptors and pipes but just long story</p>



<p class="wp-block-paragraph">pipes standard input standard output and standard error standard out is a pipe</p>



<p class="wp-block-paragraph">number one let&#8217;s see where&#8217;s that yeah it&#8217;s just assigned a number one so</p>



<p class="wp-block-paragraph">every program has its own number one pipe which is just standard output and I</p>



<p class="wp-block-paragraph">guess I should say that it gets piped to file descriptor one it&#8217;s kind of like an</p>



<p class="wp-block-paragraph">ambiguous term sort of but the system right call will write data to a file</p>



<p class="wp-block-paragraph">file and if i give it a file handle of just one that lets the system know oh actually i want to</p>



<p class="wp-block-paragraph">write to this standard output for the one particular process that we&#8217;re inside of but</p>



<p class="wp-block-paragraph">you could open any file that you want and get like a file handle to it and the operating system will</p>



<p class="wp-block-paragraph">realize oh you know i i associate that file handle i gave you with this certain open file that you</p>



<p class="wp-block-paragraph">created or opened for reading or whatever um and so when you give the handle back it knows where</p>



<p class="wp-block-paragraph">knows where to write. So in this case,</p>



<p class="wp-block-paragraph">it&#8217;s treating the standard output just like a file,</p>



<p class="wp-block-paragraph">just a file with a special handle of just one.</p>



<p class="wp-block-paragraph">I&#8217;ll talk about that more in another video.</p>



<p class="wp-block-paragraph">Then the next two arguments it wants is just the,</p>



<p class="wp-block-paragraph">a pointer to the first character of the message that you want to print.</p>



<p class="wp-block-paragraph">And then just an integer representing the length of the message that you want</p>



<p class="wp-block-paragraph">to print. And if we just look up here again at my little hello,</p>



<p class="wp-block-paragraph">my hello symbol points to an array of bytes, just characters.</p>



<p class="wp-block-paragraph">bytes just characters there is no null terminator on there it&#8217;s just bytes only</p>



<p class="wp-block-paragraph">and so when I give it you know message hello by itself what I&#8217;m really giving</p>



<p class="wp-block-paragraph">it is a pointer to that H letter just a pointer to the memory look you know I&#8217;m</p>



<p class="wp-block-paragraph">pointing to the memory location of the very first character in that string and</p>



<p class="wp-block-paragraph">then for the length I&#8217;m just using a special token little shortcut thing</p>



<p class="wp-block-paragraph">where you just put a dollar sign minus and then the name of another string and</p>



<p class="wp-block-paragraph">and then the name of another string,</p>



<p class="wp-block-paragraph">and then the assembler will compute the length of that string.</p>



<p class="wp-block-paragraph">So this right here is the same thing as me</p>



<p class="wp-block-paragraph">just typing the exact length of the string.</p>



<p class="wp-block-paragraph">I don&#8217;t even know what it is.</p>



<p class="wp-block-paragraph">I don&#8217;t feel like counting it.</p>



<p class="wp-block-paragraph">You can at home if you want.</p>



<p class="wp-block-paragraph">One, two, three, four, I&#8217;m not gonna do it.</p>



<p class="wp-block-paragraph">Anyway, so we basically just say,</p>



<p class="wp-block-paragraph">you know, I would like to write somewhere.</p>



<p class="wp-block-paragraph">I would like to write to the standard output file,</p>



<p class="wp-block-paragraph">file handle, I would like to, you know, to that pipe.</p>



<p class="wp-block-paragraph">I would like to write, you know, this string,</p>



<p class="wp-block-paragraph">you know this string and then i would like to tell you that this is how long that string is</p>



<p class="wp-block-paragraph">and then after you set all that stuff up you just say system call and the system will go ahead and</p>



<p class="wp-block-paragraph">do all of the hard work for you it&#8217;ll go figure out how to actually write data to the file and</p>



<p class="wp-block-paragraph">you know figure out how to do everything that it&#8217;s supposed to do and all you have to do is</p>



<p class="wp-block-paragraph">a little system call here right after this you can see that i have a call crlf that&#8217;s just a function</p>



<p class="wp-block-paragraph">call i talked about functions in other videos but basically you know i&#8217;m just calling a function here</p>



<p class="wp-block-paragraph">you know i&#8217;m just calling a function here and it does the same thing it makes a system call but</p>



<p class="wp-block-paragraph">instead of printing the hello string it&#8217;s printing my crlf string and if you just kind of look at what</p>



<p class="wp-block-paragraph">my crlf is it&#8217;s just these two characters 13 and 10 so like you know slash r slash n for carriage</p>



<p class="wp-block-paragraph">return line feed that&#8217;s why i call it crlf so all this program does just print a message and then a</p>



<p class="wp-block-paragraph">new line and then at the very end it does another system call to properly exit the program if this</p>



<p class="wp-block-paragraph">hybrid program and I had like the entry point of main, you know, that the GCC libraries give you,</p>



<p class="wp-block-paragraph">then I would probably just want to return at the end of this function. But since this is pure</p>



<p class="wp-block-paragraph">assembly, I can actually just exit the program and be fine. So exit with success is a different</p>



<p class="wp-block-paragraph">system call code. Notice how I have the symbol system exit loaded into RAX. And if you look up</p>



<p class="wp-block-paragraph">here, system exit is just the code 60. So if I send a code of one into RAX, that means I want to write</p>



<p class="wp-block-paragraph">a code of 60 somewhere that means i want to exit the program the only argument that takes is rdi</p>



<p class="wp-block-paragraph">which is just what is the exit code that you want to return to the operating system so um</p>



<p class="wp-block-paragraph">if you recall from some of my other videos let&#8217;s see if we do echo hello echo always succeeds at</p>



<p class="wp-block-paragraph">least as far as i know there&#8217;s like it&#8217;s like really hard to make it fail if i echo hello then</p>



<p class="wp-block-paragraph">the program echo launches and it will succeed by just printing the word hello like see how it just</p>



<p class="wp-block-paragraph">like see how it just does that so that means after that command i could echo the special</p>



<p class="wp-block-paragraph">variable dollar sign question mark just to see what the echo command exited with right it should</p>



<p class="wp-block-paragraph">be a zero because it succeeded so you can see now a zero under the hello on the other hand</p>



<p class="wp-block-paragraph">if i concatenate um let&#8217;s say a file that exists i should also get you know an error code that is a</p>



<p class="wp-block-paragraph">So it&#8217;s a zero at the very end.</p>



<p class="wp-block-paragraph">But if I try to concatenate a file that doesn&#8217;t actually exist,</p>



<p class="wp-block-paragraph">I&#8217;ll put a OS release two because that doesn&#8217;t exist.</p>



<p class="wp-block-paragraph">Notice how the cat command fails because it couldn&#8217;t find the file and the exit</p>



<p class="wp-block-paragraph">code is a one.</p>



<p class="wp-block-paragraph">So all you&#8217;re doing when setting RDI here is just controlling what exit code you</p>



<p class="wp-block-paragraph">want your program to exit with.</p>



<p class="wp-block-paragraph">This isn&#8217;t very convenient for a human running a program.</p>



<p class="wp-block-paragraph">Maybe if you want to look at the exit code for some reason, but this is really,</p>



<p class="wp-block-paragraph">really convenient for programs that want to automate other programs.</p>



<p class="wp-block-paragraph">programs just keep in mind if you have a program that is executing another</p>



<p class="wp-block-paragraph">program and it wants to see if that program succeeded the exit code is one</p>



<p class="wp-block-paragraph">of the easiest ways to find out if the program succeeded so this is the whole</p>



<p class="wp-block-paragraph">idea of this entire program we&#8217;re just using two system calls to print</p>



<p class="wp-block-paragraph">something and then to exit the program hopefully this makes sense and now I</p>



<p class="wp-block-paragraph">want to go show you this wonderful wonderful book if I didn&#8217;t already say</p>



<p class="wp-block-paragraph">If I didn&#8217;t already say this before, sorry if I already did.</p>



<p class="wp-block-paragraph">This book is written by a genius.</p>



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



<p class="wp-block-paragraph">Here&#8217;s the guy who wrote the book.</p>



<p class="wp-block-paragraph">And here&#8217;s the version that I&#8217;m using, but old versions are good.</p>



<p class="wp-block-paragraph">You can get this book for free on his website if you go find it.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to go to this assembly book, which deals with a lot of Yasm assembly tips</p>



<p class="wp-block-paragraph">and tricks and tutorials and explanations and things.</p>



<p class="wp-block-paragraph">And I&#8217;m going to go to the section labeled after system services.</p>



<p class="wp-block-paragraph">So for me, this is a chapter 13 system services, just to give you a little explanation of what</p>



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



<p class="wp-block-paragraph">You know, this is basically what I said.</p>



<p class="wp-block-paragraph">It gives you, you know, a chance for your application to ask the operating system to</p>



<p class="wp-block-paragraph">do something for you that you don&#8217;t want to program from scratch in assembly or that you</p>



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



<p class="wp-block-paragraph">But here&#8217;s the real juicy details here.</p>



<p class="wp-block-paragraph">If we go to, yeah, yeah.</p>



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



<p class="wp-block-paragraph">C, which is section 23.0 system services.</p>



<p class="wp-block-paragraph">There&#8217;s a whole bunch of system services in here that you can, uh, that you can read about.</p>



<p class="wp-block-paragraph">So for starters, if we go down a little bit, notice how it says basic system services.</p>



<p class="wp-block-paragraph">If we go down a little bit, you can see right here, the system write service that we&#8217;ve</p>



<p class="wp-block-paragraph">been using in the sample assembly program.</p>



<p class="wp-block-paragraph">You know, when we&#8217;re just printing, we say code one is to write something.</p>



<p class="wp-block-paragraph">And so this is what the table says.</p>



<p class="wp-block-paragraph">you place inside of RAX before syscall.</p>



<p class="wp-block-paragraph">Call code 1 is going to be to write characters and then it tells you what arguments it needs.</p>



<p class="wp-block-paragraph">So like RDI is the file descriptor where do you want to write to.</p>



<p class="wp-block-paragraph">Just like I said before you could give it a file handle that you already received from</p>



<p class="wp-block-paragraph">opening a file or creating a file.</p>



<p class="wp-block-paragraph">Or you could give it 0 or 1 or 2 if you want to try to write to one of the pipes although</p>



<p class="wp-block-paragraph">actually file descriptor 0 is standard input I don&#8217;t think that would actually work.</p>



<p class="wp-block-paragraph">zero to reading if you wanted to read the user&#8217;s input or or you know any input that that program</p>



<p class="wp-block-paragraph">was given through its standard input rsi is just the address of the characters to write like we</p>



<p class="wp-block-paragraph">talked about before and then rdx is the count of the characters to write notice how there are no</p>



<p class="wp-block-paragraph">more arguments and that&#8217;s why i only gave it three arguments besides rex notice how it says here if</p>



<p class="wp-block-paragraph">unsuccessful it returns a negative value if successful it returns the count of characters</p>



<p class="wp-block-paragraph">count of characters actually written this is a really really good idea if you think about it</p>



<p class="wp-block-paragraph">um a lot of new programmers they don&#8217;t really look at return codes when they call system services</p>



<p class="wp-block-paragraph">or built-in c functions or built-in c++ functions they just kind of call it and hope for the best</p>



<p class="wp-block-paragraph">but imagine if you uh if you try to write a huge long string in uh using system write call code one</p>



<p class="wp-block-paragraph">and maybe some of the characters did write but the system decided to only write i don&#8217;t know half for</p>



<p class="wp-block-paragraph">reason maybe it ran out of buffer maybe you know it interrupted you know your right or something</p>



<p class="wp-block-paragraph">like that so it could happen what that would mean is that the return value it would be greater than</p>



<p class="wp-block-paragraph">zero indicating that some characters were written but it&#8217;ll tell you exactly how many characters</p>



<p class="wp-block-paragraph">were written so you&#8217;ll know that you got to keep calling the sys call until all the characters were</p>



<p class="wp-block-paragraph">successfully written why would you do this i don&#8217;t know maybe you have a gigantic string like maybe</p>



<p class="wp-block-paragraph">like a several gigabyte file and you wanted to copy it to another file so you&#8217;ll be calling</p>



<p class="wp-block-paragraph">system write over and over again and system write it&#8217;s only going to write so many bytes at the same</p>



<p class="wp-block-paragraph">time so you use the return value to figure out how far forward in the read buffer you need to advance</p>



<p class="wp-block-paragraph">or you know whatever so that you can well write the entire complete file or the entire complete</p>



<p class="wp-block-paragraph">string or whatever it is without any gaps without it being truncated and so forth and then of course</p>



<p class="wp-block-paragraph">And then of course, if it returns a negative value, then it totally failed.</p>



<p class="wp-block-paragraph">Like you tried to write to a bad file handle, like standard input or a file handle that</p>



<p class="wp-block-paragraph">was closed or a file handle that was open for reading only, something like that.</p>



<p class="wp-block-paragraph">And then you can use some if else, you know, branching logic.</p>



<p class="wp-block-paragraph">I mean, not if else in assembly, it&#8217;s just going to be comparison and conditional jumping</p>



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



<p class="wp-block-paragraph">But you can have more control over your program, right?</p>



<p class="wp-block-paragraph">Because you want your program to be able to respond to errors.</p>



<p class="wp-block-paragraph">Let me just quickly cruise through all of the other options and let you know that</p>



<p class="wp-block-paragraph">and let you know that in another video I&#8217;m gonna do a full tutorial for how to</p>



<p class="wp-block-paragraph">copy one file to another by opening one file for reading and then like using</p>



<p class="wp-block-paragraph">system calls to read from it and then open another file for writing and then</p>



<p class="wp-block-paragraph">you know use system calls to write to it and I&#8217;m gonna do that looping buffer</p>



<p class="wp-block-paragraph">stuff that I talked about before anyway so but for now I&#8217;m just gonna go through</p>



<p class="wp-block-paragraph">the rest of these system call codes let&#8217;s see so first off we got open well</p>



<p class="wp-block-paragraph">Well, if you want to open a file, then you just give it call code 2 and you pass in the</p>



<p class="wp-block-paragraph">address here, you know, RDI, you say, here&#8217;s the address of a null terminated file name.</p>



<p class="wp-block-paragraph">So that means somewhere in memory, you have to have the file name that you want to open</p>



<p class="wp-block-paragraph">with a null terminator, like a zero at the end of the string, either the full path or</p>



<p class="wp-block-paragraph">relative path to wherever the program is currently running.</p>



<p class="wp-block-paragraph">And then RSI is going to be file status flags.</p>



<p class="wp-block-paragraph">And what does that mean?</p>



<p class="wp-block-paragraph">And what does that mean? We can actually just search for that.</p>



<p class="wp-block-paragraph">I&#8217;m going to say control C to copy,</p>



<p class="wp-block-paragraph">and then I&#8217;m going to do control F to search for that.</p>



<p class="wp-block-paragraph">And let&#8217;s see what page of mine right now, 340.</p>



<p class="wp-block-paragraph">If I go down to the search results for that,</p>



<p class="wp-block-paragraph">it just explains what the file modes are. So if you put value zero,</p>



<p class="wp-block-paragraph">that means read only you&#8217;re opening the file and read only mode value one.</p>



<p class="wp-block-paragraph">That means a write only a value to allows reading and writing to the file.</p>



<p class="wp-block-paragraph">So if you don&#8217;t know what that means, well, there it was.</p>



<p class="wp-block-paragraph">What was I just on before?</p>



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



<p class="wp-block-paragraph">Oh, gosh.</p>



<p class="wp-block-paragraph">Completely lost my…</p>



<p class="wp-block-paragraph">Oh, yeah, 40.</p>



<p class="wp-block-paragraph">Okay, so opening pretty easy, right?</p>



<p class="wp-block-paragraph">Like if you didn&#8217;t know this before, you didn&#8217;t necessarily read me.</p>



<p class="wp-block-paragraph">You could just look at this table and go, what do I want to do?</p>



<p class="wp-block-paragraph">I want to open.</p>



<p class="wp-block-paragraph">So here&#8217;s the code.</p>



<p class="wp-block-paragraph">Here&#8217;s the address that it needs.</p>



<p class="wp-block-paragraph">When you&#8217;re done opening a file, when you&#8217;re done working with a file,</p>



<p class="wp-block-paragraph">you want to close that file unless the whole program is going to terminate.</p>



<p class="wp-block-paragraph">unless the whole program is going to terminate at that point you i mean maybe you have a function</p>



<p class="wp-block-paragraph">that gets called somewhat often and you kind of always have to open a file and sort of look at</p>



<p class="wp-block-paragraph">something or write something and maybe you want to close it when the function is over right you</p>



<p class="wp-block-paragraph">don&#8217;t want to have a file handle just open forever that&#8217;s a waste of memory and it might introduce</p>



<p class="wp-block-paragraph">bad behavior to your program if you just have a bunch of file handles floating around that you</p>



<p class="wp-block-paragraph">forgot about so it&#8217;s proper to close a file when you&#8217;re finished with it that&#8217;s just going to be</p>



<p class="wp-block-paragraph">and it only wants one argument.</p>



<p class="wp-block-paragraph">It just wants the file descriptor of the file that you wanted to close.</p>



<p class="wp-block-paragraph">So if you open a file for a reading or to create it,</p>



<p class="wp-block-paragraph">you get a file descriptor back.</p>



<p class="wp-block-paragraph">You should probably hang on to it somewhere on the stack</p>



<p class="wp-block-paragraph">or like in a global variable or like, you know, in a register or whatever.</p>



<p class="wp-block-paragraph">Then when you&#8217;re done doing something to that file</p>



<p class="wp-block-paragraph">and you&#8217;re sure that it&#8217;s time to close it,</p>



<p class="wp-block-paragraph">just give that handle right back when you call code three to close it.</p>



<p class="wp-block-paragraph">Then you can sort of like seek, you know,</p>



<p class="wp-block-paragraph">forward and backwards to the file if you want to.</p>



<p class="wp-block-paragraph">You can fork the current process.</p>



<p class="wp-block-paragraph">You can fork the current process that&#8217;s kind of advanced for this video.</p>



<p class="wp-block-paragraph">You can exit.</p>



<p class="wp-block-paragraph">Remember before we exited from that program with code 60.</p>



<p class="wp-block-paragraph">So that&#8217;s it. Call code 60.</p>



<p class="wp-block-paragraph">If you didn&#8217;t watch this video, you could have gone through this table and just said,</p>



<p class="wp-block-paragraph">Oh, you know, I want to terminate the executing process.</p>



<p class="wp-block-paragraph">I do a system call with code 60 and I&#8217;ll give the exit status to RDI,</p>



<p class="wp-block-paragraph">which is typically zero for success.</p>



<p class="wp-block-paragraph">Bunch of other stuffs create, get the time of day,</p>



<p class="wp-block-paragraph">and then the file modes that we talked about before.</p>



<p class="wp-block-paragraph">we talked about before and then file permissions. Let&#8217;s see, where&#8217;s the permission. I think it&#8217;s</p>



<p class="wp-block-paragraph">when you want to create a file. Yeah, right here. If we wanted to create a file, that&#8217;s code 85.</p>



<p class="wp-block-paragraph">RDI is the name of the file, you know, like a pointer to the string. RSI is the file mode flags.</p>



<p class="wp-block-paragraph">And then you&#8217;re thinking like, what are the file mode flags? Just go down here and then here you</p>



<p class="wp-block-paragraph">go. All the file modes that you could ever want. You just look at it and go, well, I guess I can</p>



<p class="wp-block-paragraph">I guess I can just copy paste this number right here and it&#8217;ll end up being, you know,</p>



<p class="wp-block-paragraph">whatever is described on the left and the right.</p>



<p class="wp-block-paragraph">Like the group has read, write and execute permissions and so forth.</p>



<p class="wp-block-paragraph">I mean, really, if you look at this long enough, you&#8217;ll kind of realize that this is a quad</p>



<p class="wp-block-paragraph">word because it has a Q on the end of it.</p>



<p class="wp-block-paragraph">And the rest is just an octal file permissions notation where this first number here, notice</p>



<p class="wp-block-paragraph">how the first two are zeros, but this first actual number here is something that applies</p>



<p class="wp-block-paragraph">to the user and then the next one applies to the group the next one applies to others who are not</p>



<p class="wp-block-paragraph">part of the user or group and this video is not about file permissions I&#8217;ll probably make one</p>



<p class="wp-block-paragraph">later if I think people will actually watch it but yeah you can just kind of like look it up here if</p>



<p class="wp-block-paragraph">you want to and just copy paste that into a define somewhere let&#8217;s see what else I just want to show</p>



<p class="wp-block-paragraph">you a little code snip just to illustrate a little bit more what I was talking about before</p>



<p class="wp-block-paragraph">for. People usually make this mistake, they&#8217;ll do see this is what people will usually do like new</p>



<p class="wp-block-paragraph">programmers. Suppose this is a function pretend we&#8217;re in C or C++. For the time being, you call</p>



<p class="wp-block-paragraph">some sort of a system function or an API of some other library. And you just you just hope that it</p>



<p class="wp-block-paragraph">works, you just call it and then your program continues underneath, right? It&#8217;s a bad idea,</p>



<p class="wp-block-paragraph">because maybe that failed, maybe there&#8217;s some action you need to take. So instead, it&#8217;s a really</p>



<p class="wp-block-paragraph">really good idea to check the return result or i should say an even better idea is check the</p>



<p class="wp-block-paragraph">documentation usually these types of you know c system calls or function calls or api calls or</p>



<p class="wp-block-paragraph">whatever they&#8217;ll return something indicating success or failure sometimes the documentation</p>



<p class="wp-block-paragraph">will tell you oh you&#8217;ve got to put like a pointer to some other data structure in the arguments</p>



<p class="wp-block-paragraph">and then the result comes there of course just read the documentation but usually it&#8217;s just</p>



<p class="wp-block-paragraph">So you want to grab the result and then check the result.</p>



<p class="wp-block-paragraph">In this case I&#8217;m assuming this is sort of a standard function that will return zero</p>



<p class="wp-block-paragraph">or greater on success like the system write call or the system read call and return less</p>



<p class="wp-block-paragraph">than zero if it fails.</p>



<p class="wp-block-paragraph">So here I&#8217;m just responding, you know, if it&#8217;s like if the result was greater than zero</p>



<p class="wp-block-paragraph">then it&#8217;s like, yay we can proceed.</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll continue with my program in some way and if it&#8217;s not then I&#8217;ll print a complaint</p>



<p class="wp-block-paragraph">then I&#8217;ll print a complaint to the user and then I&#8217;ll take some sort of an action</p>



<p class="wp-block-paragraph">like I&#8217;ll write to a log file, send an email somewhere, you know,</p>



<p class="wp-block-paragraph">do something to try and recover from the error and alert admins to the error.</p>



<p class="wp-block-paragraph">And in this case, I&#8217;m just throwing an exception.</p>



<p class="wp-block-paragraph">This video is not about exceptions, but so I just want you to know,</p>



<p class="wp-block-paragraph">this is a good design pattern to not just discard the result of a call to</p>



<p class="wp-block-paragraph">something. You should check the result and see what you&#8217;re supposed to do.</p>



<p class="wp-block-paragraph">Let&#8217;s see, what else did I want to tip?</p>



<p class="wp-block-paragraph">system services, file handles.</p>



<p class="wp-block-paragraph">Yeah, okay, I think that&#8217;s everything that I wanted to tell you.</p>



<p class="wp-block-paragraph">In future videos, I&#8217;m going to talk about, you know, some of this stuff much more in-depth,</p>



<p class="wp-block-paragraph">like with my file.io video that I&#8217;m going to publish pretty soon, which is, you know,</p>



<p class="wp-block-paragraph">it lets you read and write to files, and it uses a loop to read a little bit at a time.</p>



<p class="wp-block-paragraph">Okay, thank you so much for watching this video.</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">I&#8217;m outie.</p>



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



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



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



<p class="wp-block-paragraph">help make more videos and grow this community so we&#8217;ll be able to do more</p>



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



<p class="wp-block-paragraph">in general so please do do me a kindness and and subscribe you know sometimes</p>



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



<p class="wp-block-paragraph">somebody subscribed or followed 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 so you could do it as a nice favor</p>



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



<p class="wp-block-paragraph">code which you can scan in order to go to the website which I think is also</p>



<p class="wp-block-paragraph">named somewhere at the bottom of this video and it&#8217;ll take you to my main</p>



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



<p class="wp-block-paragraph">services and tutorials and things that I offer and all that good stuff and if</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">send me a comment whatever i also wake up for those in the middle of the night i get i wake</p>



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



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



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



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/mastering-system-services-in-x86-64-assembly-programming/">Mastering System Services in x86-64 Assembly Programming</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/mastering-system-services-in-x86-64-assembly-programming/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How Your CPU Works: Fetch, Decode, Execute Explained</title>
		<link>https://www.NeuralLantern.com/how-your-cpu-works-fetch-decode-execute-explained/</link>
					<comments>https://www.NeuralLantern.com/how-your-cpu-works-fetch-decode-execute-explained/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 10 Aug 2025 06:23:48 +0000</pubDate>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[ALU]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[code cache]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[CPU]]></category>
		<category><![CDATA[CPU clock]]></category>
		<category><![CDATA[data cache]]></category>
		<category><![CDATA[fetch decode execute]]></category>
		<category><![CDATA[instruction pointer]]></category>
		<category><![CDATA[processor basics]]></category>
		<category><![CDATA[RIP register]]></category>
		<category><![CDATA[tech tutorials]]></category>
		<category><![CDATA[x86 processors]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=199</guid>

					<description><![CDATA[<p>Discover how your CPU fetches, decodes, and executes instructions in this beginner-friendly guide. Learn about the clock, RIP register, and ALU in a simple breakdown of computer architecture.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-your-cpu-works-fetch-decode-execute-explained/">How Your CPU Works: Fetch, Decode, Execute Explained</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 loading="lazy" title="How Your CPU Works: Fetch, Decode, Execute Explained" width="1380" height="776" src="https://www.youtube.com/embed/UXMDPnMfcAM?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">Get a clear, beginner-friendly dive into how your CPU works! This video explains the fetch-decode-execute cycle, the role of the clock, instruction pointer (RIP), and units like the ALU and cache. Learn why timing matters and how your CPU handles instructions step-by-step. Perfect for gamers, coders, or anyone curious about what makes computers tick. Inspired by Kip Irvine’s Assembly Language for x86 Processors, this is your gateway to understanding computer architecture. Subscribe for more tech insights and hit that QR code to explore more tutorials! #CPU #ComputerScience #TechTutorials</p>



<p class="wp-block-paragraph">Introduction to CPU Operations 00:00:00<br>CPU Clock Explanation 00:00:17<br>Clock Cycle and Stability 00:01:07<br>Instruction Fetch from RAM 00:03:24<br>Instruction Pointer (RIP) Role 00:03:48<br>Sequential and Conditional Execution 00:04:49<br>Code Cache Functionality 00:06:24<br>Instruction Decoding Process 00:07:51<br>Control Unit Operations 00:08:33<br>Arithmetic Logic Unit (ALU) 00:08:52<br>Floating Point Unit 00:11:03<br>Data Cache Usage 00:12:08<br>Summary of CPU Process 00:14:36<br>Closing and Subscription Call 00:15:07</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>
</ul>



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">CPU #ComputerScience #TechTutorials #ComputerArchitecture #AssemblyLanguage #TechExplained #CPUBasics #LearnTech #x86Processors #Coding</p>



<p class="wp-block-paragraph">Hello there. Let&#8217;s talk about how your CPU fetches, decodes and executes instructions.</p>



<p class="wp-block-paragraph">We&#8217;re going to just do a quick surface level overview, but I hope you like it.</p>



<p class="wp-block-paragraph">Okay. So for starters, your CPU has something called the clock.</p>



<p class="wp-block-paragraph">keep my pen onto one monitor here let me just double check that real fast</p>



<p class="wp-block-paragraph">okay is it working yeah okay so</p>



<p class="wp-block-paragraph">okay so notice how we have this line that goes up and down and up and down it kind of looks like a</p>



<p class="wp-block-paragraph">little binary line uh it actually is uh sometimes your cpu is outputting a zero to this special</p>



<p class="wp-block-paragraph">and it just oscillates between zero and one constantly it goes zero one zero one zero one</p>



<p class="wp-block-paragraph">it does so very very very very fast and the reason it does that is because something needs to control</p>



<p class="wp-block-paragraph">the timing of your cpu imagine this we kind of take this for granted but when you apply electricity</p>



<p class="wp-block-paragraph">somewhere as a human you kind of feel like the electricity goes to the other place like on the</p>



<p class="wp-block-paragraph">other side of your house or your room or whatever you kind of feel like it goes there instantaneously</p>



<p class="wp-block-paragraph">really fast but in reality electricity doesn&#8217;t go faster than the speed of light so there is a limit</p>



<p class="wp-block-paragraph">which means if you have a cpu that is executing instructions really really really really fast</p>



<p class="wp-block-paragraph">you know fetching instructions from your system ram really really fast</p>



<p class="wp-block-paragraph">there&#8217;s there&#8217;s there&#8217;s the problem of trying to um to know for sure that the electricity actually</p>



<p class="wp-block-paragraph">made it uh to the other place in time uh so like what if we said let&#8217;s load some instructions from</p>



<p class="wp-block-paragraph">load some instructions from memory, they come down the wire to your CPU.</p>



<p class="wp-block-paragraph">How do we know when it actually finished coming down the wire to the CPU?</p>



<p class="wp-block-paragraph">We just take it for granted.</p>



<p class="wp-block-paragraph">But in reality, physics has a limit.</p>



<p class="wp-block-paragraph">And so the electricity needs time to get there and it needs time to stabilize.</p>



<p class="wp-block-paragraph">So the clock cycle helps us with that timing.</p>



<p class="wp-block-paragraph">So, you know, if we make the clock go too fast, your CPU becomes unstable.</p>



<p class="wp-block-paragraph">Your gamers, you gamers probably know that.</p>



<p class="wp-block-paragraph">If we make it too slow, then it&#8217;ll work.</p>



<p class="wp-block-paragraph">it&#8217;ll work, but, um, you know, we&#8217;re not getting the fastest, you know, CPU, uh, that, that</p>



<p class="wp-block-paragraph">we, that we can, we&#8217;re not getting all the performance out of it that we can.</p>



<p class="wp-block-paragraph">So we make it, we make the clock cycle go as fast as we can, uh, without making the</p>



<p class="wp-block-paragraph">CPU, uh, instable.</p>



<p class="wp-block-paragraph">And so just keep that in mind.</p>



<p class="wp-block-paragraph">That&#8217;s one component of the CPU.</p>



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



<p class="wp-block-paragraph">So then the next thing I wanted to show you, oh, and by the way, let me give credit to</p>



<p class="wp-block-paragraph">the authors of this book.</p>



<p class="wp-block-paragraph">I&#8217;m just looking at a diagram, but, um, this book is written by someone named Kip</p>



<p class="wp-block-paragraph">This book is written by someone named Kip Irvine</p>



<p class="wp-block-paragraph">and it&#8217;s titled Assembly Language for X86 Processors.</p>



<p class="wp-block-paragraph">It&#8217;s written by Pearson or it&#8217;s published by Pearson</p>



<p class="wp-block-paragraph">and the year is 2014.</p>



<p class="wp-block-paragraph">So get a copy of this book.</p>



<p class="wp-block-paragraph">Anyway, so this is a simplified CPU block diagram.</p>



<p class="wp-block-paragraph">Let me get my notes here real fast</p>



<p class="wp-block-paragraph">so I don&#8217;t forget what I was supposed to say.</p>



<p class="wp-block-paragraph">Clock ticks and alu.</p>



<p class="wp-block-paragraph">Okay, so I guess that&#8217;s pretty much it.</p>



<p class="wp-block-paragraph">Imagine now that we have a little RAM stick or some place where we can get instructions for the CPU.</p>



<p class="wp-block-paragraph">So this is like system RAM, we&#8217;ll say.</p>



<p class="wp-block-paragraph">And we&#8217;ll just pretend for the sake of argument that we already have existing circuitry</p>



<p class="wp-block-paragraph">that will easily transfer data back and forth from the RAM stick to the CPU.</p>



<p class="wp-block-paragraph">So anytime the CPU wants to grab some kind of data or like an instruction or something off the RAM stick,</p>



<p class="wp-block-paragraph">it can just do it. And that&#8217;s the arrow right there.</p>



<p class="wp-block-paragraph">keep in mind after that is that there is a special register in the CPU called the instruction pointer</p>



<p class="wp-block-paragraph">RIP in x86-64 assembly the RIP register and the RIP register it basically just holds a memory</p>



<p class="wp-block-paragraph">location telling the CPU where is the next where&#8217;s the address of the next instruction that we want</p>



<p class="wp-block-paragraph">to execute so you know imagine you have like some kind of memory address 0x I don&#8217;t know 55 and then</p>



<p class="wp-block-paragraph">an instruction on the very next actually let&#8217;s do five zero so I can increase this by eight without</p>



<p class="wp-block-paragraph">doing hex in my head we&#8217;ll say this is like the address five zero in hexadecimal so that means</p>



<p class="wp-block-paragraph">that the next time the CPU wants to execute an instruction it&#8217;s just going to ask system ram</p>



<p class="wp-block-paragraph">for that address system ram is going to give it whatever ones and zeros it has and then it can be</p>



<p class="wp-block-paragraph">decoded and executed after that and then of course we have to execute a program you know step by step</p>



<p class="wp-block-paragraph">you know step by step we have to execute more instructions and we can&#8217;t just do one instruction</p>



<p class="wp-block-paragraph">so typically in a program you execute instructions one by one by one by one by one</p>



<p class="wp-block-paragraph">and then every once in a while there might be some decision logic like you have an if else</p>



<p class="wp-block-paragraph">branch like let&#8217;s say uh you know for the for the high level people i&#8217;ll put a question mark here</p>



<p class="wp-block-paragraph">you have like an if statement and you say like if that&#8217;s true then execute this i&#8217;ll put maybe i&#8217;ll</p>



<p class="wp-block-paragraph">then we&#8217;ll just execute this other logic down here. So you know very often we execute instructions</p>



<p class="wp-block-paragraph">sequentially but every once in a while we have to jump right because if if you executed this</p>



<p class="wp-block-paragraph">if statement and you realized that this condition was not true then you could not execute the body</p>



<p class="wp-block-paragraph">that came right after the comparison instructions you would have to jump down to wherever the else</p>



<p class="wp-block-paragraph">block is so there is circuitry inside of the CPU that allows you to either increase the instruction</p>



<p class="wp-block-paragraph">instruction pointer by 8 bytes, 8 bytes because 64 bits per instruction because that&#8217;s your CPU.</p>



<p class="wp-block-paragraph">So you just kind of go sequentially or to jump somewhere because you made a decision or you&#8217;re</p>



<p class="wp-block-paragraph">like making a function call or whatever. So just keep in mind that under the hood, under this</p>



<p class="wp-block-paragraph">diagram, there is a lot more logic even than the more detailed diagrams that you can find elsewhere.</p>



<p class="wp-block-paragraph">There&#8217;s just like a lot more detail. So anyway, the RIP register just kind of increases by 8</p>



<p class="wp-block-paragraph">every time we execute an instruction,</p>



<p class="wp-block-paragraph">sometimes it&#8217;ll drastically change its address</p>



<p class="wp-block-paragraph">so we can jump execution somewhere else</p>



<p class="wp-block-paragraph">in order to make a decision.</p>



<p class="wp-block-paragraph">So just keep that in mind.</p>



<p class="wp-block-paragraph">So suppose that we just fetched an instruction from memory.</p>



<p class="wp-block-paragraph">You know, it comes in here</p>



<p class="wp-block-paragraph">and we kind of send it down the line.</p>



<p class="wp-block-paragraph">We first give it to the code cache unit.</p>



<p class="wp-block-paragraph">Your CPU is just not a solid block of stuff.</p>



<p class="wp-block-paragraph">It&#8217;s got subunits inside of it.</p>



<p class="wp-block-paragraph">And one of these units is the code cache unit.</p>



<p class="wp-block-paragraph">it&#8217;s a lot more complicated than what you&#8217;re seeing here but basically</p>



<p class="wp-block-paragraph">system ram is very slow dealing with the cpu internally is very very very fast in comparison</p>



<p class="wp-block-paragraph">so if we have like a register or some unit that we&#8217;re accessing that&#8217;s inside of the cpu it&#8217;s</p>



<p class="wp-block-paragraph">lightning fast compared to accessing the memory so that means cpus have caches which just i&#8217;m not</p>



<p class="wp-block-paragraph">going to go too much into caches for this video but basically every time we get a new instruction</p>



<p class="wp-block-paragraph">we might put it into the cache, which is like incredibly fast because it&#8217;s hardware that&#8217;s part</p>



<p class="wp-block-paragraph">of the CPU. Then maybe sometime in the future, we might want to execute the same exact instruction</p>



<p class="wp-block-paragraph">at the same exact address. Again, that&#8217;s possible. Remember we have loops. We can execute the same</p>



<p class="wp-block-paragraph">instructions a bunch of times in a row. So if we come across an instruction that we have executed</p>



<p class="wp-block-paragraph">in the past and it&#8217;s already in the code cache, then we don&#8217;t actually need to wait for memory</p>



<p class="wp-block-paragraph">to fetch the instruction for us, we can just go directly to the code cache and it&#8217;s a dramatic</p>



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



<p class="wp-block-paragraph">Remember, when you access memory, execution has to stall for quite a long time usually</p>



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



<p class="wp-block-paragraph">Okay, so that&#8217;s what the code cache does.</p>



<p class="wp-block-paragraph">Once the memory and the code cache have figured out, okay, we got the instruction and everything&#8217;s</p>



<p class="wp-block-paragraph">good, then we just sort of decode the instruction.</p>



<p class="wp-block-paragraph">We decide, you know, what is in the instruction?</p>



<p class="wp-block-paragraph">it all like data or is it all like like is it half and half and part instruction</p>



<p class="wp-block-paragraph">part data part whatever there&#8217;s lots of different formats for instructions so</p>



<p class="wp-block-paragraph">the instruction decoder just sort of like you know decides how to separate</p>



<p class="wp-block-paragraph">it and it sends data into the control unit notice how there&#8217;s a little arrow</p>



<p class="wp-block-paragraph">pointing from the instruction decoder to the instruction pointer so we could use</p>



<p class="wp-block-paragraph">that information to decide how to modify the RIP register like if we&#8217;re gonna go</p>



<p class="wp-block-paragraph">or if we&#8217;re going to jump somewhere.</p>



<p class="wp-block-paragraph">And then the, let&#8217;s see,</p>



<p class="wp-block-paragraph">when we send the instruction to the control unit,</p>



<p class="wp-block-paragraph">the control unit is not necessarily the brain</p>



<p class="wp-block-paragraph">because the whole CPU is the brain of the computer,</p>



<p class="wp-block-paragraph">but the control unit is kind of like the decider of the CPU.</p>



<p class="wp-block-paragraph">At least that&#8217;s the way I see it.</p>



<p class="wp-block-paragraph">It decides what to do.</p>



<p class="wp-block-paragraph">There&#8217;s a lot of other subunits in the CPU.</p>



<p class="wp-block-paragraph">Notice how down here we have, you know, CPU registers</p>



<p class="wp-block-paragraph">and we also have the all you.</p>



<p class="wp-block-paragraph">the all you the all you is basically the arithmetic logical unit so it&#8217;s it&#8217;s a little subunit that</p>



<p class="wp-block-paragraph">will execute mathematical instructions or logical instructions like it&#8217;ll take some bits and it&#8217;ll</p>



<p class="wp-block-paragraph">it&#8217;ll move them to the left or the right or it&#8217;ll flip the bits or whatever or it&#8217;ll add two numbers</p>



<p class="wp-block-paragraph">together the registers you know they&#8217;re very very very fast they&#8217;re like you know the variables of</p>



<p class="wp-block-paragraph">the cpu um so the control unit will examine uh whatever it&#8217;s getting and it&#8217;ll decide all right</p>



<p class="wp-block-paragraph">want us to do it wants us to add two integers together or it wants us to</p>



<p class="wp-block-paragraph">write something to memory or it wants us to do whatever right so when</p>



<p class="wp-block-paragraph">the control unit receives input remember this is all just like ones and zeros</p>



<p class="wp-block-paragraph">right so imagine if we had like a bunch of ones and zeros coming in and they&#8217;re</p>



<p class="wp-block-paragraph">just wired directly into the control unit right so then the control unit has</p>



<p class="wp-block-paragraph">its own internal circuitry that that makes it so well if I receive this</p>



<p class="wp-block-paragraph">going to tell everybody else to add numbers together and if I see some other number I&#8217;m</p>



<p class="wp-block-paragraph">going to tell everybody else to multiply numbers together so the control unit is really responsible</p>



<p class="wp-block-paragraph">for deciding what the other units need to do in order to accomplish the current instruction</p>



<p class="wp-block-paragraph">so gosh I really need to fix this I just reconfigured this thing and now it&#8217;s just like</p>



<p class="wp-block-paragraph">drawing even when I don&#8217;t want it to draw so the control unit let&#8217;s say that we do that we have</p>



<p class="wp-block-paragraph">that we have like an addition instruction we want to add two registers together the control unit will</p>



<p class="wp-block-paragraph">send this down the wire to let the all you unit know that it needs to perform addition the all</p>



<p class="wp-block-paragraph">you unit also has inputs and based on what it receives it will decide to do something it&#8217;ll</p>



<p class="wp-block-paragraph">decide to add or subtract or multiply or just whatever so the control unit sends the right</p>



<p class="wp-block-paragraph">input into the all you unit so that it knows to add or do whatever we wanted it to do</p>



<p class="wp-block-paragraph">operands like it might receive data that just came from memory it might receive registers it might</p>



<p class="wp-block-paragraph">receive just like a raw immediate number that&#8217;s embedded into the instruction on the other hand</p>



<p class="wp-block-paragraph">we might also send something into the floating point unit so the control unit it&#8217;s always sending</p>



<p class="wp-block-paragraph">all of these signals to all of its you know connected units these wires it&#8217;s not like we</p>



<p class="wp-block-paragraph">can remove these wires so there&#8217;s always something even if it&#8217;s a zero going down the line</p>



<p class="wp-block-paragraph">so the control unit will basically send the right signals down to the all you to tell it you&#8217;re</p>



<p class="wp-block-paragraph">going to add and it&#8217;ll send the right signals down to the floating point unit to say you&#8217;re</p>



<p class="wp-block-paragraph">not going to do anything or just we&#8217;re not we&#8217;re going to ignore the result there&#8217;s like all these</p>



<p class="wp-block-paragraph">logical gates that we&#8217;re not showing on this diagram where we can just like ignore the output</p>



<p class="wp-block-paragraph">result of some unit that we&#8217;re not going to use so it&#8217;s sending all these signals to these units</p>



<p class="wp-block-paragraph">Just like a quick note here, the all you only deals with integer math and, you know, bitwise</p>



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



<p class="wp-block-paragraph">Floating point numbers in the machine are stored totally differently than integers.</p>



<p class="wp-block-paragraph">So we need a totally different unit in order to process them.</p>



<p class="wp-block-paragraph">So that&#8217;s why there are two units there.</p>



<p class="wp-block-paragraph">Well, I mean, I guess depending on, you know, what CPU we&#8217;re talking about, but like as</p>



<p class="wp-block-paragraph">far as the ones that I&#8217;m aware of and that I use.</p>



<p class="wp-block-paragraph">So then, you know, the data cache, kind of the same thing.</p>



<p class="wp-block-paragraph">kind of the same thing um you know there&#8217;s like a code cache up here and a data cache down there</p>



<p class="wp-block-paragraph">so sometimes we might want to fetch data that we&#8217;ve already fetched before and we don&#8217;t want</p>



<p class="wp-block-paragraph">to hit system ram so you know we don&#8217;t want that slow down so we&#8217;ll just take it from the cache</p>



<p class="wp-block-paragraph">and um we will also populate the cache when we have a result so when this like all you is done</p>



<p class="wp-block-paragraph">adding whatever it&#8217;s going to add it&#8217;ll send its result data to the data cache and then um</p>



<p class="wp-block-paragraph">two or three or whatever then we can reuse the result in another instruction or send it back to</p>



<p class="wp-block-paragraph">memory or do whatever we want to so this is like a basic idea right like you you start off by you</p>



<p class="wp-block-paragraph">know grabbing something from memory possibly if it&#8217;s already in the cache then you wouldn&#8217;t</p>



<p class="wp-block-paragraph">actually grab it and then the instruction decoder decides you know are we gonna we&#8217;re gonna jump</p>



<p class="wp-block-paragraph">are we gonna go to the next instruction after this is over so it&#8217;ll have the next address</p>



<p class="wp-block-paragraph">notice how there&#8217;s kind of like a little cycle that goes here between code cache instruction</p>



<p class="wp-block-paragraph">decoder and instruction pointer and so you know we&#8217;re always kind of deciding what&#8217;s the next</p>



<p class="wp-block-paragraph">instruction that we&#8217;re going to execute and then we like you know send the decoded instruction to</p>



<p class="wp-block-paragraph">the control unit the control unit decides you know are you going to do some addition are we</p>



<p class="wp-block-paragraph">going to use registers or not and then you know like are you not going to do anything or are maybe</p>



<p class="wp-block-paragraph">you are going to do something in this case I&#8217;m just saying the floating point won&#8217;t do anything</p>



<p class="wp-block-paragraph">integers. And then once that is all processed, you know, it comes to the data cache and, um,</p>



<p class="wp-block-paragraph">well, we can, we can basically just, uh, you know, write it back to memory. I think this</p>



<p class="wp-block-paragraph">diagram is not showing an arrow that will write it, write it back to memory. Um,</p>



<p class="wp-block-paragraph">yeah, the code cache is definitely not going to feed directly into memory. That&#8217;s just a cache,</p>



<p class="wp-block-paragraph">but you know, so, so imagine like there&#8217;s a, there&#8217;s like another wire here that goes</p>



<p class="wp-block-paragraph">so when we have a result we can just write it into system ram</p>



<p class="wp-block-paragraph">let me see if i forgot to say anything rip</p>



<p class="wp-block-paragraph">yeah that&#8217;s basically all i wanted to say okay so now you have a fairly good idea of at least</p>



<p class="wp-block-paragraph">one level deeper of what&#8217;s going on inside of your computer how the cpu in a general way loads</p>



<p class="wp-block-paragraph">and all these little subunits getting told what to do</p>



<p class="wp-block-paragraph">and the results getting piped back to the instruction pointer</p>



<p class="wp-block-paragraph">and also to system RAM and so forth.</p>



<p class="wp-block-paragraph">And it just keeps going like this again and again and again.</p>



<p class="wp-block-paragraph">And also just remember that at certain points</p>



<p class="wp-block-paragraph">during even one cycle, we wait for a clock tick.</p>



<p class="wp-block-paragraph">I&#8217;m not gonna say exactly when we wait</p>



<p class="wp-block-paragraph">because it&#8217;s kind of specific to what&#8217;s going on</p>



<p class="wp-block-paragraph">to be on the scope of this video.</p>



<p class="wp-block-paragraph">But yeah, the clock speed controls</p>



<p class="wp-block-paragraph">how fast these cycles go.</p>



<p class="wp-block-paragraph">And there&#8217;s much more advanced stuff</p>



<p class="wp-block-paragraph">And there&#8217;s much more advanced stuff that you could learn later,</p>



<p class="wp-block-paragraph">like pipelining and such to increase efficiency.</p>



<p class="wp-block-paragraph">But this is a basic idea of how your computer works.</p>



<p class="wp-block-paragraph">I hope you enjoyed this video.</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">I&#8217;ll see you in the next video.</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">I do hope you did learn something and have some fun.</p>



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



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



<p class="wp-block-paragraph">Website that you&#8217;re looking at right now</p>



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



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



<p class="wp-block-paragraph">So please do do me a kindness and 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">subscribed or followed it just wakes me up and I get filled with joy that&#8217;s</p>



<p class="wp-block-paragraph">exactly what happens every single time so you could do it as a nice favor to me</p>



<p class="wp-block-paragraph">or you could you control 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 just wake up I promise that&#8217;s what will</p>



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



<p class="wp-block-paragraph">code which you can scan in order to go to the website which I think is also</p>



<p class="wp-block-paragraph">named somewhere at the bottom of this video and it&#8217;ll take you to my main</p>



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-your-cpu-works-fetch-decode-execute-explained/">How Your CPU Works: Fetch, Decode, Execute Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/how-your-cpu-works-fetch-decode-execute-explained/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
