<?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, 06 Apr 2026 09:25:43 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</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>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>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>Great for beginners, interview prep, or reviewing foundational BST concepts before coding insert/search/delete.</p>



<p>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>=-=-=-=-=-=-=-=-=</p>



<p>Thanks for watching!</p>



<p>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>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>Hello there! Let&#8217;s talk about binary search tree terminology. If you saw my</p>



<p>previous video we talked about how to define a binary search tree meaning a</p>



<p>whole bunch of rules so that if the thing you&#8217;re looking at follows all the</p>



<p>rules then you know you&#8217;re actually looking at a binary search tree if not</p>



<p>then not. So see my previous video if you want to know for sure whether you&#8217;re</p>



<p>looking at a binary search tree. For now we&#8217;re just going to talk about some</p>



<p>Okay, so the first thing we should probably obviously talk about is the root node over here.</p>



<p>So I mean, well, in my previous video, we talked about nodes, right?</p>



<p>So this is kind of a graph with a whole bunch of rules on top of it.</p>



<p>That means we have nodes and edges.</p>



<p>So if you look at the very top node here, sometimes also referred to as a vertex,</p>



<p>I think in binary search tree terminology, we usually say nodes.</p>



<p>We usually say nodes. I can&#8217;t actually remember. But so look at the 42 there</p>



<p>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>First bit of terminology. Also, I tried to sneak past you ancestor</p>



<p>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>like not two children per parent, but just like one parent and then either zero or one or two</p>



<p>children. So we&#8217;ll say that ancestors are higher on the tree. So that means 42 is actually an</p>



<p>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>higher is an ancestor of anything that&#8217;s lower. We could also say that 33 is an ancestor of 39</p>



<p>and 19 and so forth. You can probably also imagine that we have children and grandchildren. Yeah,</p>



<p>we do go that far in binary search trees. So the 42 node, it has two children. It has a left child</p>



<p>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>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>the 33 has two children the 12 only has one child it was you know playing it safe i guess</p>



<p>you never know if these children are going to come out and just like run amok</p>



<p>and and engage in constant shenanigans so the 12 has a right child but no left child that&#8217;s okay</p>



<p>um in terms of going higher on the tree anything that is higher is an ancestor sorry i should have</p>



<p>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>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>It&#8217;s also an ancestor of anything that comes below it.</p>



<p>So it&#8217;s an ancestor of 12 and 39 and 19, right?</p>



<p>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>And then we have a grandchild, which is the 19 node.</p>



<p>We don&#8217;t really have left and right grandchildren.</p>



<p>You could say a grandchild in the left subtree or the right subtree.</p>



<p>subtree talk about sub trees in a second the 33 has a parent node which is just the 42 node</p>



<p>which is also the root node of course it&#8217;s got a sibling the 67 is the sibling you can tell</p>



<p>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>fighting with right anyway so if we&#8217;re looking at any node in particular it might have a whole bunch</p>



<p>of ancestors above the tree it might have a whole bunch of descendants below the tree</p>



<p>It has siblings or it usually has zero or one sibling because in a binary search tree,</p>



<p>we can only have up to two children per node.</p>



<p>It&#8217;s got sometimes, you know, grandparents and great grandparents and children and great</p>



<p>grandchildren.</p>



<p>So just think about the hierarchy like a family tree would have.</p>



<p>Okay, moving on to some more terminology.</p>



<p>Next thing is we have internal nodes and also external nodes.</p>



<p>So what do I mean by internal? Internal means a node has more than zero children. It has one or two children.</p>



<p>So I&#8217;m going to put internal on the 33 because the 33 node has children.</p>



<p>The 12 also has children, so it&#8217;s internal. The 39 does not have children, so it&#8217;s not internal.</p>



<p>67 has children, so it&#8217;s internal. And the root node, 42, also has children, so it&#8217;s considered internal.</p>



<p>That 42 has a lot of different names.</p>



<p>It&#8217;s the root node, it&#8217;s the greatest common ancestor,</p>



<p>it&#8217;s an internal node and so forth.</p>



<p>Notice how the other nodes that I have not highlighted,</p>



<p>they have zero children.</p>



<p>So when a node has zero children,</p>



<p>it&#8217;s known as an external node.</p>



<p>It&#8217;s also known as a leaf</p>



<p>because we&#8217;re talking about trees</p>



<p>and I guess it&#8217;s kind of like a nice synonym.</p>



<p>So the 19, the node with no children of its own is a leaf.</p>



<p>So is the 39.</p>



<p>So is the 56.</p>



<p>So is the 76.</p>



<p>I just want to point out also, if you were with me on my last video,</p>



<p>then the numbers need to be ordered from left to right.</p>



<p>But don&#8217;t worry, we&#8217;re going to do another video where we build a complete tree from scratch.</p>



<p>There&#8217;s some more terminology we should talk about.</p>



<p>So I&#8217;m going to get rid of all these externals and internals real fast.</p>



<p>Or the labels.</p>



<p>We should talk about the left subtree versus the right subtree.</p>



<p>left subtree versus the right subtree i mean what is a subtree anyway the subtree is basically</p>



<p>a subtree is basically just pick any node you want in the entire tree let&#8217;s pick</p>



<p>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>if there was anything below it then all those nodes would be included so this 76 right here it</p>



<p>really has nothing underneath it it&#8217;s a leaf which means well it can be the root node of its own</p>



<p>the subtree is just going to be a tree of one node.</p>



<p>So kind of boring, right?</p>



<p>You&#8217;re boring.</p>



<p>If instead we decided to look at the 33,</p>



<p>which is a little bit more interesting,</p>



<p>and we called the 33 the root node of its own subtree,</p>



<p>then really what we&#8217;re saying is all these nodes here</p>



<p>are included in that subtree.</p>



<p>So if I told you, give me the subtree starting with node 33,</p>



<p>then you would say, oh, it&#8217;s 33, 12, 39, 19.</p>



<p>descendants of the subtree root node that we picked out. So subtree just means, you know,</p>



<p>like a little fragment or a portion of the original tree. You could also say that the</p>



<p>entire tree is a subtree of itself. If you chose the subtree root to be the real root node, I mean,</p>



<p>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>with 67 and below in terms of descendancy is going to be considered part of the subtree.</p>



<p>I&#8217;ve highlighted the left subtree and the right subtree of the root node because that&#8217;s usually</p>



<p>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>this is the right subtree. Meaning if you look at any node at all, if it has a left child,</p>



<p>then that left child is the root node of the left subtree of the node in question. Same thing for</p>



<p>same thing for the right so if I say all right let me duplicate this real fast</p>



<p>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>well then you would know to include the 12 and the 19 because the left subtree of the 30 node</p>



<p>has to the 33 node has to start with the left child of the 33 node which would be the 12 and</p>



<p>And we&#8217;ll just say, okay, the 12 is now the root node of its own subtree.</p>



<p>And then anything that goes below it in descendancy is going to be considered part of that subtree.</p>



<p>So that highlighted subtree is the left subtree of the 33 node.</p>



<p>The 39 is the right subtree of the 33 node.</p>



<p>I think I just did the wrong color.</p>



<p>Let me do that in gray.</p>



<p>Right, so we can do left subtree and right subtree for any node in the entire tree.</p>



<p>if a node has no children, then there are no subtrees, but we can still look and check.</p>



<p>And if there are children, then we&#8217;ve got subtrees or left and right subtrees.</p>



<p>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>So for me, I like to say that the depth of the root node is zero.</p>



<p>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>It has zero depth.</p>



<p>Imagine maybe it&#8217;s a buoy in the water and it&#8217;s just like sitting, floating like directly</p>



<p>on the water.</p>



<p>So it has no depth.</p>



<p>It&#8217;s just like kind of on the surface.</p>



<p>But if you draw your binary search trees in this nice pretty way where every single time</p>



<p>you go down a generation from parent to child, from parent to child, you maintain, I guess,</p>



<p>like the same Y coordinate for same leveled nodes, then it&#8217;s really easy to calculate</p>



<p>the depth of every single node.</p>



<p>of every single note let me show you what show you what i mean real fast you saw my video uh</p>



<p>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>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>children are on the same i guess level as if we were looking at a family tree they should be</p>



<p>physically on the same level they should be on the same y coordinate or the same horizontal plane</p>



<p>plane if we go down one more level which means any child of 33 or any child of 67 then those all</p>



<p>should be lined up also so notice how these are all lined up on the same y coordinate then if we</p>



<p>go down another level then this 19 here is just kind of by itself because the tree is not very big</p>



<p>so if we draw the tree like this which is a really smart way because uh it&#8217;s easier to debug</p>



<p>whether it&#8217;s a valid binary search tree and all sorts of other things,</p>



<p>then we can easily write down the depth kind of on the side of the graph.</p>



<p>We can say, all right, here&#8217;s depth zero, and here&#8217;s depth one,</p>



<p>and here&#8217;s depth two, and here&#8217;s depth three.</p>



<p>Just every time you go down one level, you just increase the depth.</p>



<p>And now you know the depths of all the nodes in the entire tree pretty quickly.</p>



<p>The 19, I&#8217;m just going to maybe do this in red.</p>



<p>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>So all these trees, sorry, all these nodes in the tree have their own depth,</p>



<p>which are very easy to calculate if you draw the tree well. The next thing after depth is the height</p>



<p>of the tree. So what is the height of the tree? Well, that&#8217;s basically the depth of the deepest</p>



<p>to the very deepest node what is the minimum number of nodes that you must touch when you</p>



<p>start at the root node and then find your way to the deepest possible node in the entire tree so</p>



<p>um if you if you notice the 19 node is definitely the deepest node in the entire tree</p>



<p>it&#8217;s got a depth of three which means the height of the entire tree is four heights</p>



<p>equals four and maybe i&#8217;ll change that to like just black or something okay</p>



<p>So, let&#8217;s do it the other way real fast.</p>



<p>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>to the 33, we&#8217;ve touched two nodes so far.</p>



<p>We go down to the 12, we&#8217;ve touched three nodes.</p>



<p>We go down to the 19, we&#8217;ve touched four nodes.</p>



<p>So the height of the tree is four or the number of nodes that you need to touch as you make</p>



<p>your way down towards the deepest node or just a shortcut is the deepest nodes depth</p>



<p>plus one.</p>



<p>And that&#8217;s the height of the tree.</p>



<p>you can also have a height of a left subtree and a height of a right subtree so let me just</p>



<p>what&#8217;s going on here i think my thing is crashing hello oh i was definitely crashing i think my cpu</p>



<p>is burning right now all right i&#8217;m going to be complaining about my new cpu for a long time</p>



<p>i sprung only a few bucks for the best cpu that this motherboard could hold but it&#8217;s an old</p>



<p>something percent um i&#8217;m eventually going to have to like build a brand new computer</p>



<p>anyway so uh suppose we&#8217;re looking at the 67 node and the question is you know what is the height</p>



<p>of the left subtree of the 67 node versus the right subtree of the 67 node well if you recall</p>



<p>the left subtree is just all the nodes that are included uh beginning with the root node of the</p>



<p>subtree of the 67 node well that&#8217;s just the 56 node totally by itself what&#8217;s the right subtree</p>



<p>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>zero if we&#8217;re talking about relative depths per their subtrees that means what is the height of</p>



<p>the left subtree and the right subtree they&#8217;re just one because the depth is zero the maximum</p>



<p>depth is zero and if we want to get to the deepest node in one of those subtrees we&#8217;re just</p>



<p>end up touching the one node that&#8217;s in the in the subtree at all so that means uh</p>



<p>uh i don&#8217;t want to write down uh left subtree height right now so left subtree height of 67</p>



<p>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>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>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>Notice how the maximum depth we can find here is 1, right?</p>



<p>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>We add 1 to that.</p>



<p>So that means the height of that subtree is 2.</p>



<p>if you wanted to find the deepest node in that whole subtree, how many nodes would you have to</p>



<p>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>of that subtree is two. Maybe I&#8217;ll just put H equals two here. So now for the right subtree of</p>



<p>the 33 node, it&#8217;s kind of easier. We just basically only have one node to really look at.</p>



<p>that means that 39 has a depth of 0 and the right subtree has a height of 1.</p>



<p>Whoops, 8 equals 1.</p>



<p>I didn&#8217;t put two equal signs. I like to do two.</p>



<p>I like to do the comparison operator.</p>



<p>We could also do the same thing with the 42 node, right?</p>



<p>We can say, let&#8217;s get rid of all this stuff real fast.</p>



<p>We can do the 42.</p>



<p>Its left subtree starts with that 33 node.</p>



<p>33 node so I&#8217;m just going to highlight that real fast the 42 nodes left subtree has a height of</p>



<p>one two three and you can tell because the 33 has a depth of zero a relative depth of zero and</p>



<p>the 12 and the 39 have one and the 19 has two so the deepest node has a depth of two</p>



<p>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>and then the right subtree of the 42 node, the root node of the entire tree, is going to be this.</p>



<p>So the root node has a depth of zero, depth of zero, and then these other leaves over here have</p>



<p>depth of ones, which means the height of this subtree is going to be two, or the deepest node</p>



<p>plus one, or the number of nodes you need to touch to find the deepest node. And 56 and 76,</p>



<p>those are both equally the deepest node in those trees okay so we talked about a bunch of</p>



<p>terminology here let me just double check my notes in case I forgot to to tell you anything</p>



<p>I think I&#8217;m all right well maybe okay maybe I should real fast just briefly mention that these</p>



<p>nodes I mean this is not really part of the video exactly but let&#8217;s let me just mention that these</p>



<p>whoops that&#8217;s dumb let me do a blue circle they would have you know there&#8217;s</p>



<p>like some sort of an object you would call it a node and then they would have</p>



<p>pointers they would have each of these nodes would have a pointer to its</p>



<p>parents and it would have a left child pointer that goes down into the left and</p>



<p>a right child pointer that goes down to the right and also a little slot in that</p>



<p>object for the data so I&#8217;m just going to put t type data and C++ I usually say</p>



<p>I usually say that the templated data type for a node or data structure is just the T type.</p>



<p>That just means you could put anything you want.</p>



<p>You could have your nodes hold integers, letters, strings, custom objects, whatever you want to do.</p>



<p>And we&#8217;ll talk about this more in future videos.</p>



<p>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>graph because in a graph you could have like a whole bunch of different connections and that&#8217;s</p>



<p>usually managed by the actual graph object itself so anyway we&#8217;re done with terminology i hope you</p>



<p>learned a little bit of stuff and had a little bit of fun thanks for watching this video</p>



<p>tell your friends eat a donut and a chocolate and then be really happy and stuff okay i gotta go</p>



<p>Hey everybody!</p>



<p>Thanks for watching this video again from the bottom of my heart.</p>



<p>I really appreciate it.</p>



<p>I do hope you did learn something and have some fun.</p>



<p>If you could do me a please, a small little favor,</p>



<p>could you please subscribe and follow this channel or these videos</p>



<p>or whatever it is you do on the current social media website</p>



<p>that you&#8217;re looking at right now.</p>



<p>It would really mean the world to me and it&#8217;ll help make more videos</p>



<p>and grow this community.</p>



<p>So we&#8217;ll be able to do more videos, longer videos, better videos,</p>



<p>or just I&#8217;ll be able to keep making videos in general so please do do me a</p>



<p>kindness and and subscribe you know sometimes I&#8217;m sleeping in the middle of</p>



<p>the night and I just wake up because I know somebody subscribed or followed it</p>



<p>just wakes me up and I get filled with joy that&#8217;s exactly what happens every</p>



<p>single time so you could do it as a nice favor to me or you could you control me</p>



<p>if you want to just wake me up in the middle of the night just subscribe and</p>



<p>then I&#8217;ll just wake up I promise that&#8217;s what will happen also if you look at the</p>



<p>at the middle of the screen right now you should see a qr code which you can scan in order to go</p>



<p>to the website which i think is also named somewhere at the bottom of this video and it&#8217;ll</p>



<p>take you to my main website where you can just kind of like see all the videos i published and</p>



<p>the services and tutorials and things that i offer and all that good stuff and uh</p>



<p>if you have a suggestion for uh clarifications or errata or just future videos that you want to see</p>



<p>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>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>really mean the world to me i would really appreciate it so again thank you so much for</p>



<p>watching this video and um enjoy the cool music as as i fade into the darkness which is coming for us</p>



<p>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 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>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>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>Thanks for watching!</p>



<p>Find us on other social media here:</p>



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



<p>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>Hello there.</p>



<p>In this video, I&#8217;m going to show you a data structure called a stack.</p>



<p>We&#8217;re going to make a diagram of it.</p>



<p>We&#8217;re going to explain how it works.</p>



<p>We&#8217;re going to look at its typical interface,</p>



<p>and we&#8217;re just going to kind of work through the concept</p>



<p>of what is a stack and what does it do and all that stuff.</p>



<p>Where&#8217;s your daddy and what does he do?</p>



<p>So for starters, what am I talking about?</p>



<p>What am I talking about?</p>



<p>Let me open up a little notepad here.</p>



<p>And I&#8217;m going to start drawing.</p>



<p>A stack is pretty simple when you first think about it.</p>



<p>It just takes a little bit more work to really get all the details down.</p>



<p>Imagine I have a data structure and maybe like at the bottom here,</p>



<p>there&#8217;s like some grass.</p>



<p>And so we can say that the data structure is empty when we first start.</p>



<p>Okay, so there&#8217;s kind of a stack here, but it&#8217;s empty.</p>



<p>So there&#8217;s nothing really on screen.</p>



<p>Imagine then I wanted to add integers into this stack.</p>



<p>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>Okay, so how do we do this?</p>



<p>The first thing we do is we take the eight and we just put it on top of the stack.</p>



<p>You can imagine this as a stack of boxes or a stack of books or a stack of whatever.</p>



<p>When we add items, we&#8217;re always going to add to the top of the stack.</p>



<p>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>But that&#8217;s sort of the top.</p>



<p>when we add the two here the next thing that happens is we just put the two on top of the eight</p>



<p>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>it&#8217;s important to understand a few a few rules about stacks let me add that four real fast</p>



<p>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>or remove from anywhere else let me put a t for top right here so basically you&#8217;re allowed to add</p>



<p>So basically you&#8217;re allowed to add and remove from the top.</p>



<p>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>It couldn&#8217;t go anywhere else in this data structure.</p>



<p>And if I wanted to remove data from this stack, I could only remove the four.</p>



<p>I could not remove the three or two or the eight or anything.</p>



<p>You&#8217;re also not allowed to look at the middle of the stack.</p>



<p>You&#8217;re not allowed to look anywhere but the top.</p>



<p>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>I can only look at the four.</p>



<p>look at the four and if I want to see that three I have to pop the four off</p>



<p>first in order to just see what&#8217;s under it and technically unless you have the</p>



<p>data you added memorized somewhere else you probably shouldn&#8217;t know what&#8217;s under</p>



<p>the four until you actually remove it okay so we can add and remove from the</p>



<p>top we can&#8217;t look in the middle we can&#8217;t look at the bottom or anything like that</p>



<p>and let&#8217;s do a little let&#8217;s do a little sequence of pops let me show you what</p>



<p>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>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>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>can&#8217;t figure out what to create so uh you know i know sometimes musicians when they get writer&#8217;s</p>



<p>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>keyboard from 1985 and then they&#8217;ll write a full song just with that one piece of equipment and it</p>



<p>really stirs their creativity or it sparks their creativity and then when they&#8217;re done then they&#8217;ll</p>



<p>probably upgrade to better equipment but anyway so there&#8217;s there&#8217;s a bunch of different reasons to</p>



<p>use you know lesser data structures although this is not really lesser it&#8217;s very useful</p>



<p>It&#8217;s very useful in its own right.</p>



<p>So let&#8217;s see the interface.</p>



<p>So what can we do in a stack?</p>



<p>I&#8217;m going to put S for stack.</p>



<p>Actually, I&#8217;m just going to type this up as code.</p>



<p>No need to use the pen the whole entire time.</p>



<p>Let&#8217;s see.</p>



<p>So I&#8217;m going to put S for stack.</p>



<p>Maybe I&#8217;ll say, you know, stack that holds integers.</p>



<p>And I&#8217;ll just put an S here.</p>



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



<p>me.</p>



<p>variable of type stack and in C++ these stacks you know stacks are a template of data structure</p>



<p>which means the integers that we are holding inside of the stack they could be any other data</p>



<p>type that we wanted to including custom classes so in C++ we would use these angle brackets to</p>



<p>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>the stack so but this is not really a programming video this is just about stacks so if I wanted to</p>



<p>stack to push something like onto the top of the stack i would use the the method push the method</p>



<p>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>and again the t type it&#8217;s a templated data type which means right now if i just declared a stack</p>



<p>that was of type integer it holds integers then the t would actually expand under the hood in c</p>



<p>hood in C++ anyway at compile time to be an integer. So it would be like typing int e, right?</p>



<p>So whenever I type t, just understand that that means one instance of a thing that you could put</p>



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



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



<p>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>type of the thing that I&#8217;m pushing has to match what the stack was declared to hold. So in this</p>



<p>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>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>what the diagram has up above. So eight, two, three, four. So that&#8217;s the interface for pushing</p>



<p>usually in C++. And other languages should be similar. The other thing you can do with stacks</p>



<p>We haven&#8217;t done that yet, but let me show it to you first.</p>



<p>POP usually comes in two forms,</p>



<p>depending on what implementation you&#8217;re looking at.</p>



<p>So sometimes POP returns nothing.</p>



<p>It simply removes an item from the top of the stack.</p>



<p>And sometimes it returns a copy of the item</p>



<p>that you&#8217;re actually removing.</p>



<p>So I&#8217;m going to put like stack here and then like stack</p>



<p>just to show you that it&#8217;s inside of the namespace.</p>



<p>Oh, I don&#8217;t know.</p>



<p>Maybe that&#8217;s too C++y.</p>



<p>I&#8217;ll get rid of that stuff.</p>



<p>Hold on.</p>



<p>hold on so we&#8217;ll maybe do this just to show you that we have three separate</p>



<p>parts to this little code snip so sometimes pop doesn&#8217;t give you a copy of</p>



<p>the thing it removes sometimes it gives you a copy of the thing it removes in</p>



<p>cases where pop does not give you a copy of the thing it removes then you would</p>



<p>probably want to call top right before you popped and top will return a copy of</p>



<p>Just to clarify, if I wanted to remove something and get a copy of it and I had this line 13</p>



<p>form of pop from whatever implementation I was using, then well, I would just call pop</p>



<p>and I would do something like this, you know, auto element equals, you know, the stack dot</p>



<p>pop.</p>



<p>And what would happen is under the hood, it removes the item and then it also returns</p>



<p>a copy to me.</p>



<p>Sorry for being too C++ in this video.</p>



<p>On the other hand, if I have the form that has void for pop,</p>



<p>then I would have to do something like this.</p>



<p>I&#8217;d have to say auto element equals stack.top just to get a copy first,</p>



<p>get a copy of that for before I remove it.</p>



<p>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>So for now, we&#8217;re just going to use the form that gives you a copy of the data</p>



<p>at the same time that you&#8217;re removing it.</p>



<p>thing to keep in mind let&#8217;s see there are two other functions i wanted to mention mention real</p>



<p>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>is uh is empty and you can also check its size uh usually those are functions that have those names</p>



<p>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>an unsigned 64-bit integer so just like a count or a size something that&#8217;s just</p>



<p>going to be a whole number and never negative so we have this little</p>



<p>interface that this data structure has and we have been calling push so far so</p>



<p>let&#8217;s go ahead and do some pops I&#8217;m gonna say auto auto elements equals s</p>



<p>dot pop maybe I&#8217;ll do two of those in a row maybe instead of calling that</p>



<p>just so that we can have different variables to put our pops in.</p>



<p>And then maybe after that, I&#8217;ll do some more pushes.</p>



<p>I&#8217;ll say, let&#8217;s push like a 15 and let&#8217;s push like a 25.</p>



<p>And then I think that means it&#8217;s probably time to erase that,</p>



<p>the top part of the diagram with the 8, 2, 3, 4</p>



<p>in favor of just whatever pushes and pops we&#8217;re doing.</p>



<p>So let me just do this real fast.</p>



<p>Okay, so we did our pushes.</p>



<p>two three four and then I&#8217;m going to try to grab a couple pieces of data with these pops</p>



<p>and for pop all you have to do is just to well I should have mentioned this before but first</p>



<p>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>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>drawing it for fun but imagine I had a blank stack with no items and I decided to call pop on it</p>



<p>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>stack there&#8217;s nothing there to pop which means that&#8217;s a really really naughty thing in c++ and</p>



<p>other languages we would say that something exceptionally bad has happened the user tried</p>



<p>to pop something that wasn&#8217;t there and so then we would do something called throwing an exception</p>



<p>at the user again this is not really a code video but if you know a little code hopefully you</p>



<p>understand try catch blocks and throwing exceptions when bad things happen so we would just throw an</p>



<p>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>mind we would refuse to do it we would say something bad has happened we can&#8217;t do that</p>



<p>and then when you&#8217;re doing your pushes sometimes depending on what what you&#8217;re using to implement</p>



<p>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>using an array under the hood which has a fixed capacity if you run out of capacity and you can&#8217;t</p>



<p>then you would throw if the user tried to push something into a full stack we would call that a</p>



<p>stack overflow and then when we tried to pop from an empty stack we would call that stack underflow</p>



<p>but for the purposes of this video just assume that the stack is just a diagram and it has no</p>



<p>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>under the hood which would have an unlimited capacity or limited only by the machine&#8217;s</p>



<p>But anyway, so let&#8217;s go ahead and try to do this pop.</p>



<p>So let&#8217;s do a pop here.</p>



<p>Well, in the pop, we just look at the top only, right?</p>



<p>So we say, well, you know what?</p>



<p>Let&#8217;s make this more interesting.</p>



<p>Hang on a second.</p>



<p>Let&#8217;s make this more interesting.</p>



<p>Let&#8217;s do this.</p>



<p>I&#8217;m going to do auto a equals s dot top.</p>



<p>So I&#8217;m going to grab a value without actually removing it.</p>



<p>Okay.</p>



<p>So more interesting.</p>



<p>So we push the 8 and we push the 2.</p>



<p>So at that point in time, actually let&#8217;s trace this from scratch.</p>



<p>So we have like an empty stack here.</p>



<p>There&#8217;s nothing there.</p>



<p>And we&#8217;re going to push the 8.</p>



<p>So the 8 goes on the very top, just like we did last time.</p>



<p>I&#8217;m going to put an 8 there.</p>



<p>And then we&#8217;re going to push the 2 next.</p>



<p>So the 2 just kind of goes on top.</p>



<p>And then the next line, line 12, we&#8217;re just checking the top.</p>



<p>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>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>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>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>to look at anything in the middle, anything, anything, but the top. So for example, when the</p>



<p>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>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>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>into b so i&#8217;m going to put four right here to remind ourselves that b is now equal to four</p>



<p>and uh for the purposes of this video you can just imagine that this you know the top of the stack is</p>



<p>of the stack is just totally gone deallocated depending on what implementation you use you</p>



<p>might want to think about crossing it out maybe there&#8217;s like some junk data there now but for the</p>



<p>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>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>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>c variable so here&#8217;s a three right there then the three gets deallocated</p>



<p>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>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>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>stuff so uh the other functions that we talked about are empty and size uh at the very beginning</p>



<p>when we had just an empty stack like that if we were to call on the empty function it would tell</p>



<p>us true the stack is empty and then every step after that where we had some data in the stack</p>



<p>data in the stack like as soon as we even added that eight at the very beginning from that point</p>



<p>forward then empty would have returned false to us saying no the stack is uh it&#8217;s not empty</p>



<p>the size would have changed uh during every step of the way so you know at line 10 the size would</p>



<p>have been one after we&#8217;re finished with line 10 after line 11 the size would have been 2. 12</p>



<p>wouldn&#8217;t have changed the size so it still would have been 2. 13 would have upgraded the size to</p>



<p>have upgraded the size to four once we popped after that pop was finished it would have been</p>



<p>three again then the second pop would have brought it back down to two and then those two pushes</p>



<p>would have put it back up to three and then four and so then the final size whoops that was horrible</p>



<p>the final size of the stack is four and you can tell that there&#8217;s just four items</p>



<p>another way to double check yourself if you&#8217;re kind of trying to trace this on your own is just</p>



<p>count the number of pushes and subtract the number of pops so how many pushes do we have one two three</p>



<p>2, 3, 4, 5, 6.</p>



<p>So that&#8217;s like 6 minus however many pops we saw.</p>



<p>We see 2 pops.</p>



<p>So 6 minus 2 is equal to 4.</p>



<p>So, you know, when you&#8217;re writing this sort of thing down on,</p>



<p>like as a diagram to practice,</p>



<p>then you know it&#8217;s a 4 just by looking.</p>



<p>But what if you did something wrong?</p>



<p>It&#8217;s a great idea to double check yourself at all times.</p>



<p>So 6 minus 2 is 4.</p>



<p>So we know that the size of the stack is 4.</p>



<p>Looks pretty good.</p>



<p>Looks pretty good and we know the values of A, B, and C and you know one of those was</p>



<p>top two of those were pops.</p>



<p>And yeah, okay.</p>



<p>So notice something though that&#8217;s kind of peculiar about the data that came out.</p>



<p>If we added the data, like I guess while we were adding the data, we added first an eight</p>



<p>and then a two and then a three and then a four.</p>



<p>I&#8217;m just looking at the pushes right now and then a 15 and then a 25, right?</p>



<p>So we added the data in that order.</p>



<p>order. But then when we grab the data out, let me put this in, but maybe like a red bracket</p>



<p>or something. When we grab data out of it, the pops and the tops gave us two and then</p>



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



<p>look at the, look at the first two items. Sorry, sorry, sorry. Look at the last two items right</p>



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



<p>the three and the four only forget about these other pieces of data. So at this point, when we</p>



<p>start calling our pops, we have only an eight, two, three, four stack, right? So the data that</p>



<p>has a four and then a three if we&#8217;re removing data from the stack.</p>



<p>So notice how the four and the three are backwards.</p>



<p>Notice also like if we kept popping out data, you know, we got the four and then we got</p>



<p>the three, the next, what&#8217;s the next thing that would come out?</p>



<p>It would be the 25, right?</p>



<p>And then it would be the 15.</p>



<p>And then it would be the two.</p>



<p>And then it would be the eight.</p>



<p>So if you really think about it, the stack is reversing our input data.</p>



<p>We added a three and then a four, but we received a four and then a three.</p>



<p>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>But just looking at what goes into the stack, it&#8217;s backwards, right?</p>



<p>Eight, two, 15, 25.</p>



<p>Let me finish this push sequence up here.</p>



<p>So we added three and four, and then we added a 15.</p>



<p>And then, oh, my penmanship, dude.</p>



<p>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>25 backwards and then the three and the four are backwards and then the two and the eight are also</p>



<p>backwards and so one thing that a stack does is it reverses data let me give you a cleaner example</p>



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



<p>bit more clearly okay so i&#8217;m going to do maybe like another code page here whoops do that and</p>



<p>then i&#8217;m going to say got our stack and we&#8217;re just going to push in some data</p>



<p>and then we&#8217;ll just say s.pop and maybe i&#8217;ll just do that however many times</p>



<p>six two three four five six so now just a very quick trace because this is basically the same</p>



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



<p>empty stack. That&#8217;s not really a bottom of a stack. So we&#8217;re going to push an eight.</p>



<p>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>going to push a four. Then a 15. Got to work on my fives, dude. Okay. And then 25.</p>



<p>Okay, one more time.</p>



<p>Oh yeah, I guess I got to slow down.</p>



<p>So what&#8217;s going to come out?</p>



<p>Remember the top is the only place that will give us data.</p>



<p>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>pop.</p>



<p>Maybe I should write it down over here.</p>



<p>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>for me to write with a pen.</p>



<p>Do this and that.</p>



<p>Okay, so the 25, I&#8217;m just going to do a red line through it to designate or to denote</p>



<p>deallocation or just like gone-ness.</p>



<p>Then the top is at the 15.</p>



<p>We do another pop.</p>



<p>It&#8217;s going to be 15.</p>



<p>Oh, I did my first perfect five today.</p>



<p>And then the top of the stack is now at the four.</p>



<p>So then the four is going to pop out next.</p>



<p>And then the three is going to pop out next.</p>



<p>The top is now at the three.</p>



<p>the three so the three is coming out for that pop we deallocate the three the top goes down one and</p>



<p>then we pop it becomes the two that&#8217;s deallocated now and then we grab the eight because that&#8217;s</p>



<p>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>more time notice the data is eight that the data that went in was eight two three four fifteen twenty</p>



<p>The data that came out was backwards 25, 15, 4, 3, 2, 8 or 8, 2, 3, 4, 15, 25.</p>



<p>If you kind of like look up.</p>



<p>So a stack is a data reverser.</p>



<p>A stack is also known as something called a, let&#8217;s see,</p>



<p>last in first out data structure, LIFO,</p>



<p>or first in last out, phylo data structure.</p>



<p>What that basically means is just it&#8217;s reversing the data or priority goes to the item that is the youngest.</p>



<p>So if you looked at the stack at the point where we were about to remove the 25,</p>



<p>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>however you want to look at it.</p>



<p>And that&#8217;s the item that came out.</p>



<p>So if you think about it, it was the last item in.</p>



<p>And therefore, when we did another pop, it was the first item out.</p>



<p>So LIFO or PHILO.</p>



<p>and uh you know this eight down here that was obviously the last item that we were able to grab</p>



<p>out of it so when we said you know hop our final pop before the stack became empty uh the eight was</p>



<p>definitely the first item in so first in was the last out if you really want to you can kind of like</p>



<p>jumble these uh these acronyms uh i think most like people just use lipo or phylo</p>



<p>life if you say something to me that I understand I think it&#8217;s probably fine</p>



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



<p>definitely makes sense it&#8217;s just not very common another thing that I like to</p>



<p>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>first in and now I&#8217;m cooler than other computer people because I use the word</p>



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



<p>like swap meat shirt and I have craft beers and I have work boots on and I&#8217;d</p>



<p>roll up my jeans and my, my, my, my, my sleeves are rolled up and all that stuff.</p>



<p>You know what I&#8217;m saying?</p>



<p>I&#8217;m cooler than everybody else now because I used loaf pie anyway.</p>



<p>So we used integers.</p>



<p>It&#8217;s just important to understand that in modern coding,</p>



<p>I think I might&#8217;ve mentioned this before with the T&#8217;s,</p>



<p>these stacks are templated data structures,</p>



<p>we can hold another data type besides integers as long as we declare what the stack will hold in</p>



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



<p>every single, you know, item in the stack is actually a full instance of a my class object,</p>



<p>or, you know, floats or strings or just, you know, any data structure you want,</p>



<p>you could put another data structure inside of a stack. People do that sometimes just for fun,</p>



<p>sometimes just for fun you could take a vector and stick it inside of a stack a list put it inside</p>



<p>of a stack you know whatever you want to do just to show that you understand everything</p>



<p>um so again by the way the stack is empty if we tried to pop from it at this point</p>



<p>this would be called a stack underflow we would probably want to throw an exception at the user</p>



<p>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>common uses for stacks i&#8217;m looking at my notes right here uh obviously our stack has reversed</p>



<p>has reversed the data. So stacks are kind of good for data reversing. Imagine if you had like a word</p>



<p>and you wanted to detect if the word was a palindrome or not, you would just like put all</p>



<p>the letters of the word into your stack and then grab them back out and see if the word was still</p>



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



<p>a palindrome is, I&#8217;m going to write down the word radar in reverse from backwards, from back to</p>



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



<p>as a trail of breadcrumbs. Like you could imagine that early implementations of browsers</p>



<p>had your browsing history in a stack somewhere so that every time you visited a webpage, then</p>



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



<p>somewhere and it contained information about like when you visited the webpage, what the URL was,</p>



<p>it is that the browser wants to remember and then later when you hit the back button then you can</p>



<p>imagine the browser is popping from the stack in order to go backwards in your history or your</p>



<p>spotify playlist or whatever it is you&#8217;re doing honestly you know these systems are much more</p>



<p>advanced than just using you know vanilla stacks now but you could imagine doing something on your</p>



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



<p>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>So a trail of breadcrumbs, like browser history, you could also imagine undo history, like</p>



<p>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>sort, every major change that you did to it, maybe the program under the hood is adding</p>



<p>your change to the stack.</p>



<p>And that makes it easier to sort of reverse your changes as those changes are popped.</p>



<p>And there&#8217;s like a sort of mathematical uses like balancing parentheses and things, but</p>



<p>balancing parentheses and things, but I&#8217;m not going to talk about that too much.</p>



<p>And one of the most important uses for stacks inside of your computer is actually the call</p>



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



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



<p>integer. So imagine that I bundled up a bunch of information about a function call, and I called it</p>



<p>I just stuck a call frame on the stack.</p>



<p>That&#8217;s basically the call stack.</p>



<p>But that&#8217;s for another video.</p>



<p>Anyway, so let&#8217;s see.</p>



<p>Is there anything else that I wanted to show you?</p>



<p>I think we&#8217;re actually done just talking about the basics of stacks.</p>



<p>I&#8217;m not talking about time complexities or anything like that in this video.</p>



<p>Check a future video if you&#8217;re interested in time complexities per certain implementations</p>



<p>and so forth.</p>



<p>But for now, this is how a stack works.</p>



<p>or LIFO or LOFI data structure.</p>



<p>Nobody&#8217;s going to say LOFI but me, just FYI.</p>



<p>But maybe everybody will start doing it now.</p>



<p>I don&#8217;t know.</p>



<p>If it becomes a trend, okay?</p>



<p>You heard it here first.</p>



<p>Give everybody this link.</p>



<p>All right, I hope you feel like an expert on stacks</p>



<p>at this point.</p>



<p>I hope you had a little bit of fun</p>



<p>and learned a little bit of something, some stuffs.</p>



<p>I&#8217;ll see you in the next video.</p>



<p>hey everybody thanks for watching this video again from the bottom of my heart I really</p>



<p>appreciate it I do hope you did learn something and have some fun if you could do me a please a</p>



<p>small little favor could you please subscribe and follow this channel or these videos or whatever it</p>



<p>is you do on the current social media website that you&#8217;re looking at right now it would really mean</p>



<p>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>videos, longer videos, better videos, or just I&#8217;ll be able to keep making videos in general. So please</p>



<p>do me a kindness and subscribe. You know, sometimes I&#8217;m sleeping in the middle of the night</p>



<p>and I just wake up because I know somebody subscribed or followed. It just wakes me up</p>



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



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



<p>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>which I think is also named somewhere at the bottom of this video and</p>



<p>It&#8217;ll take you to my main website where you can just kind of like see all the videos</p>



<p>I published and the services and tutorials and things that I offer and all that good stuff and</p>



<p>If you have a suggestion for</p>



<p>Clarifications or errata or just future videos that you want to see</p>



<p>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>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>sweat and i&#8217;m like it would really it really mean the world to me i would really appreciate it so</p>



<p>again thank you so much for watching this video and um enjoy the cool music</p>



<p>as as i fade into the darkness which is coming for us all</p>



<p>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>
