<?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/category/computer-science/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/category/computer-science/</link>
	<description></description>
	<lastBuildDate>Tue, 16 Jun 2026 04:49:16 +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/category/computer-science/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>AVL Tree Rotations Practice: Balancing a Huge Linear Tree Step by Step</title>
		<link>https://www.NeuralLantern.com/avl-tree-rotations-practice-balancing-a-huge-linear-tree-step-by-step/</link>
					<comments>https://www.NeuralLantern.com/avl-tree-rotations-practice-balancing-a-huge-linear-tree-step-by-step/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Tue, 16 Jun 2026 04:49:15 +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 balance factor]]></category>
		<category><![CDATA[avl rotations]]></category>
		<category><![CDATA[AVL tree]]></category>
		<category><![CDATA[balanced binary tree]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[coding interview]]></category>
		<category><![CDATA[computer science tutorial]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[rotation examples]]></category>
		<category><![CDATA[self balancing tree]]></category>
		<category><![CDATA[tree rotations]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=391</guid>

					<description><![CDATA[<p>Learn how to perform AVL tree rotations on a completely linear binary search tree. This step-by-step practice video shows multiple rotations, balance factor updates, and how to transform an unbalanced O(n) tree into a balanced O(log n) AVL tree.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-rotations-practice-balancing-a-huge-linear-tree-step-by-step/">AVL Tree Rotations Practice: Balancing a Huge Linear Tree 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 title="AVL Tree Rotations Practice: Balancing a Huge Linear Tree Step by Step" width="1380" height="776" src="https://www.youtube.com/embed/mykKNhayTJM?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 AVL tree tutorial, we take a massive unbalanced linear binary search tree and perform multiple rotations to turn it into a properly balanced AVL tree. Watch as we identify imbalance, label X Y Z nodes, determine A B C, handle outstanding children, and reattach subtrees step by step.</p>



<p class="wp-block-paragraph">Perfect for computer science students learning data structures, self-balancing trees, and AVL rotations. We go through several rotations on the same tree to show the full process from start to finish.</p>



<p class="wp-block-paragraph">If you&#8217;ve seen the basics, this is the practice video you&#8217;ve been looking for. Timestamps and clear diagrams included.</p>



<p class="wp-block-paragraph">Like and subscribe for more data structures content!</p>



<p class="wp-block-paragraph">00:00 Introduction to AVL Rotations Practice<br>00:22 Previous Videos Overview<br>00:56 Understanding the Linear Tree Problem<br>01:24 Why Balance This Tree<br>02:20 Computing Balance Factors<br>03:16 First Rotation Setup XYZ<br>04:04 In-Order ABC Pattern<br>05:50 Reattaching Subtree<br>07:17 Recompute Balance Factors<br>08:06 Second Rotation Setup<br>09:20 XYZ and ABC for Second Rotation<br>10:08 Drawing Output Pattern<br>11:40 Placing Outstanding Children<br>13:32 Third Rotation Setup<br>14:28 XYZ for Third Rotation<br>15:02 Output Pattern and Children<br>18:28 Recompute Balance Factors<br>19:28 Fourth Rotation Setup<br>20:04 XYZ for Final Rotation<br>20:32 Handling All Outstanding Children<br>23:20 Reattaching Final Subtree<br>24:50 Last Rotation Setup<br>25:48 XYZ and ABC Final<br>26:38 Output Pattern and Children Placement<br>30:16 Final Balance Factors Check<br>30:52 Valid AVL Tree Achieved<br>31: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">Hello there! Let&#8217;s practice AVL rotations with a big ugly gross linear tree that for some reason hasn&#8217;t been rotating up to this point.</p>



<p class="wp-block-paragraph">But we&#8217;re going to rotate it all at once to make it nice and balanced per the rules of AVL trees, just for practice.</p>



<p class="wp-block-paragraph">Okay, so first off, you should have hopefully seen my previous videos.</p>



<p class="wp-block-paragraph">search trees how to define them how to build them search through them add remove stuff all that</p>



<p class="wp-block-paragraph">stuff and then we talked about abl trees which are really just self-balancing binary search</p>



<p class="wp-block-paragraph">trees with some extra special rules on top we talked about the types of rotations how to do</p>



<p class="wp-block-paragraph">the rotations when to rotate when to how to detect whether a tree is actually a valid abl tree and</p>



<p class="wp-block-paragraph">all that stuff so if you don&#8217;t know what i&#8217;m talking about see my previous videos otherwise</p>



<p class="wp-block-paragraph">we&#8217;re just going to practice on this one tree right now we&#8217;re just going to do a practice run</p>



<p class="wp-block-paragraph">run. Okay so you would never I mean you would hopefully never see an AVL tree</p>



<p class="wp-block-paragraph">that looks like this. This is way too imbalanced for an AVL tree. An AVL tree</p>



<p class="wp-block-paragraph">would have started rebalancing itself a long time ago but let&#8217;s just pretend for</p>



<p class="wp-block-paragraph">the sake of argument that you disabled the balancing feature of your AVL tree.</p>



<p class="wp-block-paragraph">You maybe like have a boolean inside of your class that you&#8217;ve written in your</p>



<p class="wp-block-paragraph">program called am I behaving like an AVL tree true or false and you set it to</p>



<p class="wp-block-paragraph">for a while then you started throwing nodes at the tree after you ended up with this giant linear</p>



<p class="wp-block-paragraph">tree then we&#8217;re going to suddenly turn on the avlness bool and start rotating this tree sucks</p>



<p class="wp-block-paragraph">this is a valid binary search tree but like if you notice it follows all the rules of a binary</p>



<p class="wp-block-paragraph">search tree and the data is in order but this is definitely not a valid avl tree the time</p>



<p class="wp-block-paragraph">complexity of searching through this tree would be o of h and since o of h is actually the number</p>



<p class="wp-block-paragraph">h is actually the number of nodes in the entire tree the time complexity of</p>



<p class="wp-block-paragraph">searching through this particular tree is o of n so this is a linear tree it&#8217;s</p>



<p class="wp-block-paragraph">no faster to search through than a linked list at least in terms of</p>



<p class="wp-block-paragraph">scalability so this is bad we need to fix this so now let&#8217;s turn on the AVL</p>



<p class="wp-block-paragraph">ness we&#8217;ll say okay now you&#8217;re no longer pretending to be a regular binary search</p>



<p class="wp-block-paragraph">tree you&#8217;re an AVL tree let&#8217;s do some rotations the first thing we should do</p>



<p class="wp-block-paragraph">do if you&#8217;re presented with a tree like this all at once is just compute the balance factors for</p>



<p class="wp-block-paragraph">every single node. So I&#8217;m going to say the 55 is a leaf it has a balance factor of 0, the 42 has a</p>



<p class="wp-block-paragraph">balance factor of 1, and really the balance factors just increase by 1 for every level up we go. So</p>



<p class="wp-block-paragraph">the balance factor of the 15 node is horrible, it&#8217;s really really really bad. Now that we&#8217;ve done</p>



<p class="wp-block-paragraph">this we could rotate anywhere we wanted to. I mean it&#8217;s valid to rotate starting with the 34 or the</p>



<p class="wp-block-paragraph">or the 27 or 22 or 15 you wouldn&#8217;t want to rotate the 42 because that&#8217;s not</p>



<p class="wp-block-paragraph">imbalanced enough for an AVL tree but really the smartest thing to do is</p>



<p class="wp-block-paragraph">rotate as low as possible because when you rotate stuff that&#8217;s lower it tends</p>



<p class="wp-block-paragraph">to fix stuff that&#8217;s higher so then you&#8217;ll end up doing less work and</p>



<p class="wp-block-paragraph">probably end up with a tree that matches what someone else expected okay so we&#8217;re</p>



<p class="wp-block-paragraph">going to rotate as low as possible that means I&#8217;m going to find the lowest node</p>



<p class="wp-block-paragraph">and I&#8217;m going to say it&#8217;s the 34 node and I&#8217;ll label that as Z.</p>



<p class="wp-block-paragraph">That&#8217;s bad. Let me try again.</p>



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



<p class="wp-block-paragraph">Okay, then we find the A child of the Z node</p>



<p class="wp-block-paragraph">and we have to find the child that has a taller subtree,</p>



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



<p class="wp-block-paragraph">We can only go to the right.</p>



<p class="wp-block-paragraph">There&#8217;s nothing on the left.</p>



<p class="wp-block-paragraph">So that means Y is the 42 node.</p>



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



<p class="wp-block-paragraph">We&#8217;re labeling for X and we have to find a child of Y.</p>



<p class="wp-block-paragraph">We have to look at the taller subtree,</p>



<p class="wp-block-paragraph">We have to look at the taller subtree, but there is no other choice.</p>



<p class="wp-block-paragraph">We can only go down and to the right.</p>



<p class="wp-block-paragraph">So now we know our XYZ.</p>



<p class="wp-block-paragraph">I&#8217;m just going to place these labels up here at the top.</p>



<p class="wp-block-paragraph">I&#8217;m going to say X is 55, Y is 42, and Z is 34.</p>



<p class="wp-block-paragraph">And maybe I&#8217;m going to change that text color real fast.</p>



<p class="wp-block-paragraph">Then we want to produce an in-order representation of XYZ.</p>



<p class="wp-block-paragraph">This step, I think it helps people with diagrams, but it also helps you a lot when it comes time to code.</p>



<p class="wp-block-paragraph">when it comes time to code,</p>



<p class="wp-block-paragraph">because coding for these rotations is way easier</p>



<p class="wp-block-paragraph">than diagramming these rotations.</p>



<p class="wp-block-paragraph">You pretty much just have to come up with three new pointers,</p>



<p class="wp-block-paragraph">A and B and C,</p>



<p class="wp-block-paragraph">and all they are is the ordered version of XYZ.</p>



<p class="wp-block-paragraph">So, you know, which node belongs on the very left,</p>



<p class="wp-block-paragraph">that&#8217;s gonna be 34,</p>



<p class="wp-block-paragraph">which node would belong in the middle,</p>



<p class="wp-block-paragraph">that&#8217;s gonna be 42,</p>



<p class="wp-block-paragraph">which node would belong on the right side,</p>



<p class="wp-block-paragraph">that&#8217;s gonna be 55.</p>



<p class="wp-block-paragraph">And so now we&#8217;re ready to come up with our output pattern.</p>



<p class="wp-block-paragraph">So I&#8217;m just gonna take one of these nodes</p>



<p class="wp-block-paragraph">So I&#8217;m just going to take one of these nodes and copy it.</p>



<p class="wp-block-paragraph">Maybe get rid of that little stem there.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to just, you know, copy paste this a couple of times as quickly as I can.</p>



<p class="wp-block-paragraph">It doesn&#8217;t need to be perfect.</p>



<p class="wp-block-paragraph">But if it&#8217;s not, they&#8217;ll laugh at you.</p>



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



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



<p class="wp-block-paragraph">And we&#8217;ll draw some connecting lines.</p>



<p class="wp-block-paragraph">So we&#8217;ve got like three nodes here.</p>



<p class="wp-block-paragraph">A perfect little tri-node subtree because we picked X and Y and Z.</p>



<p class="wp-block-paragraph">because we picked X and Y and Z and that became A and B and C.</p>



<p class="wp-block-paragraph">Can&#8217;t get that line right, I&#8217;m going to give up.</p>



<p class="wp-block-paragraph">So then we, I&#8217;m going to fix the labels here. So what is A belonging on the left?</p>



<p class="wp-block-paragraph">That&#8217;s 34. What is B which belongs in the middle? That&#8217;s 42.</p>



<p class="wp-block-paragraph">What is C which belongs on the right? That&#8217;s 55. So now we have</p>



<p class="wp-block-paragraph">a perfectly balanced trinode subtree. Notice how this is a valid binary search tree.</p>



<p class="wp-block-paragraph">If the numbers were in a different order, this wouldn&#8217;t be</p>



<p class="wp-block-paragraph">a valid binary search tree and you&#8217;d have to try again with your ordering and also this has to be</p>



<p class="wp-block-paragraph">a perfectly balanced trinode subtree otherwise this probably wouldn&#8217;t actually help if we if</p>



<p class="wp-block-paragraph">we did a different output pattern it wouldn&#8217;t help the tree so then the next thing we need to do is</p>



<p class="wp-block-paragraph">just double check that we don&#8217;t have any nodes that are unaccounted for from the input pattern</p>



<p class="wp-block-paragraph">the xyz nodes so if we look at the uh the 34 node let&#8217;s look at z first i guess we&#8217;ll look at the 34</p>



<p class="wp-block-paragraph">had 42 as a child but 42 is already handled so that&#8217;s actually okay then we look at the y node</p>



<p class="wp-block-paragraph">that&#8217;s 42 it had 34 and 55 as children the 34 was handled uh here and the 55 is handled here so we</p>



<p class="wp-block-paragraph">actually are fine we don&#8217;t need to worry about that now we&#8217;ll look at the x node the x node had</p>



<p class="wp-block-paragraph">no children so we&#8217;re pretty much done again like i said in previous videos there could have been up</p>



<p class="wp-block-paragraph">You know, each of these XYZ nodes could have had its own children.</p>



<p class="wp-block-paragraph">Notice how 34 could have had one, 42 could have had one, 55 could have had two.</p>



<p class="wp-block-paragraph">So there would be four outstanding children.</p>



<p class="wp-block-paragraph">So we&#8217;re actually done at this point with the outstanding children.</p>



<p class="wp-block-paragraph">There are none.</p>



<p class="wp-block-paragraph">So I&#8217;m ready to reattach this tree, I guess the rotated subtree, into the regular tree.</p>



<p class="wp-block-paragraph">So these three nodes can just go away from our diagram.</p>



<p class="wp-block-paragraph">In code, you&#8217;re not actually removing these nodes.</p>



<p class="wp-block-paragraph">You&#8217;re just sort of, you know, reattaching pointers.</p>



<p class="wp-block-paragraph">reattaching pointers but I&#8217;m going to redraw this in a way that looks kind of nice and I&#8217;ll just</p>



<p class="wp-block-paragraph">maybe redo this line real fast so I&#8217;m going to do a blue line here okay now we have to recompute the</p>



<p class="wp-block-paragraph">balance factors so I&#8217;m going to say that the 34 is a leaf it&#8217;s got balance factor of zero same for</p>



<p class="wp-block-paragraph">perfectly balanced and then we have to work our way up until we reach the root node. So</p>



<p class="wp-block-paragraph">the 27 node, it&#8217;s got two nodes hanging off the right side and nothing on the left.</p>



<p class="wp-block-paragraph">So its new balance factor is two. So it actually improved a little bit.</p>



<p class="wp-block-paragraph">The 22 node, one, two, three, it&#8217;s got three nodes hanging off the right side and nothing</p>



<p class="wp-block-paragraph">on the left. So it improved a little bit to three. And I&#8217;m guessing that the 15 also improved to four.</p>



<p class="wp-block-paragraph">You can see the height on the left subtree of 15 is one, two, three, four, nothing on the left,</p>



<p class="wp-block-paragraph">So we&#8217;re done with this particular rotation.</p>



<p class="wp-block-paragraph">But because we see a node that has a balance factor of two or worse,</p>



<p class="wp-block-paragraph">we&#8217;re not actually done rotating.</p>



<p class="wp-block-paragraph">This is still an invalid AVL tree.</p>



<p class="wp-block-paragraph">And under the hood, we would continually just, you know, rotate up and up and up the tree</p>



<p class="wp-block-paragraph">until everything looked like it was fixed.</p>



<p class="wp-block-paragraph">So again, the first node that&#8217;s out of whack, or I guess the lowest node that&#8217;s out of whack,</p>



<p class="wp-block-paragraph">we&#8217;ll call that our Z node.</p>



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



<p class="wp-block-paragraph">piece of text and then we have to find a child of Z with the tallest subtree so there&#8217;s no child on</p>



<p class="wp-block-paragraph">the left which means we have to go to the right to find our Y node okay then we have to go find X</p>



<p class="wp-block-paragraph">through a child of Y and we have to find the child with the taller subtree actually the left child</p>



<p class="wp-block-paragraph">and the right child have the same height of their trees of their subtrees so it doesn&#8217;t actually</p>



<p class="wp-block-paragraph">know for fun let&#8217;s just go to the left because it&#8217;ll probably be harder we&#8217;ll say that the x</p>



<p class="wp-block-paragraph">node is 34. be consistent if you always whenever you see like sub trees that are equal height</p>



<p class="wp-block-paragraph">i don&#8217;t know maybe try to be consistent going left or right but in this case it doesn&#8217;t really matter</p>



<p class="wp-block-paragraph">so we have xyz i&#8217;m going to type them up again right here we&#8217;re going to say x equals</p>



<p class="wp-block-paragraph">Let me just double check that 34 42 27 and then we&#8217;ll produce ABC, which are the in order</p>



<p class="wp-block-paragraph">representations of XYZ.</p>



<p class="wp-block-paragraph">Again, imagine you&#8217;re using pointers if you&#8217;re doing this in code.</p>



<p class="wp-block-paragraph">So we have like a is equal to the least value, the one that would belong on the left.</p>



<p class="wp-block-paragraph">That&#8217;s the 27 B should be 34 and C should be 42.</p>



<p class="wp-block-paragraph">And then, you know, just visually check.</p>



<p class="wp-block-paragraph">way so that all the nodes have their own space. The one that&#8217;s furthest on the left is Z and that</p>



<p class="wp-block-paragraph">ends up being A. The one in the middle is X, that&#8217;s B, and the one on the right is C, that&#8217;s 42, Y.</p>



<p class="wp-block-paragraph">Right, yeah. Okay, so now that we&#8217;ve selected our ABC we can start drawing the output pattern. So</p>



<p class="wp-block-paragraph">I&#8217;m going to like make a little copy of this and I&#8217;m just going to very quickly try to draw</p>



<p class="wp-block-paragraph">balanced maybe a little bit lower they&#8217;ll laugh at you um they won&#8217;t they will i&#8217;m just kidding</p>



<p class="wp-block-paragraph">let me finish this line</p>



<p class="wp-block-paragraph">all right fix the numbers real fast uh so a is supposed to be on the left so that&#8217;s 27 that&#8217;s</p>



<p class="wp-block-paragraph">already good uh b is 34 c is 42 so now we have to just worry about the unaccounted for children of</p>



<p class="wp-block-paragraph">unaccounted for children of ABC or XYZ. So first I&#8217;m going to look at X. Whoops.</p>



<p class="wp-block-paragraph">We look at X, it&#8217;s 34. It had no children of its own, so we don&#8217;t even have to worry about any of</p>



<p class="wp-block-paragraph">its children. So we&#8217;re done. We look at Y, Y had two children. It had 34, but 34 was already handled</p>



<p class="wp-block-paragraph">up here. And it had a right child of 55. Oh, so we have a child that is unaccounted for now. So I&#8217;m</p>



<p class="wp-block-paragraph">duplicate it over here. We got to put it somewhere. I don&#8217;t know where just yet.</p>



<p class="wp-block-paragraph">You can probably figure it out, but I&#8217;m just going to put it over there. Now we&#8217;re done looking for the Y.</p>



<p class="wp-block-paragraph">And then next we&#8217;ll look at the Z node.</p>



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



<p class="wp-block-paragraph">the Z node is 27. It had a 42 as a right child, but 42 was already handled in the output pattern.</p>



<p class="wp-block-paragraph">So we&#8217;re done looking for outstanding children.</p>



<p class="wp-block-paragraph">So the next thing I&#8217;m going to do is figure out where the heck does that 55 go?</p>



<p class="wp-block-paragraph">does that 55 go? Remember, we&#8217;re trying to make valid binary search trees, which means there&#8217;s</p>



<p class="wp-block-paragraph">only one place where this 55 node could actually go. If you stuck it on the left side, a greater</p>



<p class="wp-block-paragraph">value on the left of 27, that&#8217;s invalid. That wouldn&#8217;t work. 55 is greater than 34, so it can&#8217;t</p>



<p class="wp-block-paragraph">go on the left. It&#8217;s greater than 42. So actually 55 belongs all the way on the right side, because</p>



<p class="wp-block-paragraph">again, that&#8217;s the only place it would actually work to allow us to still have a valid binary</p>



<p class="wp-block-paragraph">search tree when we were done placing it so double check the subtree that you have written down after</p>



<p class="wp-block-paragraph">you&#8217;re done doing all this stuff and make sure that the whole entire tree is a valid binary search</p>



<p class="wp-block-paragraph">tree if not you probably have to try again okay so i&#8217;m going to duplicate this slide right here</p>



<p class="wp-block-paragraph">and we&#8217;re going to say that we&#8217;ve got four nodes ready to be inserted in place of these four nodes</p>



<p class="wp-block-paragraph">over here one two three four so i&#8217;m going to just select all four of these nodes and just delete them</p>



<p class="wp-block-paragraph">and reinsert the rotated, you know, the rotated nodes, which are the same nodes, but they&#8217;re just,</p>



<p class="wp-block-paragraph">you know, they&#8217;re drawn differently with, you know, different connections. And then I&#8217;m going</p>



<p class="wp-block-paragraph">to fix this connection over here. So that&#8217;s 34 is now the right child of 22. Then I have to do</p>



<p class="wp-block-paragraph">the balance factors for all of the nodes that we rearranged. So the leaves are easy. As usual,</p>



<p class="wp-block-paragraph">This 42 has a balance factor of 1, and this 34 has a balance factor of also 1.</p>



<p class="wp-block-paragraph">It&#8217;s not perfect, but it&#8217;s okay.</p>



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



<p class="wp-block-paragraph">So now we&#8217;re going to recompute the balance factor for the 22.</p>



<p class="wp-block-paragraph">Actually, it doesn&#8217;t improve.</p>



<p class="wp-block-paragraph">If you notice, the right subtree has a height of 1, 2, 3.</p>



<p class="wp-block-paragraph">There is no left subtree, so the balance factor is 3.</p>



<p class="wp-block-paragraph">Same thing for this 15 node.</p>



<p class="wp-block-paragraph">So we made a little progress.</p>



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



<p class="wp-block-paragraph">We have to keep going.</p>



<p class="wp-block-paragraph">So, you know, once again, I&#8217;m going to select the lowest node that&#8217;s out of whack.</p>



<p class="wp-block-paragraph">Or I guess as I work my way up the subtree, I can see that three node is still out of whack.</p>



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



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



<p class="wp-block-paragraph">Somebody remind me to do what types of rotations to talk about the rotation types.</p>



<p class="wp-block-paragraph">Somebody send me an email or a comment.</p>



<p class="wp-block-paragraph">I&#8217;m going to do it at the end of this video, but send me a comment anyway.</p>



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



<p class="wp-block-paragraph">22 node to find the y node we have to take a child of z with the tallest subtree but that&#8217;s obviously going to be 34</p>



<p class="wp-block-paragraph">There&#8217;s no other choice</p>



<p class="wp-block-paragraph">And now we actually do have an interesting choice to find the x node</p>



<p class="wp-block-paragraph">It has to be a child of y, but it&#8217;s got to be the child with the tallest subtree</p>



<p class="wp-block-paragraph">So for the first time we can kind of see</p>



<p class="wp-block-paragraph">That we you know, we might have wanted to put x on the left or right, but it has to be on the right</p>



<p class="wp-block-paragraph">We&#8217;re not allowed to put it on on the left at least for this method</p>



<p class="wp-block-paragraph">So there we go. We have our XYZ. I&#8217;m going to go ahead and do X is equal to 42, and Y is equal to 34, and Z is equal to 22, and then A, B, and C are going to be the in-order representations.</p>



<p class="wp-block-paragraph">the middle 42 goes on the right so now i&#8217;m ready to draw my output pattern so i&#8217;m going to like</p>



<p class="wp-block-paragraph">select this real fast duplicate it up here make some copies making copies anyone no okay just</p>



<p class="wp-block-paragraph">just wondering okay i&#8217;m going to do this do some connecting lines</p>



<p class="wp-block-paragraph">So A is supposed to be on the left, that&#8217;s 22, and then B is going to be 34, and C is</p>



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



<p class="wp-block-paragraph">So we have a perfectly balanced trinode subtree.</p>



<p class="wp-block-paragraph">Now we&#8217;ve got to double check for outstanding children.</p>



<p class="wp-block-paragraph">So we start by looking at the Z node.</p>



<p class="wp-block-paragraph">Oh my god.</p>



<p class="wp-block-paragraph">Not as skilled as I think I am.</p>



<p class="wp-block-paragraph">We look at the Z node.</p>



<p class="wp-block-paragraph">which is already handled over here so we&#8217;re fine that way then we&#8217;ll look at the y node the y node</p>



<p class="wp-block-paragraph">was 34 it had a left child of 27 that is not accounted for in the output pattern so i&#8217;m going</p>



<p class="wp-block-paragraph">to grab it and then also you know when you&#8217;re grabbing these unaccounted for children double</p>



<p class="wp-block-paragraph">check that they might also have children of their own if they do we have to take them with the node</p>



<p class="wp-block-paragraph">that we&#8217;re moving so if 27 had any children we would not be you know removing or rearranging</p>



<p class="wp-block-paragraph">children and just kind of you know let them stay in their position underneath 27. You know we would</p>



<p class="wp-block-paragraph">not rearrange pointers that are further down but in this case what have I done in this case 27</p>



<p class="wp-block-paragraph">doesn&#8217;t have any children I&#8217;ll have to do a copy that&#8217;s why make a copy put it somewhere so now</p>



<p class="wp-block-paragraph">we&#8217;re done with the 27 on the right side of 34 it had a child of 42 but that&#8217;s already handled</p>



<p class="wp-block-paragraph">42 and then I&#8217;ll just put like an arrow here. Now we&#8217;re ready to look at the X nodes children.</p>



<p class="wp-block-paragraph">So the X is 42. It had a right child of 55 which is also not accounted for in the output pattern.</p>



<p class="wp-block-paragraph">So I&#8217;m going to just move that over here.</p>



<p class="wp-block-paragraph">Okay now we have to place these children in their appropriate positions.</p>



<p class="wp-block-paragraph">So the 55 if you think about it there&#8217;s only one position that the 55 could ever go</p>



<p class="wp-block-paragraph">go to produce a valid binary search tree so it can&#8217;t go there it&#8217;s got to go all the way on the</p>



<p class="wp-block-paragraph">right side so that&#8217;s where we&#8217;ll put it same thing for the 27 there&#8217;s only one place it could go it</p>



<p class="wp-block-paragraph">can&#8217;t go here because it&#8217;s less than 34 so it can&#8217;t be on the right side of 34 it&#8217;s going to</p>



<p class="wp-block-paragraph">be on the left side of 34 and if you look at where is it going to go with respect to 22 it can&#8217;t go</p>



<p class="wp-block-paragraph">on the left it has to go on the right because again we need a valid binary search tree</p>



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



<p class="wp-block-paragraph">And now we&#8217;re done making our little output tree, subtree.</p>



<p class="wp-block-paragraph">So how many nodes did we have in the input tree?</p>



<p class="wp-block-paragraph">It was 22, 34, 42, and 55, so like five nodes.</p>



<p class="wp-block-paragraph">And if you look at the output pattern here, there&#8217;s also five nodes.</p>



<p class="wp-block-paragraph">So we&#8217;re pretty much ready to just kind of remove all of these nodes</p>



<p class="wp-block-paragraph">all the way up to that 15.</p>



<p class="wp-block-paragraph">and make it the right child of 15. So I&#8217;m going to select all of this stuff, make it the right child</p>



<p class="wp-block-paragraph">of 15, and then I guess I&#8217;ll just redo that line real fast. So this is a nicely formatted diagram.</p>



<p class="wp-block-paragraph">So we&#8217;ll do what&#8217;s going on with my computer. Okay, sometimes it lags. I&#8217;m gonna, well,</p>



<p class="wp-block-paragraph">balance factors now so I&#8217;m going to do balance factors for the leaves pretty easy the 22 and the</p>



<p class="wp-block-paragraph">42 have a balance factor of one again if this is confusing see my previous videos we practiced a</p>



<p class="wp-block-paragraph">lot of that the balance factor for the 34 is going to be a zero remember it&#8217;s about the height of the</p>



<p class="wp-block-paragraph">left subtree versus the right subtree which there&#8217;s no difference actually they both have a height of</p>



<p class="wp-block-paragraph">two it&#8217;s you know just just because there&#8217;s a little diagonal bounce there on one side doesn&#8217;t</p>



<p class="wp-block-paragraph">awesome. Not perfect, but it&#8217;s pretty good. We look at the 15. What&#8217;s the new balance factor of</p>



<p class="wp-block-paragraph">the 15? It&#8217;s actually three now because the height of its right subtree is three and the height of</p>



<p class="wp-block-paragraph">its left subtree is zero. So its balance factor improved to three. So we are making, you know,</p>



<p class="wp-block-paragraph">a little bit of progress. Now we know that the only node that&#8217;s out of whack in the whole tree</p>



<p class="wp-block-paragraph">is the 15. So we&#8217;re just going to call that our Z node for another rotation. So I&#8217;m going to put Z</p>



<p class="wp-block-paragraph">of z which would be y and there&#8217;s no other choice and then once again we have to find a child of y</p>



<p class="wp-block-paragraph">to find x and we have to take the child that has the taller subtree but both of these subtrees</p>



<p class="wp-block-paragraph">are equal so i&#8217;m just going to go to the right for fun i don&#8217;t know i&#8217;m going to do x over here</p>



<p class="wp-block-paragraph">on the on the right side well hang on have i been doing this the easy way oh yeah let&#8217;s let&#8217;s go to</p>



<p class="wp-block-paragraph">Okay, so now we have XYZ. I don&#8217;t know why I say these things. We&#8217;ll do XYZ. X equals 22. Y is equal to 34. I say them for fun. Z is equal to 15. And then we&#8217;re going to do A and B and C. So A is going to be the least value. So that&#8217;s 15. B is going to be 22. C is going to be 34.</p>



<p class="wp-block-paragraph">tree so i&#8217;m going to copy this over here a couple more times this tree is probably going to be huge</p>



<p class="wp-block-paragraph">i&#8217;m probably gonna have to rearrange this a few times just do this over here make another copy</p>



<p class="wp-block-paragraph">over here there&#8217;s a lot of nodes that are going to be unaccounted for here um i think this was</p>



<p class="wp-block-paragraph">the example where we ended up just replacing the entire tree let&#8217;s see all right i guess that&#8217;s</p>



<p class="wp-block-paragraph">And then I&#8217;m going to update the numbers. So A is 15, and B is 22, and C is 34.</p>



<p class="wp-block-paragraph">So now one by one we have to look for any outstanding children of the nodes in the output pattern.</p>



<p class="wp-block-paragraph">So we look at, let&#8217;s say, the Z first.</p>



<p class="wp-block-paragraph">The Z had a right child of 34, but that&#8217;s already taken care of in the output pattern, so we don&#8217;t have to do anything.</p>



<p class="wp-block-paragraph">now we look at the y the y had a left child of 22 but that&#8217;s already taken care of in the output</p>



<p class="wp-block-paragraph">pattern so we don&#8217;t have to worry about that it also had a right child of 42</p>



<p class="wp-block-paragraph">um so we don&#8217;t have to worry about that sorry we do have to worry about that so i&#8217;m going to copy</p>



<p class="wp-block-paragraph">the 42 over here and we&#8217;re going to have to do something with it eventually now we&#8217;re ready to</p>



<p class="wp-block-paragraph">If we&#8217;re looking at the X node, X node is 22.</p>



<p class="wp-block-paragraph">It had a right child of 27 that is not in the output pattern.</p>



<p class="wp-block-paragraph">So that&#8217;s something that&#8217;s unaccounted for.</p>



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



<p class="wp-block-paragraph">Okay, now we&#8217;re ready to attach the nodes in the output pattern.</p>



<p class="wp-block-paragraph">Let me just bring something to your attention real fast.</p>



<p class="wp-block-paragraph">Notice how the 42 node, which was an outstanding child of one of the output pattern nodes,</p>



<p class="wp-block-paragraph">it had a child of its own, right?</p>



<p class="wp-block-paragraph">on the bottom here, you know, below the output pattern, if they had any children of their</p>



<p class="wp-block-paragraph">own, we would not like change their topology. We would not assign them to different parents.</p>



<p class="wp-block-paragraph">They would just stay exactly where they are, which means just for the purposes of this diagram,</p>



<p class="wp-block-paragraph">I should probably also bring the 55 down as the right child of the 42. So I can remember,</p>



<p class="wp-block-paragraph">you know, that it was there and not totally forget it and ruin the tree.</p>



<p class="wp-block-paragraph">27 had no children, so it&#8217;s fine. The 27 should be a left child of 34 because it&#8217;s greater than</p>



<p class="wp-block-paragraph">of 34 because it&#8217;s greater than 22 and it&#8217;s less than 34 so I&#8217;m going to stick it right there and</p>



<p class="wp-block-paragraph">then where would the 42 go well it would it would be a right child of 34 because it&#8217;s greater than</p>



<p class="wp-block-paragraph">34 so maybe I should make the 27 like just a little bit more to the left okay then I&#8217;m going</p>



<p class="wp-block-paragraph">to reconnect so the 34&#8217;s left child is 27 its right child is 42 and then you know this is easy</p>



<p class="wp-block-paragraph">like I make mistakes all the time too so I just want to double check that this is a valid binary</p>



<p class="wp-block-paragraph">search tree because I could have you know done the numbers incorrectly so we have 15 22 27 34 42 55</p>



<p class="wp-block-paragraph">it all increases it is a binary tree so it&#8217;s also a binary search tree and then it&#8217;s like</p>



<p class="wp-block-paragraph">it satisfies all the other properties so now how many nodes did I actually remove four five six and</p>



<p class="wp-block-paragraph">there were only six nodes in the entire tree before we did this rotation you know z is kind of the</p>



<p class="wp-block-paragraph">know z is kind of the culprit of why this happened so that means the rotated pattern is actually the</p>



<p class="wp-block-paragraph">final binary search tree so i&#8217;m just going to erase the whole entire tree and put the rotated</p>



<p class="wp-block-paragraph">pattern in the middle here then we just have to recompute all the balance factors to make sure</p>



<p class="wp-block-paragraph">that we&#8217;re done or that we&#8217;re not done so the leaves get zero and then the 42 gets a one and</p>



<p class="wp-block-paragraph">then the 34 gets a one because it&#8217;s got a height of one on its left subtree and a height of two on</p>



<p class="wp-block-paragraph">and a height of 2 on its right subtree, take the absolute value.</p>



<p class="wp-block-paragraph">The 22 node, I don&#8217;t think that&#8217;s quite finished yet.</p>



<p class="wp-block-paragraph">It&#8217;s got a balance factor of 2 because it&#8217;s here.</p>



<p class="wp-block-paragraph">Let&#8217;s just clarify to make sure everybody&#8217;s on the same page.</p>



<p class="wp-block-paragraph">Anytime I feel that I had to think even a little bit,</p>



<p class="wp-block-paragraph">I&#8217;ll sometimes just kind of take a little step back and make sure everybody&#8217;s okay.</p>



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



<p class="wp-block-paragraph">You can see that I highlighted the left subtree.</p>



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



<p class="wp-block-paragraph">So 1 minus 3, take the absolute value.</p>



<p class="wp-block-paragraph">That&#8217;s going to be 2.</p>



<p class="wp-block-paragraph">value that&#8217;s going to be two so the balance factor of the 22 node is two meaning we&#8217;re not done</p>



<p class="wp-block-paragraph">rotating our tree it could be a little bit better a little bit faster so maybe i&#8217;m going to move this</p>



<p class="wp-block-paragraph">whole thing over to the left a little bit and then we&#8217;ll call the 22 node the z node because it&#8217;s the</p>



<p class="wp-block-paragraph">first node that&#8217;s out of whack then we have some more interesting choices right remember before</p>



<p class="wp-block-paragraph">if we&#8217;re going to go down into the left or down into the right it always seemed obvious but now</p>



<p class="wp-block-paragraph">seemed obvious but now to find our y we could go left or right but we have to go to the right</p>



<p class="wp-block-paragraph">because 34 has the taller subtree 15 it&#8217;s it&#8217;s a tree of just one it&#8217;s a height of one and 34 it&#8217;s</p>



<p class="wp-block-paragraph">a tree of height three so we go to the right to find y to find x again we need to look at a child</p>



<p class="wp-block-paragraph">of y with the tallest subtree so that&#8217;s not going to be 27 that&#8217;s a height of one it&#8217;s going to be</p>



<p class="wp-block-paragraph">42 with the height of two so the x node is this over here and now we&#8217;re ready to talk about</p>



<p class="wp-block-paragraph">ready to talk about what is XYZ and ABC. So X is, I always forget to change the color before I type</p>



<p class="wp-block-paragraph">this. Okay. X is 42. Y is 34. Z is 22. And then we get A and B and C. Okay, so A is the least value,</p>



<p class="wp-block-paragraph">the one that would belong on the left. I mean, you can just look visually on the diagram again.</p>



<p class="wp-block-paragraph">on the left right so that would have to be a because anything that starts leftmost should end</p>



<p class="wp-block-paragraph">up left leftmost in terms of a well-drawn diagram so b is going to be in the middle that&#8217;s the 34</p>



<p class="wp-block-paragraph">and then c is going to be the greatest value which is 42 again c 42 is the rightmost value both</p>



<p class="wp-block-paragraph">before and after we decided what is a xyz and abc so we&#8217;ve got that i&#8217;m going to go ahead and start</p>



<p class="wp-block-paragraph">for another new diagram.</p>



<p class="wp-block-paragraph">We&#8217;re going to say,</p>



<p class="wp-block-paragraph">we got that.</p>



<p class="wp-block-paragraph">Give it a little copy.</p>



<p class="wp-block-paragraph">Give it another little copy.</p>



<p class="wp-block-paragraph">Give it a copy.</p>



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



<p class="wp-block-paragraph">Dang, how long is this video?</p>



<p class="wp-block-paragraph">Man, I knew this was going to be a gnarly example.</p>



<p class="wp-block-paragraph">That&#8217;s why I like doing it.</p>



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



<p class="wp-block-paragraph">Fix the numbers real fast.</p>



<p class="wp-block-paragraph">So the 27 is A, it&#8217;s 22.</p>



<p class="wp-block-paragraph">And then this is going to be B, which is 34.</p>



<p class="wp-block-paragraph">And then 27, or the rightmost node, is going to be the 42.</p>



<p class="wp-block-paragraph">Then we start looking at children that might be unaccounted for in our target pattern.</p>



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



<p class="wp-block-paragraph">And we&#8217;ll say, start by looking at Z.</p>



<p class="wp-block-paragraph">It had a left child of 15, which is unaccounted for in the output pattern.</p>



<p class="wp-block-paragraph">So I&#8217;m going to move 15 over here somewhere.</p>



<p class="wp-block-paragraph">And then we look at the 34.</p>



<p class="wp-block-paragraph">That&#8217;s already accounted for in the output pattern.</p>



<p class="wp-block-paragraph">So we&#8217;re done looking at Z&#8217;s children.</p>



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



<p class="wp-block-paragraph">Now we&#8217;re ready to look at Y.</p>



<p class="wp-block-paragraph">Y had a left child of 27.</p>



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



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



<p class="wp-block-paragraph">I&#8217;m going to copy the 27 over here.</p>



<p class="wp-block-paragraph">And then we look at the 42.</p>



<p class="wp-block-paragraph">The 42 is already handled in the output pattern.</p>



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



<p class="wp-block-paragraph">worry about it okay now we&#8217;re ready to look at the x nodes children so we look at x it had a right</p>



<p class="wp-block-paragraph">child of 55 that&#8217;s also that&#8217;s also not accounted for in the output pattern so i&#8217;m going to make a</p>



<p class="wp-block-paragraph">copy so we have like three nodes to worry about and then you know just for diagram purposes in</p>



<p class="wp-block-paragraph">the code this would be easy to do but for the diagram we have to make sure are we dragging any</p>



<p class="wp-block-paragraph">of these unaccounted for children away from their other children that they might have so 55 it has</p>



<p class="wp-block-paragraph">children 27 it has no children 15 also has no children so these are pretty easy so just visually</p>



<p class="wp-block-paragraph">I&#8217;m going to rearrange I lost my camera what happened test test test whoa I&#8217;m tripping so I</p>



<p class="wp-block-paragraph">had a technical issue with my camera I had to edit out I guess a little portion where I was super</p>



<p class="wp-block-paragraph">confused and screaming and now I think we&#8217;re okay so we can continue the video so we&#8217;ve just looked</p>



<p class="wp-block-paragraph">children that are unaccounted for and accounted for them that&#8217;s so nice of us</p>



<p class="wp-block-paragraph">and we&#8217;re going to have to place them somewhere so this 15 again it can&#8217;t go</p>



<p class="wp-block-paragraph">anywhere over here on the right side that would be an invalid binary search</p>



<p class="wp-block-paragraph">tree it&#8217;s actually got to go on the left of the 22</p>



<p class="wp-block-paragraph">it&#8217;s the only place it could possibly go same thing for the 55 where is that</p>



<p class="wp-block-paragraph">going to go I don&#8217;t know maybe like all the way to the right because that&#8217;s the</p>



<p class="wp-block-paragraph">only numerically valid you know place that it could go to have a valid binary</p>



<p class="wp-block-paragraph">27 can&#8217;t go on the right of 34 it&#8217;s too small it ends up going on the right of 22.</p>



<p class="wp-block-paragraph">So now we&#8217;ve placed the children to account for them and I&#8217;m going to just like draw my</p>



<p class="wp-block-paragraph">connecting lines real fast and then how many nodes do we have here let&#8217;s see the 22</p>



<p class="wp-block-paragraph">1 2 3 4 5 6 1 2 3 4 5 6 oh actually that&#8217;s every single node in the tree just like last time we</p>



<p class="wp-block-paragraph">again guess that that would have happened because it might be likely to happen because the z node</p>



<p class="wp-block-paragraph">here was actually the root node of the tree. So I&#8217;m just going to you know replace the entire tree</p>



<p class="wp-block-paragraph">and say that we&#8217;re done rotating now. So we&#8217;ll just do this put this in the middle and then</p>



<p class="wp-block-paragraph">recompute all the balance factors just to check to see if we&#8217;re done. You can probably tell already</p>



<p class="wp-block-paragraph">this is a pretty decent tree so we might not have to rotate but I&#8217;m going to go ahead and</p>



<p class="wp-block-paragraph">have to rotate but I&#8217;m going to go ahead and just double check anyway. So the leaves get zeros</p>



<p class="wp-block-paragraph">and the 22 gets a zero because it&#8217;s got left and right subtrees of equal height. The 42 has a one</p>



<p class="wp-block-paragraph">and the 34 also has a zero because it&#8217;s left and right subtrees are equal height. This is maybe</p>



<p class="wp-block-paragraph">another good opportunity for me to point out that it&#8217;s not about weight or mass on the left versus</p>



<p class="wp-block-paragraph">right subtrees. It&#8217;s just about height only. So even though there are less nodes in the right</p>



<p class="wp-block-paragraph">The fact remains that the heights are the same. So actually that&#8217;s a zero for the root node</p>



<p class="wp-block-paragraph">This is now finally a valid AVL tree. We have rotated it enough. It is now considered</p>



<p class="wp-block-paragraph">Log time again, it&#8217;s going to be fast to search and insert and remove and all that stuff. So this is pretty great</p>



<p class="wp-block-paragraph">We are now officially done with this example tree. So thank you so much for watching this video</p>



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



<p class="wp-block-paragraph">and I hope you enjoyed doing this tree with me it&#8217;s always a little stressful because it&#8217;s like</p>



<p class="wp-block-paragraph">a half hour tree and I always wonder like did I make a mistake five hours ago I don&#8217;t even know</p>



<p class="wp-block-paragraph">so I guess I&#8217;ll see you in the next video thanks for hanging in there with me</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">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 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 exactly what happens</p>



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



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



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



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



<p class="wp-block-paragraph">somewhere at the bottom of this video and it&#8217;ll take you to my main website where you can just</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</p>



<p class="wp-block-paragraph">videos that you want to see, please leave a comment. Or if you just want to say, Hey, what&#8217;s</p>



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



<p class="wp-block-paragraph">the middle of the night. I get, I wake up in a cold sweat and I&#8217;m like, it would really,</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 class="wp-block-paragraph">so</p>



<p class="wp-block-paragraph">Hey there! Let&#8217;s rotate an AVL tree…</p>



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



<p class="wp-block-paragraph">Test test test! Whoa, I&#8217;m trippin&#8217;!</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-rotations-practice-balancing-a-huge-linear-tree-step-by-step/">AVL Tree Rotations Practice: Balancing a Huge Linear Tree 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/avl-tree-rotations-practice-balancing-a-huge-linear-tree-step-by-step/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>Binary Search Tree Removals &#8211; Delete Nodes with 0, 1, or 2 Children</title>
		<link>https://www.NeuralLantern.com/binary-search-tree-removals-delete-nodes-with-0-1-or-2-children/</link>
					<comments>https://www.NeuralLantern.com/binary-search-tree-removals-delete-nodes-with-0-1-or-2-children/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 11 May 2026 01:54:41 +0000</pubDate>
				<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[binary search tree remove]]></category>
		<category><![CDATA[binary tree deletion]]></category>
		<category><![CDATA[bst deletion]]></category>
		<category><![CDATA[bst operations]]></category>
		<category><![CDATA[bst remove node]]></category>
		<category><![CDATA[coding interview]]></category>
		<category><![CDATA[cs data structures]]></category>
		<category><![CDATA[data structures tutorial]]></category>
		<category><![CDATA[delete node bst]]></category>
		<category><![CDATA[inorder successor]]></category>
		<category><![CDATA[tree algorithms]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=340</guid>

					<description><![CDATA[<p>So there are three types of deletions... Is it a leaf with zero children? Is it a node with one child? Or is it a node with two children? Each type of node is deleted in a different way... For the hardest type we use the in-order successor.</p>
<p>The post <a href="https://www.NeuralLantern.com/binary-search-tree-removals-delete-nodes-with-0-1-or-2-children/">Binary Search Tree Removals &#8211; Delete Nodes with 0, 1, or 2 Children</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 Removals - Delete Nodes with 0, 1, or 2 Children" width="1380" height="776" src="https://www.youtube.com/embed/TPAiCeiMw18?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">Hello there. In this video we break down exactly how to delete nodes from a binary search tree. We cover all three cases step by step: deleting a leaf node with zero children, deleting a node with one child by promoting it, and the trickiest case &#8211; deleting a node with two children using the in-order successor method.</p>



<p class="wp-block-paragraph">You&#8217;ll see clear diagram walkthroughs, pointer manipulation explanations, and time complexity discussion for each type of removal. Perfect for computer science students, coding interview prep, or anyone strengthening their data structures knowledge.</p>



<p class="wp-block-paragraph">We search for the node, identify its children, and handle the reconnection properly to maintain the BST properties. Includes examples for each scenario and tips for implementing in code.</p>



<p class="wp-block-paragraph">If you&#8217;re learning BST insert, search, and now removal, this completes the core operations.</p>



<p class="wp-block-paragraph">00:00 Introduction to BST Deletions<br>00:10 BST Review and Setup<br>00:46 Three Types of Deletions<br>00:56 Deleting a Leaf Node (Zero Children)<br>03:21 Pointer Manipulation for Leaf Deletion<br>05:25 Deleting a Node with One Child<br>07:17 Pointer Updates for One Child Deletion<br>10:09 Deleting a Node with Two Children<br>12:08 In-Order Traversal and Successor Concept<br>13:18 Finding In-Order Successor<br>14:20 Copying Successor Value and Recursive Delete<br>18:28 Deleting the Root Node Example<br>22:56 Additional One Child Successor Example<br>25:12 Time Complexity Summary<br>26:08 Closing 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 talk about deletions in a binary search tree.</p>



<p class="wp-block-paragraph">Okay, so hopefully at this point you already understand binary search trees</p>



<p class="wp-block-paragraph">in all other aspects than just deleting. If you don&#8217;t, see my other videos. In my other videos,</p>



<p class="wp-block-paragraph">we talk about how to identify a binary search tree, all the rules that define it,</p>



<p class="wp-block-paragraph">search tree with the insert or add operation.</p>



<p class="wp-block-paragraph">We talk about searching through a binary search tree and time complexities</p>



<p class="wp-block-paragraph">and even drawing the diagram in a really nice and pretty way so that your trees</p>



<p class="wp-block-paragraph">are easier to debug.</p>



<p class="wp-block-paragraph">So for now, I&#8217;m just going to say it&#8217;s time to learn how to delete notes from</p>



<p class="wp-block-paragraph">a binary search tree.</p>



<p class="wp-block-paragraph">So there are three types of deletions.</p>



<p class="wp-block-paragraph">Actually, let me just kind of like, for example,</p>



<p class="wp-block-paragraph">suppose you want to delete to delete the number 48 from the tree.</p>



<p class="wp-block-paragraph">48 from the tree. Okay. Well, first you&#8217;d have to go search for 48. So already, you know,</p>



<p class="wp-block-paragraph">this is going to be an O of H operation or log time. If the tree was perfectly balanced</p>



<p class="wp-block-paragraph">or linear, if the tree was really poorly balanced. So we&#8217;re going to search for,</p>



<p class="wp-block-paragraph">what did I just say? 48. I should write it down. Let&#8217;s say we&#8217;re going to search for 48.</p>



<p class="wp-block-paragraph">We have to go search for it first. So that&#8217;s going to be O of H. Once we find it, we then</p>



<p class="wp-block-paragraph">How many children does it have?</p>



<p class="wp-block-paragraph">Is it a leaf with zero children?</p>



<p class="wp-block-paragraph">Is it a node with one child?</p>



<p class="wp-block-paragraph">Or is it a node with two children?</p>



<p class="wp-block-paragraph">Each type of node is deleted in a different way.</p>



<p class="wp-block-paragraph">So let&#8217;s see, the first type we would delete</p>



<p class="wp-block-paragraph">is a node with zero children,</p>



<p class="wp-block-paragraph">also known as a leaf, also known as a, sorry, external node.</p>



<p class="wp-block-paragraph">The second type of deletion is a node with one child.</p>



<p class="wp-block-paragraph">with one child and the third type is going to be the hardest type we&#8217;ll do that at the end of the</p>



<p class="wp-block-paragraph">video it&#8217;s going to be a node with two children uh numbers two and three are going to be internal</p>



<p class="wp-block-paragraph">nodes but i hope you already know that because you watch the videos anyway so let&#8217;s start off</p>



<p class="wp-block-paragraph">with the easiest possible node to delete first just a regular leaf so i&#8217;m going to delete this</p>



<p class="wp-block-paragraph">stuff real quick um and then just kind of like erase that and down and down okay so let&#8217;s identify</p>



<p class="wp-block-paragraph">so let&#8217;s identify a leaf well it could be anything that just doesn&#8217;t have any</p>



<p class="wp-block-paragraph">children of its own let&#8217;s do let&#8217;s do the number 11 just to make things a</p>



<p class="wp-block-paragraph">little bit easier so let&#8217;s delete the number 11 I&#8217;ll put 11 here we first</p>



<p class="wp-block-paragraph">search for 11 so we look at the root node 11 belongs on the left we look down</p>



<p class="wp-block-paragraph">here 11 belongs on the left we look down here 11 belongs on the right we find 11</p>



<p class="wp-block-paragraph">you know if we reach the end of the tree and we couldn&#8217;t find it then it&#8217;s like I</p>



<p class="wp-block-paragraph">don&#8217;t know maybe throw an exception at the user hey you tried to delete</p>



<p class="wp-block-paragraph">tried to delete something that wasn&#8217;t there but the 11 is there and now we have to figure out how</p>



<p class="wp-block-paragraph">to actually delete it so let me draw a couple of larger representations of these nodes real fast</p>



<p class="wp-block-paragraph">i&#8217;m just going to draw like a big old circle here and maybe duplicate it and we&#8217;ll just pretend that</p>



<p class="wp-block-paragraph">that&#8217;s the 9 and the 11 so i&#8217;ll put like a tiny little 9 there because i don&#8217;t want to change the</p>



<p class="wp-block-paragraph">And when we draw our diagrams, we usually just kind of do one line.</p>



<p class="wp-block-paragraph">But you should know that if you&#8217;re doing this in code,</p>



<p class="wp-block-paragraph">actually what&#8217;s happening is the nine node,</p>



<p class="wp-block-paragraph">it&#8217;s got a right child pointer that points down to the 11 node.</p>



<p class="wp-block-paragraph">And then the 11 node has a, let me just redo this real fast to make it prettier.</p>



<p class="wp-block-paragraph">And then the 11 node has a parent pointer that points up to the nine.</p>



<p class="wp-block-paragraph">This is probably going to be true for almost every language,</p>



<p class="wp-block-paragraph">at least that I can think of that you would code a tree in.</p>



<p class="wp-block-paragraph">a tree in. And then of course the nine would have a parent pointer to you know the 16 but we won&#8217;t</p>



<p class="wp-block-paragraph">consider that. So what are you really going to do? Well first you have to look at the 11 node and</p>



<p class="wp-block-paragraph">travel up to its parent and let the parent know that it no longer has a right child. So long story</p>



<p class="wp-block-paragraph">short we&#8217;re going to null the right child pointer. We&#8217;re going to say disconnect that right child by</p>



<p class="wp-block-paragraph">setting it to null. Maybe I&#8217;ll just put like a little null symbol here. Then we don&#8217;t necessarily</p>



<p class="wp-block-paragraph">null the pointer on the 11 node because we can just delete the 11 node if we&#8217;re</p>



<p class="wp-block-paragraph">using dynamic memory if we&#8217;re using a smart pointer it should go away already</p>



<p class="wp-block-paragraph">at this point because it has no one else pointing to it but long story short</p>



<p class="wp-block-paragraph">once we kill the 11 node we also end up killing its pointer so we don&#8217;t really</p>



<p class="wp-block-paragraph">need to tell its pointer to point to null it&#8217;s just going to end up going away</p>



<p class="wp-block-paragraph">it&#8217;s not going to be a memory leak because it was only pointing to the 9</p>



<p class="wp-block-paragraph">and the 9 is not going to leak memory because the 9 is still going to be inside</p>



<p class="wp-block-paragraph">going to be inside of the tree so uh the time complexity once we find the the leaf the node</p>



<p class="wp-block-paragraph">with zero children is just going to be constant time we&#8217;re just going to manipulate a couple</p>



<p class="wp-block-paragraph">pointers maybe call delete on that node so it&#8217;s really all about log time to go find the node if</p>



<p class="wp-block-paragraph">it was perfectly balanced or just o of h to find it and then plus constant is going to get cancelled</p>



<p class="wp-block-paragraph">out so it&#8217;s like an o of h operation or log time if the tree is perfect so let me just uh</p>



<p class="wp-block-paragraph">cross it out to let you know that this is what it&#8217;s going to be like.</p>



<p class="wp-block-paragraph">We&#8217;re going to say the 11 is just gone,</p>



<p class="wp-block-paragraph">and so is the pointer that the 9 has pointing down to the 11.</p>



<p class="wp-block-paragraph">That&#8217;s how you delete a node with zero children.</p>



<p class="wp-block-paragraph">So deleted a leaf.</p>



<p class="wp-block-paragraph">All right, now the next thing, which is slightly harder, but not too hard,</p>



<p class="wp-block-paragraph">is deleting a node that has one child.</p>



<p class="wp-block-paragraph">Long story short for this, we&#8217;re pretty much just going to…</p>



<p class="wp-block-paragraph">oh I forgot to full screen the annotator let me do that to get myself a little more room</p>



<p class="wp-block-paragraph">we&#8217;re really just going to promote its one child to wherever that node was so let me erase this</p>



<p class="wp-block-paragraph">real fast and we&#8217;ll say that let&#8217;s first let&#8217;s find a node with only one child it&#8217;s not the 35</p>



<p class="wp-block-paragraph">it&#8217;s the 32 node okay I guess that&#8217;s why I drew that so let&#8217;s kill the number 32</p>



<p class="wp-block-paragraph">You know, if there was just like one child, like let&#8217;s say the 23 was the only node that existed</p>



<p class="wp-block-paragraph">and there were no nodes underneath the 23, then this would really be the same operation. We would</p>



<p class="wp-block-paragraph">literally just promote the 23 up to where the 32 was. But I wanted to do this example because it&#8217;s</p>



<p class="wp-block-paragraph">a little bit more difficult. So we want to kill the 32. What we&#8217;re going to do is first search</p>



<p class="wp-block-paragraph">for it. So this is already going to be an O of H operation or a log time if the tree is perfectly</p>



<p class="wp-block-paragraph">We do, we go down here, we go down there.</p>



<p class="wp-block-paragraph">You know, where is the 32?</p>



<p class="wp-block-paragraph">We&#8217;re just searching for it.</p>



<p class="wp-block-paragraph">So we had to spend the time to search for the node.</p>



<p class="wp-block-paragraph">We then look at the node.</p>



<p class="wp-block-paragraph">Okay, we found the 32.</p>



<p class="wp-block-paragraph">We identify how many children it has.</p>



<p class="wp-block-paragraph">We see that this is a node with only one child.</p>



<p class="wp-block-paragraph">And that means the one child, which in this case is 23, just needs to be promoted up to where the 32 is.</p>



<p class="wp-block-paragraph">just you know remove the 32 from the diagram maybe get rid of get get rid of one of these uh</p>



<p class="wp-block-paragraph">connecting lines and then just move the 23 up a level and that&#8217;s pretty much it if you&#8217;re thinking</p>



<p class="wp-block-paragraph">about this in terms of code uh you you want to have let&#8217;s see let me duplicate this again in a</p>



<p class="wp-block-paragraph">different way if you&#8217;re thinking about code what you want to do is have the 32 which is the node</p>



<p class="wp-block-paragraph">you&#8217;re going to delete go to its child so determine if it&#8217;s the left child or the right</p>



<p class="wp-block-paragraph">or the right child that exists once you find that child you tell it that its new parent is the</p>



<p class="wp-block-paragraph">parent of the node you&#8217;re trying to delete so that means the parent of the 23 is no longer 32 the</p>



<p class="wp-block-paragraph">parent of the 23 is actually 35 you know it&#8217;s its original grandparent or the the parent of the node</p>



<p class="wp-block-paragraph">you&#8217;re trying to delete after you do that you tell the grandparent that it&#8217;s no longer pointing down</p>



<p class="wp-block-paragraph">to the node you&#8217;re trying to delete but instead to its grandchild so again we&#8217;re just going to</p>



<p class="wp-block-paragraph">So again, we&#8217;re just going to manipulate some pointers here.</p>



<p class="wp-block-paragraph">We&#8217;re going to say, yep, your left child is now the 23 and not the 32.</p>



<p class="wp-block-paragraph">Notice how we&#8217;re kind of, you know, drawing arrows around the original node.</p>



<p class="wp-block-paragraph">We&#8217;re sort of cutting it out of the situation, just like in a linked list where you remove one node.</p>



<p class="wp-block-paragraph">And this is going to be a constant time operation, at least after we search and find the 32 node.</p>



<p class="wp-block-paragraph">Finally, of course, you want to like delete the 32.</p>



<p class="wp-block-paragraph">you have in your tree you would call delete on it depending on your language and then uh if it was</p>



<p class="wp-block-paragraph">a smart pointer uh as soon as no other nodes are pointing to the 32 it&#8217;s just going to go away so</p>



<p class="wp-block-paragraph">you don&#8217;t really have to worry about it but yeah long story short uh notice how the 23 is actually</p>



<p class="wp-block-paragraph">the new left child of the 35 so that&#8217;s why i say that the the uh the one child that was on the 32</p>



<p class="wp-block-paragraph">is going to get promoted uh to wherever the 32 was wherever the node you wanted to delete was</p>



<p class="wp-block-paragraph">this will be the same whether or not whether it&#8217;s the left child or the right child in terms of what</p>



<p class="wp-block-paragraph">is the one child that the node had and also you know how do you decide how to tell the the</p>



<p class="wp-block-paragraph">grandparent is its left child pointer going to be changing or is its right child pointer going to be</p>



<p class="wp-block-paragraph">changing well you know when you originally looked at that 32 and you&#8217;re about to delete it you could</p>



<p class="wp-block-paragraph">tell that it had that it had one child based on its left pointer and right pointer one of them</p>



<p class="wp-block-paragraph">pointer one of them was null and one of them was not so whichever one of them was null that tells</p>



<p class="wp-block-paragraph">you the direction that the grandparent should be pointing so in this case the 32 had a left child</p>



<p class="wp-block-paragraph">pointer that was not null and a right child pointer that was null because there was no right child</p>



<p class="wp-block-paragraph">so that&#8217;s why we told the grandparent that its left child pointer is now the 23.</p>



<p class="wp-block-paragraph">and that&#8217;s it for deleting a node that has one child okay so uh i&#8217;m going to go ahead and delete</p>



<p class="wp-block-paragraph">one more time and we&#8217;re going to do the deletion of a node let me let me write down you know deleted</p>



<p class="wp-block-paragraph">a node one child yay we did it you guys um okay i&#8217;m gonna put that there too just for my notes</p>



<p class="wp-block-paragraph">so this is going to be the hardest uh part we&#8217;re going to try to delete a node that has two</p>



<p class="wp-block-paragraph">children maybe i&#8217;m first going to delete the uh the 35 nodes since that&#8217;s kind of where we&#8217;re</p>



<p class="wp-block-paragraph">node since that&#8217;s kind of where we&#8217;re hanging out lately and then we&#8217;ll delete the hardest node of</p>



<p class="wp-block-paragraph">all the 41 nodes so the first thing we need to know before we can get ready for deletion is</p>



<p class="wp-block-paragraph">the in order traversal sequence of the data in this tree if you&#8217;re watching this video as soon</p>



<p class="wp-block-paragraph">as i published it then i i don&#8217;t have videos up yet on traversal i&#8217;m going to upload those soon</p>



<p class="wp-block-paragraph">then you can just check out one of my traversal videos.</p>



<p class="wp-block-paragraph">But long story short,</p>



<p class="wp-block-paragraph">in order traversal just means produce a sorted list,</p>



<p class="wp-block-paragraph">an ascending list or a non-decreasing list</p>



<p class="wp-block-paragraph">based off the data that&#8217;s inside of the tree.</p>



<p class="wp-block-paragraph">So, you know, if I put this down here,</p>



<p class="wp-block-paragraph">I can just say, you know, looking visually</p>



<p class="wp-block-paragraph">from left to right, this is why I draw my trees in this way.</p>



<p class="wp-block-paragraph">I see a six and then a nine and then an 11</p>



<p class="wp-block-paragraph">and then a 16 and then a 19 and then a 23</p>



<p class="wp-block-paragraph">and then a 29, no, no, no, a 26, and then a 29, and then a 32, 35, 38, 41, 43, 48, 52, 59, 67,</p>



<p class="wp-block-paragraph">73, and 79. Let me just double check that I&#8217;ve done this correctly. The numbers seem to be</p>



<p class="wp-block-paragraph">to be increasing or at least not decreasing.</p>



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



<p class="wp-block-paragraph">Okay, how many do I have?</p>



<p class="wp-block-paragraph">1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19.</p>



<p class="wp-block-paragraph">I already got lost.</p>



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



<p class="wp-block-paragraph">1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19.</p>



<p class="wp-block-paragraph">Okay, did it right.</p>



<p class="wp-block-paragraph">list, an ascending list of the original data that was inside of the tree. And now we need to look</p>



<p class="wp-block-paragraph">at a concept called in order successor. Successor comes after, predecessor comes before. Think of</p>



<p class="wp-block-paragraph">like a king and a prince, right? The prince is the successor to the king. Eventually one day when</p>



<p class="wp-block-paragraph">the king grows very old and becomes grandpa king and the prince grows up and becomes the regular</p>



<p class="wp-block-paragraph">king, then you would look at the prince king and you would say, who was your predecessor? Well,</p>



<p class="wp-block-paragraph">grandpa king right so whoever comes before his predecessor whoever comes after is the successor</p>



<p class="wp-block-paragraph">so we want the in order successor of the node that we wish to delete so I can&#8217;t remember what</p>



<p class="wp-block-paragraph">I said originally but let&#8217;s just say we&#8217;re going to delete the 35 node I should have written it</p>



<p class="wp-block-paragraph">down let&#8217;s delete the 35 first we have to find the 35 itself to make sure it exists in the tree</p>



<p class="wp-block-paragraph">so I&#8217;m going to look at the 41 I&#8217;m going to go left to the 16 I&#8217;m going to go right to the 35</p>



<p class="wp-block-paragraph">We found it in O time in the worst case.</p>



<p class="wp-block-paragraph">So this is obviously not going to be the fastest operation, but you know, all the ops are kind</p>



<p class="wp-block-paragraph">of the same speed in a perfectly balanced binary search tree, not empirically, but in</p>



<p class="wp-block-paragraph">terms of time complexity.</p>



<p class="wp-block-paragraph">So we found the 35, we identified that it has two children.</p>



<p class="wp-block-paragraph">So then what we have to do is find the in order successor.</p>



<p class="wp-block-paragraph">What is the in order successor of the 35 node?</p>



<p class="wp-block-paragraph">list 35 here and it appears to be 38 and that actually makes sense because since the data in</p>



<p class="wp-block-paragraph">the tree is going to look sorted if you sweep your eyes from left to right assuming you drew</p>



<p class="wp-block-paragraph">the diagram well you&#8217;re really just looking for the the node that is as close as possible</p>



<p class="wp-block-paragraph">to the node you wish to delete but happens to be to the right of it there are some tutorials out</p>



<p class="wp-block-paragraph">there that use the in order predecessor so that would mean the 32 would be the one you select</p>



<p class="wp-block-paragraph">I like to do successor.</p>



<p class="wp-block-paragraph">So first we find the 35,</p>



<p class="wp-block-paragraph">then we find it&#8217;s in order successor.</p>



<p class="wp-block-paragraph">We&#8217;ll do another delete maybe next of the 16 node</p>



<p class="wp-block-paragraph">because that&#8217;ll be a little more difficult.</p>



<p class="wp-block-paragraph">Once we find the in order successor,</p>



<p class="wp-block-paragraph">we&#8217;re actually just gonna copy the data up into the tree.</p>



<p class="wp-block-paragraph">So I&#8217;m gonna, sorry, up into the node we want to delete.</p>



<p class="wp-block-paragraph">So that means I&#8217;m gonna take this 35</p>



<p class="wp-block-paragraph">and it no longer has 35 as its value.</p>



<p class="wp-block-paragraph">as its value. Instead, we&#8217;re going to steal the value of its in order successor. So 35, sorry,</p>



<p class="wp-block-paragraph">38 up here. So now we have two 38s in the tree. That&#8217;s bad. We need to recursively delete the 38</p>



<p class="wp-block-paragraph">node. It&#8217;s not really a recursive delete. It&#8217;s more like you&#8217;re inside the remove function or</p>



<p class="wp-block-paragraph">the delete function or whatever you chose to call it. It&#8217;s usually remove. And after you&#8217;ve copied</p>



<p class="wp-block-paragraph">of the in order successor to the node you actually wanted to delete then you call delete or remove</p>



<p class="wp-block-paragraph">again on the the in order successor you probably want to use a pointer to that node or have some</p>



<p class="wp-block-paragraph">sort of a way to reference that node without telling the tree to start deleting from the top</p>



<p class="wp-block-paragraph">again because if we say if we just call tree.remove with the number 38 it&#8217;s going to actually find the</p>



<p class="wp-block-paragraph">38 on top and that&#8217;s going to be no good because then if you&#8217;ve written your code correctly</p>



<p class="wp-block-paragraph">uh at least after that point then it&#8217;s going to find the 38 and copy its value up</p>



<p class="wp-block-paragraph">and then it&#8217;s going to happen all over again we&#8217;re going to just be stuck in an infinite loop</p>



<p class="wp-block-paragraph">um so instead you want to have like a pointer to this node or a reference to this node</p>



<p class="wp-block-paragraph">and then call a removal function or a delete function that can receive just like you know</p>



<p class="wp-block-paragraph">a reference or pointer directly to that node and then if you notice the number 38 node</p>



<p class="wp-block-paragraph">So there&#8217;s actually a guarantee that when you find the in-order successor, it will only have zero or one children.</p>



<p class="wp-block-paragraph">It&#8217;s absolutely impossible, unless your tree is super messed up in some way, that the node would have two children.</p>



<p class="wp-block-paragraph">Because if it did, then you have not gone far enough to the left towards the node you&#8217;re trying to delete.</p>



<p class="wp-block-paragraph">Remember we said we need to find the node that is as close as possible to the node that we want to delete, but just like on the right side.</p>



<p class="wp-block-paragraph">the 38 had a left child then we would actually want to follow the left child we wouldn&#8217;t stop at</p>



<p class="wp-block-paragraph">the 38 node so it&#8217;s impossible for the node to have two children which means it&#8217;s going to end</p>



<p class="wp-block-paragraph">up being an easy deletion so the first thing we do is we&#8217;re we&#8217;re saying let&#8217;s delete a node with</p>



<p class="wp-block-paragraph">two children that&#8217;s the 35 we find it&#8217;s in order successor we copy its value up and then we call</p>



<p class="wp-block-paragraph">delete again or remove again on this in order successor which is guaranteed to just be an easy</p>



<p class="wp-block-paragraph">leaf removal or a node with one child removal where we just like you know promote its child up</p>



<p class="wp-block-paragraph">and that&#8217;s it so let me uh let me duplicate this real fast and uh let me say also that everything</p>



<p class="wp-block-paragraph">we just did was basically all it&#8217;s just going to be o of h because it&#8217;s going to be o of h to go</p>



<p class="wp-block-paragraph">find the node in the worst case and then o of h to find its in order successor uh some fraction of</p>



<p class="wp-block-paragraph">each you know o of h fraction o of h fraction but when you just combine them it&#8217;s just going to be</p>



<p class="wp-block-paragraph">It&#8217;s just going to be O or log time if it&#8217;s a perfectly balanced tree.</p>



<p class="wp-block-paragraph">And then once we find the in order successor and the node we want it to delete, it&#8217;s just</p>



<p class="wp-block-paragraph">a matter of like just copying data and maybe doing the second delete.</p>



<p class="wp-block-paragraph">So, you know, the second delete itself would just be like another O or log time.</p>



<p class="wp-block-paragraph">So you probably know at this point that if you have two times log of N, it&#8217;s just going</p>



<p class="wp-block-paragraph">to cancel out to log of N if we&#8217;re talking about big O time complexity.</p>



<p class="wp-block-paragraph">time complexity. Anyway, so I&#8217;m going to duplicate this one more time and then revert everything</p>



<p class="wp-block-paragraph">to scratch to scratch here or to the very beginning again. So we can delete something</p>



<p class="wp-block-paragraph">that&#8217;s a little bit more difficult. Whoops, that&#8217;s bad. Can I get this? Nope. Can I get</p>



<p class="wp-block-paragraph">that? Yes. Okay. So let&#8217;s delete that 16 node. So we can look at how you might find the in</p>



<p class="wp-block-paragraph">order successor in a program because you don&#8217;t want to like traverse the whole entire tree and</p>



<p class="wp-block-paragraph">produce a giant list and then scan the list and then like find the successor and then once you</p>



<p class="wp-block-paragraph">know the number go search for the for the node that&#8217;s like going to be way too slow the tree</p>



<p class="wp-block-paragraph">should be way faster than that so instead this is kind of how you do it let&#8217;s search for the 16 first</p>



<p class="wp-block-paragraph">so we do like 41 16 belongs on the left side we found the 16 now we have to find the 16s in order</p>



<p class="wp-block-paragraph">successor the one that comes after you can tell just by eyeballing it that it&#8217;s going to be the</p>



<p class="wp-block-paragraph">eyeballing it that it&#8217;s going to be the number 19, right? But how do we find that number in the code</p>



<p class="wp-block-paragraph">to make it a little bit faster? So again, 16 and 19, they&#8217;re just paired up right there.</p>



<p class="wp-block-paragraph">Well, first you just take a hop to the right. So you look at the right child, if it exists.</p>



<p class="wp-block-paragraph">If it doesn&#8217;t, then you have not tried to delete a node with two children. It was definitely a</p>



<p class="wp-block-paragraph">node with the one child at most. So we take a hop to the right to find its right child.</p>



<p class="wp-block-paragraph">as far down as you can possibly go until you reach a dead end.</p>



<p class="wp-block-paragraph">So that means I&#8217;m taking a hop to the right and then I&#8217;m just going to go.</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll do like another color here.</p>



<p class="wp-block-paragraph">I&#8217;ll go left child pointer, left child pointer, left child pointer,</p>



<p class="wp-block-paragraph">as far down as I can possibly go. And that is the successor.</p>



<p class="wp-block-paragraph">Notice how that&#8217;s the 19. Again,</p>



<p class="wp-block-paragraph">if the 19 had a child on its left side,</p>



<p class="wp-block-paragraph">we would have to follow it down further,</p>



<p class="wp-block-paragraph">which means it&#8217;s guaranteed that you will stop at a node that has no left child.</p>



<p class="wp-block-paragraph">no left child if it had a right child same situation it&#8217;s still going to be the in order</p>



<p class="wp-block-paragraph">successor that just but that just means when we call a remove a second time on that node it&#8217;s</p>



<p class="wp-block-paragraph">going to be a leaf that gets removed and not a node with one child so it&#8217;s just pretty easy let</p>



<p class="wp-block-paragraph">me uh let me say that now that we&#8217;ve found the in order successor we&#8217;re just going to copy the value</p>



<p class="wp-block-paragraph">so i&#8217;m going to do maybe i&#8217;ll put this in orange so this 16 is no longer in that particular node</p>



<p class="wp-block-paragraph">no longer in that particular node we&#8217;re not even deleting that node we&#8217;re just stealing a value</p>



<p class="wp-block-paragraph">from a different node we&#8217;re going to steal the 19 value from the node which is the in order successor</p>



<p class="wp-block-paragraph">and now that we&#8217;ve done that we&#8217;ll use our reference to the 19 node to call remove on it</p>



<p class="wp-block-paragraph">specifically so when we call remove or delete it&#8217;s basically going to say all right what sort</p>



<p class="wp-block-paragraph">of node is this um it&#8217;s not even going to have to search for the node because you gave a point</p>



<p class="wp-block-paragraph">you gave a point to the node or a reference to the node it&#8217;s going to go oh this is a leaf node</p>



<p class="wp-block-paragraph">that&#8217;s easy i just tell the 23 that it has no left child anymore and then i just actually</p>



<p class="wp-block-paragraph">deallocate the 19 node or if it&#8217;s a smart pointer let it die on its own anyway let&#8217;s do something a</p>



<p class="wp-block-paragraph">little bit more difficult now so i&#8217;m going to duplicate this and try to revert all this stuff</p>



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



<p class="wp-block-paragraph">Let&#8217;s delete the root node, the hardest node of all, the 41.</p>



<p class="wp-block-paragraph">Okay, so if we want to delete the 41 node, I&#8217;m just going to put 41 here so I don&#8217;t forget.</p>



<p class="wp-block-paragraph">First, we search for 41.</p>



<p class="wp-block-paragraph">In the worst case, you know, it would have been O of H, but we were lucky.</p>



<p class="wp-block-paragraph">We just found it right away.</p>



<p class="wp-block-paragraph">Now we have to find the in-order successor for the 41.</p>



<p class="wp-block-paragraph">So again, take a hop to the right and then follow left child pointers as far down as you can possibly go.</p>



<p class="wp-block-paragraph">go so we&#8217;ll look at the 48 here and then we&#8217;ll look at the 43 now we&#8217;ve hit a</p>



<p class="wp-block-paragraph">dead end and we know that the in-order successor for the 41 is 43 and that</p>



<p class="wp-block-paragraph">makes sense because they&#8217;re paired up here in this sorted list at the bottom</p>



<p class="wp-block-paragraph">so then the next thing that we do is we just copy the data you know we steal the</p>



<p class="wp-block-paragraph">data from the 43 so I&#8217;m gonna cross out this 41 here and it&#8217;s gonna end up</p>



<p class="wp-block-paragraph">and then we call delete a second time or remove a second time on the actual 43 which is another</p>



<p class="wp-block-paragraph">easy removal because it could not possibly have two children it&#8217;s only going to have zero or one</p>



<p class="wp-block-paragraph">children and well that&#8217;s pretty much it it&#8217;s just going to be just so fast and easy so that means</p>



<p class="wp-block-paragraph">the 43 is gone when we&#8217;re finished here if you look at all the slides in this video you&#8217;ll notice</p>



<p class="wp-block-paragraph">notice that the data is still ordered correctly so we do have a valid binary search tree even after</p>



<p class="wp-block-paragraph">we kind of do this weird data stealing let me see if i can find something that would have had one</p>



<p class="wp-block-paragraph">child so i want to look through the whole tree and see if i can find a node that has one child</p>



<p class="wp-block-paragraph">that would make a good in order successor and if i can&#8217;t find one i&#8217;m just going to manufacture</p>



<p class="wp-block-paragraph">one real fast let&#8217;s see um who has a right child only no one has a right child only that sucks</p>



<p class="wp-block-paragraph">child only that sucks all right uh let&#8217;s say we want to delete the root node again</p>



<p class="wp-block-paragraph">and i think last time we just said it was the 43 how about we uh that&#8217;s boring let&#8217;s delete the 59</p>



<p class="wp-block-paragraph">okay so i&#8217;m going to uh write 59 up here so i don&#8217;t forget and then we&#8217;re going to add another</p>



<p class="wp-block-paragraph">node real fast so that the 50 uh sorry the 67 has a right child why am i doing this because the 67</p>



<p class="wp-block-paragraph">order successor of the 59 node. So I&#8217;m just going to draw like another line real fast like that.</p>



<p class="wp-block-paragraph">And I&#8217;m going to update some number. Let me just say 70 is going to be the number in that node,</p>



<p class="wp-block-paragraph">which is double check that it&#8217;s increasing 59, 67, 70. Okay. So we want to delete 79,</p>



<p class="wp-block-paragraph">sorry, 59. The first thing we do is we go find the 59. So I&#8217;m going to</p>



<p class="wp-block-paragraph">take off my line tool. Okay. So we&#8217;re going to go to the root 59 belongs on the right side.</p>



<p class="wp-block-paragraph">59 belongs on the right side so we find the 59 there it is now we&#8217;re looking for the in-order</p>



<p class="wp-block-paragraph">successor we go to the right side and then we follow all left child pointers until we hit a</p>



<p class="wp-block-paragraph">dead end left child pointer dead end already because i mean there is a right child but we&#8217;re</p>



<p class="wp-block-paragraph">supposed to be going down left child pointers there is no left child pointer so we&#8217;ve hit a</p>



<p class="wp-block-paragraph">dead end so that means the 67 is the in-order successor of the 59 which means we&#8217;re going to</p>



<p class="wp-block-paragraph">67 data and put it where the 59 data was. So again, we&#8217;re not really deleting the node that</p>



<p class="wp-block-paragraph">had two children. We&#8217;re going to eventually delete the node that had one child, the successor.</p>



<p class="wp-block-paragraph">But 67 is the number that&#8217;s going to be there. And then we call delete on this 67. And what&#8217;s</p>



<p class="wp-block-paragraph">going to end up happening is our removal function identifies that that 67 had one child. And so what</p>



<p class="wp-block-paragraph">one child which is the 70 node just one level higher where the 67 was so that means the uh</p>



<p class="wp-block-paragraph">the 67 is just gone we do actually delete that and then the 70 node is going to be the new</p>



<p class="wp-block-paragraph">left child of the 73 node notice how the data still makes sense okay i think that&#8217;s all i&#8217;ve</p>



<p class="wp-block-paragraph">log time operation just one more time log time if the tree is perfectly balanced it&#8217;s a little bit</p>



<p class="wp-block-paragraph">closer to linear time as the tree becomes more imbalanced if this was a self-balancing tree then</p>



<p class="wp-block-paragraph">you know this would be like log time in terms of worst case scenario in general we&#8217;ll say that this</p>



<p class="wp-block-paragraph">is still on average if we had just totally random data and totally random deletions on average this</p>



<p class="wp-block-paragraph">would be you know a log time operation even if we do log down to find the node and then another</p>



<p class="wp-block-paragraph">uh down to find the node and then another log to find the successor that&#8217;s still not going to be</p>



<p class="wp-block-paragraph">you know worse than log or o of h in general okay that&#8217;s all i&#8217;ve got for you thank you so</p>



<p class="wp-block-paragraph">much for watching this video i hope you learned a little bit of stuff and i hope you had a little</p>



<p class="wp-block-paragraph">bit of fun i need to see if someone is selling pi at this hour going on a searching thing</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</p>



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



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



<p class="wp-block-paragraph">and grow this community.</p>



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



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



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



<p class="wp-block-paragraph">the night and I just wake up because I know somebody subscribed or followed it</p>



<p class="wp-block-paragraph">just wakes me up and I get filled with joy that&#8217;s exactly what happens every</p>



<p class="wp-block-paragraph">single time so you could do it as a nice favor to me or you could you control me</p>



<p class="wp-block-paragraph">if you want to just wake me up in the middle of night just subscribe and then</p>



<p class="wp-block-paragraph">I&#8217;ll just wake up I promise that&#8217;s what will happen also if you look at the</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">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</p>



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



<p class="wp-block-paragraph">just want to say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever i also</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</p>



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



<p class="wp-block-paragraph">Let&#8217;s talk about.</p>
<p>The post <a href="https://www.NeuralLantern.com/binary-search-tree-removals-delete-nodes-with-0-1-or-2-children/">Binary Search Tree Removals &#8211; Delete Nodes with 0, 1, or 2 Children</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-removals-delete-nodes-with-0-1-or-2-children/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>Building a Binary Search Tree &#8211; Insertion Tutorial</title>
		<link>https://www.NeuralLantern.com/building-a-binary-search-tree-insertion-tutorial/</link>
					<comments>https://www.NeuralLantern.com/building-a-binary-search-tree-insertion-tutorial/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 12 Apr 2026 05:12:30 +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 example]]></category>
		<category><![CDATA[binary search tree tutorial]]></category>
		<category><![CDATA[binary tree insertion]]></category>
		<category><![CDATA[BST depth]]></category>
		<category><![CDATA[BST from scratch]]></category>
		<category><![CDATA[BST height]]></category>
		<category><![CDATA[BST insertion]]></category>
		<category><![CDATA[build binary search tree]]></category>
		<category><![CDATA[computer science tutorial]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[data structures and algorithms]]></category>
		<category><![CDATA[in order traversal]]></category>
		<category><![CDATA[insert into binary search tree]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=330</guid>

					<description><![CDATA[<p>In this tutorial we build a binary search tree together by inserting the numbers 73, 98, 12, 45, 67, 10, 43, 11, 99, and 32 one by one. Starting from an empty tree, each number is placed according to binary search tree rules: smaller values go left, larger values go right. We let the nodes fall where they belong without any rearranging, resulting in a realistic (and slightly lopsided) BST. The video explains tree depth, height, and confirms the final structure maintains all BST properties with a properly sorted in-order traversal.</p>
<p>The post <a href="https://www.NeuralLantern.com/building-a-binary-search-tree-insertion-tutorial/">Building a Binary Search Tree &#8211; Insertion Tutorial</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="Building a Binary Search Tree - Insertion Tutorial" width="1380" height="776" src="https://www.youtube.com/embed/iuuUaa4CxoU?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 video we build a binary search tree by inserting numbers one by one: 73, 98, 12, 45, 67, 10, 43, 11, 99, and 32. Watch exactly how each node finds its place following BST rules with no rearranging allowed.</p>



<p class="wp-block-paragraph">We discuss why the tree becomes lopsided, calculate its height, and verify the in-order traversal is sorted. Great practice for anyone learning binary search trees.</p>



<p class="wp-block-paragraph">Next videos will cover searching and deleting nodes.</p>



<p class="wp-block-paragraph">If you&#8217;re studying data structures, build along with me!</p>



<p class="wp-block-paragraph">00:00 Introduction to Building a Binary Search Tree<br>00:14 Plan for Inserting Numbers<br>00:30 First Node Becomes Root<br>02:26 Begin inserting from data set<br>12:29 Final insertion from data set<br>13:26 Final Tree Overview<br>13:29 Tree is Lopsided<br>14:00 Drawing Depths and Levels<br>14:50 Calculating Tree Height<br>15:08 Verifying Sorted Order<br>16:00 Confirming BST Properties<br>16:48 Time Complexity and Height<br>18:59 No Self-Balancing<br>19:07 End of Video 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 a binary search tree together if you&#8217;ve seen my previous videos</p>



<p class="wp-block-paragraph">you understand now how to define a binary search tree meaning what are the rules</p>



<p class="wp-block-paragraph">that you have to like adhere to if you want your thing to be a binary search tree</p>



<p class="wp-block-paragraph">also terminology we&#8217;re in previous videos so in this video we&#8217;re just going to take a bunch of</p>



<p class="wp-block-paragraph">random numbers and we&#8217;re just going to start dropping them into our tree and see how the</p>



<p class="wp-block-paragraph">tree builds itself out we have to build our tree in a specific way if it&#8217;s going to be valid we&#8217;re</p>



<p class="wp-block-paragraph">uh valid we&#8217;re not going to rearrange our tree and try to make it super pretty this is going to be</p>



<p class="wp-block-paragraph">like a real binary search tree it might not be pretty but it&#8217;ll be real let&#8217;s see if i can find</p>



<p class="wp-block-paragraph">those numbers that i was going to set up okay so for this particular video i think i&#8217;m going to do</p>



<p class="wp-block-paragraph">two videos i want to build a binary search tree using the following numbers they&#8217;re not special</p>



<p class="wp-block-paragraph">not not perfect but what I mean to say when I say inserting these numbers is one by one we&#8217;ll look</p>



<p class="wp-block-paragraph">at each number and we&#8217;ll drop it in the tree and we&#8217;ll see where it falls according to binary</p>



<p class="wp-block-paragraph">search tree rules of insertion then we&#8217;ll just go to the next number and then the next and the next</p>



<p class="wp-block-paragraph">when we&#8217;re done we should have a tree and if you follow along at home and you use the right rules</p>



<p class="wp-block-paragraph">to build your tree then if we use the same sequence of numbers to build the tree we should</p>



<p class="wp-block-paragraph">they won&#8217;t suddenly be different they&#8217;re deterministic okay so the first thing that</p>



<p class="wp-block-paragraph">we should try to understand is uh when we want to insert a node the 73 node right here the first</p>



<p class="wp-block-paragraph">thing we should ask ourselves is do we actually have a root node for our binary search tree yet</p>



<p class="wp-block-paragraph">right now the answer is no this is just a blank tree there&#8217;s nothing in it which means that first</p>



<p class="wp-block-paragraph">number is going to be the root node you might be tempted to say no that&#8217;s a sucky root node let&#8217;s</p>



<p class="wp-block-paragraph">do something else well i don&#8217;t know i mean you just have to let the numbers fall where they&#8217;re</p>



<p class="wp-block-paragraph">going to fall just because a number seems to suck as a root node doesn&#8217;t mean it&#8217;s not going to be</p>



<p class="wp-block-paragraph">the root node if it&#8217;s the first thing you added it&#8217;s going to be the root node don&#8217;t try to change</p>



<p class="wp-block-paragraph">it or you&#8217;re going to mess you&#8217;re going to end up with topology that doesn&#8217;t match whatever system</p>



<p class="wp-block-paragraph">or person is trying to grade your work or evaluate your work so we have no root node that means the</p>



<p class="wp-block-paragraph">node of the entire tree we&#8217;re now done with 73 i&#8217;m going to duplicate this slide real fast</p>



<p class="wp-block-paragraph">and we&#8217;ll say now it&#8217;s time to try to add the 98 you know what i can i think i can do better than</p>



<p class="wp-block-paragraph">that arrow i can make a nice cute arrow with this program i&#8217;m going to do this can i like yeah</p>



<p class="wp-block-paragraph">hang on one more one more try nice okay so now let&#8217;s add the 98 node</p>



<p class="wp-block-paragraph">the first thing we have to ask is do we have a root node uh yes we can&#8217;t actually put the 98 there</p>



<p class="wp-block-paragraph">we now have to make a decision uh if you recall in my previous videos the lesser values go on the</p>



<p class="wp-block-paragraph">left and the greater values go on the right uh in in these trees which we&#8217;re not going to support</p>



<p class="wp-block-paragraph">duplicate values with if you wanted to support duplicate values you could do something like</p>



<p class="wp-block-paragraph">less than or equal to would be on the left and greater than on the right there&#8217;s a bunch of</p>



<p class="wp-block-paragraph">other ways you can do it but i&#8217;m just going to say no duplicate values in our trees so we look at the</p>



<p class="wp-block-paragraph">So we look at the 73 node and we ask, you know, is that where our 98 is going to go?</p>



<p class="wp-block-paragraph">It can&#8217;t go there because 73 exists.</p>



<p class="wp-block-paragraph">Now we have to ask the question, does 98 belong on the left side or the right side of the 73?</p>



<p class="wp-block-paragraph">Well, because 98 is greater than 73, it belongs on the right side, which means I&#8217;m going to duplicate this node over here.</p>



<p class="wp-block-paragraph">And I&#8217;m going to try to draw this well, which means basically splitting the difference in terms of the empty space.</p>



<p class="wp-block-paragraph">empty space and trying to keep the same rank as we go down one level of</p>



<p class="wp-block-paragraph">descendancy or like one generation down at a time.</p>



<p class="wp-block-paragraph">So the next time I go down a level,</p>



<p class="wp-block-paragraph">let&#8217;s say I do a left child in the future,</p>



<p class="wp-block-paragraph">it should be at the same level as the new node I just added.</p>



<p class="wp-block-paragraph">So I did a duplicate and I forgot to change the number.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to do a line to connect the 73 and the 98.</p>



<p class="wp-block-paragraph">98 so now the 73 has a right child which is 98 the 98 has a parent which is 73 and we&#8217;re done</p>



<p class="wp-block-paragraph">adding the 98 we have to just move on to the next number okay so i&#8217;m going to move on to the 12</p>



<p class="wp-block-paragraph">again we look at the root node it&#8217;s occupied we cannot put the 12 right there on the 73 we have</p>



<p class="wp-block-paragraph">to make a decision do we go to the left or the right of the 73 12 is less than 73 so it belongs</p>



<p class="wp-block-paragraph">is to jump to the left side. Notice how the left side there&#8217;s no child on the left that means that&#8217;s</p>



<p class="wp-block-paragraph">where the new node is going to go so I&#8217;m just going to like select this duplicate it real fast</p>



<p class="wp-block-paragraph">maybe stick it on the same vertical coordinate and then also try to split the difference on</p>



<p class="wp-block-paragraph">the screen just to make it look pretty. I&#8217;m going to update that 98 so that it is now the 12.</p>



<p class="wp-block-paragraph">child 12 and then we&#8217;re done adding the 12. Okay, no problem so far. We then are ready to try to</p>



<p class="wp-block-paragraph">add the 45. Maybe I should squish this a little bit. Nope, that looks gross. No, it&#8217;s okay.</p>



<p class="wp-block-paragraph">We&#8217;ll do that. We&#8217;re going to add the 45. Again, we look at the root node. It already exists,</p>



<p class="wp-block-paragraph">so we can&#8217;t put the 45 there. Instead, we will just kind of decide does the 45 belong on the</p>



<p class="wp-block-paragraph">Okay, well, belongs on the left because it&#8217;s less than 73.</p>



<p class="wp-block-paragraph">So that means I&#8217;m now going to make a little decision.</p>



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



<p class="wp-block-paragraph">If you&#8217;re in some code, you&#8217;re following the left child pointer down.</p>



<p class="wp-block-paragraph">But notice how the left child pointer or the left child, it already exists.</p>



<p class="wp-block-paragraph">So we can&#8217;t actually put 45 right there.</p>



<p class="wp-block-paragraph">We have to make another decision.</p>



<p class="wp-block-paragraph">Does 45 belong on the left or the right of the 12?</p>



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



<p class="wp-block-paragraph">So it belongs on the right side of 12.</p>



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



<p class="wp-block-paragraph">and duplicate it. I guess I&#8217;ll just do it that way. Then I&#8217;m going to update that number</p>



<p class="wp-block-paragraph">with 45 and then I&#8217;m going to connect the 12 and the 45 with the line. So now 45 is the right child</p>



<p class="wp-block-paragraph">of 12 and 12 is the parent of 45. It&#8217;s at this point that a lot of people start to get frustrated</p>



<p class="wp-block-paragraph">with me and they say, well, how come, how come the 12 is like all the way to the left and the 45 is</p>



<p class="wp-block-paragraph">45 is like didn&#8217;t become like you know a level higher because it kind of seems like it&#8217;s closer</p>



<p class="wp-block-paragraph">to 73 shouldn&#8217;t the 12 be hanging off the left child of the 45 no no no no don&#8217;t rearrange the</p>



<p class="wp-block-paragraph">nodes as you&#8217;re building the tree just let the numbers let the nodes fall wherever they&#8217;re</p>



<p class="wp-block-paragraph">supposed to go another tip is if you&#8217;re building a tree that has other data types as as the t types</p>



<p class="wp-block-paragraph">inside of the nodes as the template types inside of the nodes for example right now I&#8217;m just using</p>



<p class="wp-block-paragraph">type is int, at least in my mind, if you wanted to put dinosaur objects in the tree, all you&#8217;d</p>



<p class="wp-block-paragraph">have to do is make sure that your code for your dinosaur class for your dinosaur object</p>



<p class="wp-block-paragraph">had its inequality operators implemented, you know, less than, greater than, and then the tree</p>



<p class="wp-block-paragraph">would still know where to put all the objects. So you could make any kind of custom object you want</p>



<p class="wp-block-paragraph">and they would all fall in their correct places in the tree as long as the operators actually</p>



<p class="wp-block-paragraph">just as a side note. Okay, so now we&#8217;re ready to add the next item. So I&#8217;m going to erase this</p>



<p class="wp-block-paragraph">stuff here, and I&#8217;m going to move the arrow from the 45 to the 67. So now we&#8217;re going to try to</p>



<p class="wp-block-paragraph">add the 67. We look at the root node, it&#8217;s occupied. We should go to the left because 67</p>



<p class="wp-block-paragraph">is less than 73. We look at the 12, that&#8217;s occupied. So we go left or right. We should go</p>



<p class="wp-block-paragraph">to the right because 67 is greater than 12. So we look at the 45. Again, it&#8217;s occupied. There&#8217;s a</p>



<p class="wp-block-paragraph">a node there so we have to make another decision do we go to the left or to the right well 67 is</p>



<p class="wp-block-paragraph">greater than 45 so it goes on the right side so i&#8217;m going to duplicate this off to the right side</p>



<p class="wp-block-paragraph">and down a rank and we&#8217;ll call that the right child of 45. let me update the number here</p>



<p class="wp-block-paragraph">to 67 and i&#8217;ll connect that as the right child okay</p>



<p class="wp-block-paragraph">Any questions? I&#8217;m just kidding. That was rude. Just know that my heart is with you if you&#8217;re</p>



<p class="wp-block-paragraph">asking questions. Send me a comment in the comments. So now I&#8217;m going to move over to the 10 here.</p>



<p class="wp-block-paragraph">Again, we look at the 67. The 10 belongs on the left. So we kind of go to the left.</p>



<p class="wp-block-paragraph">We look at the 12. The 10 belongs on the left of the 12 because it&#8217;s less than 12.</p>



<p class="wp-block-paragraph">available slot that&#8217;s an available child pointer so I&#8217;m going to say the 10 is going to be the</p>



<p class="wp-block-paragraph">left child of the 12 node maybe I&#8217;ll stick it all the way over there let&#8217;s see is there anything</p>



<p class="wp-block-paragraph">less than 10 in there nope okay so we&#8217;ll be good we have enough space on the screen</p>



<p class="wp-block-paragraph">so change that to a 10 do the connecting line so the 10 is now the left child</p>



<p class="wp-block-paragraph">whoops the left child of the 12 node and now we&#8217;re ready to add the next node</p>



<p class="wp-block-paragraph">node. Okay. So now the 43 is the next one we want to add. One more time, we look at the 73 node,</p>



<p class="wp-block-paragraph">the root node is occupied. So we have to go to the left because 43 is less. So oops, that&#8217;s a line.</p>



<p class="wp-block-paragraph">So now we look at the 12 node is 43 greater than or less than the 12. It&#8217;s greater than so that</p>



<p class="wp-block-paragraph">means we go down to the right. But the 45 is there. So we have to make another decision. 43 is less</p>



<p class="wp-block-paragraph">43 is less than 45 so we go to the left that means this 43 is going to be the left child of 45.</p>



<p class="wp-block-paragraph">And again don&#8217;t try to make the tree pretty or better just let the nodes fall wherever they&#8217;re</p>



<p class="wp-block-paragraph">supposed to fall according to the rules of less than or greater than. So now we got the uh whoops</p>



<p class="wp-block-paragraph">I forgot to change that to a 43. The 43 right there and we are now ready to add another node</p>



<p class="wp-block-paragraph">So I&#8217;m going to like erase this stuff here and I want to move the arrow to the 11.</p>



<p class="wp-block-paragraph">Now once again, we&#8217;re looking for the root node.</p>



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



<p class="wp-block-paragraph">We say that 11 is supposed to go on the left because it&#8217;s less than.</p>



<p class="wp-block-paragraph">We look at the 12.</p>



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



<p class="wp-block-paragraph">So we have to go to the left because 11 is less than 12.</p>



<p class="wp-block-paragraph">We go to the 10 and that&#8217;s also occupied.</p>



<p class="wp-block-paragraph">So we have to make another decision.</p>



<p class="wp-block-paragraph">Is 11 greater than or less than 10?</p>



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



<p class="wp-block-paragraph">why the 11 goes on the right side of the 10. So I&#8217;m going to duplicate this over here. Notice how</p>



<p class="wp-block-paragraph">I&#8217;m trying to keep same ranked children or same depth children physically at the same level. It&#8217;s</p>



<p class="wp-block-paragraph">a lot easier to debug your diagrams if you do things this way. Notice also, as I mentioned in</p>



<p class="wp-block-paragraph">a previous video, that every left descendant of a node should be physically on the left side in my</p>



<p class="wp-block-paragraph">diagram and every right descendant should be physically on the right side. That just makes</p>



<p class="wp-block-paragraph">right side that just makes the diagram way easier to debug what do I mean by this look at 73&#8217;s left</p>



<p class="wp-block-paragraph">descendants its left subtree they&#8217;re all physically on the left look at the 12 node all of its right</p>



<p class="wp-block-paragraph">descendants or its right subtree they&#8217;re all physically on the right so you if you follow</p>



<p class="wp-block-paragraph">that rule it&#8217;s just way easier to debug I think I forgot to update that number from 43 to 11 so</p>



<p class="wp-block-paragraph">I&#8217;m going to do that now and we are ready to add the next number the 99 so I&#8217;m going to do this</p>



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



<p class="wp-block-paragraph">So first we start off by looking at the root node.</p>



<p class="wp-block-paragraph">It&#8217;s 73, occupied. 99 belongs on the right side.</p>



<p class="wp-block-paragraph">So now we bounce to the right side and we look at the 98.</p>



<p class="wp-block-paragraph">Whoops, whoops, whoops. We look at the 98.</p>



<p class="wp-block-paragraph">That&#8217;s occupied, so we make another decision.</p>



<p class="wp-block-paragraph">The 99 belongs on the right side of 98. That slot is available.</p>



<p class="wp-block-paragraph">So now we can say that the 99 node is supposed to be</p>



<p class="wp-block-paragraph">a right child of 99.</p>



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



<p class="wp-block-paragraph">The 99 is the right child of the 98 node.</p>



<p class="wp-block-paragraph">I&#8217;m getting a little confused.</p>



<p class="wp-block-paragraph">Okay, so I&#8217;m gonna do a little connecting line there.</p>



<p class="wp-block-paragraph">And we&#8217;re ready to add the final value into our tree.</p>



<p class="wp-block-paragraph">So I&#8217;m gonna do, let&#8217;s see.</p>



<p class="wp-block-paragraph">Oh, oh, I accidentally erased the arrow.</p>



<p class="wp-block-paragraph">Oh, there it is.</p>



<p class="wp-block-paragraph">We can add the 32 next.</p>



<p class="wp-block-paragraph">add the 32 next and if we look at the 73 it&#8217;s occupied 32 belongs on the left so then we just</p>



<p class="wp-block-paragraph">kind of like follow down to the left here we look at the 12 32 belongs on the right of 12 so we</p>



<p class="wp-block-paragraph">follow down to the right 32 belongs on the left of 45 so we follow down to the left 32 belongs on</p>



<p class="wp-block-paragraph">the left of 43 and so we will make 32 the left child of the 43 node so I&#8217;m going to like select</p>



<p class="wp-block-paragraph">this real fast we go down a full rank and I&#8217;m going to try to split the difference between 12</p>



<p class="wp-block-paragraph">And I&#8217;m going to try to split the difference between 12 and 43 here just to make the diagram easier to debug.</p>



<p class="wp-block-paragraph">Okay, so then I&#8217;m going to do…</p>



<p class="wp-block-paragraph">It&#8217;s looking a little gross, but at least we&#8217;re finished.</p>



<p class="wp-block-paragraph">I have to update that 11 to be the 32.</p>



<p class="wp-block-paragraph">And now we&#8217;re done building our tree.</p>



<p class="wp-block-paragraph">Okay, so some things to point out real fast.</p>



<p class="wp-block-paragraph">Notice how the tree is lopsided.</p>



<p class="wp-block-paragraph">to look at various parts of the tree you might be frustrated why didn&#8217;t we put the 98 over there</p>



<p class="wp-block-paragraph">why didn&#8217;t we put the right just it&#8217;s fine as long as we follow the rules of building a binary search</p>



<p class="wp-block-paragraph">tree then this is the valid tree that we would have built of course later on you&#8217;ll learn techniques</p>



<p class="wp-block-paragraph">to make the tree a little bit better looking a little bit faster but right now we&#8217;re just building</p>



<p class="wp-block-paragraph">a tree with no optimizations no self balancing no anything like that this is our binary search tree</p>



<p class="wp-block-paragraph">tree if we were to take all of the uh you know all the numbers that we added and sort them</p>



<p class="wp-block-paragraph">then that should be the numbers we see from left to right this is another good argument for why we</p>



<p class="wp-block-paragraph">should draw the diagram in this way again like uh same depth nodes let me just draw the the depths</p>



<p class="wp-block-paragraph">real fast just to kind of illustrate the point a little bit same depth nodes should be on the same</p>



<p class="wp-block-paragraph">y coordinate or the same uh horizontal plane notice how 73 is the root node and then 12 with 98 have</p>



<p class="wp-block-paragraph">with 98 have a depth of 1 and then 10 45 and 99 have a depth of 2 and then 11 43 and 67 have a</p>



<p class="wp-block-paragraph">depth of 3 and then 32 has a depth of 4 so the deepest node we can find has a depth of 4 that&#8217;s</p>



<p class="wp-block-paragraph">the 32 node which means the height of the tree um height is 4 sorry 5 the height is always the</p>



<p class="wp-block-paragraph">deepest nodes depth plus 1 or the number of nodes you would have to touch as you found your way</p>



<p class="wp-block-paragraph">you found your way down to the deepest node for example if we wanted to get to that 32 we would</p>



<p class="wp-block-paragraph">touch one two three four five nodes so the height of this tree is five</p>



<p class="wp-block-paragraph">okay um let&#8217;s debug the order of our numbers because this is kind of easy to get wrong</p>



<p class="wp-block-paragraph">so from left to right we should see a sorted list so let me just uh do little markings here</p>



<p class="wp-block-paragraph">to indicate that i&#8217;m seeing increasing or at least non-decreasing numbers</p>



<p class="wp-block-paragraph">If our tree supported duplicates, then it would be non-decreasing.</p>



<p class="wp-block-paragraph">But since we don&#8217;t allow duplicates, it should be increasing or an ascending list.</p>



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



<p class="wp-block-paragraph">And as we look towards the right, we should see increasing numbers.</p>



<p class="wp-block-paragraph">So we got the 10 there.</p>



<p class="wp-block-paragraph">And then we got the 11 there.</p>



<p class="wp-block-paragraph">And then we got the 12 there.</p>



<p class="wp-block-paragraph">32, 43, 45, 67.</p>



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



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



<p class="wp-block-paragraph">73 increases, 98 increases, and 99 increases.</p>



<p class="wp-block-paragraph">and 99 increases. So from left to right, it was basically a sorted list, which is what we want.</p>



<p class="wp-block-paragraph">So we&#8217;ve created a valid ordering for the tree. Now maybe just think about all the rules of a</p>



<p class="wp-block-paragraph">binary search tree to make sure we did that right too. So is this a graph? Yeah. Is it a connected</p>



<p class="wp-block-paragraph">graph? Yeah. Is it a tree? You know, no cycles in the graph? Yeah. Is it a rooted tree? Yeah. The 73</p>



<p class="wp-block-paragraph">root node is the common ancestor of all other nodes. We have one node that&#8217;s common to the</p>



<p class="wp-block-paragraph">in terms of ancestry um does any node have more than two children no none of the nodes have more</p>



<p class="wp-block-paragraph">than two children um they&#8217;ve all got zero or one or two children and then the numbers are in order</p>



<p class="wp-block-paragraph">and therefore this is a valid binary search tree so in other videos i&#8217;m going to talk about the</p>



<p class="wp-block-paragraph">time complexity and and like you know how fast it is to do certain operations or how imbalanced a</p>



<p class="wp-block-paragraph">just so you know this is not a perfect binary search tree you can tell because it&#8217;s a little</p>



<p class="wp-block-paragraph">lopsided um we calculated the height as five and let&#8217;s see where the heck is that calculator i</p>



<p class="wp-block-paragraph">thought i had a calculator on here oh no oh there it is if we calculated the height as five</p>



<p class="wp-block-paragraph">you could probably imagine and we&#8217;ll talk about searching in a future video like how do you search</p>



<p class="wp-block-paragraph">in a way that makes the tree super super fast well we certainly built the tree in a way that</p>



<p class="wp-block-paragraph">makes it super super fast we just have to get to searching later but if we</p>



<p class="wp-block-paragraph">search properly through a properly built binary search tree then at most it</p>



<p class="wp-block-paragraph">should only take us height number of examinations to find our way to any</p>



<p class="wp-block-paragraph">piece of data or to determine that a piece of data doesn&#8217;t exist for example</p>



<p class="wp-block-paragraph">I could say that the time complexity is O and we&#8217;ll do this in other videos of</p>



<p class="wp-block-paragraph">of h no matter what the tree looks like no matter how lopsided it is and what is h here five that</p>



<p class="wp-block-paragraph">just basically is saying in the worst case scenario i would have to touch five nodes to reach the very</p>



<p class="wp-block-paragraph">bottom of the tree and determine that a value didn&#8217;t exist or you know find a value at the very</p>



<p class="wp-block-paragraph">bottom of the tree so the worst case scenario touch five nodes so it&#8217;s o of h if we if we plug</p>



<p class="wp-block-paragraph">log base 2 so if we did log base 2 of n where n is the number of nodes uh oh how many nodes do we</p>



<p class="wp-block-paragraph">have shoot one two three four five six seven eight nine ten i think one two three four five six seven</p>



<p class="wp-block-paragraph">eight nine ten yeah log base two of ten notice how the number here says well it should take like</p>



<p class="wp-block-paragraph">somewhat close to three uh examinations at most four examinations but we just said it&#8217;s going to</p>



<p class="wp-block-paragraph">of a divergence between the real speed of the tree.</p>



<p class="wp-block-paragraph">It might take up to five examinations and log base two,</p>



<p class="wp-block-paragraph">which would be a perfect binary search tree.</p>



<p class="wp-block-paragraph">So the more lopsided a tree gets, the slower it actually becomes.</p>



<p class="wp-block-paragraph">And we&#8217;ll deal with that in future videos.</p>



<p class="wp-block-paragraph">We&#8217;ll learn how to like detect that and balance that and so forth.</p>



<p class="wp-block-paragraph">But so we&#8217;ve built a binary search tree.</p>



<p class="wp-block-paragraph">It&#8217;s definitely valid if not, you know, like super, super fast.</p>



<p class="wp-block-paragraph">And I think that&#8217;s the end of this video.</p>



<p class="wp-block-paragraph">Thank you so much for watching.</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">In the next video, I think I&#8217;m just going to build another tree for you so we can get</p>



<p class="wp-block-paragraph">a second practice in.</p>



<p class="wp-block-paragraph">Okay, future videos also are going to include searching and deleting nodes and modifying</p>



<p class="wp-block-paragraph">existing nodes and just a whole bunch of other stuff.</p>



<p class="wp-block-paragraph">Then eventually we&#8217;ll do self-balancing trees.</p>



<p class="wp-block-paragraph">I&#8217;ll see you later.</p>



<p class="wp-block-paragraph">Hey everybody.</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, could you please subscribe and follow</p>



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



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



<p class="wp-block-paragraph">right now you should see a QR code which you can scan in order to go to the website which I think</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 if you have a suggestion for clarifications or</p>



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



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



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



<p class="wp-block-paragraph">to 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">hey there let&#8217;s talk about uh</p>



<p class="wp-block-paragraph">hello there hello there let&#8217;s build a binary search tree by just inserting a bunch of numbers</p>



<p class="wp-block-paragraph">inserting a bunch of numbers and will follow the rules</p>
<p>The post <a href="https://www.NeuralLantern.com/building-a-binary-search-tree-insertion-tutorial/">Building a Binary Search Tree &#8211; Insertion Tutorial</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/building-a-binary-search-tree-insertion-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>BST Terminology: Root, Leaves, Subtrees, Depth, Height, Ancestors &#038; More</title>
		<link>https://www.NeuralLantern.com/bst-terminology-root-leaves-subtrees-depth-height-ancestors-more/</link>
					<comments>https://www.NeuralLantern.com/bst-terminology-root-leaves-subtrees-depth-height-ancestors-more/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 06 Apr 2026 09:25:41 +0000</pubDate>
				<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[binary search tree basics]]></category>
		<category><![CDATA[binary search tree explained]]></category>
		<category><![CDATA[binary tree terminology]]></category>
		<category><![CDATA[BST ancestors]]></category>
		<category><![CDATA[BST descendants]]></category>
		<category><![CDATA[bst for beginners]]></category>
		<category><![CDATA[BST internal nodes]]></category>
		<category><![CDATA[BST leaf nodes]]></category>
		<category><![CDATA[BST root node]]></category>
		<category><![CDATA[BST subtrees]]></category>
		<category><![CDATA[BST terminology]]></category>
		<category><![CDATA[coding interview prep]]></category>
		<category><![CDATA[computer science trees]]></category>
		<category><![CDATA[data structures tutorial]]></category>
		<category><![CDATA[left subtree]]></category>
		<category><![CDATA[right subtree]]></category>
		<category><![CDATA[tree depth]]></category>
		<category><![CDATA[tree height]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=327</guid>

					<description><![CDATA[<p>This video explains essential binary search tree terminology including root node, internal nodes, external nodes (leaves), left and right subtrees, depth of a node, height of the tree and subtrees, ancestors, descendants, siblings, parent and child relationships using a clear example tree.</p>
<p>The post <a href="https://www.NeuralLantern.com/bst-terminology-root-leaves-subtrees-depth-height-ancestors-more/">BST Terminology: Root, Leaves, Subtrees, Depth, Height, Ancestors &amp; More</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="BST Terminology: Root, Leaves, Subtrees, Depth, Height, Ancestors &amp; More" width="1380" height="776" src="https://www.youtube.com/embed/4iuhXHw26ec?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">Quick but thorough run-through of binary search tree terminology: root, leaves, internal nodes, subtrees, depth, height, ancestors, descendants, siblings, left/right child &#8211; everything clearly labeled on a working example.</p>



<p class="wp-block-paragraph">Great for beginners, interview prep, or reviewing foundational BST concepts before coding insert/search/delete.</p>



<p class="wp-block-paragraph">00:00 Introduction to BST Terminology<br>00:28 Root Node<br>01:10 Ancestors and Descendants<br>01:58 Children, Grandchildren, and Siblings<br>04:07 Internal Nodes vs External Nodes (Leaves)<br>05:34 Understanding Subtrees<br>06:09 Left Subtree and Right Subtree Examples<br>08:34 Depth of a Node<br>11:02 Height of the Tree<br>12:48 Height of Subtrees<br>17:32 Node Structure and Pointers Overview<br>18:12 Closing Remarks and Call to Action</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 binary search tree terminology. If you saw my</p>



<p class="wp-block-paragraph">previous video we talked about how to define a binary search tree meaning a</p>



<p class="wp-block-paragraph">whole bunch of rules so that if the thing you&#8217;re looking at follows all the</p>



<p class="wp-block-paragraph">rules then you know you&#8217;re actually looking at a binary search tree if not</p>



<p class="wp-block-paragraph">then not. So see my previous video if you want to know for sure whether you&#8217;re</p>



<p class="wp-block-paragraph">looking at a binary search tree. For now we&#8217;re just going to talk about some</p>



<p class="wp-block-paragraph">Okay, so the first thing we should probably obviously talk about is the root node over here.</p>



<p class="wp-block-paragraph">So I mean, well, in my previous video, we talked about nodes, right?</p>



<p class="wp-block-paragraph">So this is kind of a graph with a whole bunch of rules on top of it.</p>



<p class="wp-block-paragraph">That means we have nodes and edges.</p>



<p class="wp-block-paragraph">So if you look at the very top node here, sometimes also referred to as a vertex,</p>



<p class="wp-block-paragraph">I think in binary search tree terminology, we usually say nodes.</p>



<p class="wp-block-paragraph">We usually say nodes. I can&#8217;t actually remember. But so look at the 42 there</p>



<p class="wp-block-paragraph">That node is the root node of all other nodes. It&#8217;s the highest common ancestor in the entire tree. So this is the root node</p>



<p class="wp-block-paragraph">First bit of terminology. Also, I tried to sneak past you ancestor</p>



<p class="wp-block-paragraph">So these trees are supposed to be written in a way that kind of looks like they have a family hierarchy with one parent per</p>



<p class="wp-block-paragraph">like not two children per parent, but just like one parent and then either zero or one or two</p>



<p class="wp-block-paragraph">children. So we&#8217;ll say that ancestors are higher on the tree. So that means 42 is actually an</p>



<p class="wp-block-paragraph">ancestor of 33. And it&#8217;s also an ancestor of 12. It&#8217;s also an ancestor of 19. Just anything that&#8217;s</p>



<p class="wp-block-paragraph">higher is an ancestor of anything that&#8217;s lower. We could also say that 33 is an ancestor of 39</p>



<p class="wp-block-paragraph">and 19 and so forth. You can probably also imagine that we have children and grandchildren. Yeah,</p>



<p class="wp-block-paragraph">we do go that far in binary search trees. So the 42 node, it has two children. It has a left child</p>



<p class="wp-block-paragraph">and a right child. The 33 is the left child. I&#8217;ll put LC for left child. And the 67 is its right</p>



<p class="wp-block-paragraph">child. I&#8217;ll put an RC there. The 67 in turn has two children. The 56 has no children of its own.</p>



<p class="wp-block-paragraph">the 33 has two children the 12 only has one child it was you know playing it safe i guess</p>



<p class="wp-block-paragraph">you never know if these children are going to come out and just like run amok</p>



<p class="wp-block-paragraph">and and engage in constant shenanigans so the 12 has a right child but no left child that&#8217;s okay</p>



<p class="wp-block-paragraph">um in terms of going higher on the tree anything that is higher is an ancestor sorry i should have</p>



<p class="wp-block-paragraph">said lower anything that&#8217;s higher is an ancestor anything that&#8217;s lower is a descendant so if we&#8217;re</p>



<p class="wp-block-paragraph">If we&#8217;re looking at the 33 node, the 33 is a descendant of 42 because it&#8217;s the left child of 42.</p>



<p class="wp-block-paragraph">It&#8217;s also an ancestor of anything that comes below it.</p>



<p class="wp-block-paragraph">So it&#8217;s an ancestor of 12 and 39 and 19, right?</p>



<p class="wp-block-paragraph">So if we&#8217;re looking at 33, we&#8217;ve got a left child over here and we&#8217;ve got a right child over here.</p>



<p class="wp-block-paragraph">And then we have a grandchild, which is the 19 node.</p>



<p class="wp-block-paragraph">We don&#8217;t really have left and right grandchildren.</p>



<p class="wp-block-paragraph">You could say a grandchild in the left subtree or the right subtree.</p>



<p class="wp-block-paragraph">subtree talk about sub trees in a second the 33 has a parent node which is just the 42 node</p>



<p class="wp-block-paragraph">which is also the root node of course it&#8217;s got a sibling the 67 is the sibling you can tell</p>



<p class="wp-block-paragraph">something&#8217;s a sibling because it&#8217;s got the same parent as you it&#8217;s the people that you&#8217;re usually</p>



<p class="wp-block-paragraph">fighting with right anyway so if we&#8217;re looking at any node in particular it might have a whole bunch</p>



<p class="wp-block-paragraph">of ancestors above the tree it might have a whole bunch of descendants below the tree</p>



<p class="wp-block-paragraph">It has siblings or it usually has zero or one sibling because in a binary search tree,</p>



<p class="wp-block-paragraph">we can only have up to two children per node.</p>



<p class="wp-block-paragraph">It&#8217;s got sometimes, you know, grandparents and great grandparents and children and great</p>



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



<p class="wp-block-paragraph">So just think about the hierarchy like a family tree would have.</p>



<p class="wp-block-paragraph">Okay, moving on to some more terminology.</p>



<p class="wp-block-paragraph">Next thing is we have internal nodes and also external nodes.</p>



<p class="wp-block-paragraph">So what do I mean by internal? Internal means a node has more than zero children. It has one or two children.</p>



<p class="wp-block-paragraph">So I&#8217;m going to put internal on the 33 because the 33 node has children.</p>



<p class="wp-block-paragraph">The 12 also has children, so it&#8217;s internal. The 39 does not have children, so it&#8217;s not internal.</p>



<p class="wp-block-paragraph">67 has children, so it&#8217;s internal. And the root node, 42, also has children, so it&#8217;s considered internal.</p>



<p class="wp-block-paragraph">That 42 has a lot of different names.</p>



<p class="wp-block-paragraph">It&#8217;s the root node, it&#8217;s the greatest common ancestor,</p>



<p class="wp-block-paragraph">it&#8217;s an internal node and so forth.</p>



<p class="wp-block-paragraph">Notice how the other nodes that I have not highlighted,</p>



<p class="wp-block-paragraph">they have zero children.</p>



<p class="wp-block-paragraph">So when a node has zero children,</p>



<p class="wp-block-paragraph">it&#8217;s known as an external node.</p>



<p class="wp-block-paragraph">It&#8217;s also known as a leaf</p>



<p class="wp-block-paragraph">because we&#8217;re talking about trees</p>



<p class="wp-block-paragraph">and I guess it&#8217;s kind of like a nice synonym.</p>



<p class="wp-block-paragraph">So the 19, the node with no children of its own is a leaf.</p>



<p class="wp-block-paragraph">So is the 39.</p>



<p class="wp-block-paragraph">So is the 56.</p>



<p class="wp-block-paragraph">So is the 76.</p>



<p class="wp-block-paragraph">I just want to point out also, if you were with me on my last video,</p>



<p class="wp-block-paragraph">then the numbers need to be ordered from left to right.</p>



<p class="wp-block-paragraph">But don&#8217;t worry, we&#8217;re going to do another video where we build a complete tree from scratch.</p>



<p class="wp-block-paragraph">There&#8217;s some more terminology we should talk about.</p>



<p class="wp-block-paragraph">So I&#8217;m going to get rid of all these externals and internals real fast.</p>



<p class="wp-block-paragraph">Or the labels.</p>



<p class="wp-block-paragraph">We should talk about the left subtree versus the right subtree.</p>



<p class="wp-block-paragraph">left subtree versus the right subtree i mean what is a subtree anyway the subtree is basically</p>



<p class="wp-block-paragraph">a subtree is basically just pick any node you want in the entire tree let&#8217;s pick</p>



<p class="wp-block-paragraph">the 76 and then we&#8217;ll just pretend that it&#8217;s the root node of a separate tree starting with 76 so</p>



<p class="wp-block-paragraph">if there was anything below it then all those nodes would be included so this 76 right here it</p>



<p class="wp-block-paragraph">really has nothing underneath it it&#8217;s a leaf which means well it can be the root node of its own</p>



<p class="wp-block-paragraph">the subtree is just going to be a tree of one node.</p>



<p class="wp-block-paragraph">So kind of boring, right?</p>



<p class="wp-block-paragraph">You&#8217;re boring.</p>



<p class="wp-block-paragraph">If instead we decided to look at the 33,</p>



<p class="wp-block-paragraph">which is a little bit more interesting,</p>



<p class="wp-block-paragraph">and we called the 33 the root node of its own subtree,</p>



<p class="wp-block-paragraph">then really what we&#8217;re saying is all these nodes here</p>



<p class="wp-block-paragraph">are included in that subtree.</p>



<p class="wp-block-paragraph">So if I told you, give me the subtree starting with node 33,</p>



<p class="wp-block-paragraph">then you would say, oh, it&#8217;s 33, 12, 39, 19.</p>



<p class="wp-block-paragraph">descendants of the subtree root node that we picked out. So subtree just means, you know,</p>



<p class="wp-block-paragraph">like a little fragment or a portion of the original tree. You could also say that the</p>



<p class="wp-block-paragraph">entire tree is a subtree of itself. If you chose the subtree root to be the real root node, I mean,</p>



<p class="wp-block-paragraph">that&#8217;s not super useful, but you can do it. Anyway, so if we decide to say that the 67 is the root of</p>



<p class="wp-block-paragraph">with 67 and below in terms of descendancy is going to be considered part of the subtree.</p>



<p class="wp-block-paragraph">I&#8217;ve highlighted the left subtree and the right subtree of the root node because that&#8217;s usually</p>



<p class="wp-block-paragraph">what we say. We&#8217;ll say this is the left subtree over here and then over here we&#8217;re going to say</p>



<p class="wp-block-paragraph">this is the right subtree. Meaning if you look at any node at all, if it has a left child,</p>



<p class="wp-block-paragraph">then that left child is the root node of the left subtree of the node in question. Same thing for</p>



<p class="wp-block-paragraph">same thing for the right so if I say all right let me duplicate this real fast</p>



<p class="wp-block-paragraph">let me get rid of actually this real fast too if I say okay give me the left subtree of the 33 node</p>



<p class="wp-block-paragraph">well then you would know to include the 12 and the 19 because the left subtree of the 30 node</p>



<p class="wp-block-paragraph">has to the 33 node has to start with the left child of the 33 node which would be the 12 and</p>



<p class="wp-block-paragraph">And we&#8217;ll just say, okay, the 12 is now the root node of its own subtree.</p>



<p class="wp-block-paragraph">And then anything that goes below it in descendancy is going to be considered part of that subtree.</p>



<p class="wp-block-paragraph">So that highlighted subtree is the left subtree of the 33 node.</p>



<p class="wp-block-paragraph">The 39 is the right subtree of the 33 node.</p>



<p class="wp-block-paragraph">I think I just did the wrong color.</p>



<p class="wp-block-paragraph">Let me do that in gray.</p>



<p class="wp-block-paragraph">Right, so we can do left subtree and right subtree for any node in the entire tree.</p>



<p class="wp-block-paragraph">if a node has no children, then there are no subtrees, but we can still look and check.</p>



<p class="wp-block-paragraph">And if there are children, then we&#8217;ve got subtrees or left and right subtrees.</p>



<p class="wp-block-paragraph">Okay, so now that we&#8217;re done talking about subtrees real fast, let&#8217;s talk about the depth of a node.</p>



<p class="wp-block-paragraph">So for me, I like to say that the depth of the root node is zero.</p>



<p class="wp-block-paragraph">And so I&#8217;ll just, I guess we could start off by putting a zero on the 42 indicating it has zero depth.</p>



<p class="wp-block-paragraph">It has zero depth.</p>



<p class="wp-block-paragraph">Imagine maybe it&#8217;s a buoy in the water and it&#8217;s just like sitting, floating like directly</p>



<p class="wp-block-paragraph">on the water.</p>



<p class="wp-block-paragraph">So it has no depth.</p>



<p class="wp-block-paragraph">It&#8217;s just like kind of on the surface.</p>



<p class="wp-block-paragraph">But if you draw your binary search trees in this nice pretty way where every single time</p>



<p class="wp-block-paragraph">you go down a generation from parent to child, from parent to child, you maintain, I guess,</p>



<p class="wp-block-paragraph">like the same Y coordinate for same leveled nodes, then it&#8217;s really easy to calculate</p>



<p class="wp-block-paragraph">the depth of every single node.</p>



<p class="wp-block-paragraph">of every single note let me show you what show you what i mean real fast you saw my video uh</p>



<p class="wp-block-paragraph">previously then you already know this but the 42 it&#8217;s got two children so if i go down to get one</p>



<p class="wp-block-paragraph">of its children i&#8217;m going down to the 33 and then i&#8217;m going down to the 67 right since those two</p>



<p class="wp-block-paragraph">children are on the same i guess level as if we were looking at a family tree they should be</p>



<p class="wp-block-paragraph">physically on the same level they should be on the same y coordinate or the same horizontal plane</p>



<p class="wp-block-paragraph">plane if we go down one more level which means any child of 33 or any child of 67 then those all</p>



<p class="wp-block-paragraph">should be lined up also so notice how these are all lined up on the same y coordinate then if we</p>



<p class="wp-block-paragraph">go down another level then this 19 here is just kind of by itself because the tree is not very big</p>



<p class="wp-block-paragraph">so if we draw the tree like this which is a really smart way because uh it&#8217;s easier to debug</p>



<p class="wp-block-paragraph">whether it&#8217;s a valid binary search tree and all sorts of other things,</p>



<p class="wp-block-paragraph">then we can easily write down the depth kind of on the side of the graph.</p>



<p class="wp-block-paragraph">We can say, all right, here&#8217;s depth zero, and here&#8217;s depth one,</p>



<p class="wp-block-paragraph">and here&#8217;s depth two, and here&#8217;s depth three.</p>



<p class="wp-block-paragraph">Just every time you go down one level, you just increase the depth.</p>



<p class="wp-block-paragraph">And now you know the depths of all the nodes in the entire tree pretty quickly.</p>



<p class="wp-block-paragraph">The 19, I&#8217;m just going to maybe do this in red.</p>



<p class="wp-block-paragraph">the 56 and the 76 have a depth of 2, the 33 has a depth of 1, and the 42 has a depth of 0.</p>



<p class="wp-block-paragraph">So all these trees, sorry, all these nodes in the tree have their own depth,</p>



<p class="wp-block-paragraph">which are very easy to calculate if you draw the tree well. The next thing after depth is the height</p>



<p class="wp-block-paragraph">of the tree. So what is the height of the tree? Well, that&#8217;s basically the depth of the deepest</p>



<p class="wp-block-paragraph">to the very deepest node what is the minimum number of nodes that you must touch when you</p>



<p class="wp-block-paragraph">start at the root node and then find your way to the deepest possible node in the entire tree so</p>



<p class="wp-block-paragraph">um if you if you notice the 19 node is definitely the deepest node in the entire tree</p>



<p class="wp-block-paragraph">it&#8217;s got a depth of three which means the height of the entire tree is four heights</p>



<p class="wp-block-paragraph">equals four and maybe i&#8217;ll change that to like just black or something okay</p>



<p class="wp-block-paragraph">So, let&#8217;s do it the other way real fast.</p>



<p class="wp-block-paragraph">If we&#8217;re kind of just walking down the tree, let&#8217;s start at the 42 and then we go down</p>



<p class="wp-block-paragraph">to the 33, we&#8217;ve touched two nodes so far.</p>



<p class="wp-block-paragraph">We go down to the 12, we&#8217;ve touched three nodes.</p>



<p class="wp-block-paragraph">We go down to the 19, we&#8217;ve touched four nodes.</p>



<p class="wp-block-paragraph">So the height of the tree is four or the number of nodes that you need to touch as you make</p>



<p class="wp-block-paragraph">your way down towards the deepest node or just a shortcut is the deepest nodes depth</p>



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



<p class="wp-block-paragraph">And that&#8217;s the height of the tree.</p>



<p class="wp-block-paragraph">you can also have a height of a left subtree and a height of a right subtree so let me just</p>



<p class="wp-block-paragraph">what&#8217;s going on here i think my thing is crashing hello oh i was definitely crashing i think my cpu</p>



<p class="wp-block-paragraph">is burning right now all right i&#8217;m going to be complaining about my new cpu for a long time</p>



<p class="wp-block-paragraph">i sprung only a few bucks for the best cpu that this motherboard could hold but it&#8217;s an old</p>



<p class="wp-block-paragraph">something percent um i&#8217;m eventually going to have to like build a brand new computer</p>



<p class="wp-block-paragraph">anyway so uh suppose we&#8217;re looking at the 67 node and the question is you know what is the height</p>



<p class="wp-block-paragraph">of the left subtree of the 67 node versus the right subtree of the 67 node well if you recall</p>



<p class="wp-block-paragraph">the left subtree is just all the nodes that are included uh beginning with the root node of the</p>



<p class="wp-block-paragraph">subtree of the 67 node well that&#8217;s just the 56 node totally by itself what&#8217;s the right subtree</p>



<p class="wp-block-paragraph">of the 67 it&#8217;s just that 76 node all by itself what&#8217;s the depth of 56 and 76 they&#8217;re both depth</p>



<p class="wp-block-paragraph">zero if we&#8217;re talking about relative depths per their subtrees that means what is the height of</p>



<p class="wp-block-paragraph">the left subtree and the right subtree they&#8217;re just one because the depth is zero the maximum</p>



<p class="wp-block-paragraph">depth is zero and if we want to get to the deepest node in one of those subtrees we&#8217;re just</p>



<p class="wp-block-paragraph">end up touching the one node that&#8217;s in the in the subtree at all so that means uh</p>



<p class="wp-block-paragraph">uh i don&#8217;t want to write down uh left subtree height right now so left subtree height of 67</p>



<p class="wp-block-paragraph">is one right subtree height of 67 is also one so we can do this with any node we want you know what</p>



<p class="wp-block-paragraph">is the uh what is the situation with the 33 node let&#8217;s do uh the 33 node yeah uh it has a left</p>



<p class="wp-block-paragraph">It has a left subtree height of 2 and you can tell because, well, the left subtree starts with the left child and the left child is going to be the root node of its own subtree.</p>



<p class="wp-block-paragraph">Notice how the maximum depth we can find here is 1, right?</p>



<p class="wp-block-paragraph">Like if we start at depth 0 for the 12, considering like it&#8217;s a relative depth, that means we take the deepest node, which is the 19 node, which has a depth of 1.</p>



<p class="wp-block-paragraph">We add 1 to that.</p>



<p class="wp-block-paragraph">So that means the height of that subtree is 2.</p>



<p class="wp-block-paragraph">if you wanted to find the deepest node in that whole subtree, how many nodes would you have to</p>



<p class="wp-block-paragraph">touch to get there? We&#8217;d have to touch the 12 and then touch the 19. We touch two nodes, so the height</p>



<p class="wp-block-paragraph">of that subtree is two. Maybe I&#8217;ll just put H equals two here. So now for the right subtree of</p>



<p class="wp-block-paragraph">the 33 node, it&#8217;s kind of easier. We just basically only have one node to really look at.</p>



<p class="wp-block-paragraph">that means that 39 has a depth of 0 and the right subtree has a height of 1.</p>



<p class="wp-block-paragraph">Whoops, 8 equals 1.</p>



<p class="wp-block-paragraph">I didn&#8217;t put two equal signs. I like to do two.</p>



<p class="wp-block-paragraph">I like to do the comparison operator.</p>



<p class="wp-block-paragraph">We could also do the same thing with the 42 node, right?</p>



<p class="wp-block-paragraph">We can say, let&#8217;s get rid of all this stuff real fast.</p>



<p class="wp-block-paragraph">We can do the 42.</p>



<p class="wp-block-paragraph">Its left subtree starts with that 33 node.</p>



<p class="wp-block-paragraph">33 node so I&#8217;m just going to highlight that real fast the 42 nodes left subtree has a height of</p>



<p class="wp-block-paragraph">one two three and you can tell because the 33 has a depth of zero a relative depth of zero and</p>



<p class="wp-block-paragraph">the 12 and the 39 have one and the 19 has two so the deepest node has a depth of two</p>



<p class="wp-block-paragraph">so that means the height of that subtree is is three so I&#8217;m just going to like do this real fast</p>



<p class="wp-block-paragraph">and then the right subtree of the 42 node, the root node of the entire tree, is going to be this.</p>



<p class="wp-block-paragraph">So the root node has a depth of zero, depth of zero, and then these other leaves over here have</p>



<p class="wp-block-paragraph">depth of ones, which means the height of this subtree is going to be two, or the deepest node</p>



<p class="wp-block-paragraph">plus one, or the number of nodes you need to touch to find the deepest node. And 56 and 76,</p>



<p class="wp-block-paragraph">those are both equally the deepest node in those trees okay so we talked about a bunch of</p>



<p class="wp-block-paragraph">terminology here let me just double check my notes in case I forgot to to tell you anything</p>



<p class="wp-block-paragraph">I think I&#8217;m all right well maybe okay maybe I should real fast just briefly mention that these</p>



<p class="wp-block-paragraph">nodes I mean this is not really part of the video exactly but let&#8217;s let me just mention that these</p>



<p class="wp-block-paragraph">whoops that&#8217;s dumb let me do a blue circle they would have you know there&#8217;s</p>



<p class="wp-block-paragraph">like some sort of an object you would call it a node and then they would have</p>



<p class="wp-block-paragraph">pointers they would have each of these nodes would have a pointer to its</p>



<p class="wp-block-paragraph">parents and it would have a left child pointer that goes down into the left and</p>



<p class="wp-block-paragraph">a right child pointer that goes down to the right and also a little slot in that</p>



<p class="wp-block-paragraph">object for the data so I&#8217;m just going to put t type data and C++ I usually say</p>



<p class="wp-block-paragraph">I usually say that the templated data type for a node or data structure is just the T type.</p>



<p class="wp-block-paragraph">That just means you could put anything you want.</p>



<p class="wp-block-paragraph">You could have your nodes hold integers, letters, strings, custom objects, whatever you want to do.</p>



<p class="wp-block-paragraph">And we&#8217;ll talk about this more in future videos.</p>



<p class="wp-block-paragraph">But long story short, I want you to just imagine that every node actually has three pointers inside of it pointing to something else, to other nodes,</p>



<p class="wp-block-paragraph">graph because in a graph you could have like a whole bunch of different connections and that&#8217;s</p>



<p class="wp-block-paragraph">usually managed by the actual graph object itself so anyway we&#8217;re done with terminology i hope you</p>



<p class="wp-block-paragraph">learned a little bit of stuff and had a little bit of fun thanks for watching this video</p>



<p class="wp-block-paragraph">tell your friends eat a donut and a chocolate and then be really happy and stuff okay i gotta go</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</p>



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



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



<p class="wp-block-paragraph">and grow this community.</p>



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



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



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



<p class="wp-block-paragraph">the night and I just wake up because I know somebody subscribed or followed it</p>



<p class="wp-block-paragraph">just wakes me up and I get filled with joy that&#8217;s exactly what happens every</p>



<p class="wp-block-paragraph">single time so you could do it as a nice favor to me or you could you control me</p>



<p class="wp-block-paragraph">if you want to just wake me up in the middle of the night just subscribe and</p>



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



<p class="wp-block-paragraph">at the middle of 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 video and it&#8217;ll</p>



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



<p class="wp-block-paragraph">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 clarifications or errata or just future videos that you want to see</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">those in the middle of the night i get i wake up in a cold sweat and i&#8217;m like it would really it</p>



<p class="wp-block-paragraph">really mean the world to me i would really appreciate it so again thank you so much for</p>



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



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/bst-terminology-root-leaves-subtrees-depth-height-ancestors-more/">BST Terminology: Root, Leaves, Subtrees, Depth, Height, Ancestors &amp; More</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/bst-terminology-root-leaves-subtrees-depth-height-ancestors-more/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
