<?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 Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/data-structures/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/data-structures/</link>
	<description></description>
	<lastBuildDate>Sun, 13 Sep 2026 19:29:07 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.1</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>data structures Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/data-structures/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Directed Weighted Graph Edge List Representation</title>
		<link>https://www.NeuralLantern.com/directed-weighted-graph-edge-list-representation/</link>
					<comments>https://www.NeuralLantern.com/directed-weighted-graph-edge-list-representation/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 13 Sep 2026 19:29:05 +0000</pubDate>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Graphs]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[adjacency]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[directed graph]]></category>
		<category><![CDATA[directed weighted graph]]></category>
		<category><![CDATA[edge list]]></category>
		<category><![CDATA[graph representation]]></category>
		<category><![CDATA[graph theory]]></category>
		<category><![CDATA[graph tutorial]]></category>
		<category><![CDATA[index mapping]]></category>
		<category><![CDATA[nodes and edges]]></category>
		<category><![CDATA[vertex list]]></category>
		<category><![CDATA[weighted graph]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=433</guid>

					<description><![CDATA[<p>A directed weighted graph can be stored as a pair of lists: every vertex in one list, and every edge as a three-part tuple of start node, end node, and weight. Replacing the node values with indexes into the vertex list lets you jump to those nodes in constant time after you find an edge.</p>
<p>The post <a href="https://www.NeuralLantern.com/directed-weighted-graph-edge-list-representation/">Directed Weighted Graph Edge List Representation</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="Directed Weighted Graph Edge List Representation" width="1380" height="776" src="https://www.youtube.com/embed/OOyWpB5Pijk?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Learn how to represent a directed and weighted graph using an edge list.</p>



<p class="wp-block-paragraph">We start from a directed unweighted example, add weights to every edge, then store the graph as a tuple of a vertex list and an edge list. Each edge is a tuple of start node, end node, and weight. Order matters because the graph is directed.</p>



<p class="wp-block-paragraph">Then we convert node values to indexes so you can jump straight to a vertex in constant time instead of scanning the vertex list. That keeps edge scans closer to O(E) instead of O(E + V).</p>



<p class="wp-block-paragraph">This is part of a series covering all four combinations: directed vs undirected and weighted vs unweighted. Watch the earlier videos for basic graph terms and the other representations.</p>



<p class="wp-block-paragraph">Leave a comment if you want a topic covered next.</p>



<p class="wp-block-paragraph">00:00 Intro to directed weighted edge lists<br>01:05 Starting from the directed unweighted graph<br>01:35 Adding a weight to every edge<br>02:48 Graph as a vertex list plus edge list<br>03:35 Writing the vertex list<br>04:18 Node classes versus raw values<br>05:18 Writing directed weighted edges<br>05:45 Order matters on directed edges<br>06:38 Walking remaining outgoing edges<br>09:08 Why value-based edges scan slowly<br>10:29 Replacing node values with indexes<br>12:36 Constant-time jumps after an edge scan<br>14:13 Series wrap-up<br>14:45 Subscribe 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><a href="https://www.NeuralLantern.com/social">https://www.NeuralLantern.com/social</a></li>



<li>Twitter / X: <a href="https://x.com/NeuralLantern">https://x.com/NeuralLantern</a></li>



<li>Rumble: <a href="https://rumble.com/c/c-3696939">https://rumble.com/c/c-3696939</a></li>



<li>BitChute: <a href="https://www.bitchute.com/channel/pg1Pvv5dN4Gt">https://www.bitchute.com/channel/pg1Pvv5dN4Gt</a></li>



<li>Daily Motion: <a href="https://www.dailymotion.com/neurallantern">https://www.dailymotion.com/neurallantern</a></li>



<li>Minds: <a href="https://www.minds.com/neurallantern/">https://www.minds.com/neurallantern/</a></li>



<li>Odysee: <a href="https://odysee.com/@NeuralLantern:5">https://odysee.com/@NeuralLantern:5</a></li>
</ul>



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



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



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



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



<li>Subscribe to the Blog: <a href="https://www.NeuralLantern.com">https://www.NeuralLantern.com</a></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 representing a directed and weighted</p>



<p class="wp-block-paragraph">graph inside your machine using an edge list representation.</p>



<p class="wp-block-paragraph">That&#8217;s a lot of words, a lot of words. Okay, so I hope you watched my previous videos. You didn&#8217;t</p>



<p class="wp-block-paragraph">necessarily need to watch the previous three where we talked about edge list</p>



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



<p class="wp-block-paragraph">where we talked about edgeless representation on the other types of graphs we&#8217;re doing a directed</p>



<p class="wp-block-paragraph">versus uh undirected and weighted versus unweighted and all four combinations of those</p>



<p class="wp-block-paragraph">so uh you don&#8217;t need to watch those others if you&#8217;re just only interested in this but it&#8217;s</p>



<p class="wp-block-paragraph">probably a good idea if you watch my other graph videos because we talk about basic graph</p>



<p class="wp-block-paragraph">terminology and like you know what is a graph and stuff like that but for now we&#8217;re just going to</p>



<p class="wp-block-paragraph">continue um the screenshot you&#8217;re looking at right now was the last part of the last video where we</p>



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



<p class="wp-block-paragraph">the last part of the last video where we did a directed unweighted graph so I&#8217;m</p>



<p class="wp-block-paragraph">gonna duplicate this real fast and I&#8217;m gonna start modifying it so we can</p>



<p class="wp-block-paragraph">basically just move on with our lives so this is gonna be directed and also</p>



<p class="wp-block-paragraph">weighted then I&#8217;m gonna erase a whole bunch of other stuff because it&#8217;s fun to</p>



<p class="wp-block-paragraph">draw again I don&#8217;t know why but it is I like to do my drawings get that and then</p>



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



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



<p class="wp-block-paragraph">that stuff okay and i&#8217;ll get rid of this because i&#8217;d like to draw it okay i probably should have</p>



<p class="wp-block-paragraph">gotten rid of it first so i guess the real first thing that i&#8217;m going to do after erasing all that</p>



<p class="wp-block-paragraph">stuff is add weights to all the edges remember this has to be a weighted graph so this particular</p>



<p class="wp-block-paragraph">graph you&#8217;re looking at right now is directed and unweighted so we have to add weights to all of</p>



<p class="wp-block-paragraph">the edges in my previous videos we talked about what is the meaning of weights you know these</p>



<p class="wp-block-paragraph">could be a cost these could be like some property like in a video game</p>



<p class="wp-block-paragraph">property like in a video game uh whatever you want them to mean uh these could even be classes</p>



<p class="wp-block-paragraph">with like complicated values um but for now i&#8217;m going to say i don&#8217;t know maybe this is an airport</p>



<p class="wp-block-paragraph">map and the weights are how much gas it takes to fly from one city to another maybe a node is a</p>



<p class="wp-block-paragraph">city number six and city number 12 you want to fly between them it&#8217;s like five million jillion</p>



<p class="wp-block-paragraph">gallons of gas so let me just take a look real fast and make sure that i&#8217;m not forgetting</p>



<p class="wp-block-paragraph">I mean, any edge.</p>



<p class="wp-block-paragraph">getting any edge every single edge should have a weight i think there were nine edges here one two</p>



<p class="wp-block-paragraph">three four five six seven eight nine so i should see nine weights one two three four five six seven</p>



<p class="wp-block-paragraph">eight nine okay we&#8217;re now ready to start representing this and what&#8217;s going on with</p>



<p class="wp-block-paragraph">this computer okay the first thing uh that we talk about in all of these you know recent related</p>



<p class="wp-block-paragraph">videos is that in an edge list representation we&#8217;ll say</p>



<p class="wp-block-paragraph">edge list representation, we&#8217;ll say that our graph is a tuple, meaning just like a collection of some</p>



<p class="wp-block-paragraph">things. A tuple where the first item in the tuple is a vertex list or a node list, and the second</p>



<p class="wp-block-paragraph">item is an edge list. Again, in previous videos, we talked about representing paths using nodes or</p>



<p class="wp-block-paragraph">using edges. So the edge list path is going to come back to haunt us where we do our edge list</p>



<p class="wp-block-paragraph">graph representation, which is different than an edge list path.</p>



<p class="wp-block-paragraph">path. So how do we do our vertex list? That&#8217;s going to be pretty easy for this diagram.</p>



<p class="wp-block-paragraph">I&#8217;m going to say that V is equal to, oh, it&#8217;s pink. Let me change the color to be more serious.</p>



<p class="wp-block-paragraph">Okay. The vertex list is going to be a collection of just all the nodes, just naming all the nodes</p>



<p class="wp-block-paragraph">or having all the nodes. I&#8217;m going to say it&#8217;s a vertex list or a node list, but really you</p>



<p class="wp-block-paragraph">probably want this to be a vector I&#8217;ve been saying that recently because it&#8217;s faster to</p>



<p class="wp-block-paragraph">because it&#8217;s faster to jump to a particular index in a vector</p>



<p class="wp-block-paragraph">than it is in an actual linked list or some other list-like data structure.</p>



<p class="wp-block-paragraph">So we&#8217;ve got a vertex list.</p>



<p class="wp-block-paragraph">I&#8217;m just going to name all the nodes.</p>



<p class="wp-block-paragraph">So for me particularly, I like to go from left to right.</p>



<p class="wp-block-paragraph">You can sort them, you can do whatever you want.</p>



<p class="wp-block-paragraph">They just need to all be in there.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to say every node that we see in the graph</p>



<p class="wp-block-paragraph">is going to end up in this vertex list.</p>



<p class="wp-block-paragraph">So we&#8217;ve got 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6.</p>



<p class="wp-block-paragraph">So we&#8217;ve got all the nodes in there.</p>



<p class="wp-block-paragraph">another point that I&#8217;ve been making in the in these recent videos is that in real life in your</p>



<p class="wp-block-paragraph">code in your in your computer you don&#8217;t really want to just have this value three here you don&#8217;t</p>



<p class="wp-block-paragraph">want to have this 12 value here that the 12 value that&#8217;s just the value that the node holds I mean</p>



<p class="wp-block-paragraph">if you want everything to be really fast maybe you know keep it bare bones but for me I like to</p>



<p class="wp-block-paragraph">have a full node class a full vertex class and make an instance of it so I can add a whole bunch</p>



<p class="wp-block-paragraph">of custom properties and function</p>



<p class="wp-block-paragraph">bunch of custom properties and functionality and other stuff onto my node so that that means i</p>



<p class="wp-block-paragraph">couldn&#8217;t really put an integer there i would have to put a full node so keep in mind for yourself</p>



<p class="wp-block-paragraph">you probably want to try it both ways but um you probably want to want an instance of a node class</p>



<p class="wp-block-paragraph">and maybe stick the instance right there in the vector or maybe make a smart pointer and so like</p>



<p class="wp-block-paragraph">you allocate you know a new pointer of an instance of the class or it&#8217;s a raw pointer it&#8217;s a smart</p>



<p class="wp-block-paragraph">point or whatever so just keep in mind this diagram is</p>



<p class="wp-block-paragraph">So just keep in mind, this diagram is more simple than your code, but we&#8217;re just diagramming</p>



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



<p class="wp-block-paragraph">Anyway, so we put all the nodes in there.</p>



<p class="wp-block-paragraph">The next thing I need to do is represent the edges.</p>



<p class="wp-block-paragraph">So I&#8217;m going to put E is equal to another list.</p>



<p class="wp-block-paragraph">And this probably does need to be a linked list or a list like data structure and not</p>



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



<p class="wp-block-paragraph">There&#8217;s not much point in jumping to an index.</p>



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



<p class="wp-block-paragraph">You could probably come up with one.</p>



<p class="wp-block-paragraph">But anyway, so the edge list is going to be a list of tuples that describe one edge at</p>



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



<p class="wp-block-paragraph">at a time so every little parenthesis that i put here that&#8217;s going to represent one edge so again</p>



<p class="wp-block-paragraph">for me i like to go from left to right i&#8217;m going to start on the left node and i&#8217;m going to say</p>



<p class="wp-block-paragraph">that the start node is three and remember this is a directed graph which means the order if you</p>



<p class="wp-block-paragraph">looked at my previous videos the hang on the order of the nodes actually matters a lot like i can put</p>



<p class="wp-block-paragraph">3 and 13 but i cannot put 13 comma 3 because the first item in the tuple is supposed to be the</p>



<p class="wp-block-paragraph">The first item in the tuple is supposed to be the starting node.</p>



<p class="wp-block-paragraph">And if you see, I only have like an edge going from 3 to 13.</p>



<p class="wp-block-paragraph">I don&#8217;t have one going from 13 to 3.</p>



<p class="wp-block-paragraph">So 13 comma 3 would be invalid.</p>



<p class="wp-block-paragraph">It would be totally bad.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say the 3 goes out to the 13.</p>



<p class="wp-block-paragraph">So the 3 comes first and the 13 is second.</p>



<p class="wp-block-paragraph">And then I have to describe how much it costs.</p>



<p class="wp-block-paragraph">So that&#8217;s the third item in that one particular tuple.</p>



<p class="wp-block-paragraph">It&#8217;s going to be a 1 because the cost of that one particular edge is 1.</p>



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



<p class="wp-block-paragraph">one millisecond delay one time whatever so then the next item is going to be where else does the</p>



<p class="wp-block-paragraph">three go to the three doesn&#8217;t go anywhere else so we&#8217;re done representing the three at least as a</p>



<p class="wp-block-paragraph">start node so next up uh i&#8217;m going to look at the 13 where does the 13 go to it goes to the six it</p>



<p class="wp-block-paragraph">doesn&#8217;t go to the three the three goes to 13 but this the 13 doesn&#8217;t go to the three so 13 goes to</p>



<p class="wp-block-paragraph">six and it costs six because that&#8217;s the cost of the edge there&#8217;s two sixes here</p>



<p class="wp-block-paragraph">the edge. There&#8217;s two 6s here, a little confusing, but that&#8217;s because one is a node and one is a</p>



<p class="wp-block-paragraph">weight. The 13 does not go out anywhere else, so we&#8217;re done with the 13. I like to make, you know,</p>



<p class="wp-block-paragraph">different rows for every single start node. You don&#8217;t have to do that. Next up, I&#8217;m going to look</p>



<p class="wp-block-paragraph">at the 6. The 6 does not go out to the 13. The 6 does not go out to the 18. There is a connection</p>



<p class="wp-block-paragraph">there, but it&#8217;s going in the other direction. The 6 goes to the 12 and also to the 15. So the 6 goes</p>



<p class="wp-block-paragraph">to the 12, and that cost us $5,000.</p>



<p class="wp-block-paragraph">to the 12 and that cost us five because that was the weight we assigned and the six also goes to</p>



<p class="wp-block-paragraph">the 15 with a cost of i think i just wrote seven in a weird way um so we go from six to fifteen it</p>



<p class="wp-block-paragraph">costs us seven and then we&#8217;re done looking at the outgoing edges of these six let me just double</p>



<p class="wp-block-paragraph">check out out yes now we&#8217;re ready to move on to the uh the 12 node the 12 node has one outgoing</p>



<p class="wp-block-paragraph">connection it goes out to the three did i get that wrong in the last video leave a comment</p>



<p class="wp-block-paragraph">Did I get that wrong in the last video?</p>



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



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



<p class="wp-block-paragraph">And then we&#8217;re done with the 12 node.</p>



<p class="wp-block-paragraph">Now we look at the 15 node.</p>



<p class="wp-block-paragraph">Oh, that&#8217;s why I had that feeling,</p>



<p class="wp-block-paragraph">because the 15 didn&#8217;t go anywhere.</p>



<p class="wp-block-paragraph">15 has incoming connections, but no outgoing connections,</p>



<p class="wp-block-paragraph">so we actually can&#8217;t write it down here.</p>



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



<p class="wp-block-paragraph">Then we look at the 18 node.</p>



<p class="wp-block-paragraph">The 18 has outgoing connections to four other nodes.</p>



<p class="wp-block-paragraph">To the three, with a cost of three.</p>



<p class="wp-block-paragraph">Am I mind-tripping myself when I&#8217;m doing these duplicates?</p>



<p class="wp-block-paragraph">18 also goes out to the 6 with the cost of 8.</p>



<p class="wp-block-paragraph">Didn&#8217;t trick myself that time.</p>



<p class="wp-block-paragraph">The 18 also goes out to the 12 with the cost of 2.</p>



<p class="wp-block-paragraph">And then the 18 also goes out to 15 with the cost of 9.</p>



<p class="wp-block-paragraph">I just realized recently it&#8217;s a good idea to make faces at the camera for emotional thumbnails.</p>



<p class="wp-block-paragraph">I don&#8217;t know if you&#8217;ve picked up on that yet, but yeah.</p>



<p class="wp-block-paragraph">if you&#8217;ve picked up on that yet but yeah so let me just double check that i&#8217;ve got like nine edges</p>



<p class="wp-block-paragraph">and nine entries one two three four five six seven eight nine so i think i probably did it all right</p>



<p class="wp-block-paragraph">now there&#8217;s a problem um i&#8217;ve been talking about this in in the recent videos there&#8217;s a problem</p>



<p class="wp-block-paragraph">with this type of representation what if you are scanning through the edge list and you&#8217;re like</p>



<p class="wp-block-paragraph">well i&#8217;m scanning this edge and that edge and that edge and that edge and that edge and then i come</p>



<p class="wp-block-paragraph">to the 18-6 edge</p>



<p class="wp-block-paragraph">and I think, well now I found</p>



<p class="wp-block-paragraph">and i think well now i found some edge that was interesting for whatever reason</p>



<p class="wp-block-paragraph">maybe if i was scanning or doing some kind of like a operation that might have costed linear time so</p>



<p class="wp-block-paragraph">you don&#8217;t have to know about this uh this is in other videos but basically it&#8217;s not incredibly</p>



<p class="wp-block-paragraph">scalable it&#8217;s not incredibly fast it is technically scalable just not incredible</p>



<p class="wp-block-paragraph">linear time based on the number of edges so that kind of sucks because if i wanted to do something</p>



<p class="wp-block-paragraph">with those nodes with the 18 and</p>



<p class="wp-block-paragraph">with those nodes with the 18 and the six node i would then have to do another linear scan</p>



<p class="wp-block-paragraph">to find each node i&#8217;d have to go into my vertex list and i&#8217;d have to say well where the heck are</p>



<p class="wp-block-paragraph">those things where&#8217;s 18 nope nope nope nope nope uh found the 18 and then there&#8217;s another scan</p>



<p class="wp-block-paragraph">to find the six so really this is going to end up being um linear time based on the number of edges</p>



<p class="wp-block-paragraph">and also the number of vertices so that&#8217;s a little bit slower not</p>



<p class="wp-block-paragraph">a little bit slower not too great the upgrade that we&#8217;re going to do which we&#8217;ve been doing</p>



<p class="wp-block-paragraph">in all the other videos of of these recent edge list representations is i&#8217;m going to</p>



<p class="wp-block-paragraph">first i&#8217;m going to duplicate the vertex list like this</p>



<p class="wp-block-paragraph">and then i&#8217;m going to show you that each node each vertex has an index so i&#8217;m going to say</p>



<p class="wp-block-paragraph">one this is index two it&#8217;s index three when i type too hard i think my hand shakes the table</p>



<p class="wp-block-paragraph">that kind of sucks i need shock absorbers for my galumpiness oh so we have uh an index for every</p>



<p class="wp-block-paragraph">single vertex what if we uh what if we copy pasted the edge list over here on the left</p>



<p class="wp-block-paragraph">um what&#8217;s going on it&#8217;s not oh there it is dumb okay what if we copy pasted it and then we</p>



<p class="wp-block-paragraph">converted all of the values.</p>



<p class="wp-block-paragraph">at all of the values uh or um i guess you maybe on the left side if you if you&#8217;re doing this in</p>



<p class="wp-block-paragraph">the code you might put pointers instead of values so we&#8217;ll just say that we&#8217;ll convert the pointers</p>



<p class="wp-block-paragraph">or these values into indexes so this first tuple we&#8217;re describing the edge that goes between 3 and</p>



<p class="wp-block-paragraph">13 with the cost of 1 3 is sitting at index 0 in the vertex list so i&#8217;m going to change it to a 0</p>



<p class="wp-block-paragraph">13 is sitting at index 1 I&#8217;m going to leave the weight alone and</p>



<p class="wp-block-paragraph">I&#8217;m going to leave the weight alone, and then I&#8217;m just going to convert every other tuple</p>



<p class="wp-block-paragraph">to indexes. So the 13 node, that&#8217;s sitting at index 1. The 6 node is sitting at index 2,</p>



<p class="wp-block-paragraph">leave the weight alone. The 6 node is at index 2, the 12 node is at index 3.</p>



<p class="wp-block-paragraph">The 6 node again is at index 2. The 15 node is at index 4, again leave the weights alone.</p>



<p class="wp-block-paragraph">The 12 node is sitting at index 3, the 3 node is sitting at index 0, leave the weight alone.</p>



<p class="wp-block-paragraph">The 18 node is sitting at index 5.</p>



<p class="wp-block-paragraph">The 3 node is sitting at index 0.</p>



<p class="wp-block-paragraph">I think I&#8217;m going too fast.</p>



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



<p class="wp-block-paragraph">We&#8217;ve got index 5 there for the 18, and then 6 is index 2.</p>



<p class="wp-block-paragraph">18 is index 5.</p>



<p class="wp-block-paragraph">12 is index 3.</p>



<p class="wp-block-paragraph">18 is index 5.</p>



<p class="wp-block-paragraph">15 is index 4.</p>



<p class="wp-block-paragraph">So all I did was just convert every node value or pointer, if it&#8217;s in your code, to an index that references the vertex list.</p>



<p class="wp-block-paragraph">the vertex list why would I want to do this okay so like last time we said we</p>



<p class="wp-block-paragraph">would do O of E it would be linear time based on the number of edges if we&#8217;re</p>



<p class="wp-block-paragraph">performing some sort of a search or examination of all the edges so we have</p>



<p class="wp-block-paragraph">to like scan all the edges and then eventually we come to something we&#8217;re</p>



<p class="wp-block-paragraph">interested in maybe we realize oh that&#8217;s that&#8217;s a special edge we want to see</p>



<p class="wp-block-paragraph">what&#8217;s going on there maybe we want to grab those nodes and extract more</p>



<p class="wp-block-paragraph">information from them or modify them or do something to them this representation</p>



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



<p class="wp-block-paragraph">this representation tells us exactly where to go,</p>



<p class="wp-block-paragraph">because this 5 is an index, it&#8217;s not a node value.</p>



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



<p class="wp-block-paragraph">So index 5 gives us the 18,</p>



<p class="wp-block-paragraph">and index 2 gives us the 6.</p>



<p class="wp-block-paragraph">Notice how we still actually were looking for the 18 and 6 nodes,</p>



<p class="wp-block-paragraph">like we were in the previous, I guess, representation.</p>



<p class="wp-block-paragraph">So now, instead of scanning the vertex list,</p>



<p class="wp-block-paragraph">we can just jump directly to an index,</p>



<p class="wp-block-paragraph">jump directly to an index and you know remember like if you&#8217;re using an array or a vector it&#8217;s</p>



<p class="wp-block-paragraph">going to be constant time to jump to an index it&#8217;s like very very fast it&#8217;s the holy grail</p>



<p class="wp-block-paragraph">of time complexities so it&#8217;s really going to be something like o of e plus c or one or whatever</p>



<p class="wp-block-paragraph">you want to call it but it&#8217;s going to reduce to just o of e so it&#8217;s going to be a little bit</p>



<p class="wp-block-paragraph">faster notice how you know this one is going to take more time than that one maybe i&#8217;ll put it in</p>



<p class="wp-block-paragraph">red to make the</p>



<p class="wp-block-paragraph">it in red to make the the more than the greater than operator feel feel bad and evil but anyway</p>



<p class="wp-block-paragraph">this is how you do edgeless representation if you&#8217;re interested in the in all four uh types</p>



<p class="wp-block-paragraph">of graphs being represented with an edgeless representation check out all my other videos</p>



<p class="wp-block-paragraph">please leave a comment with ideas for stuff you want me to go over but i think i&#8217;m finished with</p>



<p class="wp-block-paragraph">this video it&#8217;s been 15 minutes already i don&#8217;t remember what i forgot to say but i guess we&#8217;re</p>



<p class="wp-block-paragraph">done now thank you so much for watching</p>



<p class="wp-block-paragraph">we&#8217;re done now thank you so much for watching i hope you learned a little bit of stuff and had a</p>



<p class="wp-block-paragraph">little bit of fun i&#8217;ll see you in the next video i&#8217;m gonna get my gear i&#8217;m just kidding i&#8217;m gonna</p>



<p class="wp-block-paragraph">eat a bunch of cookies i&#8217;m just kidding i shouldn&#8217;t do that but i probably will</p>



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



<p class="wp-block-paragraph">appreciate it i do hope you did learn something and have some fun uh if you could do me a please</p>



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



<p class="wp-block-paragraph">subscribe and follow this channel or these videos or whatever it is you do on</p>



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



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



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



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



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



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



<p class="wp-block-paragraph">up and I could feel</p>



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



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



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



<p class="wp-block-paragraph">what will happen also uh 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</p>



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



<p class="wp-block-paragraph">all the videos I published, and the services, and</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 please leave a comment or if you just want to say hey</p>



<p class="wp-block-paragraph">What&#8217;s up? What&#8217;s going on? You know, just send me a comment, whatever</p>



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



<p class="wp-block-paragraph">It would really it really mean the world to me. I would really appreciate it. So again, thank you</p>



<p class="wp-block-paragraph">it. So again, thank you so much for watching this video and, um, enjoy the cool music as,</p>



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



<p class="wp-block-paragraph">ご視聴ありがとうございました</p>



<p class="wp-block-paragraph">Teksting av Nicolai Winther</p>



<p class="wp-block-paragraph">Hey there!</p>
<p>The post <a href="https://www.NeuralLantern.com/directed-weighted-graph-edge-list-representation/">Directed Weighted Graph Edge List Representation</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/directed-weighted-graph-edge-list-representation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Represent Undirected Weighted Graphs Using Edge Lists</title>
		<link>https://www.NeuralLantern.com/how-to-represent-undirected-weighted-graphs-using-edge-lists/</link>
					<comments>https://www.NeuralLantern.com/how-to-represent-undirected-weighted-graphs-using-edge-lists/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Wed, 26 Aug 2026 17:23:28 +0000</pubDate>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Graphs]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[big o complexity]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[constant time lookup]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[edge list]]></category>
		<category><![CDATA[edge list representation]]></category>
		<category><![CDATA[graph algorithms]]></category>
		<category><![CDATA[graph representation]]></category>
		<category><![CDATA[graph theory]]></category>
		<category><![CDATA[node index]]></category>
		<category><![CDATA[programming graphs]]></category>
		<category><![CDATA[undirected graphs]]></category>
		<category><![CDATA[undirected weighted graph]]></category>
		<category><![CDATA[vertex list]]></category>
		<category><![CDATA[weighted graphs]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=428</guid>

					<description><![CDATA[<p>Learn the edge list representation for undirected weighted graphs. Build a vertex list and an edge list of triples that store both endpoints and the weight of each edge. Convert the list to use indexes so node lookups become constant time when the vertices are stored in a vector or array. This approach keeps each undirected edge only once and improves the speed of operations that need both edges and nodes.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-to-represent-undirected-weighted-graphs-using-edge-lists/">How to Represent Undirected Weighted Graphs Using Edge Lists</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">In the last video we talked about representing an unweighted and undirected graph using the edge list representation inside your machine. Now let&#8217;s talk about how to do that exact same thing with undirected weighted graphs.</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="How to Represent Undirected Weighted Graphs Using Edge Lists" width="1380" height="776" src="https://www.youtube.com/embed/p1sD8ZQxFgg?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">We start from the previous diagram and add weight costs on every single edge. The graph is a tuple consisting of a vertex list and an edge list. Vertices are listed from left to right. Edges are stored as triples with starting node, ending node, and weight.</p>



<p class="wp-block-paragraph">We build the edge list by scanning nodes left to right so each undirected edge appears only once. Then we convert the representation to use indexes instead of node values. This lets you jump to any node in constant time when using a vector or array for the vertex list.</p>



<p class="wp-block-paragraph">This makes operations on edges and nodes much faster than linear scans for both. Next up: directed graphs, both unweighted and weighted.</p>



<p class="wp-block-paragraph">0:00 Introduction to Weighted Graphs<br>1:00 Adding Edge Weights<br>3:16 Creating the Vertex List<br>5:19 Building the Edge List<br>10:34 Completing the Value-Based List<br>10:44 Why Value-Based Is Slow<br>13:04 Converting to Index-Based<br>15:29 Benefits of Index Lookups<br>17:49 Wrap-Up and Next Videos<br>18:22 Thank You 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><a href="https://www.NeuralLantern.com/social">https://www.NeuralLantern.com/social</a></li>



<li>Twitter / X: <a href="https://x.com/NeuralLantern">https://x.com/NeuralLantern</a></li>



<li>Rumble: <a href="https://rumble.com/c/c-3696939">https://rumble.com/c/c-3696939</a></li>



<li>BitChute: <a href="https://www.bitchute.com/channel/pg1Pvv5dN4Gt">https://www.bitchute.com/channel/pg1Pvv5dN4Gt</a></li>



<li>Daily Motion: <a href="https://www.dailymotion.com/neurallantern">https://www.dailymotion.com/neurallantern</a></li>



<li>Minds: <a href="https://www.minds.com/neurallantern/">https://www.minds.com/neurallantern/</a></li>



<li>Odysee: <a href="https://odysee.com/@NeuralLantern:5">https://odysee.com/@NeuralLantern:5</a></li>
</ul>



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



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



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



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



<li>Subscribe to the Blog: <a href="https://www.NeuralLantern.com">https://www.NeuralLantern.com</a></li>



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



<p class="wp-block-paragraph">Hey there! Hello there. Hey there. In the last video we talked about representing</p>



<p class="wp-block-paragraph">an unweighted and undirected graph using the edge list representation inside your</p>



<p class="wp-block-paragraph">machine. Now let&#8217;s talk about how to do that exact same thing with undirected</p>



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



<p class="wp-block-paragraph">seeing on the screen right now is basically the last diagram that I drew on my previous video</p>



<p class="wp-block-paragraph">where we talked about how to represent unweighted undirected graphs in the machine. You don&#8217;t</p>



<p class="wp-block-paragraph">necessarily need to watch that video to understand this video but you probably want to watch a few</p>



<p class="wp-block-paragraph">of my other previous graph videos to know the basics of graphs and edge lists and edge-based</p>



<p class="wp-block-paragraph">paths and node-based paths and all that stuff. So for now I&#8217;m just going to continue from this</p>



<p class="wp-block-paragraph">document. So let&#8217;s see, I&#8217;m going to duplicate</p>



<p class="wp-block-paragraph">I&#8217;m going to duplicate this real fast I am going to just kind of modify the captions here because</p>



<p class="wp-block-paragraph">we&#8217;re not going to do unweighted anywhere we&#8217;re going to say a weighted undirected graph did I</p>



<p class="wp-block-paragraph">get that backwards I think sometimes people say undirected and then the weighted part after so</p>



<p class="wp-block-paragraph">I&#8217;m just going to I&#8217;m just going to change it so it&#8217;s still undirected which means the edges don&#8217;t</p>



<p class="wp-block-paragraph">have arrows so that&#8217;s fine but it&#8217;s going to be weighted now which means I have to assign a cost</p>



<p class="wp-block-paragraph">of traveling along every single edge.</p>



<p class="wp-block-paragraph">So I am going to just</p>



<p class="wp-block-paragraph">so i am going to just sort of like erase a bunch of this stuff real fast erase these notations</p>



<p class="wp-block-paragraph">uh you know what i&#8217;m going to erase all of this because i want you to you know see it</p>



<p class="wp-block-paragraph">from start to finish okay so now i&#8217;m going to add weight costs on every single edge</p>



<p class="wp-block-paragraph">um in the last video what i mentioned is that the the edge list representation of a graph in</p>



<p class="wp-block-paragraph">the machine uh is basically your graph is a tuple that&#8217;s the orange you know marking</p>



<p class="wp-block-paragraph">a tuple that&#8217;s the orange you know markings up here your graph is a tuple where the tuple consists</p>



<p class="wp-block-paragraph">of a vertex list and also an edge list so we&#8217;ll do that from scratch together again but um let</p>



<p class="wp-block-paragraph">me get the weights on there first what color do you want i don&#8217;t know i&#8217;m going to put arbitrary</p>



<p class="wp-block-paragraph">numbers on all the edges just to say that every edge has a cost remember in a graph when you</p>



<p class="wp-block-paragraph">travel along an edge if the edge has a weight then you consider that weight to be part of the cost</p>



<p class="wp-block-paragraph">usually of traveling there so like if this was a network diagram</p>



<p class="wp-block-paragraph">traveling there so like if this was a network diagram maybe these weights represent pings or</p>



<p class="wp-block-paragraph">lags or bandwidth cost or something like that if this is like an airport diagram maybe the weights</p>



<p class="wp-block-paragraph">represent gasoline or time or something like that so i&#8217;m going to do like four there we can repeat</p>



<p class="wp-block-paragraph">weights if we really want to i&#8217;m going to line this up a little bit better that 12 looks gross</p>



<p class="wp-block-paragraph">okay i think we&#8217;re okay now i have 14 all right did i forget anything raise your hand if i forgot</p>



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



<p class="wp-block-paragraph">raise your hand if i forgot something oh i forgot okay did you raise your hand okay</p>



<p class="wp-block-paragraph">i&#8217;m going to do the 13 and 6 uh let&#8217;s just put a 2 weight there you could put ones everywhere</p>



<p class="wp-block-paragraph">if you wanted to i mean any any number is fine um also in an unweighted graph you kind of can</p>



<p class="wp-block-paragraph">consider the number of hops to be a path cost which means you can kind of consider every uh</p>



<p class="wp-block-paragraph">edge to just have a weight of one but for now we&#8217;re just gonna say this is the weighted graph</p>



<p class="wp-block-paragraph">Okay, so the first&#8230;</p>



<p class="wp-block-paragraph">okay so the first thing we need to do uh after that is make a vertex list so our vertex list</p>



<p class="wp-block-paragraph">is just going to be a list of all of our vertices all of our nodes in the graph</p>



<p class="wp-block-paragraph">how do we list them so for starters like we usually say vertex list but for me personally</p>



<p class="wp-block-paragraph">this this particular data structure just that we&#8217;re going to type up on the bottom</p>



<p class="wp-block-paragraph">i usually like to consider that a vector or an array at least because you can jump into a</p>



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



<p class="wp-block-paragraph">uh because you can jump into a specific index in constant time which is really really fast if you</p>



<p class="wp-block-paragraph">don&#8217;t know uh big o and time complexities check out my other videos but um it&#8217;s just really really</p>



<p class="wp-block-paragraph">fast to jump to an index when you&#8217;re using an array or a vector but it&#8217;s kind of slow when</p>



<p class="wp-block-paragraph">you&#8217;re using an actual linked list or some other data structure that makes you scan through it so</p>



<p class="wp-block-paragraph">i&#8217;m going to say that my vertex list is actually a vector but i&#8217;ll keep calling it a list anyway</p>



<p class="wp-block-paragraph">so we got the vertices uh we have to start naming them okay so the first thing is i&#8217;m going to do</p>



<p class="wp-block-paragraph">we got the three there and we got the 13 and we got the 6 and the 12 and the 15 i&#8217;m just naming</p>



<p class="wp-block-paragraph">all of the nodes in my graph i like to go from left to right because that makes it easier for</p>



<p class="wp-block-paragraph">me to debug what i&#8217;m doing um you probably in your code i said this in the last video you probably</p>



<p class="wp-block-paragraph">don&#8217;t want to store the actual value in your vertex list you probably actually want to create</p>



<p class="wp-block-paragraph">an object of type node or of type vertex and give it the template data</p>



<p class="wp-block-paragraph">and give it the template data type of in this case integer and then set the 13 inside of the class</p>



<p class="wp-block-paragraph">it&#8217;s not as fast and memory efficient as just putting a number there but uh you know for me</p>



<p class="wp-block-paragraph">i like more flexibility and i like to be able to do more powerful things with my stuff so i&#8217;m going</p>



<p class="wp-block-paragraph">to say that 13 and all of the other nodes really that represents to me i made a brand new instance</p>



<p class="wp-block-paragraph">of a class called node or vertex and then i set it up however i wanted and one of the things i did</p>



<p class="wp-block-paragraph">set it up, we&#8217;ll stick to number 30.</p>



<p class="wp-block-paragraph">things i did to set it up we&#8217;ll stick the number 13 inside of the class but for the for the diagram</p>



<p class="wp-block-paragraph">it&#8217;s fine like this okay so we have our vertex list that&#8217;s the v if you&#8217;re looking at the uh</p>



<p class="wp-block-paragraph">you know the v comma e the v part now we need to do the edge list so i&#8217;m going to say here&#8217;s my</p>



<p class="wp-block-paragraph">edge list and we&#8217;re going to use the edge list representation like i talked about in previous</p>



<p class="wp-block-paragraph">videos, which is basically a tuple where one tuple describes one edge. So in our</p>



<p class="wp-block-paragraph">So, in our graph that we have right now, we definitely need a starting point and an ending point.</p>



<p class="wp-block-paragraph">So, let me just start by looking at the 3 node, and I want to describe one of the edges for the 3.</p>



<p class="wp-block-paragraph">So, the 3 goes to the 18 and the 13 and the 12.</p>



<p class="wp-block-paragraph">I&#8217;m going to scan my eyes from left to right to make it a little more simple.</p>



<p class="wp-block-paragraph">So, the 3 node, it connects to the 13 node, so I&#8217;m going to put a 3, 13.</p>



<p class="wp-block-paragraph">the nodes doesn&#8217;t matter in an undirected graph so i could have easily put 13 and 3 it&#8217;s fine</p>



<p class="wp-block-paragraph">you don&#8217;t want to duplicate it by putting 13 comma 3 sorry you don&#8217;t want to put 3 comma 13 and then</p>



<p class="wp-block-paragraph">later put 13 comma 3. you don&#8217;t want to double it up because that would be like representing two</p>



<p class="wp-block-paragraph">separate edges i&#8217;m just going to sort it in my mind before so that i don&#8217;t have to sort it again</p>



<p class="wp-block-paragraph">later when i&#8217;m reading the graph which makes it faster for read operations but so 3 goes to 13</p>



<p class="wp-block-paragraph">And then we have to represent the weight, so the tuple gets a little more complicated.</p>



<p class="wp-block-paragraph">so the tuple gets a little more complicated than the last video just by a little bit i&#8217;m going to</p>



<p class="wp-block-paragraph">say 3 comma 13 comma the weight which is just a 1 here and i think i mentioned this in my other</p>



<p class="wp-block-paragraph">videos like a while back but uh the weight doesn&#8217;t have to be integer it could be a float it could be</p>



<p class="wp-block-paragraph">any data type you wanted really as long as your algorithms support it and you have you know your</p>



<p class="wp-block-paragraph">operator overloads all done and all that could be a full class just for the weight if you wanted to</p>



<p class="wp-block-paragraph">but for this video it&#8217;s just going to be the weight is an integer so that&#8217;s the cost of</p>



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



<p class="wp-block-paragraph">is an integer so that&#8217;s the cost of traveling along that one edge so notice how all of our</p>



<p class="wp-block-paragraph">tuples have three items each it&#8217;s a triple tuple so i&#8217;m going to continue trying to represent uh</p>



<p class="wp-block-paragraph">what the three touches so the three uh it touches the 13 and it also touches the 12 so i&#8217;m going to</p>



<p class="wp-block-paragraph">say 3 goes to 12. what does that edge cost it costs us six because that&#8217;s the little pink six</p>



<p class="wp-block-paragraph">there we&#8217;re done with that tuple so what else does three touch uh touches the 13 and then it touches</p>



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



<p class="wp-block-paragraph">touches the 13 and then it touches the uh 12 and then it touches the 18. so i&#8217;m going to put an 18</p>



<p class="wp-block-paragraph">there what does that edge cost it costs a four because there&#8217;s a pink little four at the very</p>



<p class="wp-block-paragraph">top of the graph surprise you didn&#8217;t know that okay so then uh i&#8217;m sorry we&#8217;re going to move on</p>



<p class="wp-block-paragraph">to the next node so we did the three already so i&#8217;m done with that i&#8217;m going to look at the 13.</p>



<p class="wp-block-paragraph">where does the 13 connect it connects to the three but that&#8217;s to the left</p>



<p class="wp-block-paragraph">because i&#8217;m this is why i like to scan my eyes from left to right i already know stuff on the</p>



<p class="wp-block-paragraph">left is handled i don&#8217;t have to do it again so forget about the three i already described it</p>



<p class="wp-block-paragraph">up here in the very first tuple three comma thirteen comma one i don&#8217;t want to describe it</p>



<p class="wp-block-paragraph">again so forget about the three now the thirteen connects to the six so thirteen comma six and what</p>



<p class="wp-block-paragraph">does that cost it costed two for our arbitrary labeling thirteen doesn&#8217;t touch any other node</p>



<p class="wp-block-paragraph">so we&#8217;re done with the thirteen row and by the way i&#8217;m doing a line new lines here you don&#8217;t have to</p>



<p class="wp-block-paragraph">to do that, I just</p>



<p class="wp-block-paragraph">here you don&#8217;t have to do that i just like to i like to do one you know row per per start node</p>



<p class="wp-block-paragraph">to make it look cooler and nicer okay so the uh 13 is done now let&#8217;s do the six</p>



<p class="wp-block-paragraph">six touches the 13 but remember in the previous row we just handled that so i&#8217;m not going to do</p>



<p class="wp-block-paragraph">it again the six to the right touches three other nodes you can see there are three lines</p>



<p class="wp-block-paragraph">protruding on the right side of the six so um i&#8217;m just going to take those from left to right</p>



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



<p class="wp-block-paragraph">I&#8217;m just going to take those from left to right.</p>



<p class="wp-block-paragraph">It&#8217;s touching the 12 at a cost of 14.</p>



<p class="wp-block-paragraph">The 6 is also touching the 15 at a cost of 12, I think.</p>



<p class="wp-block-paragraph">And the 6, you know, if I make mistakes, just leave it in a comment.</p>



<p class="wp-block-paragraph">I will leave the video up so you can always remember my humiliation.</p>



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



<p class="wp-block-paragraph">I did not practice this.</p>



<p class="wp-block-paragraph">6 touches the 15, and then the 6 touches the 18.</p>



<p class="wp-block-paragraph">So 6 touches 18, and then it costs 9.</p>



<p class="wp-block-paragraph">So now we&#8217;re probably done with the 6.</p>



<p class="wp-block-paragraph">we&#8217;re probably done with the six um moving on uh one node to the right now we&#8217;re going to look at</p>



<p class="wp-block-paragraph">the 12 okay so the 12 it touches 3 and 13 and 6 but those are already handled so forget it</p>



<p class="wp-block-paragraph">the 12 touches the 15 we did not handle that right yeah because we&#8217;re going from left to right</p>



<p class="wp-block-paragraph">uh and and then it costs us from going from 12 what am i doing it doesn&#8217;t touch the 15 oh my god</p>



<p class="wp-block-paragraph">so the 12 touches the</p>



<p class="wp-block-paragraph">uh so the 12 touches the uh the three which is already handled and it touches the</p>



<p class="wp-block-paragraph">six which is already handled and then it only goes to the right to the 18. okay</p>



<p class="wp-block-paragraph">anyway you know when i make mistakes on camera and i say do over i always go i don&#8217;t know why</p>



<p class="wp-block-paragraph">so the 12 and the 18 uh it&#8217;s going to cost us three so then we&#8217;re done with the 12th so i&#8217;m</p>



<p class="wp-block-paragraph">I&#8217;m going to go to the next one. We&#8217;ll look at the 15. The 15. 6 is already handled. 8.</p>



<p class="wp-block-paragraph">6 is already handled, 18 is not handled yet, so I&#8217;m going to put 18 there, and it cost us $88, and then the 18 is the last one.</p>



<p class="wp-block-paragraph">There&#8217;s nothing to do, because every node is on the left of the 18.</p>



<p class="wp-block-paragraph">If you carefully, if you pause the video and look at every single node, you&#8217;ll see it&#8217;s already represented in a previous node.</p>



<p class="wp-block-paragraph">So we&#8217;re officially done with the basic edge list representation of this graph, this undirected but weighted graph.</p>



<p class="wp-block-paragraph">We need to do something else, though.</p>



<p class="wp-block-paragraph">we need to do something else though i mentioned in the last video i&#8217;m going to repeat it in case</p>



<p class="wp-block-paragraph">you didn&#8217;t watch the last video but we need to make sure that uh oh i guess i forgot to separate</p>



<p class="wp-block-paragraph">those two things that means i have to duplicate the vertex list over here on the right side too</p>



<p class="wp-block-paragraph">okay um so on the left this is okay but it&#8217;s kind of slow because what if we&#8217;re trying to look at uh</p>



<p class="wp-block-paragraph">you know one particular edge let&#8217;s say that we we want to see what&#8217;s going on with this uh this</p>



<p class="wp-block-paragraph">edge right here. Let&#8217;s say the 3 and the 13 edge, for whatever reason.</p>



<p class="wp-block-paragraph">13 edge for whatever reason um i don&#8217;t know green 3 and the 13 edge okay so we&#8217;re looking at that</p>



<p class="wp-block-paragraph">edge maybe for some reason we had just finished scanning uh maybe that&#8217;s not a fun edge well</p>



<p class="wp-block-paragraph">what&#8217;s going on hello uh oh i forgot to set up a thing where you can make your cursor really</p>



<p class="wp-block-paragraph">visible okay um actually so so this is not the most fun edge let me let me see about the 15 and</p>



<p class="wp-block-paragraph">and 18-inch.</p>



<p class="wp-block-paragraph">18 edge excuse me i had to take a drink of water so we&#8217;re going to look at this edge connecting</p>



<p class="wp-block-paragraph">the 15 and the 18 node that&#8217;s a little more interesting so you can imagine maybe you were</p>



<p class="wp-block-paragraph">doing a little scan uh if you know big o complexity by now we had to check out every single edge in</p>



<p class="wp-block-paragraph">this edge list until we found the one that connected the 15 and the 18 so this was a linear</p>



<p class="wp-block-paragraph">time scan based on the number of edges so this is just it&#8217;s scalable but it&#8217;s not very fast right so</p>



<p class="wp-block-paragraph">We ended up kind of looking at this one.</p>



<p class="wp-block-paragraph">so we ended up kind of looking at this one right here not fast um but what if we want to examine</p>



<p class="wp-block-paragraph">uh some properties of the nodes or the vertices uh involved like the 15 and the 18.</p>



<p class="wp-block-paragraph">here in this representation on the left we have the values we have the 15 value and the 18 value</p>



<p class="wp-block-paragraph">we don&#8217;t really have a pointer to a full node object if that&#8217;s what you&#8217;re doing in your code</p>



<p class="wp-block-paragraph">so um how do we get those those node objects we&#8217;re gonna have to do another linear scan we&#8217;re gonna</p>



<p class="wp-block-paragraph">I&#8217;m gonna have to look at this one right here.</p>



<p class="wp-block-paragraph">to look at this one right here oh that doesn&#8217;t match the value of 15 look at this one look at</p>



<p class="wp-block-paragraph">this one look at this one we&#8217;re gonna have to scan every single node until we realize that it&#8217;s those</p>



<p class="wp-block-paragraph">two at the end this is true even if it&#8217;s a vector uh holding your vertices because if we don&#8217;t know</p>



<p class="wp-block-paragraph">the index then we have to actually just sweep through the whole vector so that&#8217;s really slow</p>



<p class="wp-block-paragraph">it&#8217;s still kind of scalable but uh it&#8217;s not the best instead let&#8217;s update the the representation</p>



<p class="wp-block-paragraph">on the right to have index</p>



<p class="wp-block-paragraph">on the right to have indexes we did this in the last video but basically if you just kind of look</p>



<p class="wp-block-paragraph">at this three node here the three node well let me let me copy this one more time the three node</p>



<p class="wp-block-paragraph">corresponds to index zero which i forgot to write down my bad let me write that down for you real</p>



<p class="wp-block-paragraph">fast get rid of that get rid of that get rid of that okay so i&#8217;m just going to update this vertex</p>



<p class="wp-block-paragraph">list to just show you that if we have a vector or in a</p>



<p class="wp-block-paragraph">that if we have a vector or an array then every single item has an index here&#8217;s index one index</p>



<p class="wp-block-paragraph">two index three and four and five and then i guess i&#8217;ll just copy paste that to the other one since</p>



<p class="wp-block-paragraph">i was too dumb to make an extra separated graph there um separated item okay so the uh the 15</p>



<p class="wp-block-paragraph">sorry no the 13 the 3 and the 13 i guess i got to get rid of that that&#8217;s distracting me</p>



<p class="wp-block-paragraph">I guess I gotta get rid of that, that&#8217;s distracting me.</p>



<p class="wp-block-paragraph">The three and the 13,</p>



<p class="wp-block-paragraph">those correspond to indexes zero and one</p>



<p class="wp-block-paragraph">in the vertex list, right?</p>



<p class="wp-block-paragraph">So zero and one, that&#8217;s the three and the 13.</p>



<p class="wp-block-paragraph">So I&#8217;m just gonna change the values,</p>



<p class="wp-block-paragraph">the T-type values, to the raw indexes.</p>



<p class="wp-block-paragraph">It&#8217;s gonna seem a little weird at first,</p>



<p class="wp-block-paragraph">and I&#8217;m gonna leave the weight alone,</p>



<p class="wp-block-paragraph">because that&#8217;s totally fine.</p>



<p class="wp-block-paragraph">We&#8217;re not gonna jump to a weight,</p>



<p class="wp-block-paragraph">unless maybe you had a weight list, a weight vector,</p>



<p class="wp-block-paragraph">if you wanted to make instances of weights.</p>



<p class="wp-block-paragraph">You could do that, I&#8217;m not gonna do it.</p>



<p class="wp-block-paragraph">do that i&#8217;m not going to do it so then again the 3 is a 0 the 12 is index 3 the 3 is another 0</p>



<p class="wp-block-paragraph">the 18 is index 5. i&#8217;ll just do this for every single row so the 13 here is index 1 and then</p>



<p class="wp-block-paragraph">the 6 index 2 notice how i&#8217;m just looking up here at the indexes of the vertex list and i&#8217;m just</p>



<p class="wp-block-paragraph">changing the node values to the indexes where the nodes actually are in the vertex list so that&#8217;s a</p>



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



<p class="wp-block-paragraph">vertex list so that&#8217;s a 2 and then the 12 is going to be a 3 and then the 6 is also going to be a 2</p>



<p class="wp-block-paragraph">and the 15 is going to be a 4 and then the 6 is going to be a 2 and 18 is going to be 5</p>



<p class="wp-block-paragraph">on to the next row the 12 node is index 3 the 18 node is index 5 uh the 15 node is index 4 and the</p>



<p class="wp-block-paragraph">18 node is index 5 so whoa whoa correct me if i made a typo in the comments please i&#8217;ll release</p>



<p class="wp-block-paragraph">another video as a thank you.</p>



<p class="wp-block-paragraph">i&#8217;ll release another video as a thank you but uh now i&#8217;ve converted the whole entire representation</p>



<p class="wp-block-paragraph">on the right side to an index based uh uh you know edge list so i&#8217;m gonna do this one over</p>



<p class="wp-block-paragraph">here is by value and the one on the right is by index why is the one on the right better now</p>



<p class="wp-block-paragraph">remember at some point we might have been scanning to find an edge well what did i just do wrong oh i</p>



<p class="wp-block-paragraph">clicked over there okay we might have been scanning to find you know some edge so we&#8217;re like</p>



<p class="wp-block-paragraph">Like, subscribe, and hit the bell icon to be notified when I post a new video. Thanks for watching, and I&#8217;ll see you in the next one.</p>



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



<p class="wp-block-paragraph">edge so we&#8217;re like scanning scanning scanning scanning and then maybe we decide we&#8217;re trying</p>



<p class="wp-block-paragraph">to figure out what&#8217;s going on with the two and the not the two and the four the uh the six and</p>



<p class="wp-block-paragraph">the fifteen node right so that&#8217;s uh the six and the fifteen node that&#8217;s basically you know this</p>



<p class="wp-block-paragraph">right here uh so now what if we want to do something with the nodes we want to do something</p>



<p class="wp-block-paragraph">with the six node do something with the 15 node we don&#8217;t want to scan through the vertex list again</p>



<p class="wp-block-paragraph">we don&#8217;t want to go scan scan scan scan scan that&#8217;s linear time that&#8217;s not as fast as it could</p>



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



<p class="wp-block-paragraph">That&#8217;s linear time, that&#8217;s not as fast as it could be.</p>



<p class="wp-block-paragraph">So instead, we&#8217;ve already converted here so that we have indexes.</p>



<p class="wp-block-paragraph">So if I look at the 2 and the 4, the 2 and the 4 represent indexes, not node values.</p>



<p class="wp-block-paragraph">So this 2 tells me that I can go to index 2 to get the 6 node,</p>



<p class="wp-block-paragraph">and the 4 tells me I can go to index 4 to get the 15 node.</p>



<p class="wp-block-paragraph">Notice how I did not have to scan the vertex list to figure out where those nodes are.</p>



<p class="wp-block-paragraph">And if you&#8217;re using a vector for your vertex list,</p>



<p class="wp-block-paragraph">list then you can jump to every index in constant time so now if we were going to you know search</p>



<p class="wp-block-paragraph">and do a bunch of stuff on edges we probably only had to spend linear time on the edge list but then</p>



<p class="wp-block-paragraph">we only spent constant time uh to to manipulate each node whereas it would have been linear plus</p>



<p class="wp-block-paragraph">linear uh in the past like on the left side that would have been uh you know if we&#8217;re going to do</p>



<p class="wp-block-paragraph">stuff with with both the edges and the nodes it would have been o of linear time based on the</p>



<p class="wp-block-paragraph">number of edges if we&#8217;re gonna do every</p>



<p class="wp-block-paragraph">on the number of edges if we&#8217;re going to do every you know something to every single edge</p>



<p class="wp-block-paragraph">plus linear time based on number of vertices but over here it&#8217;s going to be linear time of the</p>



<p class="wp-block-paragraph">edges plus just constant time which basically means it&#8217;s going to be you know big o of just</p>



<p class="wp-block-paragraph">the number of edges and i&#8217;m talking about a you know a fantasy scenario where we&#8217;re just going</p>



<p class="wp-block-paragraph">to scan a bunch of stuff you can imagine there&#8217;s a whole bunch of different operations you could</p>



<p class="wp-block-paragraph">do on a graph, and it would be very advantageous and fast.</p>



<p class="wp-block-paragraph">very advantageous and fast if you could just jump to a node if you wanted to be able to manipulate</p>



<p class="wp-block-paragraph">it directly um let&#8217;s see so i did i think 20 minutes already so i think this is going to be</p>



<p class="wp-block-paragraph">it for for today&#8217;s video um and i&#8217;m going to do another video after this where we start working</p>



<p class="wp-block-paragraph">with directed graphs so i&#8217;m going to do directed unweighted and then directed weighted so by the</p>



<p class="wp-block-paragraph">time you watch all four of these videos</p>



<p class="wp-block-paragraph">by the time you watch all four of these videos you&#8217;ll know how to represent all four types of</p>



<p class="wp-block-paragraph">graphs in the machine okay anyway so thank you for watching this video i hope you learned a little</p>



<p class="wp-block-paragraph">bit of stuff and had a little bit of fun i&#8217;ll see you next time tell your friends eat a chocolate</p>



<p class="wp-block-paragraph">okay i&#8217;m outie hey everybody thanks for watching this video again from the bottom of my heart i</p>



<p class="wp-block-paragraph">really appreciate it i do hope you did learn something and have some fun uh if you could do</p>



<p class="wp-block-paragraph">me a please a small little favor could you please</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.</p>



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



<p class="wp-block-paragraph">videos in general. So please do me a kindness and subscribe. You know, sometimes I&#8217;m sleeping</p>



<p class="wp-block-paragraph">in the middle of the night and I just wake up because I know somebody subscribed.</p>



<p class="wp-block-paragraph">just wake up because i know somebody subscribed or followed it just wakes me up and i get filled</p>



<p class="wp-block-paragraph">with joy that&#8217;s exactly what happens every single time so you could do it as a nice favor to me or</p>



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



<p class="wp-block-paragraph">and then i&#8217;ll i&#8217;ll just wake up i promise that&#8217;s what will happen also uh if you look at the middle</p>



<p class="wp-block-paragraph">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 it&#8217;ll take you to my</p>



<p class="wp-block-paragraph">main website where you can just kind of like see all the</p>



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



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



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



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



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



<p class="wp-block-paragraph">it 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 enjoy the cool music as as i fade into the darkness which is coming for us all</p>



<p class="wp-block-paragraph">Teksting av Nicolai Winther</p>



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



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-to-represent-undirected-weighted-graphs-using-edge-lists/">How to Represent Undirected Weighted Graphs Using Edge Lists</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/how-to-represent-undirected-weighted-graphs-using-edge-lists/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Graph Connectedness Explained: Connected Strongly Connected and Weakly Connected</title>
		<link>https://www.NeuralLantern.com/graph-connectedness-explained-connected-strongly-connected-and-weakly-connected/</link>
					<comments>https://www.NeuralLantern.com/graph-connectedness-explained-connected-strongly-connected-and-weakly-connected/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sat, 25 Jul 2026 23:04:51 +0000</pubDate>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Graphs]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[connected graph]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[directed graph]]></category>
		<category><![CDATA[graph algorithms]]></category>
		<category><![CDATA[graph connectedness]]></category>
		<category><![CDATA[graph paths]]></category>
		<category><![CDATA[graph theory]]></category>
		<category><![CDATA[nodes and edges]]></category>
		<category><![CDATA[strongly connected graph]]></category>
		<category><![CDATA[subgraphs]]></category>
		<category><![CDATA[undirected graph]]></category>
		<category><![CDATA[weakly connected graph]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=417</guid>

					<description><![CDATA[<p>This video explains graph connectedness for both undirected and directed graphs. For undirected graphs a connected graph means every node can reach every other node through a path. Disconnected graphs have nodes that cannot reach others and may contain separate subgraphs. For directed graphs the terms change to strongly connected where every node reaches every other node following edge directions and weakly connected where the graph is connected if directions are ignored. Step by step examples show how adding edges turns a graph from not connected to weakly connected and finally to strongly connected.</p>
<p>The post <a href="https://www.NeuralLantern.com/graph-connectedness-explained-connected-strongly-connected-and-weakly-connected/">Graph Connectedness Explained: Connected Strongly Connected and Weakly Connected</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="Graph Connectedness Explained: Connected Strongly Connected and Weakly Connected" width="1380" height="776" src="https://www.youtube.com/embed/u1D6huK1DCA?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. Lets talk about graphs and whether they are connected or not or maybe whether they are strongly connected versus weakly connected. We look at undirected graphs first and show what makes a graph connected meaning every node can reach every other node by a path. We add edges and check the connections step by step while also noting subgraphs. Then we switch to directed graphs and explain strongly connected versus weakly connected. Strongly connected follows the edge directions for every path. Weakly connected ignores the directions and checks if the graph would still be connected. Examples show how adding edges changes the status of the graph.</p>



<p class="wp-block-paragraph">00:00 Introduction to Graph Connectedness<br>00:01 Drawing Nodes and a Disconnected Graph<br>01:56 Defining a Connected Graph<br>03:21 Introducing Subgraphs<br>04:48 Adding Edges to Create a Connected Graph<br>06:32 Adding Directions to Create a Directed Graph<br>08:03 Strongly Connected Versus Weakly Connected<br>08:34 Removing an Edge to Show Disconnection<br>09:29 Checking and Achieving Weak Connectivity<br>11:18 Adding Edges to Create a Strongly Connected Graph<br>13:20 Confirming the Strongly Connected Graph<br>13:32 Conclusion<br>14:17 Outro and Subscribe Request</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 graphs and whether they&#8217;re connected or not or maybe whether they&#8217;re</p>



<p class="wp-block-paragraph">strongly connected versus weakly connected versus not connected. You never know.</p>



<p class="wp-block-paragraph">Okay, so hopefully you&#8217;ve seen my previous videos by now where we just talked about the basics of</p>



<p class="wp-block-paragraph">paper here i&#8217;m just going to start drawing a graph remember graphs are nodes and edges where</p>



<p class="wp-block-paragraph">the nodes are like little t-type data structures that can hold a specific type of value um so i&#8217;m</p>



<p class="wp-block-paragraph">going to say our our nodes hold integers um i think i made that one too small that&#8217;s okay</p>



<p class="wp-block-paragraph">and the edges uh could have weights or maybe not weights at all depending on if you have</p>



<p class="wp-block-paragraph">a weighted or an unweighted graph so i&#8217;m just going to start duplicating my nodes a little bit</p>



<p class="wp-block-paragraph">going to make a random graph. I need to probably change these values. Technically your graphs</p>



<p class="wp-block-paragraph">should support duplicate values in the nodes and also the weights, but I don&#8217;t want to because</p>



<p class="wp-block-paragraph">it&#8217;s easier to talk about the graph when all the values are distinct. So for starters, we have a</p>



<p class="wp-block-paragraph">bunch of nodes and they&#8217;re not really connected to each other. Is this a valid graph? Yeah it is.</p>



<p class="wp-block-paragraph">It&#8217;s just not a connected graph. So the terminology that we&#8217;re going to use for</p>



<p class="wp-block-paragraph">undirected graphs meaning the edges don&#8217;t have direction is going to be connected or not you</p>



<p class="wp-block-paragraph">know i usually just say a not connected graph or a disconnected graph so just basically like</p>



<p class="wp-block-paragraph">connected or not connected okay so this is a not connected graph i&#8217;m going to say not connected</p>



<p class="wp-block-paragraph">not cinnamon i&#8217;m thinking about food as usual</p>



<p class="wp-block-paragraph">connected graph why is it not a connected graph well to define what does it mean to be a connected</p>



<p class="wp-block-paragraph">graph it just means that every single node can find a path to every single other node</p>



<p class="wp-block-paragraph">in an undirected graph so we can just double check this real fast we can say you know can</p>



<p class="wp-block-paragraph">node number two find a path to uh you know let me put a question here let&#8217;s say we&#8217;re going to check</p>



<p class="wp-block-paragraph">you should be checking all the nodes one by one by one to see if if the node in particular can</p>



<p class="wp-block-paragraph">first node that can&#8217;t do it means the whole graph is not considered connected so you know two it</p>



<p class="wp-block-paragraph">can&#8217;t reach one so that means it&#8217;s not a connected graph so this is not a connected graph because one</p>



<p class="wp-block-paragraph">or more nodes violate the rule of you have to be able to reach every single other node okay so that</p>



<p class="wp-block-paragraph">means if we want a connected graph we probably just have to start adding edges so I&#8217;m going to</p>



<p class="wp-block-paragraph">go ahead and uh let me let me write the word connected here and then I can like either circle</p>



<p class="wp-block-paragraph">cross it out. So right now we have no, this is not a connected graph. I&#8217;m going to start adding</p>



<p class="wp-block-paragraph">edges. We&#8217;re going to stick with an undirected graph for now. So I&#8217;m going to connect two and one</p>



<p class="wp-block-paragraph">and two and nine, maybe one and 13. Actually, you know what? I&#8217;m going to erase this edge</p>



<p class="wp-block-paragraph">and maybe connect these over here. And the thing is, this is a valid graph that you&#8217;re looking at.</p>



<p class="wp-block-paragraph">it&#8217;s just not a connected graph.</p>



<p class="wp-block-paragraph">Some other terminology I didn&#8217;t mention in other videos</p>



<p class="wp-block-paragraph">is that we can have subgraphs.</p>



<p class="wp-block-paragraph">We have like a subgraph here and another subgraph here.</p>



<p class="wp-block-paragraph">Those are basically just two different portions of the same graph</p>



<p class="wp-block-paragraph">that appear to be connected within themselves.</p>



<p class="wp-block-paragraph">So those are subgraphs.</p>



<p class="wp-block-paragraph">You can probably tell right away that this is not a connected graph.</p>



<p class="wp-block-paragraph">You can double check to be sure.</p>



<p class="wp-block-paragraph">You know, it&#8217;s good to do things the hard way</p>



<p class="wp-block-paragraph">so you don&#8217;t get the wrong answer</p>



<p class="wp-block-paragraph">when you&#8217;re really trying to figure something out.</p>



<p class="wp-block-paragraph">So let&#8217;s look at two.</p>



<p class="wp-block-paragraph">So let&#8217;s look at two. Can two reach one? Yes. So that&#8217;s good. Can two reach nine? Yeah,</p>



<p class="wp-block-paragraph">it just hops to one and then hops to nine. So two can reach one and also nine. That&#8217;s good.</p>



<p class="wp-block-paragraph">Can two reach three? Nope. So this is still not a connected graph.</p>



<p class="wp-block-paragraph">Okay, I&#8217;m going to add one more edge. And then we&#8217;ll just check again.</p>



<p class="wp-block-paragraph">Where do you think I&#8217;m going to do it? Maybe if I add, I&#8217;ll add a funky edge going from 13 to like</p>



<p class="wp-block-paragraph">to like nine or something just so it feels a little bit more awkward okay let&#8217;s check one by</p>



<p class="wp-block-paragraph">one by one let me actually get rid of this edge on the previous slide so we can still say that&#8217;s</p>



<p class="wp-block-paragraph">not connected let&#8217;s check one by one by one every node must be able to reach every single other</p>



<p class="wp-block-paragraph">node so I&#8217;m gonna look at two and I&#8217;m gonna say all right two can it reach one yes can it reach</p>



<p class="wp-block-paragraph">to nine. So that&#8217;s good. Can it reach three? Yeah, it can go from two to one to nine to 13</p>



<p class="wp-block-paragraph">to three, and then it can reach three. And because we just saw that it went through 13 to reach three,</p>



<p class="wp-block-paragraph">that means two can reach every single other node. This is going to be more important as a double</p>



<p class="wp-block-paragraph">check when we have directed graphs in a second. So two is good. So the next thing you want to do is</p>



<p class="wp-block-paragraph">the next thing you want to do is just move on to some other node and do the exact same process you</p>



<p class="wp-block-paragraph">know can can nine reach a one yes okay it can reach a one can it reach a two yeah it goes through</p>



<p class="wp-block-paragraph">the one can it reach the 13 yep can it reach the three yep okay so we just check every single</p>



<p class="wp-block-paragraph">destination node starting at the node nine but here&#8217;s a little bit of a shortcut try to find a</p>



<p class="wp-block-paragraph">node that is right next to a node that you know can reach everything and if the node in question</p>



<p class="wp-block-paragraph">that previously connected node, vertex,</p>



<p class="wp-block-paragraph">then we know that that node in particular is also connected.</p>



<p class="wp-block-paragraph">So what I&#8217;m saying is the one,</p>



<p class="wp-block-paragraph">instead of checking every single other node in the graph,</p>



<p class="wp-block-paragraph">which might be really hard</p>



<p class="wp-block-paragraph">if we have a whole bunch of nodes in our graph,</p>



<p class="wp-block-paragraph">we&#8217;ll just ask, can the one reach the two?</p>



<p class="wp-block-paragraph">Because if it can, the two can reach everything.</p>



<p class="wp-block-paragraph">So that means the one actually can reach everything as well.</p>



<p class="wp-block-paragraph">So the one can reach the two.</p>



<p class="wp-block-paragraph">So the one is connected to everything.</p>



<p class="wp-block-paragraph">the three, the three can reach the one and the one can reach everything. I&#8217;m doing things the</p>



<p class="wp-block-paragraph">hard way on purpose just to prove a point. So the three can reach everything. The 13 can reach the</p>



<p class="wp-block-paragraph">three in one hop. So the 13 can reach everything. The nine, it can reach the one and it can reach</p>



<p class="wp-block-paragraph">the 13 in one hop. So the nine can also actually reach everything. So that means this is indeed a</p>



<p class="wp-block-paragraph">the next thing that we&#8217;re going to do is add direction to our graph you know we could do like</p>



<p class="wp-block-paragraph">more examples maybe leave a comment if you want me to do some super extra complicated examples for</p>



<p class="wp-block-paragraph">just a undirected and connected graph i&#8217;m ignoring weight by the way because it doesn&#8217;t really matter</p>



<p class="wp-block-paragraph">for these purposes so let&#8217;s upgrade our graph so uh maybe i should put a question mark here</p>



<p class="wp-block-paragraph">that would have been a good idea we&#8217;re just asking you know is it connected or not</p>



<p class="wp-block-paragraph">And I&#8217;m just going to start adding direction to every single edge.</p>



<p class="wp-block-paragraph">Let&#8217;s see if I&#8217;m following my notes.</p>



<p class="wp-block-paragraph">Single node, subgraphs, right?</p>



<p class="wp-block-paragraph">Well, it wasn&#8217;t connected at first and then we started adding.</p>



<p class="wp-block-paragraph">Okay, so that&#8217;s basically okay.</p>



<p class="wp-block-paragraph">Let&#8217;s add direction to every single edge.</p>



<p class="wp-block-paragraph">So every single edge has to have a direction</p>



<p class="wp-block-paragraph">or every single edge has to have no direction.</p>



<p class="wp-block-paragraph">You can&#8217;t really mix and match.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to go through every single edge and add a direction like this.</p>



<p class="wp-block-paragraph">and you know to be fair I think I&#8217;ve said this in my other videos an undirected edge is kind of the</p>



<p class="wp-block-paragraph">same thing as a directed edge going in or two directed edges going in both directions so this</p>



<p class="wp-block-paragraph">3 and 13 what&#8217;s going on with my computer hello this 3 and 13 edge it would have kind of been the</p>



<p class="wp-block-paragraph">same as just having two edges going in opposite directions so maybe I&#8217;ll just do that right now</p>



<p class="wp-block-paragraph">for this huge weird edge I&#8217;m just going to put like an arrow over here instead of totally redrawing</p>



<p class="wp-block-paragraph">Then I double check that every single edge has a direction so that I have a valid graph.</p>



<p class="wp-block-paragraph">And now I can ask, is this connected or not?</p>



<p class="wp-block-paragraph">Actually, when it comes to a directed graph, you don&#8217;t really have connected or not anymore.</p>



<p class="wp-block-paragraph">You have strongly connected versus weakly connected.</p>



<p class="wp-block-paragraph">Maybe I should just separate these words and then just kind of like move them.</p>



<p class="wp-block-paragraph">So let&#8217;s get rid of this.</p>



<p class="wp-block-paragraph">to this save this over here move it down so what am i talking about um well first let me remove an</p>



<p class="wp-block-paragraph">edge real quick i&#8217;ll remove this edge right here so now we have two sub graphs in this graph you</p>



<p class="wp-block-paragraph">can tell for sure if this was an undirected graph it would not be connected but now that we&#8217;re</p>



<p class="wp-block-paragraph">dealing with direction we have to ask is it strongly connected versus weakly so what does</p>



<p class="wp-block-paragraph">can every node reach every other node if you obey the direction of the edges so that&#8217;s clearly not</p>



<p class="wp-block-paragraph">true there&#8217;s no edge at all that even connects this one and nine so it&#8217;s definitely not strongly</p>



<p class="wp-block-paragraph">connected weakly connected on the other hand means if you temporarily ignore the direction</p>



<p class="wp-block-paragraph">of the edges then could you consider the graph connected so if we pretended that there were no</p>



<p class="wp-block-paragraph">arrows here would we think that it&#8217;s a connected graph if the answer is yes then it&#8217;s at least a</p>



<p class="wp-block-paragraph">So this is not weakly connected because if this was an undirected graph, it&#8217;s not even connected at all.</p>



<p class="wp-block-paragraph">So that sucks.</p>



<p class="wp-block-paragraph">I&#8217;m going to go ahead and add another edge here.</p>



<p class="wp-block-paragraph">So I&#8217;m going to add an edge from 2 to 9.</p>



<p class="wp-block-paragraph">So the first thing is, oh, sorry, I have to add another edge.</p>



<p class="wp-block-paragraph">Let me do from 1 to 13 for fun.</p>



<p class="wp-block-paragraph">So now the first thing is, let&#8217;s check to see if this is a strongly connected graph.</p>



<p class="wp-block-paragraph">node is unreachable by any other node so this is definitely not strongly</p>



<p class="wp-block-paragraph">connected because all the other nodes would be would have to be able to reach</p>



<p class="wp-block-paragraph">too alright so first thing you could do just to very quickly check is just look</p>



<p class="wp-block-paragraph">at every single node and ask is it reachable you know by anything and then</p>



<p class="wp-block-paragraph">after that you can do things the hard way so it&#8217;s not strongly connected what</p>



<p class="wp-block-paragraph">about weakly connected if we ignore the direction of the graph sorry if we</p>



<p class="wp-block-paragraph">then yeah, we could reach every single node from every other node. Let&#8217;s just double check that</p>



<p class="wp-block-paragraph">real fast. I&#8217;m going to say, you know, the two node, if we&#8217;re ignoring direction, can it reach</p>



<p class="wp-block-paragraph">the one? Yes. Can it reach the 13? Yes. The three? Yes. Can it reach the nine? Yes. So that means the</p>



<p class="wp-block-paragraph">two node is sort of like weakly connected to every other node in the graph. Do the same thing for the</p>



<p class="wp-block-paragraph">one node. Previously, we just said, you know, check everything one by one or use a shortcut.</p>



<p class="wp-block-paragraph">one by one or use a shortcut. So I&#8217;m going to use the shortcut. The one node, if we ignore direction</p>



<p class="wp-block-paragraph">can reach the two nodes. So I&#8217;m going to say the one is weakly connected, at least everything else.</p>



<p class="wp-block-paragraph">I&#8217;m not even going to check to see if it&#8217;s strongly connected to everything else. I think the answer</p>



<p class="wp-block-paragraph">is no, but we do the same thing for the 13. The 13 can reach the one pretty fast. The three can</p>



<p class="wp-block-paragraph">reach the 13. The nine can reach the two. So every single node can weakly reach. That&#8217;s not an</p>



<p class="wp-block-paragraph">a weakly connected graph. Okay, cool. How can we make this a strongly connected graph? Well,</p>



<p class="wp-block-paragraph">we just have to add a bunch of edges. So that&#8217;s what I&#8217;m going to do. I&#8217;m going to kind of like</p>



<p class="wp-block-paragraph">look at what is missing here. So we&#8217;ll ask the two, can the two reach and let&#8217;s obey the direction</p>



<p class="wp-block-paragraph">of the edges again. Can the two reach the one? Yes. Can the two reach the 13? Yes. Can it reach</p>



<p class="wp-block-paragraph">Yes, so that means right now the two is strongly connected to everything else.</p>



<p class="wp-block-paragraph">So we&#8217;re on track for a strongly connected graph.</p>



<p class="wp-block-paragraph">We then look at the one node and we realize that the one node,</p>



<p class="wp-block-paragraph">it can reach the 13 and the three and it can also reach the nine,</p>



<p class="wp-block-paragraph">but it can&#8217;t reach the two.</p>



<p class="wp-block-paragraph">That&#8217;s one thing that was blocking us last time from saying strongly connected.</p>



<p class="wp-block-paragraph">So we just have to add another edge if we want this to be a strongly connected graph.</p>



<p class="wp-block-paragraph">We have to figure out how can we get the one to reach the two.</p>



<p class="wp-block-paragraph">the 2 we can go through something if we want to that&#8217;s fine we can also just</p>



<p class="wp-block-paragraph">write an edge that just goes back to the 2 but that&#8217;s I don&#8217;t know that&#8217;s kind of</p>



<p class="wp-block-paragraph">boring I&#8217;m gonna make an edge that goes from 1 to 9 and then I&#8217;m gonna make an</p>



<p class="wp-block-paragraph">edge that goes from 9 to 2 so looking at the 1 node it can now reach the 2 if we</p>



<p class="wp-block-paragraph">go through the 9 so that&#8217;s good can the 1 reach the 9 yes can the 1 reach the 13</p>



<p class="wp-block-paragraph">three yes okay so the one node can now reach every single other node so now we&#8217;re a little bit more</p>



<p class="wp-block-paragraph">on track for a strongly connected graph look at the 13 node can it reach the one node um actually</p>



<p class="wp-block-paragraph">yes if we go from 13 to 9 and then 2 and then 1 we can reach the one node since we know the one</p>



<p class="wp-block-paragraph">node is connected to everything else we can now say that the 13 node is connected to everything</p>



<p class="wp-block-paragraph">13 so i&#8217;ll say three is connected to everything the nine node can reach the two node which is</p>



<p class="wp-block-paragraph">connected to everything so that means the nine node is also connected to everything and at this</p>



<p class="wp-block-paragraph">point we now have a strongly connected graph it&#8217;s not weakly connected it&#8217;s stronger than that</p>



<p class="wp-block-paragraph">any questions um so let&#8217;s see we added a bunch of uh edges i think maybe this is actually all we</p>



<p class="wp-block-paragraph">need to do for this particular video because we&#8217;re just going to talk about connectedness</p>



<p class="wp-block-paragraph">going to talk about connectedness about 15 minutes not too bad all right so i think i&#8217;m</p>



<p class="wp-block-paragraph">going to leave it here drop a comment if you want to do way more complicated examples if you want to</p>



<p class="wp-block-paragraph">provide me a sample graph that&#8217;s awful and i could just i don&#8217;t know in the sometime in the future i</p>



<p class="wp-block-paragraph">could record a video about it um or just let me know your thoughts thank you so much for watching</p>



<p class="wp-block-paragraph">this video i hope you had a little bit of fun and i hope you learned a little bit of stuff i&#8217;m outie</p>



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



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



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



<p class="wp-block-paragraph">Whoa. Can you see that guy in the background?</p>



<p class="wp-block-paragraph">Oh, shoot.</p>



<p class="wp-block-paragraph">I&#8217;m just kidding.</p>



<p class="wp-block-paragraph">I set that as the screensaver.</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">please subscribe and follow this channel or these videos or whatever it is you do on the current</p>



<p class="wp-block-paragraph">social media website that you&#8217;re looking at right now. It would really mean the world to me and it&#8217;ll</p>



<p class="wp-block-paragraph">help make more videos and grow this community. So we&#8217;ll be able to do more videos, longer videos,</p>



<p class="wp-block-paragraph">better videos, or just I&#8217;ll be able to keep making videos in general. So please do me a kindness</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">I&#8217;m going to go ahead and get started.</p>



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



<p class="wp-block-paragraph">Yeah, I think I&#8217;m good.</p>



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



<p class="wp-block-paragraph">Let&#8217;s talk about whether a graph is connected or not,</p>



<p class="wp-block-paragraph">or maybe whether it&#8217;s strongly connected or weakly connected or not connected at all.</p>



<p class="wp-block-paragraph">You never know.</p>
<p>The post <a href="https://www.NeuralLantern.com/graph-connectedness-explained-connected-strongly-connected-and-weakly-connected/">Graph Connectedness Explained: Connected Strongly Connected and Weakly Connected</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/graph-connectedness-explained-connected-strongly-connected-and-weakly-connected/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>4 Graph Types: Undirected vs Directed, Weighted vs Unweighted</title>
		<link>https://www.NeuralLantern.com/4-graph-types-undirected-vs-directed-weighted-vs-unweighted/</link>
					<comments>https://www.NeuralLantern.com/4-graph-types-undirected-vs-directed-weighted-vs-unweighted/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 06 Jul 2026 10:05:07 +0000</pubDate>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Graphs]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[directed graph]]></category>
		<category><![CDATA[edges]]></category>
		<category><![CDATA[graph theory]]></category>
		<category><![CDATA[graph types]]></category>
		<category><![CDATA[graphs]]></category>
		<category><![CDATA[network topology]]></category>
		<category><![CDATA[nodes]]></category>
		<category><![CDATA[pathfinding]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[undirected graph]]></category>
		<category><![CDATA[unweighted graph]]></category>
		<category><![CDATA[vertices]]></category>
		<category><![CDATA[weighted graph]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=407</guid>

					<description><![CDATA[<p>A graph is basically a collection of nodes, also known as vertices, and edges, which are the lines that connect nodes to each other. The four types of graphs are undirected unweighted, undirected weighted, directed unweighted, and directed weighted. Graphs help represent relationships between things like computers in a network or cities and airports.</p>
<p>The post <a href="https://www.NeuralLantern.com/4-graph-types-undirected-vs-directed-weighted-vs-unweighted/">4 Graph Types: Undirected vs Directed, Weighted vs Unweighted</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="4 Graph Types: Undirected vs Directed, Weighted vs Unweighted" width="1380" height="776" src="https://www.youtube.com/embed/8Ml20VhpdrI?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 break down the four main types of graphs used in computer science: undirected unweighted, undirected weighted, directed unweighted, and directed weighted.</p>



<p class="wp-block-paragraph">We start with the basic idea of graphs &#8211; collections of nodes (vertices) and edges &#8211; and then explore each type with clear examples. Learn how these graphs represent real-world relationships like computer networks, airport routes, road maps, and game maps.</p>



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



<p class="wp-block-paragraph">00:00 Introduction to Graphs<br>00:19 Basic Graph Definition<br>00:48 Nodes and Vertices<br>01:01 Empty Graph Example<br>01:17 Graph Implementation Concepts<br>02:02 Node Data and Templates<br>03:22 Adding Nodes<br>04:00 Adding Edges<br>04:44 What Graphs Represent<br>05:36 Graph Use Cases<br>06:56 Real-World Examples<br>08:35 Four Types of Graphs<br>08:52 Undirected Unweighted Graph<br>10:40 Unweighted as Unit Weights<br>11:40 Undirected Weighted Graph<br>13:32 Weighted Graph Benefits<br>15:52 Directed Weighted Graph<br>19:50 Directed Unweighted Graph<br>20:32 Summary of Four Graph Types<br>21:19 Closing and Future Videos</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 four different types of graphs that you can use and how they relate to computer science.</p>



<p class="wp-block-paragraph">Okay, so you can see like I&#8217;ve got like a blank little graph paper up here.</p>



<p class="wp-block-paragraph">I&#8217;m going to talk about graphs and just the very basic idea of graphs in general</p>



<p class="wp-block-paragraph">and then show you the four different types of graphs that we have.</p>



<p class="wp-block-paragraph">about how to implement graphs inside the machine, time complexities, maybe graph algorithms,</p>



<p class="wp-block-paragraph">and a whole bunch of stuff, terminology, everything. Okay, so the first thing is, I guess I&#8217;m going to</p>



<p class="wp-block-paragraph">assume probably most of you have sort of seen a graph before and you kind of understand a little</p>



<p class="wp-block-paragraph">bit about it already, but I&#8217;ll say that a graph is basically a collection of nodes, also known as</p>



<p class="wp-block-paragraph">vertices or vertexes, and edges, which are the lines that connect nodes to each other. This screen</p>



<p class="wp-block-paragraph">See, this is a valid graph.</p>



<p class="wp-block-paragraph">I&#8217;m not talking about the graphing paper.</p>



<p class="wp-block-paragraph">It&#8217;s just an empty graph.</p>



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



<p class="wp-block-paragraph">So it&#8217;s still valid.</p>



<p class="wp-block-paragraph">It&#8217;s just blank.</p>



<p class="wp-block-paragraph">Okay, so then what I want to do is start drawing some nodes.</p>



<p class="wp-block-paragraph">What are the nodes again?</p>



<p class="wp-block-paragraph">Those are just vertices.</p>



<p class="wp-block-paragraph">Those are just like little data structure objects you can imagine in the code.</p>



<p class="wp-block-paragraph">I don&#8217;t want to get too far ahead of myself, but imagine that you have a class called graph.</p>



<p class="wp-block-paragraph">This is just one way to imagine.</p>



<p class="wp-block-paragraph">We have a class called graph and then a nested class inside of a graph called a node or a vertex</p>



<p class="wp-block-paragraph">If you want to be more fancy</p>



<p class="wp-block-paragraph">So the vertex class itself by the way</p>



<p class="wp-block-paragraph">This is not always going to be the way that you actually do this in your code the other representations that we&#8217;ll talk about sooner a little bit easier, but</p>



<p class="wp-block-paragraph">The vertex class itself needs to basically hold some data</p>



<p class="wp-block-paragraph">Depending on your implementation it might know about who it&#8217;s connected to maybe the graph knows that instead</p>



<p class="wp-block-paragraph">instead. In my implementations, the graph is going to know everything, but the vertex or the node</p>



<p class="wp-block-paragraph">itself needs to hold one piece of data. That&#8217;s kind of the whole point. So I&#8217;m going to say that</p>



<p class="wp-block-paragraph">the data type that these nodes hold are just integers. So I&#8217;m just going to put like a six in</p>



<p class="wp-block-paragraph">here just to indicate we have one node and the templated data type that it holds is an integer.</p>



<p class="wp-block-paragraph">And the specific integer for this specific node is just the number six. You can have any template</p>



<p class="wp-block-paragraph">any template type if you really want to you can people put floats in there you could put a custom</p>



<p class="wp-block-paragraph">custom class in there whatever you want but i just want to show you that we have a class called</p>



<p class="wp-block-paragraph">vertex or node and inside of it&#8217;s got a template type maybe just to make this a little bit more</p>



<p class="wp-block-paragraph">clear i&#8217;ll say you can imagine inside the vertex class uh there is a template type and we&#8217;ll call</p>



<p class="wp-block-paragraph">it uh i don&#8217;t know node node value type or something and we&#8217;ll call it data and then up</p>



<p class="wp-block-paragraph">I&#8217;m doing C++ up here.</p>



<p class="wp-block-paragraph">We have to kind of define the whole graph class as a template class.</p>



<p class="wp-block-paragraph">Now, of course, you could define the vertex class as the template class,</p>



<p class="wp-block-paragraph">but if you define the graph class as a template, then it all kind of falls through.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to say class node value type.</p>



<p class="wp-block-paragraph">And so if you&#8217;ve written template classes before in C++,</p>



<p class="wp-block-paragraph">now you know that the user can instance the graph and at the same time,</p>



<p class="wp-block-paragraph">specify what is the data type of the stuff that the nodes hold.</p>



<p class="wp-block-paragraph">the stuff that the nodes hold.</p>



<p class="wp-block-paragraph">We do the same thing for edges too,</p>



<p class="wp-block-paragraph">but notice how there are no nodes that are connected to each other.</p>



<p class="wp-block-paragraph">So we have no edges in this graph.</p>



<p class="wp-block-paragraph">This is still a valid graph,</p>



<p class="wp-block-paragraph">but we could just sort of copy a node just for fun and say,</p>



<p class="wp-block-paragraph">all right, we&#8217;ve got two nodes now, two vertices.</p>



<p class="wp-block-paragraph">They could have the same value if you want.</p>



<p class="wp-block-paragraph">Graphs are supposed to, you know, support duplicate values in the nodes,</p>



<p class="wp-block-paragraph">but I&#8217;m just going to change the value here.</p>



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



<p class="wp-block-paragraph">You don&#8217;t really need edges to have a valid graph.</p>



<p class="wp-block-paragraph">edges to have a valid graph. This is just something called a something called a graph that is not</p>



<p class="wp-block-paragraph">connected. We&#8217;ll talk about all that terminology and how to detect it in a future video. For now,</p>



<p class="wp-block-paragraph">I&#8217;m just going to say we have like a little graph here, which is kind of boring, but I&#8217;m going to</p>



<p class="wp-block-paragraph">add an edge. An edge is basically just, you know, a little mini data structure that connects two</p>



<p class="wp-block-paragraph">nodes. Depending on the way you represent this in the machine, which we&#8217;ll talk about in future</p>



<p class="wp-block-paragraph">well, a whole bunch of different ways.</p>



<p class="wp-block-paragraph">You could imagine this as a full-fledged class,</p>



<p class="wp-block-paragraph">although that&#8217;s not always going to be the way that we represent an actual edge.</p>



<p class="wp-block-paragraph">So imagine that we have a nested class called edge.</p>



<p class="wp-block-paragraph">And inside the edge class, we&#8217;ve got, you know, some stuff.</p>



<p class="wp-block-paragraph">We could have a weight, which means the cost of traveling against the edge.</p>



<p class="wp-block-paragraph">We could have many other properties.</p>



<p class="wp-block-paragraph">For now, I&#8217;ll just try to keep it simple.</p>



<p class="wp-block-paragraph">is pretty much a collection of vertices and edges and some sort of connection.</p>



<p class="wp-block-paragraph">What is a graph really for?</p>



<p class="wp-block-paragraph">Some people at first think that a graph is another way to represent a database.</p>



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



<p class="wp-block-paragraph">Graphs, you could sort of use them as databases,</p>



<p class="wp-block-paragraph">but they&#8217;re not very good as databases because</p>



<p class="wp-block-paragraph">it would be really, really slow to search through the graph</p>



<p class="wp-block-paragraph">to find some piece of data that you&#8217;re looking for.</p>



<p class="wp-block-paragraph">It could be like a linear time database.</p>



<p class="wp-block-paragraph">better would be to take this graph and add a bunch of rules as you probably have found</p>



<p class="wp-block-paragraph">in my other videos by now and turn it into a binary search tree.</p>



<p class="wp-block-paragraph">A binary search tree is lightning fast.</p>



<p class="wp-block-paragraph">It runs in log time.</p>



<p class="wp-block-paragraph">Hash tables are faster than binary search trees, but basically a graph is always going</p>



<p class="wp-block-paragraph">to be way slower than a binary search tree unless the binary search tree is severely</p>



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



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



<p class="wp-block-paragraph">Anyway, so we have a graph and it&#8217;s not a good database.</p>



<p class="wp-block-paragraph">What would a graph be good for?</p>



<p class="wp-block-paragraph">would a graph be good for? Well, graphs are usually pretty good to help you represent</p>



<p class="wp-block-paragraph">relationships between things. For example, the six and the one, imagine the six is something,</p>



<p class="wp-block-paragraph">maybe it&#8217;s a computer, maybe it&#8217;s a city, maybe it&#8217;s, I don&#8217;t know, like a train station,</p>



<p class="wp-block-paragraph">and the edges could represent connections. So I&#8217;m just going to start changing these values</p>



<p class="wp-block-paragraph">have some nodes just kind of hanging out off to the side, not really connected to anything.</p>



<p class="wp-block-paragraph">Maybe not great for your particular use case, but like it&#8217;s a valid graph. So I&#8217;m just going to,</p>



<p class="wp-block-paragraph">I&#8217;m just going to connect this and try to make this into a better graph.</p>



<p class="wp-block-paragraph">That&#8217;s a little bit more interesting. So we&#8217;ve got graphs that do a whole bunch of different</p>



<p class="wp-block-paragraph">things. Let me change these values real fast. I&#8217;ll maybe turn that into a 13 and turn that</p>



<p class="wp-block-paragraph">So imagine that we have like a little connection here, a little connection here, maybe another</p>



<p class="wp-block-paragraph">connection here and another one there.</p>



<p class="wp-block-paragraph">And you know, you are allowed to have connections that sort of look weird, like going from this</p>



<p class="wp-block-paragraph">six all the way around to this 13.</p>



<p class="wp-block-paragraph">That&#8217;s totally valid also.</p>



<p class="wp-block-paragraph">So I&#8217;ve basically just drawn a random graph, random nodes and random edges.</p>



<p class="wp-block-paragraph">Imagine that every single node here is a computer in a computer network and imagine every edge</p>



<p class="wp-block-paragraph">every edge which connects the nodes is just some sort of a connection let&#8217;s say a network cable</p>



<p class="wp-block-paragraph">or a wi-fi signal or something like that so if you imagine it that way then this graph could</p>



<p class="wp-block-paragraph">represent a network topology a bunch of computers connected together if these were cities and this</p>



<p class="wp-block-paragraph">is like actually maybe these are airports in cities you could imagine a graph could represent</p>



<p class="wp-block-paragraph">the relationship between different airports in which planes will fly notice on this graph just</p>



<p class="wp-block-paragraph">that the uh you know the uh the city number one uh there are no flights connecting it directly to</p>



<p class="wp-block-paragraph">city 44 but you could probably imagine well if we take a flight to uh from city one to city 10 and</p>



<p class="wp-block-paragraph">then from city 10 to 44 then we can get from one to 44 we just have to be clever about our connecting</p>



<p class="wp-block-paragraph">flights um let&#8217;s see so we could do like road maps airport maps the worldwide web is basically a graph</p>



<p class="wp-block-paragraph">at least from a certain perspective electrical circuits that are connected to each other</p>



<p class="wp-block-paragraph">are connected to each other even game maps what if your character is allowed to stand here in</p>



<p class="wp-block-paragraph">in space number three on some map and the character is only allowed to travel to space 13 or</p>



<p class="wp-block-paragraph">space 6 but not directly to space 1 well it could be a map that helps you understand where your</p>



<p class="wp-block-paragraph">character can go in a game okay so that&#8217;s like the basic idea of of what a graph is and uh how it</p>



<p class="wp-block-paragraph">actual four types of graphs that I&#8217;m going to go over in more detail in future videos. Let&#8217;s see,</p>



<p class="wp-block-paragraph">what time is it right now? We got like 10 minutes. Okay, so I can still kind of make this fast.</p>



<p class="wp-block-paragraph">So the first type of graph that we&#8217;re looking at right now is actually something called an</p>



<p class="wp-block-paragraph">undirected, unweighted graph. Why is it, let&#8217;s see, let me write this down first. It&#8217;s an undirected,</p>



<p class="wp-block-paragraph">undirected, unweighted graph. So why is this undirected? Because the edges don&#8217;t have direction.</p>



<p class="wp-block-paragraph">If I was standing in, you know, city six and I wanted to like drive or fly or whatever</p>



<p class="wp-block-paragraph">from city six to city three, then I should be able to do it because this edge that connects</p>



<p class="wp-block-paragraph">them doesn&#8217;t really say that I have to go in one direction or the other. I could also go to city</p>



<p class="wp-block-paragraph">three and then just walk right back or fly right back to city six. There&#8217;s no direction. So there&#8217;s</p>



<p class="wp-block-paragraph">no direction. So there&#8217;s an this that&#8217;s that&#8217;s why this is an undirected graph. This is an</p>



<p class="wp-block-paragraph">unweighted graph because we haven&#8217;t specified a cost for traveling along an edge. For example,</p>



<p class="wp-block-paragraph">if you know, if you&#8217;re at city three here, and you want to fly to city 13, how much jet fuel</p>



<p class="wp-block-paragraph">does it cost? How much time does it cost? If three is a computer and 13 is a computer,</p>



<p class="wp-block-paragraph">and the edges like a network cable, like how much money does it cost to transmit the data?</p>



<p class="wp-block-paragraph">transmit the data or what is the lag in milliseconds.</p>



<p class="wp-block-paragraph">You know, you can represent all sorts of things with these edges,</p>



<p class="wp-block-paragraph">but there&#8217;s no value on these edges,</p>



<p class="wp-block-paragraph">which means there are no weights to these edges.</p>



<p class="wp-block-paragraph">So that&#8217;s why this is an undirected and also unweighted graph.</p>



<p class="wp-block-paragraph">Uh, the first upgrade that I want to do is just kind of show you what you can</p>



<p class="wp-block-paragraph">sort of assume about an unweighted graph. This is not always true.</p>



<p class="wp-block-paragraph">It just depends on who you talk to and what your application implementations are.</p>



<p class="wp-block-paragraph">So imagine the node that goes from six, sorry, the edge that goes from six to one.</p>



<p class="wp-block-paragraph">If it&#8217;s an unweighted graph, you could just probably imagine that every weight is just a one.</p>



<p class="wp-block-paragraph">The point being that it always costs the same amount to travel along every edge,</p>



<p class="wp-block-paragraph">because in an unweighted graph, we don&#8217;t differentiate between the different edges</p>



<p class="wp-block-paragraph">in terms of their costs.</p>



<p class="wp-block-paragraph">We could just put a bunch of ones on all these edges.</p>



<p class="wp-block-paragraph">And it would still, you know, basically be considered an unweighted graph.</p>



<p class="wp-block-paragraph">It depends on who you talk to.</p>



<p class="wp-block-paragraph">maybe all the weights are zero, maybe all the weights are 10 or whatever.</p>



<p class="wp-block-paragraph">But, you know, usually I put a one there because I&#8217;d like to imagine the cost could just be the number of hops.</p>



<p class="wp-block-paragraph">So, for example, if I wanted to go from city six to city 10,</p>



<p class="wp-block-paragraph">I could probably, you know, start at city six and then go to city one and then hop to city 10.</p>



<p class="wp-block-paragraph">How many actual hops did I do? I did two.</p>



<p class="wp-block-paragraph">I could also come up with that number by just looking at the sum of all the weights of all the edges.</p>



<p class="wp-block-paragraph">all the edges. So notice how when I go from six to one, that costs me one weight or one hop. And</p>



<p class="wp-block-paragraph">then when I go from one to 10, that&#8217;s another one. So if you put ones on all the edges in an</p>



<p class="wp-block-paragraph">unweighted graph, then you&#8217;re kind of saying the total cost of some path is really just the number</p>



<p class="wp-block-paragraph">of hops. So it&#8217;s up to you. Anyway, that&#8217;s unweighted and undirected. Let&#8217;s upgrade this</p>



<p class="wp-block-paragraph">see we have an undirected and unweighted and i&#8217;m just going to duplicate this real fast and change</p>



<p class="wp-block-paragraph">this to a weighted graph so undirected but also weighted and the thing is as soon as you start</p>



<p class="wp-block-paragraph">putting numbers on a graph on the edges of a graph well then it&#8217;s considered weighted so even even a</p>



<p class="wp-block-paragraph">graph that has all ones on all the edges you could also call that weighted but now you have the</p>



<p class="wp-block-paragraph">freedom since it is considered a weighted graph to just assign different values to all the edges</p>



<p class="wp-block-paragraph">to all the edges. Again, these edges are a class sometimes, or you can think of them that way</p>



<p class="wp-block-paragraph">inside the graph. And the edge weights themselves are definitely considered a T type, a templated</p>



<p class="wp-block-paragraph">type, which means, you know, right now I&#8217;m clearly typing integers, but you could type some other</p>



<p class="wp-block-paragraph">data type. You could type floats. You could type some weird data type, like a string.</p>



<p class="wp-block-paragraph">Whether or not that&#8217;s going to be useful depends on your program, you know, what you&#8217;re trying to</p>



<p class="wp-block-paragraph">you know, what you&#8217;re trying to do with the graph. But let&#8217;s see, I think I&#8217;ve added custom numbers</p>



<p class="wp-block-paragraph">to every single edge. So now this is definitely an undirected, but weighted graph. That&#8217;s the</p>



<p class="wp-block-paragraph">second type of graph that we&#8217;re talking about. Okay, what&#8217;s what&#8217;s cool about a weighted graph?</p>



<p class="wp-block-paragraph">Well, imagine again, that this is an airport map, where we&#8217;re representing relationships between,</p>



<p class="wp-block-paragraph">you know, cities, like where can we fly and stuff. We&#8217;re basically saying at this point,</p>



<p class="wp-block-paragraph">it&#8217;s going to cost you 44 of something.</p>



<p class="wp-block-paragraph">Maybe this is a graph representing jet fuel or time or frequent flyer points or something like that.</p>



<p class="wp-block-paragraph">If you consider your edges to be custom classes,</p>



<p class="wp-block-paragraph">you could even put a whole bunch of different types of values in your graph at the same time</p>



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



<p class="wp-block-paragraph">But for now, we&#8217;ll just say there&#8217;s one templated type,</p>



<p class="wp-block-paragraph">which means if we go back up to this code real fast, where was that code at?</p>



<p class="wp-block-paragraph">Where was that code at?</p>



<p class="wp-block-paragraph">And we had, you know, our rough idea of, you know, what the, what the graph holds, you</p>



<p class="wp-block-paragraph">know, it holds like vertices and edges.</p>



<p class="wp-block-paragraph">We could upgrade this just a little bit to say that there is a, an edge value type.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say class edge value, edge weight type, something like that.</p>



<p class="wp-block-paragraph">for the edge so that I can save characters. And then here, I&#8217;ll change this to a type for the</p>



<p class="wp-block-paragraph">node. This is why we call it T type. There&#8217;s like, we put the keyword template here in C++.</p>



<p class="wp-block-paragraph">And then sometimes people just use T&#8217;s here for these tokens. So I&#8217;m just going to say</p>



<p class="wp-block-paragraph">the type of data that a node can hold is going to be the T node. And then the type of data that</p>



<p class="wp-block-paragraph">the edge could hold would be the T edge. We&#8217;ll call that data. Or we could call that weight,</p>



<p class="wp-block-paragraph">because usually that data comes into play when you&#8217;re traveling along an edge, right? So there&#8217;s</p>



<p class="wp-block-paragraph">traveling along an edge, right? So there&#8217;s like two different types here. Anyway, so we got an</p>



<p class="wp-block-paragraph">undirected and unweighted graph. If we traveled from city six to city one, that costs us 44. But</p>



<p class="wp-block-paragraph">if you look at the graph a little bit more closely, we could take the longer way around. We could say</p>



<p class="wp-block-paragraph">go from six to three, and then go from three to 13. And then actually, let me change the color</p>



<p class="wp-block-paragraph">to orange so that you can see there&#8217;s different paths, six to three, and then three to 13,</p>



<p class="wp-block-paragraph">and then 13 to one, even though we took more hops,</p>



<p class="wp-block-paragraph">and that might have been considered more expensive</p>



<p class="wp-block-paragraph">in an unweighted graph,</p>



<p class="wp-block-paragraph">you can see that the cost of the first path</p>



<p class="wp-block-paragraph">from six to one, that&#8217;s 44.</p>



<p class="wp-block-paragraph">And the cost of the second path is seven plus three,</p>



<p class="wp-block-paragraph">that&#8217;s 10, and then plus nine, so that&#8217;s 19.</p>



<p class="wp-block-paragraph">So if we take the long way,</p>



<p class="wp-block-paragraph">it actually costs us less</p>



<p class="wp-block-paragraph">of whatever we&#8217;ve determined this unit is.</p>



<p class="wp-block-paragraph">You know, less jet fuel, less time, less whatever.</p>



<p class="wp-block-paragraph">less whatever these graphs in the machine are not necessarily going to be to scale because the</p>



<p class="wp-block-paragraph">machine in your computer your computer doesn&#8217;t really keep track of like the physical positioning</p>



<p class="wp-block-paragraph">of all these nodes and edges it just can keeps track of the relationship between the nodes and</p>



<p class="wp-block-paragraph">the edges and the weights and all that stuff so two different paths that we could take to get</p>



<p class="wp-block-paragraph">from six to one one of the paths is less expensive um or less time costly or just whatever and a graph</p>



<p class="wp-block-paragraph">data structure can kind of help us figure that out if we&#8217;re using an algorithm that understands</p>



<p class="wp-block-paragraph">weights okay so undirected and unweighted let&#8217;s add direction so um i&#8217;m going to erase this stuff</p>



<p class="wp-block-paragraph">right here real quick and in an undirected graph remember you can just kind of go in either</p>



<p class="wp-block-paragraph">direction along any edge but in a directed graph every single edge needs to have a direction so as</p>



<p class="wp-block-paragraph">I change one of the edges to have direction, then I must change all of the edges to have direction</p>



<p class="wp-block-paragraph">as well. So I&#8217;m just saying, okay, as soon as I did that, now this is an invalid graph</p>



<p class="wp-block-paragraph">because some of the edges don&#8217;t have direction. So now I need to fix everything. So I just,</p>



<p class="wp-block-paragraph">I&#8217;m going to arbitrarily and randomly choose a direction for every single edge like this.</p>



<p class="wp-block-paragraph">And then as soon as I&#8217;m done, I&#8217;m going to deal, I guess, with that giant edge</p>



<p class="wp-block-paragraph">that giant edge. So I&#8217;m going to go like that. So let&#8217;s see, all the edges seem to have direction.</p>



<p class="wp-block-paragraph">Now I&#8217;m just going to hand draw an arrow on one side of this giant edge. Okay. So this is directed</p>



<p class="wp-block-paragraph">and also weighted. What can we do with the directed graph? Well, it just basically tells</p>



<p class="wp-block-paragraph">you which direction you&#8217;re allowed to travel in. What if you have an airport where city six can</p>



<p class="wp-block-paragraph">only fly to city one, but city one, for some reason, never flies to city six. You got to take</p>



<p class="wp-block-paragraph">take like the long way you got to go from city one to 13 to three to six that&#8217;s something that</p>



<p class="wp-block-paragraph">a graph could help you represent what if your character in your game can only go in one direction</p>



<p class="wp-block-paragraph">but not the other direction okay no problem i mean that&#8217;s pretty much it you&#8217;re only allowed</p>



<p class="wp-block-paragraph">to travel in the same direction as the edge points but now we have a graph that is a lot less</p>



<p class="wp-block-paragraph">flexible because for example before we used to be able to go from city uh 10 to city 44 and then</p>



<p class="wp-block-paragraph">then come back again later if we wanted in the opposite direction. We can no longer do that</p>



<p class="wp-block-paragraph">because this edge only goes to 10. It doesn&#8217;t even go to 44. We can&#8217;t even ever actually get to 44.</p>



<p class="wp-block-paragraph">How do you sort of solve that? Well, just add another edge in the opposite direction.</p>



<p class="wp-block-paragraph">In graphs, you are allowed to do something called a parallel edge,</p>



<p class="wp-block-paragraph">which just basically means, you know, two edges between the same pair of nodes that just point</p>



<p class="wp-block-paragraph">in the same direction. You&#8217;re also allowed to do, you know, opposite facing edges. So</p>



<p class="wp-block-paragraph">edges so i just added a second edge there going from 10 to 44 and now if i was you know hanging</p>



<p class="wp-block-paragraph">out in city 10 i could just go back and forth between city 10 and 44 as much as i wanted to</p>



<p class="wp-block-paragraph">oh i forgot to add an edge weight for that new edge which means i wrote down an invalid graph</p>



<p class="wp-block-paragraph">so i&#8217;m just going to say two so uh maybe the wind is blowing in in your favor when you&#8217;re going from</p>



<p class="wp-block-paragraph">city 10 to city 44 maybe it&#8217;s blowing against you if you&#8217;re talking about city 44 to city 10 you</p>



<p class="wp-block-paragraph">you know whatever but you just have to have edge uh edge weight on every single edge</p>



<p class="wp-block-paragraph">okay so uh that&#8217;s directed and also weighted um and you can probably imagine previously when we</p>



<p class="wp-block-paragraph">had an undirected um graph when there was just like you know one edge going between uh two nodes</p>



<p class="wp-block-paragraph">and there was no direction on it like you know 44 to 10 if we just had like one edge with no</p>



<p class="wp-block-paragraph">have two edges each going in the opposite direction so we have directed and weighted i think the last</p>



<p class="wp-block-paragraph">thing that we didn&#8217;t do was directed and unweighted right so we have let me just double check real</p>



<p class="wp-block-paragraph">fast here we have undirected and unweighted and then we had undirected and weighted and then</p>



<p class="wp-block-paragraph">directed and weighted okay so now let&#8217;s do um i&#8217;m getting lost already oh my gosh i can&#8217;t believe i</p>



<p class="wp-block-paragraph">I saw undirected and unweighted so we did undirected twice already undirected so we&#8217;re</p>



<p class="wp-block-paragraph">going to do directed and unweighted next okay so the next graph a little bit less useful than</p>



<p class="wp-block-paragraph">what we&#8217;re looking at this this type of graph is a little bit more useful we&#8217;re going to do</p>



<p class="wp-block-paragraph">directed and unweighted oh my god okay directed and unweighted so directed and unweighted you</p>



<p class="wp-block-paragraph">Directed and unweighted, you can imagine, just means we get rid of all these weights.</p>



<p class="wp-block-paragraph">And then we keep the direction on all the edges.</p>



<p class="wp-block-paragraph">So this means we can keep track of where we&#8217;re allowed to go, if we&#8217;re pathfinding or just whatever.</p>



<p class="wp-block-paragraph">But we don&#8217;t really know what it costs to travel between the different graphs.</p>



<p class="wp-block-paragraph">So that&#8217;s directed and unweighted.</p>



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



<p class="wp-block-paragraph">I&#8217;m going to just duplicate this or just make a new blank slide.</p>



<p class="wp-block-paragraph">Okay, undirected, oh gosh, I&#8217;ve got pink letters.</p>



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



<p class="wp-block-paragraph">And we&#8217;ve got undirected plus weighted.</p>



<p class="wp-block-paragraph">And then we&#8217;ve got directed plus unweighted.</p>



<p class="wp-block-paragraph">I don&#8217;t think I&#8217;m writing these down in the same order,</p>



<p class="wp-block-paragraph">but that&#8217;s okay.</p>



<p class="wp-block-paragraph">And then we&#8217;ve got directed plus weighted.</p>



<p class="wp-block-paragraph">So basically, you know, four types of graphs</p>



<p class="wp-block-paragraph">that we can talk about.</p>



<p class="wp-block-paragraph">In future videos, we&#8217;re going to go over graphs</p>



<p class="wp-block-paragraph">in more detail.</p>



<p class="wp-block-paragraph">We&#8217;ll talk about graph terminology,</p>



<p class="wp-block-paragraph">maybe some pathfinding algorithms.</p>



<p class="wp-block-paragraph">And yeah, so this is just a basic intro.</p>



<p class="wp-block-paragraph">How much time do we spend already?</p>



<p class="wp-block-paragraph">Oh God, we spent so much time.</p>



<p class="wp-block-paragraph">Spent like 22 minutes.</p>



<p class="wp-block-paragraph">Dang, man, even the smallest thing takes time.</p>



<p class="wp-block-paragraph">So I&#8217;ll see you in a future video</p>



<p class="wp-block-paragraph">when we talk more and more and more about graphs</p>



<p class="wp-block-paragraph">and other data structures concepts.</p>



<p class="wp-block-paragraph">Thanks for watching this video.</p>



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



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



<p class="wp-block-paragraph">I&#8217;m out of here.</p>



<p class="wp-block-paragraph">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 that you&#8217;re looking at right now,</p>



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



<p class="wp-block-paragraph">And it&#8217;ll help make more videos and grow this community.</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.</p>



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">which I think is also named somewhere at the bottom of this video and</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">and if you have a suggestion for clarifications or errata or just future</p>



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



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



<p class="wp-block-paragraph">up for those in the middle of the night I get I wake up in a cold sweat 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">enjoy the cool music as as I fade into the darkness which is coming for us all.</p>



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



<p class="wp-block-paragraph">I&#8217;m going to go ahead and get started.</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 the four types of…</p>



<p class="wp-block-paragraph">Hello there.</p>
<p>The post <a href="https://www.NeuralLantern.com/4-graph-types-undirected-vs-directed-weighted-vs-unweighted/">4 Graph Types: Undirected vs Directed, Weighted vs Unweighted</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/4-graph-types-undirected-vs-directed-weighted-vs-unweighted/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AVL Trees: Adding Linear Data and Performing Rotations</title>
		<link>https://www.NeuralLantern.com/avl-trees-adding-linear-data-and-performing-rotations/</link>
					<comments>https://www.NeuralLantern.com/avl-trees-adding-linear-data-and-performing-rotations/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sat, 27 Jun 2026 21:16:58 +0000</pubDate>
				<category><![CDATA[AVL Trees]]></category>
		<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[AVL rotation example]]></category>
		<category><![CDATA[AVL tree]]></category>
		<category><![CDATA[AVL trees]]></category>
		<category><![CDATA[balance factor]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[binary search tree insertion]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[data structures tutorial]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[self balancing tree]]></category>
		<category><![CDATA[tree rotations]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=396</guid>

					<description><![CDATA[<p>In this AVL tree example we add linear data starting with 12 then 21 30 38 42 and 55. After each insertion we update balance factors and when we hit imbalance we select the XYZ trinode and perform rotations to restore the AVL property. The tree stays balanced despite the bad input data.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-trees-adding-linear-data-and-performing-rotations/">AVL Trees: Adding Linear Data and Performing Rotations</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="AVL Trees: Adding Linear Data and Performing Rotations" width="1380" height="776" src="https://www.youtube.com/embed/6kasiB918fE?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Watch me build an AVL tree step by step with linear data that would normally create a terrible unbalanced BST. See insertions, balance factor calculations, and rotations in action.</p>



<p class="wp-block-paragraph">Perfect follow-up to my BST and AVL intro videos.</p>



<p class="wp-block-paragraph">00:00 Introduction to AVL Tree with Bad Data<br>00:14 Previous Videos on BST and AVL Trees<br>00:28 Practice Example Building Step by Step<br>00:50 Adding First Node 12<br>01:11 Adding Node 21<br>02:10 Adding Node 30 and First Rotation<br>04:03 Recomputing Balance Factors<br>05:08 Adding Node 38<br>08:03 Adding Node 42 and Second Rotation<br>12:51 Recomputing Balance After Rotation<br>14:09 Adding Node 55 and Third Rotation<br>18:55 Placing Unaccounted Nodes<br>19:21 Final Tree and Balance Factors<br>20:07 Conclusion and Thanks</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">Hey there! Let&#8217;s build an AVL self-balancing binary search tree with some really bad data.</p>



<p class="wp-block-paragraph">It&#8217;s all going to work out, trust me.</p>



<p class="wp-block-paragraph">Okay, so hopefully by now you&#8217;ve seen my other videos where we talked about how to define binary</p>



<p class="wp-block-paragraph">search trees, how to build them, search through them, add, delete, all that stuff. And also AVL</p>



<p class="wp-block-paragraph">trees. What are rotations? Why do we rotate? How does the AVL tree maintain itself? And so forth.</p>



<p class="wp-block-paragraph">just going to be a practice example. In the previous video that I that I</p>



<p class="wp-block-paragraph">recorded regarding AVL trees, I started off with a really long and gnarly linear</p>



<p class="wp-block-paragraph">tree and then I suddenly turned on AVLness and we rotated the crap out of</p>



<p class="wp-block-paragraph">it for about 30 minutes until it was really really good. So now I&#8217;m just</p>



<p class="wp-block-paragraph">going to build a tree one step at a time. Suppose for the sake of argument that</p>



<p class="wp-block-paragraph">you have some really bad data. You know a long time ago, I love telling this story.</p>



<p class="wp-block-paragraph">time ago you know my grandma before she passed on she was kind of bitter and she had some enemies in</p>



<p class="wp-block-paragraph">the neighborhood she used to call the cops on her neighbors for gossip um she had a neighbor who</p>



<p class="wp-block-paragraph">kind of feuded with her and at one point according to my grandma anyway uh my neighbor like didn&#8217;t</p>



<p class="wp-block-paragraph">like my grandma&#8217;s tree and so one day when my grandma was looking out into the backyard</p>



<p class="wp-block-paragraph">through the back window because she likes to yell at ducks who are swimming in her pool</p>



<p class="wp-block-paragraph">notice that the other little old lady&#8217;s arm just kind of went over my grandma&#8217;s fence</p>



<p class="wp-block-paragraph">with a spray bottle and then sprayed her tree. And then my grandma&#8217;s tree died. So this kind of</p>



<p class="wp-block-paragraph">thing I like to call, you know, tree poisoning or data poisoning or bad data, it can affect a binary</p>



<p class="wp-block-paragraph">search tree and make it really, really, really slow. But in AVL trees, if we have bad data,</p>



<p class="wp-block-paragraph">it should actually end up being fine. So I&#8217;m just going to say we&#8217;re poisoning our grandma&#8217;s tree</p>



<p class="wp-block-paragraph">grandma&#8217;s tree with some bad data. I&#8217;ll just do maybe maybe five pieces of data. We could go</p>



<p class="wp-block-paragraph">further than that but it&#8217;s it&#8217;s going to be a super long video if we do. So we&#8217;re going to add</p>



<p class="wp-block-paragraph">these pieces of data one by one. So that means the first thing I&#8217;m going to do remember we are kind</p>



<p class="wp-block-paragraph">of just regularly you know building a regular binary search tree. It&#8217;s just that the differences</p>



<p class="wp-block-paragraph">after we modify the topology in some way we&#8217;ll just check to see that it satisfies the rules of</p>



<p class="wp-block-paragraph">then we&#8217;re going to start performing rotations to make sure that it&#8217;s self-balancing.</p>



<p class="wp-block-paragraph">So we&#8217;ll add the 12 first.</p>



<p class="wp-block-paragraph">So let me see if I can draw a little arrow just indicating,</p>



<p class="wp-block-paragraph">yeah, we&#8217;re going to add this number right now.</p>



<p class="wp-block-paragraph">I&#8217;m going to do an ellipse for a node.</p>



<p class="wp-block-paragraph">And you know, like for binary, what is that?</p>



<p class="wp-block-paragraph">Flying saucer for binary search trees.</p>



<p class="wp-block-paragraph">It&#8217;s like, well, if there is no data already, if the tree is empty,</p>



<p class="wp-block-paragraph">first piece of data you add is going to end up being the root node. So that&#8217;s it. We have our</p>



<p class="wp-block-paragraph">root node here. See my other videos if you want more practice on building trees. So we&#8217;re just</p>



<p class="wp-block-paragraph">going to call that the root node. And then we&#8217;re going to compute its balance factor, which is</p>



<p class="wp-block-paragraph">obviously just going to be a zero. So that was super easy. Now we&#8217;re going to move on to adding</p>



<p class="wp-block-paragraph">the next piece of data. Things are getting a little bit worse. This is going to be a linear tree.</p>



<p class="wp-block-paragraph">tree if it weren&#8217;t for our trusty I guess AVL functionality so the 21 I&#8217;m</p>



<p class="wp-block-paragraph">gonna maybe just duplicate this node right now and set that to a 21 and then</p>



<p class="wp-block-paragraph">we&#8217;ll figure out where does the 21 go well it can&#8217;t be the root because the</p>



<p class="wp-block-paragraph">root is already occupied 21 is larger than 12 so it&#8217;s got to be on the right</p>



<p class="wp-block-paragraph">side so the 21 is going to be the right child of the root node so I&#8217;m just going</p>



<p class="wp-block-paragraph">Connecting line right here.</p>



<p class="wp-block-paragraph">And then we have to recompute balance factors.</p>



<p class="wp-block-paragraph">So the 21 is the thing we just added.</p>



<p class="wp-block-paragraph">We have to compute that.</p>



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



<p class="wp-block-paragraph">So we see the root node.</p>



<p class="wp-block-paragraph">It now has a balance factor of 1.</p>



<p class="wp-block-paragraph">So it got a little bit worse.</p>



<p class="wp-block-paragraph">But we don&#8217;t see 2 or worse.</p>



<p class="wp-block-paragraph">So that means this is currently a valid AVL tree.</p>



<p class="wp-block-paragraph">And we don&#8217;t have to do any rotations.</p>



<p class="wp-block-paragraph">So now it&#8217;s time to add the 30.</p>



<p class="wp-block-paragraph">just realized I could just upload like a hundred videos like this and I think you&#8217;d all be pretty</p>



<p class="wp-block-paragraph">happy or some people would be pretty happy um I don&#8217;t know if I want to do that to myself maybe</p>



<p class="wp-block-paragraph">I will so we&#8217;re going to add the 30 node uh so first I&#8217;m just going to duplicate some node</p>



<p class="wp-block-paragraph">and I&#8217;m going to call it 30 then we have to figure out where the 30 node belongs uh it&#8217;s not going</p>



<p class="wp-block-paragraph">to be the root node that&#8217;s already occupied 30 belongs on the right side of 12 but we can&#8217;t use</p>



<p class="wp-block-paragraph">of 12 but we can&#8217;t use that right child it&#8217;s occupied 30 belongs on the right side of 21</p>



<p class="wp-block-paragraph">so you know it makes sense we called this linear data that would make a linear tree so everything</p>



<p class="wp-block-paragraph">ended up being on the right side so far it&#8217;s just getting worse and worse but now the ablness is</p>



<p class="wp-block-paragraph">going to kind of come to the rescue we have to recompute the balance factors so the 30 has a 0</p>



<p class="wp-block-paragraph">the 21 ends up having a 1 again if you want practice computing balance factors see my other</p>



<p class="wp-block-paragraph">The 12 has a balance factor of 2 and now we know right away this is not a valid ABL tree anymore</p>



<p class="wp-block-paragraph">because we have a balance factor that is bad. It&#8217;s too bad.</p>



<p class="wp-block-paragraph">So we have to now select an XYZ trinode subtree so we can perform a rotation.</p>



<p class="wp-block-paragraph">I&#8217;m sure you can imagine it&#8217;s just going to end up being three nodes that are perfectly balanced like that.</p>



<p class="wp-block-paragraph">But let&#8217;s do it one step at a time.</p>



<p class="wp-block-paragraph">So the Z node is going to be the 12.</p>



<p class="wp-block-paragraph">That was the first node that we noticed that was out of whack or like I guess the highest node.</p>



<p class="wp-block-paragraph">or like I guess the highest node of the lowest nodes.</p>



<p class="wp-block-paragraph">And then we have to find Y by taking a child of Z.</p>



<p class="wp-block-paragraph">There&#8217;s only one possible child we could choose, so that&#8217;s going to be Y.</p>



<p class="wp-block-paragraph">And then we find X by taking a child of Y.</p>



<p class="wp-block-paragraph">There&#8217;s only one choice again, so it&#8217;s just going to be there.</p>



<p class="wp-block-paragraph">Now we have X, Y, and Z, and I&#8217;m going to write it down real fast.</p>



<p class="wp-block-paragraph">Let me just put this in black ink.</p>



<p class="wp-block-paragraph">So X is 30, and Y is 21, and Z is 12.</p>



<ol start="12" class="wp-block-list">
<li>Then we&#8217;re going to do A and B and C, which are going to be just in order representations of</li>
</ol>



<p class="wp-block-paragraph">XYZ. So again, think in your code, you would be doing pointers, you&#8217;d probably want to make three</p>



<p class="wp-block-paragraph">more pointers, call them ABC. If you had a rotation function, the rotation function,</p>



<p class="wp-block-paragraph">after you selected three nodes to rotate, it would receive XYZ. And then you say, let&#8217;s reorder,</p>



<p class="wp-block-paragraph">you know, XYZ and call them ABC. So A is going to be 12, and B is going to be 21,</p>



<p class="wp-block-paragraph">it should you know ascend from left to right if we&#8217;re not supporting duplicates in our tree</p>



<p class="wp-block-paragraph">and then i&#8217;m going to um just you know make some make some nodes for our target pattern here</p>



<p class="wp-block-paragraph">our output pattern so i&#8217;m going to like make some nodes here kind of place them uh in the perfect</p>



<p class="wp-block-paragraph">avl tree output pattern a perfectly balanced trinode subtree i&#8217;m just going to reconnect this</p>



<p class="wp-block-paragraph">The output pattern is always the same.</p>



<p class="wp-block-paragraph">You know, it always starts this way because this is better</p>



<p class="wp-block-paragraph">than the linear pattern that we have above.</p>



<p class="wp-block-paragraph">And then I just have to update the numbers.</p>



<p class="wp-block-paragraph">So the left child is going to be 12.</p>



<p class="wp-block-paragraph">The right child is going to be 21.</p>



<p class="wp-block-paragraph">Sorry, the center is going to be 21, the new parent.</p>



<p class="wp-block-paragraph">And the right child is going to be 30.</p>



<p class="wp-block-paragraph">Okay, now we look for any nodes that are children of the input nodes</p>



<p class="wp-block-paragraph">that have been unaccounted for.</p>



<p class="wp-block-paragraph">You can tell easily that all nodes are being used.</p>



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



<p class="wp-block-paragraph">So now that we have our trinode subtree which is perfectly balanced, we are ready to just sort of override it into the diagram.</p>



<p class="wp-block-paragraph">And since the diagram only has the three nodes to begin with, I could pretty much just like erase everything, call it a day, and say this is the new tree, the new rotated tree.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to maybe put this in the middle I guess.</p>



<p class="wp-block-paragraph">I&#8217;m going to delete this over here and recompute the balance factors.</p>



<p class="wp-block-paragraph">a zero, get zeros, and this 21 also gets a zero because it&#8217;s perfectly balanced.</p>



<p class="wp-block-paragraph">Next thing is let&#8217;s move on to adding the number 38. So we&#8217;ll add the 38 here and</p>



<p class="wp-block-paragraph">same thing that we do before. Let&#8217;s make a copy of one of the nodes for our diagram.</p>



<p class="wp-block-paragraph">Let&#8217;s call it the 38 and then let&#8217;s figure out where it belongs. It&#8217;s not going to be the new</p>



<p class="wp-block-paragraph">root node because that&#8217;s occupied. It belongs on the right side. It&#8217;s not going to be the right</p>



<p class="wp-block-paragraph">It&#8217;s not going to be the right child because that&#8217;s occupied.</p>



<p class="wp-block-paragraph">It belongs on the right side of the 30.</p>



<p class="wp-block-paragraph">It couldn&#8217;t go on the left.</p>



<p class="wp-block-paragraph">That would be an invalid binary search tree.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to stick it right there and do a connection.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to update the balance factors.</p>



<p class="wp-block-paragraph">So the 38 is a leaf.</p>



<p class="wp-block-paragraph">It gets 0.</p>



<p class="wp-block-paragraph">The 30 gets a 1.</p>



<p class="wp-block-paragraph">And the 21 also gets a 1 because its left subtree is a 1 and its right subtree is 2,</p>



<p class="wp-block-paragraph">or height, and the difference is just 1.</p>



<p class="wp-block-paragraph">is just one. Remember we don&#8217;t really need to update the 12 or any nodes that are not</p>



<p class="wp-block-paragraph">directly above the node that we just added or removed. So if you look at this tree there&#8217;s</p>



<p class="wp-block-paragraph">a little bit of imbalance but nothing is two or worse so we don&#8217;t actually need to perform a</p>



<p class="wp-block-paragraph">rotation. This is already a valid AVL tree. So I&#8217;m going to move on to adding the next number.</p>



<p class="wp-block-paragraph">Let&#8217;s add a 42. Same thing we&#8217;re going to duplicate a node for our diagram and we&#8217;re going to call it</p>



<p class="wp-block-paragraph">going to call it the 42 where does it belong well we start by looking at root that&#8217;s occupied 42</p>



<p class="wp-block-paragraph">belongs on the right side of 21 the right child is occupied 42 belongs on the right side belongs on</p>



<p class="wp-block-paragraph">the right side i mean it&#8217;s always going to be on the very right of the of the diagram because that&#8217;s</p>



<p class="wp-block-paragraph">the poison data that we received uh so i&#8217;m going to put it over there and then connect um the child</p>



<p class="wp-block-paragraph">child relationship and then let&#8217;s update the the balance factor so 42 gets a 0</p>



<p class="wp-block-paragraph">38 gets a 1 and 30 gets a 2 now because it&#8217;s got 2 height of subtree on its</p>



<p class="wp-block-paragraph">right side and no subtree on its left side the 21 we should update left subtree</p>



<p class="wp-block-paragraph">is a 1 right subtree is a 3 oh it&#8217;s still gonna it&#8217;s actually gonna be 2 so</p>



<p class="wp-block-paragraph">we have two instances of the number 2 and again which one do we actually want</p>



<p class="wp-block-paragraph">by the way here&#8217;s here&#8217;s how I like to eyeball it you can kind of see oh there&#8217;s two levels of</p>



<p class="wp-block-paragraph">difference in terms of the root node which one do we rotate first well as low as possible because</p>



<p class="wp-block-paragraph">if we rotate the 30 it might end up fixing the 21 so we don&#8217;t want to do any more more work than we</p>



<p class="wp-block-paragraph">have to so I&#8217;m just going to maybe move this over here a little bit and select our xyz um hang on</p>



<p class="wp-block-paragraph">a second I think I just like didn&#8217;t do a snap to grid when I was doing that okay hang on</p>



<p class="wp-block-paragraph">doing that okay hang on okay so we got to choose our x y and z uh we&#8217;re going to say that the 30</p>



<p class="wp-block-paragraph">is z the lowest node possible that is out of whack and then we hop down to a child of z to find y</p>



<p class="wp-block-paragraph">we hop down to a child of y to find x and so now we have our xyz i&#8217;m just going to write them down</p>



<p class="wp-block-paragraph">I&#8217;m going to change the ink real fast and then we have to get ABC so A is the least value B is the</p>



<p class="wp-block-paragraph">one that belongs in the middle and C is 42 now we have our ABC we&#8217;re ready to draw our output pattern</p>



<p class="wp-block-paragraph">or target pattern so I&#8217;m just going to put like some nodes up here duplicate that a couple times</p>



<p class="wp-block-paragraph">all right not the prettiest but I&#8217;ll take it I&#8217;m gonna do my connecting lines</p>



<p class="wp-block-paragraph">and then I have to update the numbers so a is 30 and then b is 38 and c is 42 still so we&#8217;ll just</p>



<p class="wp-block-paragraph">leave that as is now we have to look for children of any of the output pattern nodes that are</p>



<p class="wp-block-paragraph">for so we&#8217;ll first look at the z node here the 30 it had a right child of 38 that&#8217;s already handled</p>



<p class="wp-block-paragraph">as the y node so we don&#8217;t need to worry about the 38 okay next we look at the uh y node it had a</p>



<p class="wp-block-paragraph">right child of 42 that&#8217;s also handled in the output pattern so we don&#8217;t have to worry about that uh</p>



<p class="wp-block-paragraph">then the 42 had no children so we don&#8217;t need to really do anything this is going to be super easy</p>



<p class="wp-block-paragraph">Just as a reminder, the Z could have had a left child, the Y could have had a left child,</p>



<p class="wp-block-paragraph">and the X could have had two children, at least in this particular pattern.</p>



<p class="wp-block-paragraph">Oh, I think in my previous video, I didn&#8217;t name off all the rotation types.</p>



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



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



<p class="wp-block-paragraph">So now I&#8217;m ready to just kind of like move this into the diagram.</p>



<p class="wp-block-paragraph">So the 30 and 38 and 42, we can just kind of get rid of that</p>



<p class="wp-block-paragraph">move the rotated output pattern into the diagram like this.</p>



<p class="wp-block-paragraph">We&#8217;ll make the 38 the right child of 21.</p>



<p class="wp-block-paragraph">I want to make it go a little bit more to the right</p>



<p class="wp-block-paragraph">so that we have a nicely drawn diagram</p>



<p class="wp-block-paragraph">so that all the nodes have space so it&#8217;s easy to debug visually.</p>



<p class="wp-block-paragraph">It&#8217;s easy to just kind of eyeball it.</p>



<p class="wp-block-paragraph">Then I&#8217;m going to update the balance factors.</p>



<p class="wp-block-paragraph">So the 42 has 0, the 30 has 0, the 38 has 0.</p>



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



<p class="wp-block-paragraph">The 21, hopefully that improved.</p>



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



<p class="wp-block-paragraph">of one the right subtree has a height of two so actually the balance factor of the 21 node improved</p>



<p class="wp-block-paragraph">to one we are now done uh rotating and we have a valid avl tree and uh we could have had you know</p>



<p class="wp-block-paragraph">a tree that was just totally linear all the way down to the right it could have worked out that</p>



<p class="wp-block-paragraph">way if we weren&#8217;t using avl trees uh and we would have had a really slow binary search tree but with</p>



<p class="wp-block-paragraph">avl trees this is like you know a lot a lot faster reasonably and you can imagine if i added some</p>



<p class="wp-block-paragraph">And you can imagine if I added some more data, let&#8217;s see, how long is this video?</p>



<p class="wp-block-paragraph">Oh, it&#8217;s this video.</p>



<p class="wp-block-paragraph">It&#8217;s already 15 minutes.</p>



<p class="wp-block-paragraph">Let&#8217;s add one more piece of data just for fun, Zars.</p>



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



<p class="wp-block-paragraph">Anybody remember that old song, I Can&#8217;t Drive, 55?</p>



<p class="wp-block-paragraph">So we&#8217;re going to add the 55.</p>



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



<p class="wp-block-paragraph">And then I&#8217;m going to do just like a new node duplicate.</p>



<p class="wp-block-paragraph">node duplicate. I&#8217;m going to stick a 55 in there. And then where does the 55 belong? Well,</p>



<p class="wp-block-paragraph">it&#8217;s not going to go with the root node belongs on the right side. Not going to go there belongs</p>



<p class="wp-block-paragraph">on the right side, not going to go there belongs on the right side, we just end up going all the</p>



<p class="wp-block-paragraph">way to the right. Pretty much. Okay, so I&#8217;m going to say it belongs there. And I&#8217;m going to connect</p>



<p class="wp-block-paragraph">it. And then we update the balance factors. So zero here, and then the 42 becomes a one and the</p>



<p class="wp-block-paragraph">42 becomes a 1 and the 38 becomes a 1 and the 21 becomes a 1 on the left and 3 on the right</p>



<p class="wp-block-paragraph">which means we have a balance factor of 2 so now the 21 node is imbalanced this is no longer a</p>



<p class="wp-block-paragraph">valid abl tree we got to rotate to make it better so once again we look at the first node that&#8217;s out</p>



<p class="wp-block-paragraph">of whack or the lowest node that&#8217;s out of whack it&#8217;s going to be the 21 we call it our z and then</p>



<p class="wp-block-paragraph">we find a child of z with the tallest subtree so it you know is it going to be the 12 or the 38</p>



<p class="wp-block-paragraph">the 12 or the 38 it&#8217;s definitely going to be the 38 because 38 is the taller subtree same thing to</p>



<p class="wp-block-paragraph">find x it&#8217;s got to be a child of y should we go left or should we go right we have to go right</p>



<p class="wp-block-paragraph">because that&#8217;s the taller subtree so now we have our x y and z um i think i&#8217;ve been forgetting to</p>



<p class="wp-block-paragraph">is 38, Z is 21, X, Y, and Z, and then we do A and B and C. So A is the least value, 21, B is the one</p>



<p class="wp-block-paragraph">that belongs in the middle, C is the one that belongs on the right side. Now we&#8217;re ready to</p>



<p class="wp-block-paragraph">draw our target output pattern. So I&#8217;m going to do this, a little duplication.</p>



<p class="wp-block-paragraph">Do you like when I say, oh, we&#8217;re going to do this, we&#8217;re going to do this, we&#8217;re going to do this?</p>



<p class="wp-block-paragraph">Because sometimes I just go, I don&#8217;t like it.</p>



<p class="wp-block-paragraph">And I don&#8217;t think people like it when I say that.</p>



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



<p class="wp-block-paragraph">I&#8217;m going to connect these real fast and then change the numbers to match.</p>



<p class="wp-block-paragraph">I think I made that one.</p>



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



<p class="wp-block-paragraph">I can never tell if it&#8217;s perfectly symmetrical.</p>



<p class="wp-block-paragraph">I can&#8217;t do it.</p>



<p class="wp-block-paragraph">It&#8217;s perfectly symmetric.</p>



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



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



<p class="wp-block-paragraph">Alright, so A is going to be on the left, that&#8217;s 21.</p>



<p class="wp-block-paragraph">B is in the middle, that&#8217;s 38.</p>



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



<p class="wp-block-paragraph">Then we have to look for children of the output pattern that are unaccounted for.</p>



<p class="wp-block-paragraph">So we&#8217;re going to do, look at the Z node.</p>



<p class="wp-block-paragraph">The Z node had 12, which is unaccounted for as a left child.</p>



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



<p class="wp-block-paragraph">We&#8217;ll deal with it in a second.</p>



<p class="wp-block-paragraph">Then we look for the right child of 21, that&#8217;s a 38.</p>



<p class="wp-block-paragraph">that&#8217;s a 38 that&#8217;s already in the output pattern so we don&#8217;t have to worry about that anymore</p>



<p class="wp-block-paragraph">and then we look at the y node so y had left child was 30 that&#8217;s not accounted for so i&#8217;m</p>



<p class="wp-block-paragraph">going to copy that put it somewhere else and then its right child was 42 that&#8217;s already</p>



<p class="wp-block-paragraph">handled in the output pattern so we&#8217;re done there i&#8217;m going to duplicate this and then we look at</p>



<p class="wp-block-paragraph">the uh we look at the x node the 42 it had a right child of 55 that also was not accounted for so i&#8217;m</p>



<p class="wp-block-paragraph">for so I&#8217;m gonna just copy it over here and we&#8217;ll do something with it okay we&#8217;re done looking for</p>



<p class="wp-block-paragraph">unaccounted for children again if you are just rearranging pointers you don&#8217;t really have to</p>



<p class="wp-block-paragraph">worry about this in your code but since we&#8217;re doing a diagram we have to double check that</p>



<p class="wp-block-paragraph">none of these nodes at the bottom here the unaccounted for children have children of their</p>



<p class="wp-block-paragraph">own if they did the children would have to come with them so 55 uh no children 12 no children 30</p>



<p class="wp-block-paragraph">So those are fine. If there were any children underneath, they would just stay on those nodes.</p>



<p class="wp-block-paragraph">So now we have to figure out who goes where. So the 12 is the least value. That means it&#8217;s going</p>



<p class="wp-block-paragraph">to go probably in the leftmost position compared to the other ones. But if we figure out like,</p>



<p class="wp-block-paragraph">where does this belong in the binary search tree? It belongs right here as the left child of 21.</p>



<p class="wp-block-paragraph">The 30 node, same thing. Where does it belong? It belongs between the 21 and 38. That&#8217;s the only</p>



<p class="wp-block-paragraph">And then the 55, it belongs on the right side of 42 for the same reason.</p>



<p class="wp-block-paragraph">Okay, I&#8217;m going to do my connecting lines real fast.</p>



<p class="wp-block-paragraph">If I don&#8217;t do my connecting lines, then I don&#8217;t like it.</p>



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



<p class="wp-block-paragraph">We have one, two, three, four, five, six.</p>



<p class="wp-block-paragraph">there&#8217;s also just six which means the final tree after rotation is just everything that we&#8217;ve</p>



<p class="wp-block-paragraph">written on the right side so i&#8217;m just going to erase all of this stuff get rid of that too</p>



<p class="wp-block-paragraph">and then move all these nodes over to the left call that the tree this is now the tree and then</p>



<p class="wp-block-paragraph">i just have to recompute the balance factors to make sure that everything is okay so the leaves</p>



<p class="wp-block-paragraph">0, 0, the 42 gets a 1, the 21 is perfectly balanced so that gets a 0.</p>



<p class="wp-block-paragraph">The 38 is also perfectly balanced.</p>



<p class="wp-block-paragraph">Again, it&#8217;s not about weight or mass even though there are more nodes on the left subtree,</p>



<p class="wp-block-paragraph">it&#8217;s all about the height.</p>



<p class="wp-block-paragraph">The height of the left subtree is 2, the height of the right subtree is 2, so 38 is actually</p>



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



<p class="wp-block-paragraph">Because there are no nodes that have a balance factor of 2 or worse, we are done rotating</p>



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



<p class="wp-block-paragraph">This is a valid AVL tree and it&#8217;s pretty fast.</p>



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



<p class="wp-block-paragraph">That&#8217;s all I wanted to go through today for this video.</p>



<p class="wp-block-paragraph">Thank you so much for following along.</p>



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



<p class="wp-block-paragraph">And I will see you in the next video.</p>



<p class="wp-block-paragraph">Have a great night and or day and or week and or weekend.</p>



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



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



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



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



<p class="wp-block-paragraph">some fun if you could do me a please a small little favor could you please</p>



<p class="wp-block-paragraph">subscribe and follow this channel or these videos or whatever it is you do on</p>



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



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



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



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



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



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



<p class="wp-block-paragraph">i get filled with joy that&#8217;s exactly what happens every single time so you could do it as a nice</p>



<p class="wp-block-paragraph">favor to me or you could you could troll me if you want to just wake me up in the middle of the</p>



<p class="wp-block-paragraph">night just subscribe and then i&#8217;ll i&#8217;ll just wake up i promise that&#8217;s what will happen also uh if</p>



<p class="wp-block-paragraph">you look at the middle of the screen right now you should see a qr code which you can scan</p>



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



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



<p class="wp-block-paragraph">and the services and tutorials and things that i offer and all that good stuff and uh</p>



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



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



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



<p class="wp-block-paragraph">it would really it really mean the world to me i would really appreciate it so again thank you so</p>



<p class="wp-block-paragraph">much for watching this video and um enjoy the cool music as as i fade into the darkness which is</p>



<p class="wp-block-paragraph">coming for us all</p>



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



<p class="wp-block-paragraph">It should all work out according to plan, trust me.</p>



<p class="wp-block-paragraph">Anyway, um, so…</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-trees-adding-linear-data-and-performing-rotations/">AVL Trees: Adding Linear Data and Performing Rotations</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/avl-trees-adding-linear-data-and-performing-rotations/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AVL Tree Rotations Practice: Balancing a Huge Linear Tree Step by Step</title>
		<link>https://www.NeuralLantern.com/avl-tree-rotations-practice-balancing-a-huge-linear-tree-step-by-step/</link>
					<comments>https://www.NeuralLantern.com/avl-tree-rotations-practice-balancing-a-huge-linear-tree-step-by-step/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Tue, 16 Jun 2026 04:49:15 +0000</pubDate>
				<category><![CDATA[AVL Trees]]></category>
		<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[AVL balance factor]]></category>
		<category><![CDATA[avl rotations]]></category>
		<category><![CDATA[AVL tree]]></category>
		<category><![CDATA[balanced binary tree]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[coding interview]]></category>
		<category><![CDATA[computer science tutorial]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[rotation examples]]></category>
		<category><![CDATA[self balancing tree]]></category>
		<category><![CDATA[tree rotations]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=391</guid>

					<description><![CDATA[<p>Learn how to perform AVL tree rotations on a completely linear binary search tree. This step-by-step practice video shows multiple rotations, balance factor updates, and how to transform an unbalanced O(n) tree into a balanced O(log n) AVL tree.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-rotations-practice-balancing-a-huge-linear-tree-step-by-step/">AVL Tree Rotations Practice: Balancing a Huge Linear Tree Step by Step</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="AVL Tree Rotations Practice: Balancing a Huge Linear Tree Step by Step" width="1380" height="776" src="https://www.youtube.com/embed/mykKNhayTJM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">In this hands-on AVL tree tutorial, we take a massive unbalanced linear binary search tree and perform multiple rotations to turn it into a properly balanced AVL tree. Watch as we identify imbalance, label X Y Z nodes, determine A B C, handle outstanding children, and reattach subtrees step by step.</p>



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



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



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



<p class="wp-block-paragraph">00:00 Introduction to AVL Rotations Practice<br>00:22 Previous Videos Overview<br>00:56 Understanding the Linear Tree Problem<br>01:24 Why Balance This Tree<br>02:20 Computing Balance Factors<br>03:16 First Rotation Setup XYZ<br>04:04 In-Order ABC Pattern<br>05:50 Reattaching Subtree<br>07:17 Recompute Balance Factors<br>08:06 Second Rotation Setup<br>09:20 XYZ and ABC for Second Rotation<br>10:08 Drawing Output Pattern<br>11:40 Placing Outstanding Children<br>13:32 Third Rotation Setup<br>14:28 XYZ for Third Rotation<br>15:02 Output Pattern and Children<br>18:28 Recompute Balance Factors<br>19:28 Fourth Rotation Setup<br>20:04 XYZ for Final Rotation<br>20:32 Handling All Outstanding Children<br>23:20 Reattaching Final Subtree<br>24:50 Last Rotation Setup<br>25:48 XYZ and ABC Final<br>26:38 Output Pattern and Children Placement<br>30:16 Final Balance Factors Check<br>30:52 Valid AVL Tree Achieved<br>31:07 Conclusion and Thanks</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">Hello there! Let&#8217;s practice AVL rotations with a big ugly gross linear tree that for some reason hasn&#8217;t been rotating up to this point.</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">so I guess I&#8217;ll see you in the next video thanks for hanging in there with me</p>



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



<p class="wp-block-paragraph">appreciate it I do hope you did learn something and have some fun if you could do me a please</p>



<p class="wp-block-paragraph">Could you please subscribe and follow this channel or these videos or whatever it is you do on the current social media?</p>



<p class="wp-block-paragraph">It&#8217;s a website that you&#8217;re looking at right now</p>



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



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



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



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



<p class="wp-block-paragraph">subscribed or followed. It just wakes me up and I get filled with joy. That&#8217;s exactly what happens</p>



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



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



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



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



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



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



<p class="wp-block-paragraph">good stuff. And, uh, if you have a suggestion for, uh, uh, clarifications or errata or just future</p>



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">Test test test! Whoa, I&#8217;m trippin&#8217;!</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-rotations-practice-balancing-a-huge-linear-tree-step-by-step/">AVL Tree Rotations Practice: Balancing a Huge Linear Tree Step by Step</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/avl-tree-rotations-practice-balancing-a-huge-linear-tree-step-by-step/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Perform AVL Tree Rotations &#8211; Step By Step Example</title>
		<link>https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/</link>
					<comments>https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 08 Jun 2026 03:26:17 +0000</pubDate>
				<category><![CDATA[AVL Trees]]></category>
		<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[AVL tree]]></category>
		<category><![CDATA[avl tree example]]></category>
		<category><![CDATA[AVL tree rotation]]></category>
		<category><![CDATA[balance factor]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[double rotation]]></category>
		<category><![CDATA[self balancing tree]]></category>
		<category><![CDATA[tree rotation]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=387</guid>

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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">which is recursive. Hello there. Let&#8217;s,</p>
<p>The post <a href="https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/">How to Perform AVL Tree Rotations &#8211; Step By Step Example</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/how-to-perform-avl-tree-rotations-step-by-step-example/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AVL Tree Rotation Types Explained for Self-Balancing Binary Search Trees</title>
		<link>https://www.NeuralLantern.com/avl-tree-rotation-types-explained-for-self-balancing-binary-search-trees/</link>
					<comments>https://www.NeuralLantern.com/avl-tree-rotation-types-explained-for-self-balancing-binary-search-trees/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 25 May 2026 20:03:52 +0000</pubDate>
				<category><![CDATA[AVL Trees]]></category>
		<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[avl rotations]]></category>
		<category><![CDATA[AVL tree]]></category>
		<category><![CDATA[avl tree tutorial]]></category>
		<category><![CDATA[balance factor]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[coding interview]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[double left rotation]]></category>
		<category><![CDATA[double right rotation]]></category>
		<category><![CDATA[self balancing binary search tree]]></category>
		<category><![CDATA[single left rotation]]></category>
		<category><![CDATA[single right rotation]]></category>
		<category><![CDATA[tree rotations]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=377</guid>

					<description><![CDATA[<p>In AVL trees, when we find a node with a balance factor of 2 or worse, we perform rotations on the trinode subtree. There are four input patterns that all resolve to the same balanced output pattern through single or double rotations. Single rotations handle straight line imbalances while double rotations address the zigzag cases. Each rotation rearranges parent-child pointers to reduce the height from 3 to 2, helping keep the overall tree balanced for log time operations.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-rotation-types-explained-for-self-balancing-binary-search-trees/">AVL Tree Rotation Types Explained for Self-Balancing Binary Search Trees</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="AVL Tree Rotation Types Explained for Self-Balancing Binary Search Trees" width="1380" height="776" src="https://www.youtube.com/embed/zY8ItrR-j1U?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Hey everyone, in this video we break down the four types of rotations you need to know for AVL trees &#8211; self-balancing binary search trees.</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">trees which are self-balancing binary search trees this video is going to be</p>



<p class="wp-block-paragraph">about the types of rotations that you can perform in an AVL tree what do I</p>



<p class="wp-block-paragraph">mean by rotation well the basic idea that we talked about in the last video</p>



<p class="wp-block-paragraph">was that throughout your tree you&#8217;ll notice that different nodes have</p>



<p class="wp-block-paragraph">different balance factors which means how imbalanced or balanced they are when</p>



<p class="wp-block-paragraph">we identify a node that has a balance factor that&#8217;s too bad then we consider</p>



<p class="wp-block-paragraph">AVL tree and we have to stop and rotate a couple of nodes you know kind of rearrange them to make</p>



<p class="wp-block-paragraph">them better before we have a valid AVL tree so we have four different input patterns that we can</p>



<p class="wp-block-paragraph">have so imagine you know this little trinode subtree up on the top left that could be you know</p>



<p class="wp-block-paragraph">three nodes that we found somewhere in our tree that had a bad balance if you just quickly compute</p>



<p class="wp-block-paragraph">the balance factor for this trinode subtree you&#8217;ll notice actually let me just do it real fast for fun</p>



<p class="wp-block-paragraph">that the 65 node has a balance factor of 2 because its left subtree has a height of 2</p>



<p class="wp-block-paragraph">and there is no right subtree. The 55 has a balance factor of 1 and 48 being a leaf, at</p>



<p class="wp-block-paragraph">least in terms of this trinode subtree, has a balance factor of 0. All of these trinode</p>



<p class="wp-block-paragraph">subtrees are going to have the same situation. The one on top is going to be 2 and so forth.</p>



<p class="wp-block-paragraph">So you can imagine that you were going through the whole tree, computing balance factors like</p>



<p class="wp-block-paragraph">And you found a two, which remember we said in our last video, two or worse, meaning two or higher,</p>



<p class="wp-block-paragraph">if we&#8217;re using balance factors that are absolute or negative two and also positive two or worse,</p>



<p class="wp-block-paragraph">if we&#8217;re not using absolute balance factors, means you got to stop and try to rebalance that trinode subtree.</p>



<p class="wp-block-paragraph">So we found a node that had a balance factor of two and we realized we have to stop and rotate.</p>



<p class="wp-block-paragraph">The pattern of the trino subtree before we perform the rotation could be one of these four things.</p>



<p class="wp-block-paragraph">It could be the thing on the left on top, the thing on the right on top, the thing on the bottom left, and the thing on the bottom right.</p>



<p class="wp-block-paragraph">And all four of these patterns should end up becoming, after we do our rotation, they should end up becoming the pattern in the middle.</p>



<p class="wp-block-paragraph">So this is always going to be the target pattern.</p>



<p class="wp-block-paragraph">Let me draw some arrows real fast.</p>



<p class="wp-block-paragraph">pattern is going to end up looking like the output pattern in the middle.</p>



<p class="wp-block-paragraph">Why is that?</p>



<p class="wp-block-paragraph">How is that possible?</p>



<p class="wp-block-paragraph">Well, we only have three nodes.</p>



<p class="wp-block-paragraph">If you look at all the input patterns, they&#8217;re all the same numbers.</p>



<p class="wp-block-paragraph">What we&#8217;re really doing is disconnecting all the pointers, disconnecting the parent child</p>



<p class="wp-block-paragraph">relationships and then reconnecting them in the best possible way to make, you know,</p>



<p class="wp-block-paragraph">the most balanced trinode subtree.</p>



<p class="wp-block-paragraph">Notice how the height of all of these input trees is always three, right?</p>



<p class="wp-block-paragraph">because it&#8217;s a trinode subtree that&#8217;s really really bad and out of whack so we&#8217;ll you know</p>



<p class="wp-block-paragraph">like one two three one two three right height of three we&#8217;ll do a rotation rearrange the pointers</p>



<p class="wp-block-paragraph">rearrange the parent-child relationships so that the final height ends up being actually just two</p>



<p class="wp-block-paragraph">and if you think about reducing the height we&#8217;re making the tree better we&#8217;re making it more</p>



<p class="wp-block-paragraph">balanced we&#8217;re making it faster to search through and add and remove things from so we&#8217;re making a</p>



<p class="wp-block-paragraph">So we&#8217;re making a better tree by reducing the height a little bit.</p>



<p class="wp-block-paragraph">If you had a really, really, really bad tree and you just went around to all the bad nodes</p>



<p class="wp-block-paragraph">and just started rotating them all, you&#8217;re reducing the height by one for each rotation.</p>



<p class="wp-block-paragraph">Eventually, when you get up to the top of the tree, you&#8217;ll have an overall AVL tree,</p>



<p class="wp-block-paragraph">which is pretty fast.</p>



<p class="wp-block-paragraph">We should say now that AVL trees are always going to be log time trees because the imbalance</p>



<p class="wp-block-paragraph">factor is limited by a constant number.</p>



<p class="wp-block-paragraph">We&#8217;ll say that again in a future video.</p>



<p class="wp-block-paragraph">in a in a future video so anyway i&#8217;m going to label these trees now</p>



<p class="wp-block-paragraph">i&#8217;m going to say that the one on the top left is a single right rotation</p>



<p class="wp-block-paragraph">and the uh the tree on the top right you can probably imagine is a single left rotation</p>



<p class="wp-block-paragraph">let me just duplicate this real fast so i can come on man can i duplicate this yeah so i can</p>



<p class="wp-block-paragraph">edit the text real fast. This is going to be a single left rotation. And then the trees at the</p>



<p class="wp-block-paragraph">bottom, those are going to be double rotations. Let me just write over here, maybe next to that</p>



<p class="wp-block-paragraph">one, double right rotation. And then I&#8217;ll duplicate this. And this one over here is going to be a</p>



<p class="wp-block-paragraph">these on different sides than I usually do because I was trying to make it look a little more tricky</p>



<p class="wp-block-paragraph">but it&#8217;s really not that bad. So how do we know that the top pattern is a single right rotation</p>



<p class="wp-block-paragraph">and not a double right rotation or it&#8217;s a single right rotation and not a single left rotation?</p>



<p class="wp-block-paragraph">The first thing to try to understand is what&#8217;s going to happen between the input pattern and</p>



<p class="wp-block-paragraph">the output pattern. So if you look at the input pattern here we&#8217;ve got like a straight line and</p>



<p class="wp-block-paragraph">And the output pattern is like a perfectly balanced trinode subtree.</p>



<p class="wp-block-paragraph">Notice how the 55 is the new parent.</p>



<p class="wp-block-paragraph">And it&#8217;s also in the middle for the output pattern.</p>



<p class="wp-block-paragraph">And then for the input pattern, the 55 is also in the middle, but it&#8217;s not the new parent.</p>



<p class="wp-block-paragraph">But it is the parent already of the 48 node.</p>



<p class="wp-block-paragraph">Notice how it&#8217;s the parent already of one node, the 48 node.</p>



<p class="wp-block-paragraph">So that means its relationship to the 48 node, the left child, doesn&#8217;t actually change.</p>



<p class="wp-block-paragraph">bit more you&#8217;ll realize that only one node actually needs to be moved into position so we&#8217;ll call this</p>



<p class="wp-block-paragraph">a rotation try to imagine visualize maybe like your point your left pointer finger being placed</p>



<p class="wp-block-paragraph">on the 55 node and your right pointer finger being placed on the 65 node and then just rotate</p>



<p class="wp-block-paragraph">the 65 node downward we rotate that clockwise and the physics people out there love to say that</p>



<p class="wp-block-paragraph">are right rotations.</p>



<p class="wp-block-paragraph">At least as far as I remember.</p>



<p class="wp-block-paragraph">Not totally sure about that,</p>



<p class="wp-block-paragraph">but I&#8217;m just going to say it anyway.</p>



<p class="wp-block-paragraph">So notice how the 65 needs to be rotated</p>



<p class="wp-block-paragraph">to the right or clockwise.</p>



<p class="wp-block-paragraph">And only one node actually needs to be rotated</p>



<p class="wp-block-paragraph">to produce that final output pattern.</p>



<p class="wp-block-paragraph">That&#8217;s why we call this a single right rotation</p>



<p class="wp-block-paragraph">because there&#8217;s only one node to rotate.</p>



<p class="wp-block-paragraph">The next thing I&#8217;ll do is just try to give you</p>



<p class="wp-block-paragraph">a few extra ways of being able to remember</p>



<p class="wp-block-paragraph">being able to remember whether it&#8217;s a left or a right so for starters one node needs to be rotated</p>



<p class="wp-block-paragraph">so it&#8217;s a single rotation and we&#8217;re rotating clockwise so it&#8217;s a right rotation but you can</p>



<p class="wp-block-paragraph">also do this you can think of it like can i get rid of that real fast you can think of it like</p>



<p class="wp-block-paragraph">well there is a bunch of empty space kind of sitting here on the right side so that could be</p>



<p class="wp-block-paragraph">another way to remember that this is a right rotation also if you were to start at the node</p>



<p class="wp-block-paragraph">and work your way to the node at the top,</p>



<p class="wp-block-paragraph">you know, the Z node,</p>



<p class="wp-block-paragraph">then we&#8217;re kind of going up and to the right.</p>



<p class="wp-block-paragraph">So that&#8217;s another way to remember</p>



<p class="wp-block-paragraph">that this is a right rotation.</p>



<p class="wp-block-paragraph">Single, you can remember that also</p>



<p class="wp-block-paragraph">by just the fact that this is a straight line.</p>



<p class="wp-block-paragraph">Like notice how the nodes are kind of in a straight line.</p>



<p class="wp-block-paragraph">They don&#8217;t do a zigzag or a bounce.</p>



<p class="wp-block-paragraph">So single rotations are rotations that look straight at first,</p>



<p class="wp-block-paragraph">or I guess input patterns that look straight</p>



<p class="wp-block-paragraph">and only one node actually needs to be rotated.</p>



<p class="wp-block-paragraph">um right versus left is there&#8217;s a pocket of empty space on the right side and you go up and to the</p>



<p class="wp-block-paragraph">right for the right rotations similarly you can probably already figure out by now that</p>



<p class="wp-block-paragraph">the left rotation just means well we&#8217;re going let&#8217;s see we&#8217;re going up and to the left to get</p>



<p class="wp-block-paragraph">to that top node or the only node that has to be rotated is that 48 node and it&#8217;s going to be</p>



<p class="wp-block-paragraph">rotated counterclockwise which is left so again only one node needs to be rotated and we&#8217;re going</p>



<p class="wp-block-paragraph">to be rotated and we&#8217;re going to rotate it counterclockwise which is a left rotation so</p>



<p class="wp-block-paragraph">one node rotated single counterclockwise left or just like a pocket of empty space sitting on the</p>



<p class="wp-block-paragraph">left side okay so then for the double rotations the first thing you can look at is the idea that</p>



<p class="wp-block-paragraph">there&#8217;s a little bit of a bounce you know there&#8217;s like a angle bounce there if you see a bounce or</p>



<p class="wp-block-paragraph">sick a couple weeks ago then that&#8217;s a double rotation another way you can try to remember it</p>



<p class="wp-block-paragraph">as two nodes need to be moved into position so if I maybe I should copy this to another slide real</p>



<p class="wp-block-paragraph">fast let me copy this and we&#8217;ll do another slide okay so you know which nodes need to be moved into</p>



<p class="wp-block-paragraph">position let me copy also the output pattern just as a quick reminder if we wanted to make that</p>



<p class="wp-block-paragraph">If we wanted to make that output pattern, let me put it up here maybe,</p>



<p class="wp-block-paragraph">then obviously the 55 still has to be in the middle</p>



<p class="wp-block-paragraph">because it was already in the middle before we rotated.</p>



<p class="wp-block-paragraph">That means both the 48 and the 65 need to be rotated underneath the 55.</p>



<p class="wp-block-paragraph">So that means we&#8217;re going to do the 65, we&#8217;re going to rotate it clockwise underneath the 55.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do like a little circle here, a connector, and then I&#8217;ll do like 65 there.</p>



<p class="wp-block-paragraph">65 there and then after we do that rotation we have to rotate the 48</p>



<p class="wp-block-paragraph">underneath the 55 so that&#8217;s going to be a rotation that is counterclockwise so</p>



<p class="wp-block-paragraph">I&#8217;m going to do this circle that there and then do a 48 notice how when we&#8217;re</p>



<p class="wp-block-paragraph">done doing the rotations we have a valid binary search tree you should always</p>



<p class="wp-block-paragraph">check your rotated pattern your output pattern to make sure it&#8217;s actually in</p>



<p class="wp-block-paragraph">binary search tree because if it&#8217;s not let&#8217;s say the numbers are out of order for some reason from</p>



<p class="wp-block-paragraph">left to right then you have rotated incorrectly and you have to double check yourself so two nodes</p>



<p class="wp-block-paragraph">need to be rotated into position therefore it&#8217;s a double rotation or you see a diagonal input pattern</p>



<p class="wp-block-paragraph">double rotation how do you remember the difference between left and right well um if you start at the</p>



<p class="wp-block-paragraph">node in the middle just like we did in the last slides we start at the node in the middle and go</p>



<p class="wp-block-paragraph">and go to the node at the top, the Z node,</p>



<p class="wp-block-paragraph">we&#8217;re going up and to the right.</p>



<p class="wp-block-paragraph">And so this is a right rotation.</p>



<p class="wp-block-paragraph">So it&#8217;s a double right rotation.</p>



<p class="wp-block-paragraph">Another way to remember it is you should probably try to rotate the Z node first.</p>



<p class="wp-block-paragraph">The node that&#8217;s on very top is the 65.</p>



<p class="wp-block-paragraph">Rotate that first and then call that the direction of the rotation.</p>



<p class="wp-block-paragraph">So, you know, we rotate the 65 first.</p>



<p class="wp-block-paragraph">That&#8217;s a clockwise.</p>



<p class="wp-block-paragraph">So that&#8217;s why it&#8217;s a right rotation.</p>



<p class="wp-block-paragraph">Then just ignore the direction of the other node that you rotated.</p>



<p class="wp-block-paragraph">you can also kind of imagine taking the original node if you don&#8217;t if you don&#8217;t</p>



<p class="wp-block-paragraph">want to think of like actually rotating let me just kind of erase this real fast</p>



<p class="wp-block-paragraph">you can kind of imagine that you are you&#8217;re sort of taking the new parent</p>



<p class="wp-block-paragraph">that&#8217;s supposed to be the new parent and just sort of pulling it up</p>



<p class="wp-block-paragraph">I draw a picture of Batman and he shoots a batarang at it and he sounds like Christian</p>



<p class="wp-block-paragraph">Bale and he&#8217;s like, where is the rotation?</p>



<p class="wp-block-paragraph">I&#8217;m not going to do that.</p>



<p class="wp-block-paragraph">Maybe if there are enough comments, I&#8217;ll do a special video just for my really, really</p>



<p class="wp-block-paragraph">bad drawing.</p>



<p class="wp-block-paragraph">But imagine the 55 is getting pulled up and it&#8217;s getting pulled up past two other nodes.</p>



<p class="wp-block-paragraph">So that&#8217;s why it&#8217;s a double rotation.</p>



<p class="wp-block-paragraph">Another way to remember.</p>



<p class="wp-block-paragraph">The 55 would be the mob boss that&#8217;s hanging by his ankle.</p>



<p class="wp-block-paragraph">anyway so uh we we kind of know how we can remember the difference between uh single and</p>



<p class="wp-block-paragraph">double rotations uh we&#8217;ve got a single right rotation a single left a double right a double</p>



<p class="wp-block-paragraph">left there&#8217;s only going to be four possible input patterns and they&#8217;re all going to look like this</p>



<p class="wp-block-paragraph">one output pattern because really what we&#8217;re doing is we&#8217;re just we&#8217;re not you know creating</p>



<p class="wp-block-paragraph">any nodes or removing any nodes we&#8217;re just disconnecting pointers we&#8217;re disconnecting</p>



<p class="wp-block-paragraph">just get this over here we&#8217;re saying okay input pattern I disconnect the pointers I&#8217;m saying the</p>



<p class="wp-block-paragraph">65 no longer has a left child the 48 no longer has a parent the 55 no longer has a parent and</p>



<p class="wp-block-paragraph">the 48 no longer has a right child and then when we&#8217;re done with that we just kind of like reconnect</p>



<p class="wp-block-paragraph">the relationships we&#8217;re like okay you know what just kidding you all have parents and children</p>



<p class="wp-block-paragraph">again we&#8217;re just rearranging them okay let me check out my thing real fast here</p>



<p class="wp-block-paragraph">I&#8217;m going to cut the video here because this is the basic idea for the different types of rotations that you could use.</p>



<p class="wp-block-paragraph">In the next videos, we&#8217;re going to actually start rotating bad trees to make them work.</p>



<p class="wp-block-paragraph">So thanks for watching this video.</p>



<p class="wp-block-paragraph">I hope you learned a little bit of stuff and had a little bit of fun.</p>



<p class="wp-block-paragraph">I&#8217;ll see you in the next video.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">Hey, everybody.</p>



<p class="wp-block-paragraph">Thanks for watching this video again from the bottom of my heart.</p>



<p class="wp-block-paragraph">I really appreciate it.</p>



<p class="wp-block-paragraph">it. I do hope you did learn something and have some fun. If you could do me a please, a small</p>



<p class="wp-block-paragraph">little favor, could you please subscribe and follow this channel or these videos or whatever</p>



<p class="wp-block-paragraph">it is you do on the current social media website that you&#8217;re looking at right now. It would really</p>



<p class="wp-block-paragraph">mean the world to me and it&#8217;ll help make more videos and grow this community. So we&#8217;ll be able</p>



<p class="wp-block-paragraph">to do more videos, longer videos, better videos, or just I&#8217;ll be able to keep making videos in</p>



<p class="wp-block-paragraph">and subscribe. You know sometimes I&#8217;m sleeping in the middle of the night and</p>



<p class="wp-block-paragraph">I just wake up because I know somebody subscribed or followed. It just wakes me</p>



<p class="wp-block-paragraph">up and I get filled with joy. That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me or you could you could troll me if you</p>



<p class="wp-block-paragraph">want to just wake me up in the middle of the night just subscribe and then I&#8217;ll</p>



<p class="wp-block-paragraph">just wake up. I promise that&#8217;s what will happen. Also if you look at the middle of</p>



<p class="wp-block-paragraph">the screen right now you should see a QR code which you can scan in order to go</p>



<p class="wp-block-paragraph">to the website which I think is also named somewhere at the bottom of this</p>



<p class="wp-block-paragraph">is also named somewhere at the bottom of this video and it&#8217;ll take you to my main website where</p>



<p class="wp-block-paragraph">you can just kind of like see all the videos i published and the services and tutorials and</p>



<p class="wp-block-paragraph">things that i offer and all that good stuff and uh if you have a suggestion for uh uh clarifications</p>



<p class="wp-block-paragraph">or errata or just future videos that you want to see please leave a comment or if you just want to</p>



<p class="wp-block-paragraph">say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever i also wake up for</p>



<p class="wp-block-paragraph">the night I get I wake up in a cold sweat I&#8217;m like it would really it really mean the world to</p>



<p class="wp-block-paragraph">me I would really appreciate it so again thank you so much for watching this video and um enjoy</p>



<p class="wp-block-paragraph">the cool music as as I fade into the darkness which is coming for us all</p>



<p class="wp-block-paragraph">Thank you.</p>



<p class="wp-block-paragraph">Hello there.</p>



<p class="wp-block-paragraph">Let&#8217;s talk about rotation types in an AVL self-balancing binary search tree.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-rotation-types-explained-for-self-balancing-binary-search-trees/">AVL Tree Rotation Types Explained for Self-Balancing Binary Search Trees</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/avl-tree-rotation-types-explained-for-self-balancing-binary-search-trees/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AVL Tree Tutorial: Balance Factors and Why They Fix Slow BSTs</title>
		<link>https://www.NeuralLantern.com/avl-tree-tutorial-balance-factors-and-why-they-fix-slow-bsts/</link>
					<comments>https://www.NeuralLantern.com/avl-tree-tutorial-balance-factors-and-why-they-fix-slow-bsts/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 18 May 2026 06:09:32 +0000</pubDate>
				<category><![CDATA[AVL Trees]]></category>
		<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[AVL tree]]></category>
		<category><![CDATA[AVL tree rotation]]></category>
		<category><![CDATA[AVL trees]]></category>
		<category><![CDATA[balance factor]]></category>
		<category><![CDATA[balanced binary tree]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[bst]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[self balancing binary search tree]]></category>
		<category><![CDATA[tree rotation]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=371</guid>

					<description><![CDATA[<p>AVL trees are self-balancing binary search trees that prevent the tree from becoming unbalanced. We compute balance factors as the absolute value of left subtree height minus right subtree height. If any node has a balance factor of 2 or worse, we rebalance using rotations on trinode subtrees. This keeps search, insert, and other operations efficient at logarithmic time.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-tutorial-balance-factors-and-why-they-fix-slow-bsts/">AVL Tree Tutorial: Balance Factors and Why They Fix Slow BSTs</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="AVL Tree Tutorial: Balance Factors and Why They Fix Slow BSTs" width="1380" height="776" src="https://www.youtube.com/embed/xfMFNMpGVz0?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Learn AVL trees in this beginner-friendly introduction. We cover balance factors, why regular BSTs get slow, and how AVL trees stay balanced with rotations. Great for coding interviews and data structure understanding.</p>



<p class="wp-block-paragraph">00:00 AVL Trees Introduction<br>00:00:28 Problems with Regular BSTs<br>00:00:56 AVL Tree Balance Rule<br>00:02:10 Balance Factor Explained<br>00:02:48 Computing Balance Factors<br>00:03:11 Example Tree Analysis<br>00:04:40 Imbalance at 65 Node<br>00:05:08 Invalid AVL Tree<br>00:06:18 Linear Tree Problem<br>00:06:50 Trinode Subtree<br>00:07:40 Selecting Z Y X Nodes<br>00:09:20 Rotation Overview<br>00:10:03 Next Videos Preview<br>00:11:09 Thank You and Subscribe</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p class="wp-block-paragraph">Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to the Blog: https://www.NeuralLantern.com</li>



<li>Watch the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hello there. Let&#8217;s talk about a type of self-balancing binary search tree called an AVL tree.</p>



<p class="wp-block-paragraph">Okay, so by now I hope you&#8217;ve seen my other videos that I&#8217;ve posted</p>



<p class="wp-block-paragraph">previously where we talked about binary search trees, how to define them,</p>



<p class="wp-block-paragraph">terminology, how to know you&#8217;re actually looking at one, how to build them, insert,</p>



<p class="wp-block-paragraph">If you finish those videos already with me, then you know that binary search trees can actually get slow sometimes because if we have bad data, regular binary search trees don&#8217;t really know how to rearrange themselves.</p>



<p class="wp-block-paragraph">They just might become slow depending on how good or bad the data is.</p>



<p class="wp-block-paragraph">The data is totally random.</p>



<p class="wp-block-paragraph">Then on average, it&#8217;ll, you know, the trees will be log time, but that&#8217;s not always the case.</p>



<p class="wp-block-paragraph">Maybe sometimes you have bad data, sorted data, poison data, whatever kind of data.</p>



<p class="wp-block-paragraph">The secret to AVL trees is we&#8217;re going to start with the binary search tree.</p>



<p class="wp-block-paragraph">And so first it has to satisfy all the rules of the binary search tree.</p>



<p class="wp-block-paragraph">And then we add on another rule.</p>



<p class="wp-block-paragraph">The rule is going to be that we don&#8217;t have a valid AVL tree.</p>



<p class="wp-block-paragraph">If the balance factor for any of the nodes, we&#8217;ll talk about balance factors in a second.</p>



<p class="wp-block-paragraph">If the balance factor for any node is two or worse.</p>



<p class="wp-block-paragraph">Meaning if we think the tree is too imbalanced at any node, it&#8217;s not a valid AVL tree.</p>



<p class="wp-block-paragraph">and therefore we must rebalance the tree with something called a rotation.</p>



<p class="wp-block-paragraph">After we do enough rotations and we see that the tree is balanced again, then it&#8217;ll be valid.</p>



<p class="wp-block-paragraph">And so AVL trees sometimes are invalid in some intermediate state.</p>



<p class="wp-block-paragraph">Like if you have an AVL tree and you add some data into it,</p>



<p class="wp-block-paragraph">for a moment it might be an invalid AVL tree and then internally it&#8217;s sort of just like rotating itself and rebalancing itself.</p>



<p class="wp-block-paragraph">Then when the tree is done, you&#8217;ll have a valid AVL tree again, if that makes sense.</p>



<p class="wp-block-paragraph">AVL tree again if that makes sense. So suppose this tree here we can tell this is a valid binary</p>



<p class="wp-block-paragraph">search tree because it follows all the rules that we talked about before. So let&#8217;s start implementing</p>



<p class="wp-block-paragraph">the first rule of an AVL tree which is let&#8217;s compute the balance factors for every single node.</p>



<p class="wp-block-paragraph">How do we compute balance factors? The basic idea is I want to say BF for balance factor</p>



<p class="wp-block-paragraph">the left subtree height minus the right subtree height.</p>



<p class="wp-block-paragraph">If you don&#8217;t know what a subtree or height is,</p>



<p class="wp-block-paragraph">you should probably check out my other videos right now.</p>



<p class="wp-block-paragraph">But basically, we&#8217;re just going to take the absolute value</p>



<p class="wp-block-paragraph">just to get the difference in the height of the left</p>



<p class="wp-block-paragraph">and the height of the right.</p>



<p class="wp-block-paragraph">So if you know binary search trees already,</p>



<p class="wp-block-paragraph">then you know that the leaves, they don&#8217;t have any subtrees.</p>



<p class="wp-block-paragraph">So their balance factor is going to be pretty easy to compute.</p>



<p class="wp-block-paragraph">It&#8217;s just going to be a zero.</p>



<p class="wp-block-paragraph">I should also point out that some other tutorials out there use positive and negative numbers</p>



<p class="wp-block-paragraph">for the balance factor.</p>



<p class="wp-block-paragraph">That&#8217;s okay.</p>



<p class="wp-block-paragraph">I&#8217;m just going to use absolute values here because it&#8217;s a little bit more simple.</p>



<p class="wp-block-paragraph">So all the leaves get a balance factor of zero, which is fine.</p>



<p class="wp-block-paragraph">We&#8217;re looking to see if we can find a balance factor of two or worse to indicate to us that</p>



<p class="wp-block-paragraph">we have an invalid AVL tree.</p>



<p class="wp-block-paragraph">So far, so good.</p>



<p class="wp-block-paragraph">Let&#8217;s look at the 22 node.</p>



<p class="wp-block-paragraph">we&#8217;ll compute the height of the left subtree versus the height of the right subtree.</p>



<p class="wp-block-paragraph">So, you know, I&#8217;m going to highlight its left subtree.</p>



<p class="wp-block-paragraph">That&#8217;s just the 14 node that has a height of 1.</p>



<p class="wp-block-paragraph">And then its right subtree is just the 36 node that also has a height of 1,</p>



<p class="wp-block-paragraph">which means the balance factor for the 22 node is the absolute value of 1 minus 1 is 0.</p>



<p class="wp-block-paragraph">So actually the 22 node is perfectly balanced.</p>



<p class="wp-block-paragraph">If you think about it, that makes sense.</p>



<p class="wp-block-paragraph">Left and right subtrees have the same height.</p>



<p class="wp-block-paragraph">No problem.</p>



<p class="wp-block-paragraph">have the same height no problem so I&#8217;m going to move on let&#8217;s compute the balance factor for the</p>



<p class="wp-block-paragraph">48 node this is a little trickier notice how the right subtree has a height of one and there is no</p>



<p class="wp-block-paragraph">left subtree which means its height is zero so for the 48 node it&#8217;s actually going to be</p>



<p class="wp-block-paragraph">the absolute value of zero minus one or just one so we find our first node that&#8217;s a little bit out</p>



<p class="wp-block-paragraph">imbalance here but avl trees tolerate imbalances of one they don&#8217;t really care we sort of try to</p>



<p class="wp-block-paragraph">make a trade-off between constantly always rotating every single time anything happens</p>



<p class="wp-block-paragraph">which would perhaps burn a little too much cpu versus letting the tree just be imbalanced to a</p>



<p class="wp-block-paragraph">reasonable amount so we could still call this a log tree in the end so we consider this to be okay</p>



<p class="wp-block-paragraph">65 node and compute the balance factor so 65 node has no right subtree so the height is zero there</p>



<p class="wp-block-paragraph">and its left subtree is those two nodes on the left so that has a height of two which means the</p>



<p class="wp-block-paragraph">balance factor for the 65 node is going to be the absolute value of two minus zero is two at this</p>



<p class="wp-block-paragraph">point we are already certain that this is not a valid avl tree and it would need to be rebalanced</p>



<p class="wp-block-paragraph">you know, consider it a valid AVL tree in the future.</p>



<p class="wp-block-paragraph">So invalid AVL tree, still a valid binary search tree.</p>



<p class="wp-block-paragraph">Something has to be done.</p>



<p class="wp-block-paragraph">Normally what I would say is if you find some nodes</p>



<p class="wp-block-paragraph">way down lower in the tree that are imbalanced,</p>



<p class="wp-block-paragraph">then just go ahead and perform a rebalancing</p>



<p class="wp-block-paragraph">or a rotation immediately,</p>



<p class="wp-block-paragraph">because sometimes when you rotate lower in the tree,</p>



<p class="wp-block-paragraph">you&#8217;ll end up fixing nodes that are a little higher.</p>



<p class="wp-block-paragraph">But since we started with this tree</p>



<p class="wp-block-paragraph">and we&#8217;re not kind of, you know,</p>



<p class="wp-block-paragraph">building a tree step-by-step,</p>



<p class="wp-block-paragraph">I just want to compute the balance factor for all of the nodes at the same time.</p>



<p class="wp-block-paragraph">So we&#8217;ll do the same thing for the 41 node, the root node.</p>



<p class="wp-block-paragraph">Its left subtree has a height of two and its right subtree has a height of three.</p>



<p class="wp-block-paragraph">So its balance factor is the absolute value of two minus three is one.</p>



<p class="wp-block-paragraph">So actually the root node is okay.</p>



<p class="wp-block-paragraph">If it was only up to the root node, we would say this is a valid AVL tree</p>



<p class="wp-block-paragraph">and we don&#8217;t really need to do anything.</p>



<p class="wp-block-paragraph">need to do anything however we&#8217;ve already seen that the 65 node is invalid so again uh we we</p>



<p class="wp-block-paragraph">need to do something here&#8217;s another quick example before we move on to actually identifying which</p>



<p class="wp-block-paragraph">nodes to to modify and rotate just real fast i want to show you you know that a binary search</p>



<p class="wp-block-paragraph">tree could actually end up being a linear tree if you had really really really bad data the binary</p>



<p class="wp-block-paragraph">This is slow because this is a linear tree.</p>



<p class="wp-block-paragraph">The time complexity of searching through this tree is linear time.</p>



<p class="wp-block-paragraph">It scales linearly with the number of nodes in your data set.</p>



<p class="wp-block-paragraph">That&#8217;s really far away from log time, which is supposed to be lightning fast.</p>



<p class="wp-block-paragraph">So an AVL tree will fix this kind of bad data.</p>



<p class="wp-block-paragraph">Let&#8217;s take a step back here and focus your attention for a second on these three nodes.</p>



<p class="wp-block-paragraph">We noticed that there is one node that&#8217;s actually out of whack.</p>



<p class="wp-block-paragraph">It&#8217;s the 65 node, right?</p>



<p class="wp-block-paragraph">What we&#8217;re going to do is we&#8217;re going to find a trinode subtree that starts with the first node or the lowest node we can find that&#8217;s out of whack.</p>



<p class="wp-block-paragraph">It&#8217;s the 65 node.</p>



<p class="wp-block-paragraph">And you can tell that if we just kind of go down a level and down another level to select the other two nodes,</p>



<p class="wp-block-paragraph">it&#8217;s just going to be this little, you know, subtree of three nodes or otherwise known as a trinode subtree.</p>



<p class="wp-block-paragraph">So here&#8217;s kind of a diagram for that.</p>



<p class="wp-block-paragraph">Whoops, forgot to delete that earlier.</p>



<p class="wp-block-paragraph">Let me get rid of that.</p>



<p class="wp-block-paragraph">So this is the trinode subtree that we selected, right?</p>



<p class="wp-block-paragraph">So we&#8217;ve got the 65 and the 48 and the 55.</p>



<p class="wp-block-paragraph">How did we actually select this?</p>



<p class="wp-block-paragraph">So what you kind of want to do is the first node that is out of whack,</p>



<p class="wp-block-paragraph">you want to call that Z.</p>



<p class="wp-block-paragraph">So I&#8217;m going to write a Z here.</p>



<p class="wp-block-paragraph">What we&#8217;re looking for is a trinode subtree,</p>



<p class="wp-block-paragraph">which we&#8217;ll first call X and Y and Z.</p>



<p class="wp-block-paragraph">And then we&#8217;ll end up reordering the nodes</p>



<p class="wp-block-paragraph">and then rearranging all the pointers to the nodes</p>



<p class="wp-block-paragraph">so that the trinode subtree is a little bit more in balance.</p>



<p class="wp-block-paragraph">more in balance. So we see the Z node. We have to find the Y node next. To find the Y node,</p>



<p class="wp-block-paragraph">what&#8217;s going on with my computer? Oh, there we go. We have the Z node. To find the Y node,</p>



<p class="wp-block-paragraph">really, we&#8217;re just going to take the child of the 65 node that has the taller subtree. So</p>



<p class="wp-block-paragraph">there&#8217;s only one subtree, you know, on the left, there&#8217;s no subtree on the right of the 65 node.</p>



<p class="wp-block-paragraph">So the choice is obvious. But just so you know, if there was, you know, another, you know, child</p>



<p class="wp-block-paragraph">another you know child hanging off of the right for some reason we would still choose the number</p>



<p class="wp-block-paragraph">48 node as the y node because it has the taller subtree notice how the subtree on the left is two</p>



<p class="wp-block-paragraph">and the subtree on the right has a height of one so always take the taller subtree when you&#8217;re</p>



<p class="wp-block-paragraph">looking for uh y and then x so then we do the same thing we go down to the grandchild of the z node</p>



<p class="wp-block-paragraph">and if we had a choice to go left or right again we would always choose the taller subtree in this</p>



<p class="wp-block-paragraph">taller subtree in this case there&#8217;s only one choice so that x node is going to be the 55</p>



<p class="wp-block-paragraph">now in this next slide i&#8217;ve kind of redrawn this in a little bit different of a way</p>



<p class="wp-block-paragraph">notice the trinode subtree that we just selected here the 65 48 55 subtree it has a height of three</p>



<p class="wp-block-paragraph">i mean that should be obvious right it&#8217;s just got a height of three</p>



<p class="wp-block-paragraph">or height. Notice on the left, these are the same three nodes. Exactly. 48, 55, and 65. That&#8217;s the</p>



<p class="wp-block-paragraph">same three nodes that we originally had. But notice how the height is too. So the basic idea for ABL</p>



<p class="wp-block-paragraph">trees is, you know, compute the balance factors. And as soon as you identify a node that&#8217;s out of</p>



<p class="wp-block-paragraph">whack, you grab a tri-node sub-tree starting at the node that&#8217;s out of whack, the Z node.</p>



<p class="wp-block-paragraph">And then we&#8217;ll perform, quote unquote, a rotation where we just kind of smoosh it to be a little</p>



<p class="wp-block-paragraph">be a little bit more balanced and a little bit more a little bit shorter so the height goes from</p>



<p class="wp-block-paragraph">three to two once we do that the rest of the tree it&#8217;s going to get a little bit better you&#8217;ll notice</p>



<p class="wp-block-paragraph">that this will end up sometimes completely but sometimes partially fixing bad balance factors</p>



<p class="wp-block-paragraph">in the tree so um this is the end of the intro i only wanted to spend a little time just kind of</p>



<p class="wp-block-paragraph">like warming you up to the idea in the next videos uh we&#8217;re going to actually look at performing</p>



<p class="wp-block-paragraph">identifying the four different types of rotations that you could see and like how to do them.</p>



<p class="wp-block-paragraph">We&#8217;ll talk more about the idea that we&#8217;re really just rearranging pointers here.</p>



<p class="wp-block-paragraph">If you&#8217;re thinking about code, you&#8217;re thinking about, oh, I&#8217;ve got some pointers between these</p>



<p class="wp-block-paragraph">nodes. We&#8217;re not going to delete any nodes or add any nodes. We&#8217;re just going to like</p>



<p class="wp-block-paragraph">rearrange who is a parent of who, who is a child of who. And then eventually we&#8217;ll deal with some</p>



<p class="wp-block-paragraph">really bad trees and we&#8217;ll become experts at just like maintaining ABL trees.</p>



<p class="wp-block-paragraph">talk about ways to visualize the rotations, which makes it a little bit easier to remember these</p>



<p class="wp-block-paragraph">patterns and stuff. Okay, so thank you for watching this video. I hope you learned a little bit of</p>



<p class="wp-block-paragraph">stuff and had a little bit of fun. In the next one, we&#8217;re going to get started. We&#8217;re going to</p>



<p class="wp-block-paragraph">go deep. I&#8217;m going to get my gear. Sorry.</p>



<p class="wp-block-paragraph">Hey, everybody. Thanks for watching this video again from the bottom of my heart. I really</p>



<p class="wp-block-paragraph">from the bottom of my heart. I really appreciate it. I do hope you did learn something and have</p>



<p class="wp-block-paragraph">some fun. If you could do me a please, a small little favor, could you please subscribe and</p>



<p class="wp-block-paragraph">follow this channel or these videos or whatever it is you do on the current social media website</p>



<p class="wp-block-paragraph">that you&#8217;re looking at right now? It would really mean the world to me and it&#8217;ll help make more</p>



<p class="wp-block-paragraph">videos and grow this community. So we&#8217;ll be able to do more videos, longer videos, better videos,</p>



<p class="wp-block-paragraph">Do me a kindness and subscribe.</p>



<p class="wp-block-paragraph">You know, sometimes I&#8217;m sleeping in the middle of the night and I just wake up because I know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy.</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me or you could troll me if you want to just wake me up in the middle of the night.</p>



<p class="wp-block-paragraph">Just subscribe and then I&#8217;ll just wake up.</p>



<p class="wp-block-paragraph">I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now, you should see a QR code which you can scan in order to go to the website.</p>



<p class="wp-block-paragraph">in order to go to the website which i think is also named somewhere at the bottom of this video</p>



<p class="wp-block-paragraph">and it&#8217;ll take you to my main website where you can just kind of like see all the videos i published</p>



<p class="wp-block-paragraph">and the services and tutorials and things that i offer and all that good stuff and uh</p>



<p class="wp-block-paragraph">if you have a suggestion for uh uh clarifications or errata or just future videos that you want to</p>



<p class="wp-block-paragraph">see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on you know</p>



<p class="wp-block-paragraph">I also wake up for those in the middle of the night. I get I wake up in a cold sweat and I&#8217;m like</p>



<p class="wp-block-paragraph">it would really it really mean the world to me. I would really appreciate it. So again thank you</p>



<p class="wp-block-paragraph">so much for watching this video and enjoy the cool music as I fade into the darkness</p>



<p class="wp-block-paragraph">which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/avl-tree-tutorial-balance-factors-and-why-they-fix-slow-bsts/">AVL Tree Tutorial: Balance Factors and Why They Fix Slow BSTs</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/avl-tree-tutorial-balance-factors-and-why-they-fix-slow-bsts/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Search in a Binary Search Tree (BST) &#8211; Step by Step Explanation with Examples</title>
		<link>https://www.NeuralLantern.com/how-to-search-in-a-binary-search-tree-bst-step-by-step-explanation-with-examples/</link>
					<comments>https://www.NeuralLantern.com/how-to-search-in-a-binary-search-tree-bst-step-by-step-explanation-with-examples/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sat, 02 May 2026 23:54:59 +0000</pubDate>
				<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[binary search tree tutorial]]></category>
		<category><![CDATA[binary tree search]]></category>
		<category><![CDATA[BST explained]]></category>
		<category><![CDATA[BST search]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[data structures and algorithms]]></category>
		<category><![CDATA[log n time]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[search in BST]]></category>
		<category><![CDATA[tree traversal]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=336</guid>

					<description><![CDATA[<p>Learn how to search in a Binary Search Tree. We demonstrate searching for existing and non-existing values, explain why BST search is O(log n) on average, and show how poor data ordering can turn your tree into a slow linear structure similar to a linked list. Includes discussion of tree height and balanced vs unbalanced BSTs.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-to-search-in-a-binary-search-tree-bst-step-by-step-explanation-with-examples/">How to Search in a Binary Search Tree (BST) &#8211; Step by Step Explanation with Examples</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="How to Search in a Binary Search Tree (BST) - Step by Step Explanation with Examples" width="1380" height="776" src="https://www.youtube.com/embed/FCz9IbLywI4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">In this video we walk through how to perform searches in a Binary Search Tree. Starting with a perfectly balanced BST, we cover the search process step by step, including finding existing nodes like 73 and determining that numbers like 44 do not exist in the tree.</p>



<p class="wp-block-paragraph">We discuss time complexity for searches in BSTs &#8211; O(log n) on average for balanced trees and O(n) in the worst case when the tree becomes skewed like a linked list due to sorted or bad data. Learn why each comparison eliminates half the remaining data and how height affects performance.</p>



<p class="wp-block-paragraph">Perfect for students learning data structures and algorithms. If you&#8217;re studying BSTs, this clear explanation will help you understand searching, insertion paths, and why self-balancing trees matter.</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">00:00 Introduction to BST Search<br>00:31 BST Not Self-Balancing<br>00:40 Average Case Log Time<br>00:53 Linear Time in Worst Case<br>01:13 How BST Search Works<br>01:18 Search Path Example<br>02:08 Searching for Existing Node<br>02:35 Searching for Non-Existent Node<br>03:19 Tree Size and Height<br>03:30 Time Complexity O(h)<br>05:40 Bad Data Example<br>08:23 Skewed Tree Like Linked List<br>09:53 Linear Time in Worst Case<br>11:58 Why Log Time<br>12:36 Halving the Search Space<br>14:14 O(log n) Summary<br>14:28 Conclusion</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p class="wp-block-paragraph">Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to the Blog: https://www.NeuralLantern.com</li>



<li>Watch the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">hey there let&#8217;s perform searches in a binary search tree</p>



<p class="wp-block-paragraph">okay if you watch my previous videos you know how to define a binary search tree by now</p>



<p class="wp-block-paragraph">you know some terminology you know how to construct a tree and so now let&#8217;s just take</p>



<p class="wp-block-paragraph">a tree that&#8217;s already been constructed and let&#8217;s search through it first thing i want to say is</p>



<p class="wp-block-paragraph">first thing I want to say is this is a binary search tree this is not a self</p>



<p class="wp-block-paragraph">balancing tree so in the future this tree could get lopsided and messed up</p>



<p class="wp-block-paragraph">depending on what kind of data we add we can always expect that binary search</p>



<p class="wp-block-paragraph">trees have a search time or an insert time or removal time of log log base 2</p>



<p class="wp-block-paragraph">of n on average assuming that the data is random or in the worst possible case</p>



<p class="wp-block-paragraph">scenario linear time so these could get slower right now you can see this tree</p>



<p class="wp-block-paragraph">Right now you can see this tree is perfectly balanced.</p>



<p class="wp-block-paragraph">So if we say that our tree is currently perfectly balanced and it&#8217;s always going to be perfectly balanced,</p>



<p class="wp-block-paragraph">which is a fantasy, but let&#8217;s just say that it is,</p>



<p class="wp-block-paragraph">then we could just say it&#8217;s a log tree.</p>



<p class="wp-block-paragraph">It&#8217;s going to take log time to search through.</p>



<p class="wp-block-paragraph">So how do we perform searches?</p>



<p class="wp-block-paragraph">Again, if you saw my previous videos, you should know that it&#8217;s really easy to find out where a node belongs.</p>



<p class="wp-block-paragraph">Like for example, if I was going to add the number 14,</p>



<p class="wp-block-paragraph">I would first look at the root node and I&#8217;d say, okay, the root node is occupied.</p>



<p class="wp-block-paragraph">and I&#8217;d say, okay, the root node is occupied.</p>



<p class="wp-block-paragraph">And so where would 14 belong?</p>



<p class="wp-block-paragraph">Well, it would belong on the left of 37</p>



<p class="wp-block-paragraph">because it&#8217;s less than 37.</p>



<p class="wp-block-paragraph">It would belong on the left of 16</p>



<p class="wp-block-paragraph">because it&#8217;s less than 16.</p>



<p class="wp-block-paragraph">It would belong on the right of 9</p>



<p class="wp-block-paragraph">because it&#8217;s greater than 9.</p>



<p class="wp-block-paragraph">It would belong on the right of 11</p>



<p class="wp-block-paragraph">because it&#8217;s greater than 11.</p>



<p class="wp-block-paragraph">And so the 14 node that we just wanted to add,</p>



<p class="wp-block-paragraph">is that what I just said, 14?</p>



<p class="wp-block-paragraph">Would end up going there.</p>



<p class="wp-block-paragraph">So when you&#8217;re searching through a binary search tree,</p>



<p class="wp-block-paragraph">you really just have to go find</p>



<p class="wp-block-paragraph">have to go find where the node would belong as if you&#8217;re performing insert or you could think of it</p>



<p class="wp-block-paragraph">the other way around like insert is really thinking of a search first but basically we&#8217;re just going</p>



<p class="wp-block-paragraph">to start by saying you know pick a number and we&#8217;ll see if it exists so uh let&#8217;s pick the number</p>



<p class="wp-block-paragraph">67 or actually let&#8217;s pick the number 73 just to see if it exists in the tree so 73 i&#8217;m going to</p>



<p class="wp-block-paragraph">do like you know seven three with like a question mark we check the root node 73 belongs on the</p>



<p class="wp-block-paragraph">side we look at the 59 it belongs on the right side and we found our 73 so 73 exists in the tree</p>



<p class="wp-block-paragraph">and it only took us three examinations only three examinations let&#8217;s look for a number that doesn&#8217;t</p>



<p class="wp-block-paragraph">actually exist in the tree so maybe oh my thing is freezing what&#8217;s going on okay so maybe uh let&#8217;s</p>



<p class="wp-block-paragraph">The 37 here, 44 belongs on the right of 37.</p>



<p class="wp-block-paragraph">We look at the 59, it belongs on the left.</p>



<p class="wp-block-paragraph">We look at the 48, it belongs on the left.</p>



<p class="wp-block-paragraph">We look at the 43, it belongs on the right.</p>



<p class="wp-block-paragraph">The 44 would have been there, but it&#8217;s not because that right child pointer off the 43</p>



<p class="wp-block-paragraph">is null.</p>



<p class="wp-block-paragraph">So we only had to really examine one, two, three, four nodes in order to determine that</p>



<p class="wp-block-paragraph">our number 44 didn&#8217;t exist in the tree.</p>



<p class="wp-block-paragraph">Notice how we&#8217;re not doing a lot of examinations compared to the total size of the tree.</p>



<p class="wp-block-paragraph">compared to the total size of the tree.</p>



<p class="wp-block-paragraph">So let me just write this up here.</p>



<p class="wp-block-paragraph">How many nodes do we have?</p>



<p class="wp-block-paragraph">1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15.</p>



<p class="wp-block-paragraph">We got 15 nodes up here.</p>



<p class="wp-block-paragraph">Someone say N is equal to 15.</p>



<p class="wp-block-paragraph">And what&#8217;s the height of the tree?</p>



<p class="wp-block-paragraph">Remember, the height of the tree is the number of nodes you must touch</p>



<p class="wp-block-paragraph">as you make your way down towards the deepest node.</p>



<p class="wp-block-paragraph">Or if you&#8217;ve already found the deepest node,</p>



<p class="wp-block-paragraph">you take its depth and you just add 1, assuming that depths start at 0.</p>



<p class="wp-block-paragraph">start at zero. So I&#8217;ll just say that the depth of the root node is zero and the 59 and the 16 are</p>



<p class="wp-block-paragraph">one. And then that third row is two. And that last row is three. And then I just add one to the</p>



<p class="wp-block-paragraph">deepest node that I can find, which is just going to have a depth of three. All of these on the</p>



<p class="wp-block-paragraph">bottom row have a depth of three. So the height of this tree is actually four, because we would</p>



<p class="wp-block-paragraph">so like 37 59 48 43 that&#8217;s four touches um or the or the depth of the deepest node plus one okay um</p>



<p class="wp-block-paragraph">all trees all binary search trees whether they&#8217;re actually avl trees or regular binary search trees</p>



<p class="wp-block-paragraph">or whether they&#8217;re balanced or imbalanced they&#8217;re always going to have a time complexity to search</p>



<p class="wp-block-paragraph">through of o of h meaning in the worst case scenario you&#8217;ll have to touch h number of nodes</p>



<p class="wp-block-paragraph">order to find a node by search query or to add a new node or even to delete a new node.</p>



<p class="wp-block-paragraph">So how does that compare to this tree in reality?</p>



<p class="wp-block-paragraph">Let me just open up a little calculator real fast.</p>



<p class="wp-block-paragraph">I just want to show you.</p>



<p class="wp-block-paragraph">So if we do log base two, because we have a binary search tree, not a trinary search</p>



<p class="wp-block-paragraph">nodes 15 it tells us that it should take us no more than four examinations to find the node that</p>



<p class="wp-block-paragraph">we&#8217;re looking for if the tree was perfectly balanced and if you if you if you notice that&#8217;s</p>



<p class="wp-block-paragraph">exactly what the height is telling us here if the tree was more imbalanced the height would get</p>



<p class="wp-block-paragraph">really bad compared to the number of nodes and it would no longer be considered a log tree it would</p>



<p class="wp-block-paragraph">just be you know somewhere in between linear time and log time um let&#8217;s see what else can i show you</p>



<p class="wp-block-paragraph">let&#8217;s do well we did that computation let&#8217;s do a search through the tree that</p>



<p class="wp-block-paragraph">through a tree that&#8217;s actually really really bad let&#8217;s pretend that we have a</p>



<p class="wp-block-paragraph">bunch of nodes here and you know I&#8217;ve got an old story that I love to tell</p>



<p class="wp-block-paragraph">about this when like when my grandma was alive she was kind of bitter towards the</p>



<p class="wp-block-paragraph">end you know I loved her but she used to call the cops on her neighbors for</p>



<p class="wp-block-paragraph">gossip and at some point in time she got into a feud with her neighbor who was</p>



<p class="wp-block-paragraph">with her neighbor who was also a bitter old lady.</p>



<p class="wp-block-paragraph">You know, towards the end, it kind of happens sometimes.</p>



<p class="wp-block-paragraph">It sucks, but it happened.</p>



<p class="wp-block-paragraph">And I guess the neighbor didn&#8217;t like my grandma&#8217;s tree</p>



<p class="wp-block-paragraph">that she had in her backyard.</p>



<p class="wp-block-paragraph">So they fought over it, they argued over it,</p>



<p class="wp-block-paragraph">and then one day my grandma said</p>



<p class="wp-block-paragraph">she was looking out the backyard window</p>



<p class="wp-block-paragraph">to yell at ducks who were jumping into her pool</p>



<p class="wp-block-paragraph">because she just hated when the ducks went into her pool.</p>



<p class="wp-block-paragraph">She was waiting for them to show up.</p>



<p class="wp-block-paragraph">She wasn&#8217;t even looking at ducks.</p>



<p class="wp-block-paragraph">even looking at ducks she was just waiting and she saw the other little old lady reach her arm over</p>



<p class="wp-block-paragraph">the fence and spray my grandma&#8217;s tree with some sort of poison then my grandma&#8217;s tree died and uh</p>



<p class="wp-block-paragraph">i guess you know my grandma never never got over it i always thought it was like a funny story it</p>



<p class="wp-block-paragraph">was a little sad but um she poisoned my grandma&#8217;s tree so we can kind of do the same thing with the</p>



<p class="wp-block-paragraph">binary search tree we can give bad data let me show you what happens here if i use this data set</p>



<p class="wp-block-paragraph">search tree. So we&#8217;re going to add this node right here. It&#8217;s going to be the 14.</p>



<p class="wp-block-paragraph">And then when we add the 19 node, I&#8217;m just going to duplicate it. The 19 is supposed to be on the</p>



<p class="wp-block-paragraph">right side of 14 because it&#8217;s greater than 14. So I&#8217;m just going to like, you know, draw a 19 right</p>



<p class="wp-block-paragraph">here and a little connecting line. Okay. Then when we add the 24, I&#8217;m going to duplicate this node</p>



<p class="wp-block-paragraph">the 24 go belongs on the right side of the 14 and also belongs on the right side of the 19</p>



<p class="wp-block-paragraph">because it&#8217;s greater than 19 so again we have a node that shows up on the right side let me</p>



<p class="wp-block-paragraph">remember to actually fix the 19 so it looks like 24. then when we add the uh 29 it&#8217;s gonna end up</p>



<p class="wp-block-paragraph">being the same thing right the 29 is gonna end up showing on the right the right the right it&#8217;s</p>



<p class="wp-block-paragraph">If we have bad data, we can end up with a tree that kind of looks like a straight line.</p>



<p class="wp-block-paragraph">And if you are watching this video from the future,</p>



<p class="wp-block-paragraph">after I&#8217;ve posted all of my data structures videos that I have,</p>



<p class="wp-block-paragraph">you might notice that this looks like another data structure.</p>



<p class="wp-block-paragraph">If you&#8217;re watching this right away, I haven&#8217;t posted those videos just yet.</p>



<p class="wp-block-paragraph">But just tilt your head to the right a little bit</p>



<p class="wp-block-paragraph">and tell me if that looks like something that you recognize.</p>



<p class="wp-block-paragraph">It looks like a linked list.</p>



<p class="wp-block-paragraph">looks like a linked list just like a straight line or even i guess you could kind of say maybe an array</p>



<p class="wp-block-paragraph">um just like a straight line of data there&#8217;s no uh you know there&#8217;s no splitting of the data</p>



<p class="wp-block-paragraph">so we have the 29 and then the 35 and then uh maybe i&#8217;ll just do one more node only and i&#8217;ll</p>



<p class="wp-block-paragraph">get rid of that 54 because that&#8217;s too many nodes so what happens is if you have you know bad data</p>



<p class="wp-block-paragraph">tree doesn&#8217;t actually search or add insert or remove or anything like that in log time anymore</p>



<p class="wp-block-paragraph">instead it operates in linear time just like a linked list would meaning we would have to scan</p>



<p class="wp-block-paragraph">through every single node in the entire tree to be sure that some value did not exist well</p>



<p class="wp-block-paragraph">not exactly but in the worst case scenario we&#8217;d have to scan through the whole entire tree</p>



<p class="wp-block-paragraph">for example let&#8217;s just look at the the number of nodes real fast we&#8217;ll say the</p>



<p class="wp-block-paragraph">number of nodes here is one two three four oh I put two 24s in there oh dearie</p>



<p class="wp-block-paragraph">let me get that says 14 19 24 29 and then I guess I got to make that a 35 oh</p>



<p class="wp-block-paragraph">okay and then there&#8217;s a 49 I guess I did add every single node okay so we added</p>



<p class="wp-block-paragraph">so there&#8217;s like seven uh items in the tree so i&#8217;m going to say n is equal to seven the height also</p>



<p class="wp-block-paragraph">is seven though so the height is seven notice how if we say that it&#8217;s always true that a binary</p>



<p class="wp-block-paragraph">search tree has a time complexity in the worst case of o of h meaning you&#8217;d have to go down</p>



<p class="wp-block-paragraph">in the worst case the number of levels that is equal to the whole entire height of the entire</p>



<p class="wp-block-paragraph">tree that&#8217;s also the number of nodes in the tree which means uh if i wanted to add let&#8217;s say the</p>



<p class="wp-block-paragraph">I don&#8217;t know, if you wanted to add the number 60,</p>



<p class="wp-block-paragraph">60 would end up going down here.</p>



<p class="wp-block-paragraph">And in order to find where 60 would go,</p>



<p class="wp-block-paragraph">we&#8217;d have to look at the 14, that&#8217;s 1, 19, 24.</p>



<p class="wp-block-paragraph">We&#8217;d have to look at all these nodes.</p>



<p class="wp-block-paragraph">That is seven nodes.</p>



<p class="wp-block-paragraph">We&#8217;d have to examine seven nodes</p>



<p class="wp-block-paragraph">just to figure out where the 60 would go.</p>



<p class="wp-block-paragraph">Or if we were searching for 60,</p>



<p class="wp-block-paragraph">then we&#8217;d have to go down that far</p>



<p class="wp-block-paragraph">to find out that 60 didn&#8217;t exist.</p>



<p class="wp-block-paragraph">So this is nowhere near a log tree.</p>



<p class="wp-block-paragraph">This is a linear tree, a linear time tree.</p>



<p class="wp-block-paragraph">year time tree and just to drive the point further if we did log base two of the number of nodes in</p>



<p class="wp-block-paragraph">this case seven notice how it&#8217;s telling us that we should if it was a log tree if it was perfectly</p>



<p class="wp-block-paragraph">balanced or a self-balancing tree we should have to touch no more than three nodes in order to find</p>



<p class="wp-block-paragraph">what we&#8217;re looking for to search through the tree or to insert a new node or even to delete a node</p>



<p class="wp-block-paragraph">but that&#8217;s not the case here because the tree is really really slow compared to a perfectly balanced</p>



<p class="wp-block-paragraph">slow compared to a perfectly balanced tree.</p>



<p class="wp-block-paragraph">So we call this a linear tree.</p>



<p class="wp-block-paragraph">It sucks.</p>



<p class="wp-block-paragraph">You want to try to avoid that.</p>



<p class="wp-block-paragraph">You don&#8217;t want your data to be poisoned.</p>



<p class="wp-block-paragraph">On average, your trees won&#8217;t really look like this if you just have totally</p>



<p class="wp-block-paragraph">random data, but if you think there&#8217;s a chance that your data might be skewed</p>



<p class="wp-block-paragraph">in some way, it&#8217;s probably a good idea to upgrade to a self-balancing tree,</p>



<p class="wp-block-paragraph">which we&#8217;re going to talk about later.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">What else can I tell you real fast?</p>



<p class="wp-block-paragraph">So we talked about searching.</p>



<p class="wp-block-paragraph">why are these trees considered log time? I mean, they&#8217;re really, really fast, but why?</p>



<p class="wp-block-paragraph">Let&#8217;s just duplicate this slide here and I&#8217;ll move it down one level. I just want to kind of</p>



<p class="wp-block-paragraph">show you why this ends up being a log time tree. Okay. So why, why is this a log time tree?</p>



<p class="wp-block-paragraph">Suppose for the sake of argument, we&#8217;re searching for the number 52. So if we&#8217;re searching for the</p>



<p class="wp-block-paragraph">node the root node and decide where would the 52 belong let me actually write that down so i don&#8217;t</p>



<p class="wp-block-paragraph">forget 52 question mark where would the 52 belong it would belong on the right side of the 37 because</p>



<p class="wp-block-paragraph">it&#8217;s greater than 37 right which means it&#8217;s actually impossible that the 52 would be in any</p>



<p class="wp-block-paragraph">of these other nodes on the left side there&#8217;s no need to check them we&#8217;re not going to scan the</p>



<p class="wp-block-paragraph">whole tree we&#8217;re just going to go left or right and ignore the whole other side of the tree</p>



<p class="wp-block-paragraph">we just did is with one examination we eliminated half of the remaining data set or i guess of the</p>



<p class="wp-block-paragraph">initial data set if we&#8217;re looking at the root node so then the next thing that we do is we just kind</p>



<p class="wp-block-paragraph">of say okay it belongs on the right side so we look at the 59 node and we do the same thing we</p>



<p class="wp-block-paragraph">say where would 52 be would it be on the left side or the right side of the 59 node it would be on</p>



<p class="wp-block-paragraph">the left side which means we&#8217;re going to eliminate from consideration the entire right subtree of the</p>



<p class="wp-block-paragraph">So again, we&#8217;re eliminating half of the remaining data set.</p>



<p class="wp-block-paragraph">Then we kind of go down here and we look at the 48.</p>



<p class="wp-block-paragraph">We do the same thing.</p>



<p class="wp-block-paragraph">52 belongs on the right side.</p>



<p class="wp-block-paragraph">So we eliminate half of the remaining data set, you know, and then eventually we do find</p>



<p class="wp-block-paragraph">the 52 and then that&#8217;s it.</p>



<p class="wp-block-paragraph">So if we&#8217;re eliminating half of the data set with every single, just one examination at</p>



<p class="wp-block-paragraph">a time, that&#8217;s why it ends up being log base two.</p>



<p class="wp-block-paragraph">getting twice as close to your destination at every step you took if you&#8217;re like in a car like</p>



<p class="wp-block-paragraph">running maybe you&#8217;re on the moon or something then it would be a log time you know uh progress</p>



<p class="wp-block-paragraph">towards your goal or eliminating half of the data set at every single step sometimes people see</p>



<p class="wp-block-paragraph">for loops on exams and stuff when we talk about this um like for i equals zero uh i is less than</p>



<p class="wp-block-paragraph">it would be like i times equals two.</p>



<p class="wp-block-paragraph">So i&#8217;s jumping twice as far each time.</p>



<p class="wp-block-paragraph">That&#8217;s the sort of thing that makes it log.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to write down log here real fast</p>



<p class="wp-block-paragraph">to drive the point home a little bit more.</p>



<p class="wp-block-paragraph">O of log base two of n.</p>



<p class="wp-block-paragraph">Yeah, so I guess that&#8217;s all I wanted to tell you in this video.</p>



<p class="wp-block-paragraph">Hopefully you feel like an expert in searching now.</p>



<p class="wp-block-paragraph">If you want to see more searching videos,</p>



<p class="wp-block-paragraph">leave a comment or something like that.</p>



<p class="wp-block-paragraph">leave a comment or something like that. But yeah, thank you for watching. And I hope you</p>



<p class="wp-block-paragraph">learned a little bit of stuff and had a little bit of fun. I&#8217;ll see you in the next video.</p>



<p class="wp-block-paragraph">I&#8217;m going to go find a blueberry muffin.</p>



<p class="wp-block-paragraph">Hey, everybody. Thanks for watching this video again from the bottom of my heart. I really</p>



<p class="wp-block-paragraph">appreciate it. I do hope you did learn something and have some fun. If you could do me a please,</p>



<p class="wp-block-paragraph">subscribe and follow this channel or these videos or whatever it is you do on the current social</p>



<p class="wp-block-paragraph">media website that you&#8217;re looking at right now it would really mean the world to me and it&#8217;ll</p>



<p class="wp-block-paragraph">help make more videos and grow this community so we&#8217;ll be able to do more videos longer videos</p>



<p class="wp-block-paragraph">better videos or just i&#8217;ll be able to keep making videos in general so please do do me a kindness</p>



<p class="wp-block-paragraph">and uh and subscribe you know sometimes i&#8217;m sleeping in the middle of the night and i just</p>



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy. That&#8217;s exactly what happens every single time</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me or you could you control me if you want to just wake me up in the middle</p>



<p class="wp-block-paragraph">And I just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now</p>



<p class="wp-block-paragraph">You should see a QR code which you can scan in order to go to the website</p>



<p class="wp-block-paragraph">Which I think is also named somewhere at the bottom of this video and it&#8217;ll take you to my main website where you can just kind of like</p>



<p class="wp-block-paragraph">and the services and tutorials and things that I offer and all that good stuff and</p>



<p class="wp-block-paragraph">if you have a suggestion for</p>



<p class="wp-block-paragraph">Clarifications or errata or just future videos that you want to see please leave a comment or if you just want to say hey</p>



<p class="wp-block-paragraph">What&#8217;s up? What&#8217;s going on? You know, just send me a comment, whatever</p>



<p class="wp-block-paragraph">I also wake up for those in the middle of the night. I get I wake up in a cold sweat. I&#8217;m like this</p>



<p class="wp-block-paragraph">It would really it really mean the world to me. I would really appreciate it. So</p>



<p class="wp-block-paragraph">So again, thank you so much for watching this video and enjoy the cool music as I fade into the darkness, which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-to-search-in-a-binary-search-tree-bst-step-by-step-explanation-with-examples/">How to Search in a Binary Search Tree (BST) &#8211; Step by Step Explanation with Examples</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/how-to-search-in-a-binary-search-tree-bst-step-by-step-explanation-with-examples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
