<?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>data structures tutorial Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/data-structures-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/data-structures-tutorial/</link>
	<description></description>
	<lastBuildDate>Mon, 01 Jun 2026 05:46:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>data structures tutorial Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/data-structures-tutorial/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>AVL Tree Rotations Tutorial: Fixing Imbalance After Adding a Node</title>
		<link>https://www.NeuralLantern.com/avl-tree-rotations-tutorial-fixing-imbalance-after-adding-a-node/</link>
					<comments>https://www.NeuralLantern.com/avl-tree-rotations-tutorial-fixing-imbalance-after-adding-a-node/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 01 Jun 2026 05:46:56 +0000</pubDate>
				<category><![CDATA[AVL Trees]]></category>
		<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[avl insertion]]></category>
		<category><![CDATA[avl rotations]]></category>
		<category><![CDATA[AVL tree]]></category>
		<category><![CDATA[avl tree example]]></category>
		<category><![CDATA[AVL tree rotation]]></category>
		<category><![CDATA[balance factor]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[coding interview]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures tutorial]]></category>
		<category><![CDATA[double right rotation]]></category>
		<category><![CDATA[self balancing tree]]></category>
		<category><![CDATA[tree rotation]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=383</guid>

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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">and eh.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-rotations-tutorial-fixing-imbalance-after-adding-a-node/">AVL Tree Rotations Tutorial: Fixing Imbalance After Adding a Node</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/avl-tree-rotations-tutorial-fixing-imbalance-after-adding-a-node/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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 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>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 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>
		<item>
		<title>Master Stacks: Fun Guide to Data Structures, LIFO &#038; Real-World Uses</title>
		<link>https://www.NeuralLantern.com/master-stacks-fun-guide-to-data-structures-lifo-real-world-uses/</link>
					<comments>https://www.NeuralLantern.com/master-stacks-fun-guide-to-data-structures-lifo-real-world-uses/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 20 Jul 2025 09:15:59 +0000</pubDate>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[browser history stack]]></category>
		<category><![CDATA[call stack]]></category>
		<category><![CDATA[coding for beginners]]></category>
		<category><![CDATA[computer science basics]]></category>
		<category><![CDATA[data reversing]]></category>
		<category><![CDATA[data structure basics]]></category>
		<category><![CDATA[data structures tutorial]]></category>
		<category><![CDATA[learn coding]]></category>
		<category><![CDATA[LIFO data structure]]></category>
		<category><![CDATA[programming tutorials]]></category>
		<category><![CDATA[push pop stack]]></category>
		<category><![CDATA[stack data structure]]></category>
		<category><![CDATA[stack in C++]]></category>
		<category><![CDATA[stack operations]]></category>
		<category><![CDATA[stack programming]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=186</guid>

					<description><![CDATA[<p>Learn stacks: fun guide to data structures, LIFO, &#038; uses like browser history. Clear diagrams for beginners!</p>
<p>The post <a href="https://www.NeuralLantern.com/master-stacks-fun-guide-to-data-structures-lifo-real-world-uses/">Master Stacks: Fun Guide to Data Structures, LIFO &amp; Real-World Uses</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="Master Stacks: Fun Guide to Data Structures, LIFO &amp; Real-World Uses" width="1380" height="776" src="https://www.youtube.com/embed/mAqB77KUUKY?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">Want to nail down stacks? This beginner-friendly video breaks down the stack data structure with clear diagrams and a laid-back vibe. We’ll walk you through how stacks work, why they reverse data (hello, LIFO &#8211; Last In, First Out), and how they’re used in real-world stuff like browser history, undo features in editors, and even the call stack in your code. Whether you’re just starting out or brushing up on data structures, we keep it simple with hands-on demos of pushing, popping, and checking stack size—no boring jargon here! Subscribe for more coding tutorials to level up your skills, and let us know in the comments what you want to learn next. Check out our site via the QR code in the video for more coding goodness!</p>



<p class="wp-block-paragraph">Introduction to Stack 00:00:00<br>Explaining Stack Concept 00:00:06<br>Drawing Stack Diagram 00:00:30<br>Adding Elements to Stack 00:00:53<br>Stack Rules and Operations 00:01:16<br>Pop Operation and Interface 00:06:09<br>Stack Size and Empty Check 00:07:59<br>Tracing Stack Operations 00:11:46<br>Stack as Data Reverser 00:17:44<br>LIFO/FILO Explanation 00:21:13<br>Stack Use Cases 00:24:10<br>Call Stack Mention 00:26:18<br>Conclusion and Subscribe Request 00:27:21<br>Outro and Website Promotion 00:28:34</p>



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



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



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



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



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



<p class="wp-block-paragraph">In this video, I&#8217;m going to show you a data structure called a stack.</p>



<p class="wp-block-paragraph">We&#8217;re going to make a diagram of it.</p>



<p class="wp-block-paragraph">We&#8217;re going to explain how it works.</p>



<p class="wp-block-paragraph">We&#8217;re going to look at its typical interface,</p>



<p class="wp-block-paragraph">and we&#8217;re just going to kind of work through the concept</p>



<p class="wp-block-paragraph">of what is a stack and what does it do and all that stuff.</p>



<p class="wp-block-paragraph">Where&#8217;s your daddy and what does he do?</p>



<p class="wp-block-paragraph">So for starters, what am I talking about?</p>



<p class="wp-block-paragraph">What am I talking about?</p>



<p class="wp-block-paragraph">Let me open up a little notepad here.</p>



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



<p class="wp-block-paragraph">A stack is pretty simple when you first think about it.</p>



<p class="wp-block-paragraph">It just takes a little bit more work to really get all the details down.</p>



<p class="wp-block-paragraph">Imagine I have a data structure and maybe like at the bottom here,</p>



<p class="wp-block-paragraph">there&#8217;s like some grass.</p>



<p class="wp-block-paragraph">And so we can say that the data structure is empty when we first start.</p>



<p class="wp-block-paragraph">Okay, so there&#8217;s kind of a stack here, but it&#8217;s empty.</p>



<p class="wp-block-paragraph">So there&#8217;s nothing really on screen.</p>



<p class="wp-block-paragraph">Imagine then I wanted to add integers into this stack.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say let&#8217;s add the integers eight and then two and then three and then four for some reason.</p>



<p class="wp-block-paragraph">Okay, so how do we do this?</p>



<p class="wp-block-paragraph">The first thing we do is we take the eight and we just put it on top of the stack.</p>



<p class="wp-block-paragraph">You can imagine this as a stack of boxes or a stack of books or a stack of whatever.</p>



<p class="wp-block-paragraph">When we add items, we&#8217;re always going to add to the top of the stack.</p>



<p class="wp-block-paragraph">When the stack is empty, we&#8217;re obviously just going to add like at the very bottom, you know, at the ground floor.</p>



<p class="wp-block-paragraph">But that&#8217;s sort of the top.</p>



<p class="wp-block-paragraph">when we add the two here the next thing that happens is we just put the two on top of the eight</p>



<p class="wp-block-paragraph">and the next thing that we do is we uh put the three on top of the two and so forth right so</p>



<p class="wp-block-paragraph">it&#8217;s important to understand a few a few rules about stacks let me add that four real fast</p>



<p class="wp-block-paragraph">in stacks you&#8217;re only allowed to add to the top or remove from the top you&#8217;re not allowed to add</p>



<p class="wp-block-paragraph">or remove from anywhere else let me put a t for top right here so basically you&#8217;re allowed to add</p>



<p class="wp-block-paragraph">So basically you&#8217;re allowed to add and remove from the top.</p>



<p class="wp-block-paragraph">So if I wanted to add, let&#8217;s say a 12, the 12 would have to go on top of the four.</p>



<p class="wp-block-paragraph">It couldn&#8217;t go anywhere else in this data structure.</p>



<p class="wp-block-paragraph">And if I wanted to remove data from this stack, I could only remove the four.</p>



<p class="wp-block-paragraph">I could not remove the three or two or the eight or anything.</p>



<p class="wp-block-paragraph">You&#8217;re also not allowed to look at the middle of the stack.</p>



<p class="wp-block-paragraph">You&#8217;re not allowed to look anywhere but the top.</p>



<p class="wp-block-paragraph">So if I wanted to look at the three, the two or the eight, I wouldn&#8217;t be allowed to do it.</p>



<p class="wp-block-paragraph">I can only look at the four.</p>



<p class="wp-block-paragraph">look at the four and if I want to see that three I have to pop the four off</p>



<p class="wp-block-paragraph">first in order to just see what&#8217;s under it and technically unless you have the</p>



<p class="wp-block-paragraph">data you added memorized somewhere else you probably shouldn&#8217;t know what&#8217;s under</p>



<p class="wp-block-paragraph">the four until you actually remove it okay so we can add and remove from the</p>



<p class="wp-block-paragraph">top we can&#8217;t look in the middle we can&#8217;t look at the bottom or anything like that</p>



<p class="wp-block-paragraph">and let&#8217;s do a little let&#8217;s do a little sequence of pops let me show you what</p>



<p class="wp-block-paragraph">So first off, you can imagine a stack is sort of like a vector or a list with less functionality or more restrictions.</p>



<p class="wp-block-paragraph">It&#8217;s useful to add restrictions on top of more robust data structures for the purposes of, I don&#8217;t know, kindling your imagination or allowing yourself to use easier implementations or whatever.</p>



<p class="wp-block-paragraph">If you&#8217;re a musician or artist, probably at some point you&#8217;ve been stumped and you&#8217;ve had writer&#8217;s block and you can&#8217;t figure out what to create.</p>



<p class="wp-block-paragraph">can&#8217;t figure out what to create so uh you know i know sometimes musicians when they get writer&#8217;s</p>



<p class="wp-block-paragraph">block they&#8217;ll they&#8217;ll go to the swap meet and they&#8217;ll buy like an old dusty dirty uh casio</p>



<p class="wp-block-paragraph">keyboard from 1985 and then they&#8217;ll write a full song just with that one piece of equipment and it</p>



<p class="wp-block-paragraph">really stirs their creativity or it sparks their creativity and then when they&#8217;re done then they&#8217;ll</p>



<p class="wp-block-paragraph">probably upgrade to better equipment but anyway so there&#8217;s there&#8217;s a bunch of different reasons to</p>



<p class="wp-block-paragraph">use you know lesser data structures although this is not really lesser it&#8217;s very useful</p>



<p class="wp-block-paragraph">It&#8217;s very useful in its own right.</p>



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



<p class="wp-block-paragraph">So what can we do in a stack?</p>



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



<p class="wp-block-paragraph">Actually, I&#8217;m just going to type this up as code.</p>



<p class="wp-block-paragraph">No need to use the pen the whole entire time.</p>



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



<p class="wp-block-paragraph">So I&#8217;m going to put S for stack.</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll say, you know, stack that holds integers.</p>



<p class="wp-block-paragraph">And I&#8217;ll just put an S here.</p>



<p class="wp-block-paragraph">If you don&#8217;t know how to code right now, at this point in your career, just bear with</p>



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



<p class="wp-block-paragraph">variable of type stack and in C++ these stacks you know stacks are a template of data structure</p>



<p class="wp-block-paragraph">which means the integers that we are holding inside of the stack they could be any other data</p>



<p class="wp-block-paragraph">type that we wanted to including custom classes so in C++ we would use these angle brackets to</p>



<p class="wp-block-paragraph">say I want my stack to hold integers and then I&#8217;m going to say s is the variable that I&#8217;ll use for</p>



<p class="wp-block-paragraph">the stack so but this is not really a programming video this is just about stacks so if I wanted to</p>



<p class="wp-block-paragraph">stack to push something like onto the top of the stack i would use the the method push the method</p>



<p class="wp-block-paragraph">push usually has this prototype let&#8217;s see i&#8217;ll do stack push and it usually takes in a t type element</p>



<p class="wp-block-paragraph">and again the t type it&#8217;s a templated data type which means right now if i just declared a stack</p>



<p class="wp-block-paragraph">that was of type integer it holds integers then the t would actually expand under the hood in c</p>



<p class="wp-block-paragraph">hood in C++ anyway at compile time to be an integer. So it would be like typing int e, right?</p>



<p class="wp-block-paragraph">So whenever I type t, just understand that that means one instance of a thing that you could put</p>



<p class="wp-block-paragraph">into the stack. So we&#8217;ve got a push here and usually the return type is void and it takes in</p>



<p class="wp-block-paragraph">one element to push. And that means if I&#8217;m going to actually call, let me put some comments here.</p>



<p class="wp-block-paragraph">If I&#8217;m going to actually call push on the stack, I&#8217;ve got to give it something to push and the</p>



<p class="wp-block-paragraph">type of the thing that I&#8217;m pushing has to match what the stack was declared to hold. So in this</p>



<p class="wp-block-paragraph">case, it&#8217;s integers. So I&#8217;m going to put an eight to match the diagram up above. And then after that,</p>



<p class="wp-block-paragraph">maybe I wanted to push another number. So it&#8217;s going to be a two. And I&#8217;m just going to match</p>



<p class="wp-block-paragraph">what the diagram has up above. So eight, two, three, four. So that&#8217;s the interface for pushing</p>



<p class="wp-block-paragraph">usually in C++. And other languages should be similar. The other thing you can do with stacks</p>



<p class="wp-block-paragraph">We haven&#8217;t done that yet, but let me show it to you first.</p>



<p class="wp-block-paragraph">POP usually comes in two forms,</p>



<p class="wp-block-paragraph">depending on what implementation you&#8217;re looking at.</p>



<p class="wp-block-paragraph">So sometimes POP returns nothing.</p>



<p class="wp-block-paragraph">It simply removes an item from the top of the stack.</p>



<p class="wp-block-paragraph">And sometimes it returns a copy of the item</p>



<p class="wp-block-paragraph">that you&#8217;re actually removing.</p>



<p class="wp-block-paragraph">So I&#8217;m going to put like stack here and then like stack</p>



<p class="wp-block-paragraph">just to show you that it&#8217;s inside of the namespace.</p>



<p class="wp-block-paragraph">Oh, I don&#8217;t know.</p>



<p class="wp-block-paragraph">Maybe that&#8217;s too C++y.</p>



<p class="wp-block-paragraph">I&#8217;ll get rid of that stuff.</p>



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



<p class="wp-block-paragraph">hold on so we&#8217;ll maybe do this just to show you that we have three separate</p>



<p class="wp-block-paragraph">parts to this little code snip so sometimes pop doesn&#8217;t give you a copy of</p>



<p class="wp-block-paragraph">the thing it removes sometimes it gives you a copy of the thing it removes in</p>



<p class="wp-block-paragraph">cases where pop does not give you a copy of the thing it removes then you would</p>



<p class="wp-block-paragraph">probably want to call top right before you popped and top will return a copy of</p>



<p class="wp-block-paragraph">Just to clarify, if I wanted to remove something and get a copy of it and I had this line 13</p>



<p class="wp-block-paragraph">form of pop from whatever implementation I was using, then well, I would just call pop</p>



<p class="wp-block-paragraph">and I would do something like this, you know, auto element equals, you know, the stack dot</p>



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



<p class="wp-block-paragraph">And what would happen is under the hood, it removes the item and then it also returns</p>



<p class="wp-block-paragraph">a copy to me.</p>



<p class="wp-block-paragraph">Sorry for being too C++ in this video.</p>



<p class="wp-block-paragraph">On the other hand, if I have the form that has void for pop,</p>



<p class="wp-block-paragraph">then I would have to do something like this.</p>



<p class="wp-block-paragraph">I&#8217;d have to say auto element equals stack.top just to get a copy first,</p>



<p class="wp-block-paragraph">get a copy of that for before I remove it.</p>



<p class="wp-block-paragraph">And then when I&#8217;m finished, then I can say s.pop just to grab a copy of it and then remove it.</p>



<p class="wp-block-paragraph">So for now, we&#8217;re just going to use the form that gives you a copy of the data</p>



<p class="wp-block-paragraph">at the same time that you&#8217;re removing it.</p>



<p class="wp-block-paragraph">thing to keep in mind let&#8217;s see there are two other functions i wanted to mention mention real</p>



<p class="wp-block-paragraph">fast so for now i&#8217;m going to say that we only have the t pop type you can also check to see if a stack</p>



<p class="wp-block-paragraph">is uh is empty and you can also check its size uh usually those are functions that have those names</p>



<p class="wp-block-paragraph">but i&#8217;m going to do let&#8217;s see boolean empty and then i&#8217;m going to say size type size again sorry</p>



<p class="wp-block-paragraph">an unsigned 64-bit integer so just like a count or a size something that&#8217;s just</p>



<p class="wp-block-paragraph">going to be a whole number and never negative so we have this little</p>



<p class="wp-block-paragraph">interface that this data structure has and we have been calling push so far so</p>



<p class="wp-block-paragraph">let&#8217;s go ahead and do some pops I&#8217;m gonna say auto auto elements equals s</p>



<p class="wp-block-paragraph">dot pop maybe I&#8217;ll do two of those in a row maybe instead of calling that</p>



<p class="wp-block-paragraph">just so that we can have different variables to put our pops in.</p>



<p class="wp-block-paragraph">And then maybe after that, I&#8217;ll do some more pushes.</p>



<p class="wp-block-paragraph">I&#8217;ll say, let&#8217;s push like a 15 and let&#8217;s push like a 25.</p>



<p class="wp-block-paragraph">And then I think that means it&#8217;s probably time to erase that,</p>



<p class="wp-block-paragraph">the top part of the diagram with the 8, 2, 3, 4</p>



<p class="wp-block-paragraph">in favor of just whatever pushes and pops we&#8217;re doing.</p>



<p class="wp-block-paragraph">So let me just do this real fast.</p>



<p class="wp-block-paragraph">Okay, so we did our pushes.</p>



<p class="wp-block-paragraph">two three four and then I&#8217;m going to try to grab a couple pieces of data with these pops</p>



<p class="wp-block-paragraph">and for pop all you have to do is just to well I should have mentioned this before but first</p>



<p class="wp-block-paragraph">check to make sure that there is something to pop for example if I had a blank stack let&#8217;s say I had</p>



<p class="wp-block-paragraph">a blank stack right here there&#8217;s just like nothing on it and this green line is non-standard I&#8217;m just</p>



<p class="wp-block-paragraph">drawing it for fun but imagine I had a blank stack with no items and I decided to call pop on it</p>



<p class="wp-block-paragraph">well you&#8217;re not allowed to pop them an empty stack there&#8217;s nothing there to pop which means that&#8217;s a</p>



<p class="wp-block-paragraph">stack there&#8217;s nothing there to pop which means that&#8217;s a really really naughty thing in c++ and</p>



<p class="wp-block-paragraph">other languages we would say that something exceptionally bad has happened the user tried</p>



<p class="wp-block-paragraph">to pop something that wasn&#8217;t there and so then we would do something called throwing an exception</p>



<p class="wp-block-paragraph">at the user again this is not really a code video but if you know a little code hopefully you</p>



<p class="wp-block-paragraph">understand try catch blocks and throwing exceptions when bad things happen so we would just throw an</p>



<p class="wp-block-paragraph">exception at the user if you don&#8217;t know how to code at this point then just keep in mind we would</p>



<p class="wp-block-paragraph">mind we would refuse to do it we would say something bad has happened we can&#8217;t do that</p>



<p class="wp-block-paragraph">and then when you&#8217;re doing your pushes sometimes depending on what what you&#8217;re using to implement</p>



<p class="wp-block-paragraph">the stack under the hood inside of your code you might decide that you&#8217;re using let&#8217;s say you&#8217;re</p>



<p class="wp-block-paragraph">using an array under the hood which has a fixed capacity if you run out of capacity and you can&#8217;t</p>



<p class="wp-block-paragraph">then you would throw if the user tried to push something into a full stack we would call that a</p>



<p class="wp-block-paragraph">stack overflow and then when we tried to pop from an empty stack we would call that stack underflow</p>



<p class="wp-block-paragraph">but for the purposes of this video just assume that the stack is just a diagram and it has no</p>



<p class="wp-block-paragraph">capacity so we can just add as many things as we want on it or maybe we&#8217;re using a linked list</p>



<p class="wp-block-paragraph">under the hood which would have an unlimited capacity or limited only by the machine&#8217;s</p>



<p class="wp-block-paragraph">But anyway, so let&#8217;s go ahead and try to do this pop.</p>



<p class="wp-block-paragraph">So let&#8217;s do a pop here.</p>



<p class="wp-block-paragraph">Well, in the pop, we just look at the top only, right?</p>



<p class="wp-block-paragraph">So we say, well, you know what?</p>



<p class="wp-block-paragraph">Let&#8217;s make this more interesting.</p>



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



<p class="wp-block-paragraph">Let&#8217;s make this more interesting.</p>



<p class="wp-block-paragraph">Let&#8217;s do this.</p>



<p class="wp-block-paragraph">I&#8217;m going to do auto a equals s dot top.</p>



<p class="wp-block-paragraph">So I&#8217;m going to grab a value without actually removing it.</p>



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



<p class="wp-block-paragraph">So more interesting.</p>



<p class="wp-block-paragraph">So we push the 8 and we push the 2.</p>



<p class="wp-block-paragraph">So at that point in time, actually let&#8217;s trace this from scratch.</p>



<p class="wp-block-paragraph">So we have like an empty stack here.</p>



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



<p class="wp-block-paragraph">And we&#8217;re going to push the 8.</p>



<p class="wp-block-paragraph">So the 8 goes on the very top, just like we did last time.</p>



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



<p class="wp-block-paragraph">And then we&#8217;re going to push the 2 next.</p>



<p class="wp-block-paragraph">So the 2 just kind of goes on top.</p>



<p class="wp-block-paragraph">And then the next line, line 12, we&#8217;re just checking the top.</p>



<p class="wp-block-paragraph">we&#8217;re allowed to actually look at the top if we want to. So since two is at the top, that means</p>



<p class="wp-block-paragraph">a is equal to two. I&#8217;m going to put a two here to remind ourselves that a is now equal to two.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to do a push three. So I&#8217;m just going to stick a three on top of the stack.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to push a four, four is on top of the stack. Now, again, we&#8217;re not allowed</p>



<p class="wp-block-paragraph">to look at anything in the middle, anything, anything, but the top. So for example, when the</p>



<p class="wp-block-paragraph">it and remove it but now that we have a four on top we can no longer even see the three it&#8217;s not</p>



<p class="wp-block-paragraph">available to us anymore so then we&#8217;re going to pop and put that value into b so um you know what&#8217;s</p>



<p class="wp-block-paragraph">at the very top it&#8217;s a four so that means we&#8217;re going to actually remove the four and stick that</p>



<p class="wp-block-paragraph">into b so i&#8217;m going to put four right here to remind ourselves that b is now equal to four</p>



<p class="wp-block-paragraph">and uh for the purposes of this video you can just imagine that this you know the top of the stack is</p>



<p class="wp-block-paragraph">of the stack is just totally gone deallocated depending on what implementation you use you</p>



<p class="wp-block-paragraph">might want to think about crossing it out maybe there&#8217;s like some junk data there now but for the</p>



<p class="wp-block-paragraph">purposes of this video in this diagram we&#8217;re just saying it&#8217;s gone so it&#8217;s just totally gone</p>



<p class="wp-block-paragraph">okay then let&#8217;s do another pop so we&#8217;re going to pop and that&#8217;s going to become c so obviously the</p>



<p class="wp-block-paragraph">top is uh is now the three so that means the three is going to go into the c variable so here&#8217;s a</p>



<p class="wp-block-paragraph">c variable so here&#8217;s a three right there then the three gets deallocated</p>



<p class="wp-block-paragraph">and now the top is the two so then we&#8217;ll push a 15 and we&#8217;ll push a 25 so again we just put those</p>



<p class="wp-block-paragraph">new items on top of the stack even if there was stuff uh there before it&#8217;s gone now so we&#8217;re just</p>



<p class="wp-block-paragraph">be a 15 and a 25 on top of the stack so we got this and then 25 and then we&#8217;re done adding our</p>



<p class="wp-block-paragraph">stuff so uh the other functions that we talked about are empty and size uh at the very beginning</p>



<p class="wp-block-paragraph">when we had just an empty stack like that if we were to call on the empty function it would tell</p>



<p class="wp-block-paragraph">us true the stack is empty and then every step after that where we had some data in the stack</p>



<p class="wp-block-paragraph">data in the stack like as soon as we even added that eight at the very beginning from that point</p>



<p class="wp-block-paragraph">forward then empty would have returned false to us saying no the stack is uh it&#8217;s not empty</p>



<p class="wp-block-paragraph">the size would have changed uh during every step of the way so you know at line 10 the size would</p>



<p class="wp-block-paragraph">have been one after we&#8217;re finished with line 10 after line 11 the size would have been 2. 12</p>



<p class="wp-block-paragraph">wouldn&#8217;t have changed the size so it still would have been 2. 13 would have upgraded the size to</p>



<p class="wp-block-paragraph">have upgraded the size to four once we popped after that pop was finished it would have been</p>



<p class="wp-block-paragraph">three again then the second pop would have brought it back down to two and then those two pushes</p>



<p class="wp-block-paragraph">would have put it back up to three and then four and so then the final size whoops that was horrible</p>



<p class="wp-block-paragraph">the final size of the stack is four and you can tell that there&#8217;s just four items</p>



<p class="wp-block-paragraph">another way to double check yourself if you&#8217;re kind of trying to trace this on your own is just</p>



<p class="wp-block-paragraph">count the number of pushes and subtract the number of pops so how many pushes do we have one two three</p>



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



<p class="wp-block-paragraph">So that&#8217;s like 6 minus however many pops we saw.</p>



<p class="wp-block-paragraph">We see 2 pops.</p>



<p class="wp-block-paragraph">So 6 minus 2 is equal to 4.</p>



<p class="wp-block-paragraph">So, you know, when you&#8217;re writing this sort of thing down on,</p>



<p class="wp-block-paragraph">like as a diagram to practice,</p>



<p class="wp-block-paragraph">then you know it&#8217;s a 4 just by looking.</p>



<p class="wp-block-paragraph">But what if you did something wrong?</p>



<p class="wp-block-paragraph">It&#8217;s a great idea to double check yourself at all times.</p>



<p class="wp-block-paragraph">So 6 minus 2 is 4.</p>



<p class="wp-block-paragraph">So we know that the size of the stack is 4.</p>



<p class="wp-block-paragraph">Looks pretty good.</p>



<p class="wp-block-paragraph">Looks pretty good and we know the values of A, B, and C and you know one of those was</p>



<p class="wp-block-paragraph">top two of those were pops.</p>



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



<p class="wp-block-paragraph">So notice something though that&#8217;s kind of peculiar about the data that came out.</p>



<p class="wp-block-paragraph">If we added the data, like I guess while we were adding the data, we added first an eight</p>



<p class="wp-block-paragraph">and then a two and then a three and then a four.</p>



<p class="wp-block-paragraph">I&#8217;m just looking at the pushes right now and then a 15 and then a 25, right?</p>



<p class="wp-block-paragraph">So we added the data in that order.</p>



<p class="wp-block-paragraph">order. But then when we grab the data out, let me put this in, but maybe like a red bracket</p>



<p class="wp-block-paragraph">or something. When we grab data out of it, the pops and the tops gave us two and then</p>



<p class="wp-block-paragraph">a, okay, nevermind. That was not a pop. It gave us a four and then a three if we&#8217;re just</p>



<p class="wp-block-paragraph">look at the, look at the first two items. Sorry, sorry, sorry. Look at the last two items right</p>



<p class="wp-block-paragraph">before, and I didn&#8217;t draw this very well. Imagine that we had just pushed the eight and the two and</p>



<p class="wp-block-paragraph">the three and the four only forget about these other pieces of data. So at this point, when we</p>



<p class="wp-block-paragraph">start calling our pops, we have only an eight, two, three, four stack, right? So the data that</p>



<p class="wp-block-paragraph">has a four and then a three if we&#8217;re removing data from the stack.</p>



<p class="wp-block-paragraph">So notice how the four and the three are backwards.</p>



<p class="wp-block-paragraph">Notice also like if we kept popping out data, you know, we got the four and then we got</p>



<p class="wp-block-paragraph">the three, the next, what&#8217;s the next thing that would come out?</p>



<p class="wp-block-paragraph">It would be the 25, right?</p>



<p class="wp-block-paragraph">And then it would be the 15.</p>



<p class="wp-block-paragraph">And then it would be the two.</p>



<p class="wp-block-paragraph">And then it would be the eight.</p>



<p class="wp-block-paragraph">So if you really think about it, the stack is reversing our input data.</p>



<p class="wp-block-paragraph">We added a three and then a four, but we received a four and then a three.</p>



<p class="wp-block-paragraph">And of course, it&#8217;s a little bit muddled because we have some pushes in between some tops and pops and stuff.</p>



<p class="wp-block-paragraph">But just looking at what goes into the stack, it&#8217;s backwards, right?</p>



<p class="wp-block-paragraph">Eight, two, 15, 25.</p>



<p class="wp-block-paragraph">Let me finish this push sequence up here.</p>



<p class="wp-block-paragraph">So we added three and four, and then we added a 15.</p>



<p class="wp-block-paragraph">And then, oh, my penmanship, dude.</p>



<p class="wp-block-paragraph">shit dude 15 and then we added a 25 so if you look at the data that comes out this is like a 15 and a</p>



<p class="wp-block-paragraph">25 backwards and then the three and the four are backwards and then the two and the eight are also</p>



<p class="wp-block-paragraph">backwards and so one thing that a stack does is it reverses data let me give you a cleaner example</p>



<p class="wp-block-paragraph">with no pops in between the pushes just to show you what we&#8217;re talking about a little bit more</p>



<p class="wp-block-paragraph">bit more clearly okay so i&#8217;m going to do maybe like another code page here whoops do that and</p>



<p class="wp-block-paragraph">then i&#8217;m going to say got our stack and we&#8217;re just going to push in some data</p>



<p class="wp-block-paragraph">and then we&#8217;ll just say s.pop and maybe i&#8217;ll just do that however many times</p>



<p class="wp-block-paragraph">six two three four five six so now just a very quick trace because this is basically the same</p>



<p class="wp-block-paragraph">intermixed. So I&#8217;m going to do like the bottom of the stack here. And then, well, I mean like an</p>



<p class="wp-block-paragraph">empty stack. That&#8217;s not really a bottom of a stack. So we&#8217;re going to push an eight.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to push a two. And then we&#8217;re going to push a three. And then we&#8217;re</p>



<p class="wp-block-paragraph">going to push a four. Then a 15. Got to work on my fives, dude. Okay. And then 25.</p>



<p class="wp-block-paragraph">Okay, one more time.</p>



<p class="wp-block-paragraph">Oh yeah, I guess I got to slow down.</p>



<p class="wp-block-paragraph">So what&#8217;s going to come out?</p>



<p class="wp-block-paragraph">Remember the top is the only place that will give us data.</p>



<p class="wp-block-paragraph">So if we start popping all these one by one, we&#8217;re going to end up with a 25 for that first</p>



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



<p class="wp-block-paragraph">Maybe I should write it down over here.</p>



<p class="wp-block-paragraph">Do like a 25 and maybe I&#8217;ll add some line breaks in the code here so that it&#8217;s easier</p>



<p class="wp-block-paragraph">for me to write with a pen.</p>



<p class="wp-block-paragraph">Do this and that.</p>



<p class="wp-block-paragraph">Okay, so the 25, I&#8217;m just going to do a red line through it to designate or to denote</p>



<p class="wp-block-paragraph">deallocation or just like gone-ness.</p>



<p class="wp-block-paragraph">Then the top is at the 15.</p>



<p class="wp-block-paragraph">We do another pop.</p>



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



<p class="wp-block-paragraph">Oh, I did my first perfect five today.</p>



<p class="wp-block-paragraph">And then the top of the stack is now at the four.</p>



<p class="wp-block-paragraph">So then the four is going to pop out next.</p>



<p class="wp-block-paragraph">And then the three is going to pop out next.</p>



<p class="wp-block-paragraph">The top is now at the three.</p>



<p class="wp-block-paragraph">the three so the three is coming out for that pop we deallocate the three the top goes down one and</p>



<p class="wp-block-paragraph">then we pop it becomes the two that&#8217;s deallocated now and then we grab the eight because that&#8217;s</p>



<p class="wp-block-paragraph">where the top is so now we&#8217;ve grabbed all of our data out and I just want you to look at this one</p>



<p class="wp-block-paragraph">more time notice the data is eight that the data that went in was eight two three four fifteen twenty</p>



<p class="wp-block-paragraph">The data that came out was backwards 25, 15, 4, 3, 2, 8 or 8, 2, 3, 4, 15, 25.</p>



<p class="wp-block-paragraph">If you kind of like look up.</p>



<p class="wp-block-paragraph">So a stack is a data reverser.</p>



<p class="wp-block-paragraph">A stack is also known as something called a, let&#8217;s see,</p>



<p class="wp-block-paragraph">last in first out data structure, LIFO,</p>



<p class="wp-block-paragraph">or first in last out, phylo data structure.</p>



<p class="wp-block-paragraph">What that basically means is just it&#8217;s reversing the data or priority goes to the item that is the youngest.</p>



<p class="wp-block-paragraph">So if you looked at the stack at the point where we were about to remove the 25,</p>



<p class="wp-block-paragraph">25 was the most recently added or the youngest item or the item with, I guess, the timestamp, the furthest in the future,</p>



<p class="wp-block-paragraph">however you want to look at it.</p>



<p class="wp-block-paragraph">And that&#8217;s the item that came out.</p>



<p class="wp-block-paragraph">So if you think about it, it was the last item in.</p>



<p class="wp-block-paragraph">And therefore, when we did another pop, it was the first item out.</p>



<p class="wp-block-paragraph">So LIFO or PHILO.</p>



<p class="wp-block-paragraph">and uh you know this eight down here that was obviously the last item that we were able to grab</p>



<p class="wp-block-paragraph">out of it so when we said you know hop our final pop before the stack became empty uh the eight was</p>



<p class="wp-block-paragraph">definitely the first item in so first in was the last out if you really want to you can kind of like</p>



<p class="wp-block-paragraph">jumble these uh these acronyms uh i think most like people just use lipo or phylo</p>



<p class="wp-block-paragraph">life if you say something to me that I understand I think it&#8217;s probably fine</p>



<p class="wp-block-paragraph">but it&#8217;s it&#8217;s non-standard I like to say you know foley first out last in that</p>



<p class="wp-block-paragraph">definitely makes sense it&#8217;s just not very common another thing that I like to</p>



<p class="wp-block-paragraph">do for fun is also I&#8217;ll mix up the phylo and I&#8217;ll say lo-fi because last out is</p>



<p class="wp-block-paragraph">first in and now I&#8217;m cooler than other computer people because I use the word</p>



<p class="wp-block-paragraph">lo-fi I&#8217;ve now got like a giant mustache and like a little like swap meet shirt</p>



<p class="wp-block-paragraph">like swap meat shirt and I have craft beers and I have work boots on and I&#8217;d</p>



<p class="wp-block-paragraph">roll up my jeans and my, my, my, my, my sleeves are rolled up and all that stuff.</p>



<p class="wp-block-paragraph">You know what I&#8217;m saying?</p>



<p class="wp-block-paragraph">I&#8217;m cooler than everybody else now because I used loaf pie anyway.</p>



<p class="wp-block-paragraph">So we used integers.</p>



<p class="wp-block-paragraph">It&#8217;s just important to understand that in modern coding,</p>



<p class="wp-block-paragraph">I think I might&#8217;ve mentioned this before with the T&#8217;s,</p>



<p class="wp-block-paragraph">these stacks are templated data structures,</p>



<p class="wp-block-paragraph">we can hold another data type besides integers as long as we declare what the stack will hold in</p>



<p class="wp-block-paragraph">advance. So instead of integers, you could imagine there&#8217;s like a custom class, like my class,</p>



<p class="wp-block-paragraph">every single, you know, item in the stack is actually a full instance of a my class object,</p>



<p class="wp-block-paragraph">or, you know, floats or strings or just, you know, any data structure you want,</p>



<p class="wp-block-paragraph">you could put another data structure inside of a stack. People do that sometimes just for fun,</p>



<p class="wp-block-paragraph">sometimes just for fun you could take a vector and stick it inside of a stack a list put it inside</p>



<p class="wp-block-paragraph">of a stack you know whatever you want to do just to show that you understand everything</p>



<p class="wp-block-paragraph">um so again by the way the stack is empty if we tried to pop from it at this point</p>



<p class="wp-block-paragraph">this would be called a stack underflow we would probably want to throw an exception at the user</p>



<p class="wp-block-paragraph">or just say we&#8217;re not allowed to do it let&#8217;s see um so i guess now i should tell you some</p>



<p class="wp-block-paragraph">common uses for stacks i&#8217;m looking at my notes right here uh obviously our stack has reversed</p>



<p class="wp-block-paragraph">has reversed the data. So stacks are kind of good for data reversing. Imagine if you had like a word</p>



<p class="wp-block-paragraph">and you wanted to detect if the word was a palindrome or not, you would just like put all</p>



<p class="wp-block-paragraph">the letters of the word into your stack and then grab them back out and see if the word was still</p>



<p class="wp-block-paragraph">in the same order. If it was, then your word was probably a palindrome. If you don&#8217;t remember what</p>



<p class="wp-block-paragraph">a palindrome is, I&#8217;m going to write down the word radar in reverse from backwards, from back to</p>



<p class="wp-block-paragraph">front, right? It&#8217;s the same word. So that&#8217;s a palindrome. You could also use stacks kind of</p>



<p class="wp-block-paragraph">as a trail of breadcrumbs. Like you could imagine that early implementations of browsers</p>



<p class="wp-block-paragraph">had your browsing history in a stack somewhere so that every time you visited a webpage, then</p>



<p class="wp-block-paragraph">some kind of block of information, maybe like a custom class instance was put onto a stack</p>



<p class="wp-block-paragraph">somewhere and it contained information about like when you visited the webpage, what the URL was,</p>



<p class="wp-block-paragraph">it is that the browser wants to remember and then later when you hit the back button then you can</p>



<p class="wp-block-paragraph">imagine the browser is popping from the stack in order to go backwards in your history or your</p>



<p class="wp-block-paragraph">spotify playlist or whatever it is you&#8217;re doing honestly you know these systems are much more</p>



<p class="wp-block-paragraph">advanced than just using you know vanilla stacks now but you could imagine doing something on your</p>



<p class="wp-block-paragraph">own for fun or just what it might have been like in the very beginning i&#8217;m going to erase fully</p>



<p class="wp-block-paragraph">because that&#8217;s just that&#8217;s just that&#8217;s just cringe lo-fi i think is way cooler um so trail of bread</p>



<p class="wp-block-paragraph">So a trail of breadcrumbs, like browser history, you could also imagine undo history, like</p>



<p class="wp-block-paragraph">if you had a, I don&#8217;t know, like a text editor of some sort or like a image editor of some</p>



<p class="wp-block-paragraph">sort, every major change that you did to it, maybe the program under the hood is adding</p>



<p class="wp-block-paragraph">your change to the stack.</p>



<p class="wp-block-paragraph">And that makes it easier to sort of reverse your changes as those changes are popped.</p>



<p class="wp-block-paragraph">And there&#8217;s like a sort of mathematical uses like balancing parentheses and things, but</p>



<p class="wp-block-paragraph">balancing parentheses and things, but I&#8217;m not going to talk about that too much.</p>



<p class="wp-block-paragraph">And one of the most important uses for stacks inside of your computer is actually the call</p>



<p class="wp-block-paragraph">stack. I&#8217;m going to talk about that in a future video. But again, remember each, you know, little</p>



<p class="wp-block-paragraph">item on the stack, it could be a different data structure on its own. It doesn&#8217;t have to be an</p>



<p class="wp-block-paragraph">integer. So imagine that I bundled up a bunch of information about a function call, and I called it</p>



<p class="wp-block-paragraph">I just stuck a call frame on the stack.</p>



<p class="wp-block-paragraph">That&#8217;s basically the call stack.</p>



<p class="wp-block-paragraph">But that&#8217;s for another video.</p>



<p class="wp-block-paragraph">Anyway, so let&#8217;s see.</p>



<p class="wp-block-paragraph">Is there anything else that I wanted to show you?</p>



<p class="wp-block-paragraph">I think we&#8217;re actually done just talking about the basics of stacks.</p>



<p class="wp-block-paragraph">I&#8217;m not talking about time complexities or anything like that in this video.</p>



<p class="wp-block-paragraph">Check a future video if you&#8217;re interested in time complexities per certain implementations</p>



<p class="wp-block-paragraph">and so forth.</p>



<p class="wp-block-paragraph">But for now, this is how a stack works.</p>



<p class="wp-block-paragraph">or LIFO or LOFI data structure.</p>



<p class="wp-block-paragraph">Nobody&#8217;s going to say LOFI but me, just FYI.</p>



<p class="wp-block-paragraph">But maybe everybody will start doing it now.</p>



<p class="wp-block-paragraph">I don&#8217;t know.</p>



<p class="wp-block-paragraph">If it becomes a trend, okay?</p>



<p class="wp-block-paragraph">You heard it here first.</p>



<p class="wp-block-paragraph">Give everybody this link.</p>



<p class="wp-block-paragraph">All right, I hope you feel like an expert on stacks</p>



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



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



<p class="wp-block-paragraph">and learned a little bit of something, some stuffs.</p>



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



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



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



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



<p class="wp-block-paragraph">videos, 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">do me a kindness and subscribe. You know, sometimes I&#8217;m sleeping in the middle of the night</p>



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



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



<p class="wp-block-paragraph">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">of the night, just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen.</p>



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



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



<p class="wp-block-paragraph">I published and the services and tutorials and things that I offer and all that good 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</p>



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



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



<p class="wp-block-paragraph">sweat and i&#8217;m like 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>The post <a href="https://www.NeuralLantern.com/master-stacks-fun-guide-to-data-structures-lifo-real-world-uses/">Master Stacks: Fun Guide to Data Structures, LIFO &amp; Real-World Uses</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-stacks-fun-guide-to-data-structures-lifo-real-world-uses/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
