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

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>cs data structures Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/cs-data-structures/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Binary Search Tree Removals &#8211; Delete Nodes with 0, 1, or 2 Children</title>
		<link>https://www.NeuralLantern.com/binary-search-tree-removals-delete-nodes-with-0-1-or-2-children/</link>
					<comments>https://www.NeuralLantern.com/binary-search-tree-removals-delete-nodes-with-0-1-or-2-children/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 11 May 2026 01:54:41 +0000</pubDate>
				<category><![CDATA[Binary Search Trees]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[binary search tree]]></category>
		<category><![CDATA[binary search tree remove]]></category>
		<category><![CDATA[binary tree deletion]]></category>
		<category><![CDATA[bst deletion]]></category>
		<category><![CDATA[bst operations]]></category>
		<category><![CDATA[bst remove node]]></category>
		<category><![CDATA[coding interview]]></category>
		<category><![CDATA[cs data structures]]></category>
		<category><![CDATA[data structures tutorial]]></category>
		<category><![CDATA[delete node bst]]></category>
		<category><![CDATA[inorder successor]]></category>
		<category><![CDATA[tree algorithms]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=340</guid>

					<description><![CDATA[<p>So there are three types of deletions... Is it a leaf with zero children? Is it a node with one child? Or is it a node with two children? Each type of node is deleted in a different way... For the hardest type we use the in-order successor.</p>
<p>The post <a href="https://www.NeuralLantern.com/binary-search-tree-removals-delete-nodes-with-0-1-or-2-children/">Binary Search Tree Removals &#8211; Delete Nodes with 0, 1, or 2 Children</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Binary Search Tree Removals - Delete Nodes with 0, 1, or 2 Children" width="1380" height="776" src="https://www.youtube.com/embed/TPAiCeiMw18?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>Hello there. In this video we break down exactly how to delete nodes from a binary search tree. We cover all three cases step by step: deleting a leaf node with zero children, deleting a node with one child by promoting it, and the trickiest case &#8211; deleting a node with two children using the in-order successor method.</p>



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



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



<p>If you&#8217;re learning BST insert, search, and now removal, this completes the core operations.</p>



<p>00:00 Introduction to BST Deletions<br>00:10 BST Review and Setup<br>00:46 Three Types of Deletions<br>00:56 Deleting a Leaf Node (Zero Children)<br>03:21 Pointer Manipulation for Leaf Deletion<br>05:25 Deleting a Node with One Child<br>07:17 Pointer Updates for One Child Deletion<br>10:09 Deleting a Node with Two Children<br>12:08 In-Order Traversal and Successor Concept<br>13:18 Finding In-Order Successor<br>14:20 Copying Successor Value and Recursive Delete<br>18:28 Deleting the Root Node Example<br>22:56 Additional One Child Successor Example<br>25:12 Time Complexity Summary<br>26:08 Closing and Outro</p>



<p>=-=-=-=-=-=-=-=-=</p>



<p>Thanks for watching!</p>



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



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



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



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



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



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



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



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



<p>Please show your support!</p>



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



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



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



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



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



<p>Hello there. Let&#8217;s talk about deletions in a binary search tree.</p>



<p>Okay, so hopefully at this point you already understand binary search trees</p>



<p>in all other aspects than just deleting. If you don&#8217;t, see my other videos. In my other videos,</p>



<p>we talk about how to identify a binary search tree, all the rules that define it,</p>



<p>search tree with the insert or add operation.</p>



<p>We talk about searching through a binary search tree and time complexities</p>



<p>and even drawing the diagram in a really nice and pretty way so that your trees</p>



<p>are easier to debug.</p>



<p>So for now, I&#8217;m just going to say it&#8217;s time to learn how to delete notes from</p>



<p>a binary search tree.</p>



<p>So there are three types of deletions.</p>



<p>Actually, let me just kind of like, for example,</p>



<p>suppose you want to delete to delete the number 48 from the tree.</p>



<p>48 from the tree. Okay. Well, first you&#8217;d have to go search for 48. So already, you know,</p>



<p>this is going to be an O of H operation or log time. If the tree was perfectly balanced</p>



<p>or linear, if the tree was really poorly balanced. So we&#8217;re going to search for,</p>



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



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



<p>How many children does it have?</p>



<p>Is it a leaf with zero children?</p>



<p>Is it a node with one child?</p>



<p>Or is it a node with two children?</p>



<p>Each type of node is deleted in a different way.</p>



<p>So let&#8217;s see, the first type we would delete</p>



<p>is a node with zero children,</p>



<p>also known as a leaf, also known as a, sorry, external node.</p>



<p>The second type of deletion is a node with one child.</p>



<p>with one child and the third type is going to be the hardest type we&#8217;ll do that at the end of the</p>



<p>video it&#8217;s going to be a node with two children uh numbers two and three are going to be internal</p>



<p>nodes but i hope you already know that because you watch the videos anyway so let&#8217;s start off</p>



<p>with the easiest possible node to delete first just a regular leaf so i&#8217;m going to delete this</p>



<p>stuff real quick um and then just kind of like erase that and down and down okay so let&#8217;s identify</p>



<p>so let&#8217;s identify a leaf well it could be anything that just doesn&#8217;t have any</p>



<p>children of its own let&#8217;s do let&#8217;s do the number 11 just to make things a</p>



<p>little bit easier so let&#8217;s delete the number 11 I&#8217;ll put 11 here we first</p>



<p>search for 11 so we look at the root node 11 belongs on the left we look down</p>



<p>here 11 belongs on the left we look down here 11 belongs on the right we find 11</p>



<p>you know if we reach the end of the tree and we couldn&#8217;t find it then it&#8217;s like I</p>



<p>don&#8217;t know maybe throw an exception at the user hey you tried to delete</p>



<p>tried to delete something that wasn&#8217;t there but the 11 is there and now we have to figure out how</p>



<p>to actually delete it so let me draw a couple of larger representations of these nodes real fast</p>



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



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



<p>And when we draw our diagrams, we usually just kind of do one line.</p>



<p>But you should know that if you&#8217;re doing this in code,</p>



<p>actually what&#8217;s happening is the nine node,</p>



<p>it&#8217;s got a right child pointer that points down to the 11 node.</p>



<p>And then the 11 node has a, let me just redo this real fast to make it prettier.</p>



<p>And then the 11 node has a parent pointer that points up to the nine.</p>



<p>This is probably going to be true for almost every language,</p>



<p>at least that I can think of that you would code a tree in.</p>



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



<p>consider that. So what are you really going to do? Well first you have to look at the 11 node and</p>



<p>travel up to its parent and let the parent know that it no longer has a right child. So long story</p>



<p>short we&#8217;re going to null the right child pointer. We&#8217;re going to say disconnect that right child by</p>



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



<p>null the pointer on the 11 node because we can just delete the 11 node if we&#8217;re</p>



<p>using dynamic memory if we&#8217;re using a smart pointer it should go away already</p>



<p>at this point because it has no one else pointing to it but long story short</p>



<p>once we kill the 11 node we also end up killing its pointer so we don&#8217;t really</p>



<p>need to tell its pointer to point to null it&#8217;s just going to end up going away</p>



<p>it&#8217;s not going to be a memory leak because it was only pointing to the 9</p>



<p>and the 9 is not going to leak memory because the 9 is still going to be inside</p>



<p>going to be inside of the tree so uh the time complexity once we find the the leaf the node</p>



<p>with zero children is just going to be constant time we&#8217;re just going to manipulate a couple</p>



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



<p>it was perfectly balanced or just o of h to find it and then plus constant is going to get cancelled</p>



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



<p>cross it out to let you know that this is what it&#8217;s going to be like.</p>



<p>We&#8217;re going to say the 11 is just gone,</p>



<p>and so is the pointer that the 9 has pointing down to the 11.</p>



<p>That&#8217;s how you delete a node with zero children.</p>



<p>So deleted a leaf.</p>



<p>All right, now the next thing, which is slightly harder, but not too hard,</p>



<p>is deleting a node that has one child.</p>



<p>Long story short for this, we&#8217;re pretty much just going to…</p>



<p>oh I forgot to full screen the annotator let me do that to get myself a little more room</p>



<p>we&#8217;re really just going to promote its one child to wherever that node was so let me erase this</p>



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



<p>it&#8217;s the 32 node okay I guess that&#8217;s why I drew that so let&#8217;s kill the number 32</p>



<p>You know, if there was just like one child, like let&#8217;s say the 23 was the only node that existed</p>



<p>and there were no nodes underneath the 23, then this would really be the same operation. We would</p>



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



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



<p>for it. So this is already going to be an O of H operation or a log time if the tree is perfectly</p>



<p>We do, we go down here, we go down there.</p>



<p>You know, where is the 32?</p>



<p>We&#8217;re just searching for it.</p>



<p>So we had to spend the time to search for the node.</p>



<p>We then look at the node.</p>



<p>Okay, we found the 32.</p>



<p>We identify how many children it has.</p>



<p>We see that this is a node with only one child.</p>



<p>And that means the one child, which in this case is 23, just needs to be promoted up to where the 32 is.</p>



<p>just you know remove the 32 from the diagram maybe get rid of get get rid of one of these uh</p>



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



<p>about this in terms of code uh you you want to have let&#8217;s see let me duplicate this again in a</p>



<p>different way if you&#8217;re thinking about code what you want to do is have the 32 which is the node</p>



<p>you&#8217;re going to delete go to its child so determine if it&#8217;s the left child or the right</p>



<p>or the right child that exists once you find that child you tell it that its new parent is the</p>



<p>parent of the node you&#8217;re trying to delete so that means the parent of the 23 is no longer 32 the</p>



<p>parent of the 23 is actually 35 you know it&#8217;s its original grandparent or the the parent of the node</p>



<p>you&#8217;re trying to delete after you do that you tell the grandparent that it&#8217;s no longer pointing down</p>



<p>to the node you&#8217;re trying to delete but instead to its grandchild so again we&#8217;re just going to</p>



<p>So again, we&#8217;re just going to manipulate some pointers here.</p>



<p>We&#8217;re going to say, yep, your left child is now the 23 and not the 32.</p>



<p>Notice how we&#8217;re kind of, you know, drawing arrows around the original node.</p>



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



<p>And this is going to be a constant time operation, at least after we search and find the 32 node.</p>



<p>Finally, of course, you want to like delete the 32.</p>



<p>you have in your tree you would call delete on it depending on your language and then uh if it was</p>



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



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



<p>the new left child of the 35 so that&#8217;s why i say that the the uh the one child that was on the 32</p>



<p>is going to get promoted uh to wherever the 32 was wherever the node you wanted to delete was</p>



<p>this will be the same whether or not whether it&#8217;s the left child or the right child in terms of what</p>



<p>is the one child that the node had and also you know how do you decide how to tell the the</p>



<p>grandparent is its left child pointer going to be changing or is its right child pointer going to be</p>



<p>changing well you know when you originally looked at that 32 and you&#8217;re about to delete it you could</p>



<p>tell that it had that it had one child based on its left pointer and right pointer one of them</p>



<p>pointer one of them was null and one of them was not so whichever one of them was null that tells</p>



<p>you the direction that the grandparent should be pointing so in this case the 32 had a left child</p>



<p>pointer that was not null and a right child pointer that was null because there was no right child</p>



<p>so that&#8217;s why we told the grandparent that its left child pointer is now the 23.</p>



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



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



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



<p>so this is going to be the hardest uh part we&#8217;re going to try to delete a node that has two</p>



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



<p>node since that&#8217;s kind of where we&#8217;re hanging out lately and then we&#8217;ll delete the hardest node of</p>



<p>all the 41 nodes so the first thing we need to know before we can get ready for deletion is</p>



<p>the in order traversal sequence of the data in this tree if you&#8217;re watching this video as soon</p>



<p>as i published it then i i don&#8217;t have videos up yet on traversal i&#8217;m going to upload those soon</p>



<p>then you can just check out one of my traversal videos.</p>



<p>But long story short,</p>



<p>in order traversal just means produce a sorted list,</p>



<p>an ascending list or a non-decreasing list</p>



<p>based off the data that&#8217;s inside of the tree.</p>



<p>So, you know, if I put this down here,</p>



<p>I can just say, you know, looking visually</p>



<p>from left to right, this is why I draw my trees in this way.</p>



<p>I see a six and then a nine and then an 11</p>



<p>and then a 16 and then a 19 and then a 23</p>



<p>and then a 29, no, no, no, a 26, and then a 29, and then a 32, 35, 38, 41, 43, 48, 52, 59, 67,</p>



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



<p>to be increasing or at least not decreasing.</p>



<p>Let me just double check.</p>



<p>Okay, how many do I have?</p>



<p>1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19.</p>



<p>I already got lost.</p>



<p>I think it&#8217;s 19.</p>



<p>1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19.</p>



<p>Okay, did it right.</p>



<p>list, an ascending list of the original data that was inside of the tree. And now we need to look</p>



<p>at a concept called in order successor. Successor comes after, predecessor comes before. Think of</p>



<p>like a king and a prince, right? The prince is the successor to the king. Eventually one day when</p>



<p>the king grows very old and becomes grandpa king and the prince grows up and becomes the regular</p>



<p>king, then you would look at the prince king and you would say, who was your predecessor? Well,</p>



<p>grandpa king right so whoever comes before his predecessor whoever comes after is the successor</p>



<p>so we want the in order successor of the node that we wish to delete so I can&#8217;t remember what</p>



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



<p>down let&#8217;s delete the 35 first we have to find the 35 itself to make sure it exists in the tree</p>



<p>so I&#8217;m going to look at the 41 I&#8217;m going to go left to the 16 I&#8217;m going to go right to the 35</p>



<p>We found it in O time in the worst case.</p>



<p>So this is obviously not going to be the fastest operation, but you know, all the ops are kind</p>



<p>of the same speed in a perfectly balanced binary search tree, not empirically, but in</p>



<p>terms of time complexity.</p>



<p>So we found the 35, we identified that it has two children.</p>



<p>So then what we have to do is find the in order successor.</p>



<p>What is the in order successor of the 35 node?</p>



<p>list 35 here and it appears to be 38 and that actually makes sense because since the data in</p>



<p>the tree is going to look sorted if you sweep your eyes from left to right assuming you drew</p>



<p>the diagram well you&#8217;re really just looking for the the node that is as close as possible</p>



<p>to the node you wish to delete but happens to be to the right of it there are some tutorials out</p>



<p>there that use the in order predecessor so that would mean the 32 would be the one you select</p>



<p>I like to do successor.</p>



<p>So first we find the 35,</p>



<p>then we find it&#8217;s in order successor.</p>



<p>We&#8217;ll do another delete maybe next of the 16 node</p>



<p>because that&#8217;ll be a little more difficult.</p>



<p>Once we find the in order successor,</p>



<p>we&#8217;re actually just gonna copy the data up into the tree.</p>



<p>So I&#8217;m gonna, sorry, up into the node we want to delete.</p>



<p>So that means I&#8217;m gonna take this 35</p>



<p>and it no longer has 35 as its value.</p>



<p>as its value. Instead, we&#8217;re going to steal the value of its in order successor. So 35, sorry,</p>



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



<p>node. It&#8217;s not really a recursive delete. It&#8217;s more like you&#8217;re inside the remove function or</p>



<p>the delete function or whatever you chose to call it. It&#8217;s usually remove. And after you&#8217;ve copied</p>



<p>of the in order successor to the node you actually wanted to delete then you call delete or remove</p>



<p>again on the the in order successor you probably want to use a pointer to that node or have some</p>



<p>sort of a way to reference that node without telling the tree to start deleting from the top</p>



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



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



<p>uh at least after that point then it&#8217;s going to find the 38 and copy its value up</p>



<p>and then it&#8217;s going to happen all over again we&#8217;re going to just be stuck in an infinite loop</p>



<p>um so instead you want to have like a pointer to this node or a reference to this node</p>



<p>and then call a removal function or a delete function that can receive just like you know</p>



<p>a reference or pointer directly to that node and then if you notice the number 38 node</p>



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



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



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



<p>Remember we said we need to find the node that is as close as possible to the node that we want to delete, but just like on the right side.</p>



<p>the 38 had a left child then we would actually want to follow the left child we wouldn&#8217;t stop at</p>



<p>the 38 node so it&#8217;s impossible for the node to have two children which means it&#8217;s going to end</p>



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



<p>two children that&#8217;s the 35 we find it&#8217;s in order successor we copy its value up and then we call</p>



<p>delete again or remove again on this in order successor which is guaranteed to just be an easy</p>



<p>leaf removal or a node with one child removal where we just like you know promote its child up</p>



<p>and that&#8217;s it so let me uh let me duplicate this real fast and uh let me say also that everything</p>



<p>we just did was basically all it&#8217;s just going to be o of h because it&#8217;s going to be o of h to go</p>



<p>find the node in the worst case and then o of h to find its in order successor uh some fraction of</p>



<p>each you know o of h fraction o of h fraction but when you just combine them it&#8217;s just going to be</p>



<p>It&#8217;s just going to be O or log time if it&#8217;s a perfectly balanced tree.</p>



<p>And then once we find the in order successor and the node we want it to delete, it&#8217;s just</p>



<p>a matter of like just copying data and maybe doing the second delete.</p>



<p>So, you know, the second delete itself would just be like another O or log time.</p>



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



<p>to cancel out to log of N if we&#8217;re talking about big O time complexity.</p>



<p>time complexity. Anyway, so I&#8217;m going to duplicate this one more time and then revert everything</p>



<p>to scratch to scratch here or to the very beginning again. So we can delete something</p>



<p>that&#8217;s a little bit more difficult. Whoops, that&#8217;s bad. Can I get this? Nope. Can I get</p>



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



<p>order successor in a program because you don&#8217;t want to like traverse the whole entire tree and</p>



<p>produce a giant list and then scan the list and then like find the successor and then once you</p>



<p>know the number go search for the for the node that&#8217;s like going to be way too slow the tree</p>



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



<p>so we do like 41 16 belongs on the left side we found the 16 now we have to find the 16s in order</p>



<p>successor the one that comes after you can tell just by eyeballing it that it&#8217;s going to be the</p>



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



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



<p>Well, first you just take a hop to the right. So you look at the right child, if it exists.</p>



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



<p>node with the one child at most. So we take a hop to the right to find its right child.</p>



<p>as far down as you can possibly go until you reach a dead end.</p>



<p>So that means I&#8217;m taking a hop to the right and then I&#8217;m just going to go.</p>



<p>Maybe I&#8217;ll do like another color here.</p>



<p>I&#8217;ll go left child pointer, left child pointer, left child pointer,</p>



<p>as far down as I can possibly go. And that is the successor.</p>



<p>Notice how that&#8217;s the 19. Again,</p>



<p>if the 19 had a child on its left side,</p>



<p>we would have to follow it down further,</p>



<p>which means it&#8217;s guaranteed that you will stop at a node that has no left child.</p>



<p>no left child if it had a right child same situation it&#8217;s still going to be the in order</p>



<p>successor that just but that just means when we call a remove a second time on that node it&#8217;s</p>



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



<p>me uh let me say that now that we&#8217;ve found the in order successor we&#8217;re just going to copy the value</p>



<p>so i&#8217;m going to do maybe i&#8217;ll put this in orange so this 16 is no longer in that particular node</p>



<p>no longer in that particular node we&#8217;re not even deleting that node we&#8217;re just stealing a value</p>



<p>from a different node we&#8217;re going to steal the 19 value from the node which is the in order successor</p>



<p>and now that we&#8217;ve done that we&#8217;ll use our reference to the 19 node to call remove on it</p>



<p>specifically so when we call remove or delete it&#8217;s basically going to say all right what sort</p>



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



<p>you gave a point to the node or a reference to the node it&#8217;s going to go oh this is a leaf node</p>



<p>that&#8217;s easy i just tell the 23 that it has no left child anymore and then i just actually</p>



<p>deallocate the 19 node or if it&#8217;s a smart pointer let it die on its own anyway let&#8217;s do something a</p>



<p>little bit more difficult now so i&#8217;m going to duplicate this and try to revert all this stuff</p>



<p>Okay, okay.</p>



<p>Let&#8217;s delete the root node, the hardest node of all, the 41.</p>



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



<p>First, we search for 41.</p>



<p>In the worst case, you know, it would have been O of H, but we were lucky.</p>



<p>We just found it right away.</p>



<p>Now we have to find the in-order successor for the 41.</p>



<p>So again, take a hop to the right and then follow left child pointers as far down as you can possibly go.</p>



<p>go so we&#8217;ll look at the 48 here and then we&#8217;ll look at the 43 now we&#8217;ve hit a</p>



<p>dead end and we know that the in-order successor for the 41 is 43 and that</p>



<p>makes sense because they&#8217;re paired up here in this sorted list at the bottom</p>



<p>so then the next thing that we do is we just copy the data you know we steal the</p>



<p>data from the 43 so I&#8217;m gonna cross out this 41 here and it&#8217;s gonna end up</p>



<p>and then we call delete a second time or remove a second time on the actual 43 which is another</p>



<p>easy removal because it could not possibly have two children it&#8217;s only going to have zero or one</p>



<p>children and well that&#8217;s pretty much it it&#8217;s just going to be just so fast and easy so that means</p>



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



<p>notice that the data is still ordered correctly so we do have a valid binary search tree even after</p>



<p>we kind of do this weird data stealing let me see if i can find something that would have had one</p>



<p>child so i want to look through the whole tree and see if i can find a node that has one child</p>



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



<p>one real fast let&#8217;s see um who has a right child only no one has a right child only that sucks</p>



<p>child only that sucks all right uh let&#8217;s say we want to delete the root node again</p>



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



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



<p>node real fast so that the 50 uh sorry the 67 has a right child why am i doing this because the 67</p>



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



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



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



<p>sorry, 59. The first thing we do is we go find the 59. So I&#8217;m going to</p>



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



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



<p>successor we go to the right side and then we follow all left child pointers until we hit a</p>



<p>dead end left child pointer dead end already because i mean there is a right child but we&#8217;re</p>



<p>supposed to be going down left child pointers there is no left child pointer so we&#8217;ve hit a</p>



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



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



<p>had two children. We&#8217;re going to eventually delete the node that had one child, the successor.</p>



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



<p>going to end up happening is our removal function identifies that that 67 had one child. And so what</p>



<p>one child which is the 70 node just one level higher where the 67 was so that means the uh</p>



<p>the 67 is just gone we do actually delete that and then the 70 node is going to be the new</p>



<p>left child of the 73 node notice how the data still makes sense okay i think that&#8217;s all i&#8217;ve</p>



<p>log time operation just one more time log time if the tree is perfectly balanced it&#8217;s a little bit</p>



<p>closer to linear time as the tree becomes more imbalanced if this was a self-balancing tree then</p>



<p>you know this would be like log time in terms of worst case scenario in general we&#8217;ll say that this</p>



<p>is still on average if we had just totally random data and totally random deletions on average this</p>



<p>would be you know a log time operation even if we do log down to find the node and then another</p>



<p>uh down to find the node and then another log to find the successor that&#8217;s still not going to be</p>



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



<p>much for watching this video i hope you learned a little bit of stuff and i hope you had a little</p>



<p>bit of fun i need to see if someone is selling pi at this hour going on a searching thing</p>



<p>Hey everybody!</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p>you look at the middle of the screen right now you should see a QR code which you can scan</p>



<p>in order to go to the website which I think is also named somewhere at the bottom of this video</p>



<p>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>and the services and tutorials and things that I offer and all that good stuff and</p>



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



<p>just want to say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever i also</p>



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



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



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



<p>Thank you.</p>



<p>Hello there.</p>



<p>Let&#8217;s talk about.</p>
<p>The post <a href="https://www.NeuralLantern.com/binary-search-tree-removals-delete-nodes-with-0-1-or-2-children/">Binary Search Tree Removals &#8211; Delete Nodes with 0, 1, or 2 Children</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/binary-search-tree-removals-delete-nodes-with-0-1-or-2-children/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
