{"id":438,"date":"2018-02-13T22:10:06","date_gmt":"2018-02-13T22:10:06","guid":{"rendered":"http:\/\/www.datastructure.org\/?p=1"},"modified":"2025-04-21T13:49:11","modified_gmt":"2025-04-21T13:49:11","slug":"how-to-delete-a-node-from-binary-search-tree","status":"publish","type":"post","link":"https:\/\/www.bhargaviurs.com\/index.php\/2018\/02\/13\/how-to-delete-a-node-from-binary-search-tree\/","title":{"rendered":"How to delete a node from Binary Search Tree"},"content":{"rendered":"<p class=\"ui_qtext_para\">We must always follow the below 2 points when deleting a node from Binary Search Tree:<\/p>\n<ol>\n<li>Delete the node.<\/li>\n<li>Retain the Binary Search Tree property.<\/li>\n<\/ol>\n<p class=\"ui_qtext_para\">Consider the following scenarios that we encounter when deleting a node from a BST:<\/p>\n<p class=\"ui_qtext_para\"><strong>Scenario 1: Deleting a leaf node or a childless node.<\/strong><\/p>\n<p class=\"ui_qtext_para\">This is certainly the easiest case you can encounter. Since the node to be deleted has no children, we just delete the node and the BST property remains unaltered.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-53\" src=\"http:\/\/www.datastructure.org\/wp-content\/uploads\/2018\/02\/main-qimg-0736420f08ad39ee57a08abd6ce350c7-300x226.png\" alt=\"\" width=\"368\" height=\"277\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/main-qimg-0736420f08ad39ee57a08abd6ce350c7-300x226.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/main-qimg-0736420f08ad39ee57a08abd6ce350c7.png 684w\" sizes=\"auto, (max-width: 368px) 100vw, 368px\" \/><\/p>\n<p class=\"ui_qtext_para\"><strong>Scenario 2: Deleting a node with one child.<\/strong><\/p>\n<p class=\"ui_qtext_para\">Delete the node, and make the node\u2019s parent, the parent of its only child.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-55\" src=\"http:\/\/www.datastructure.org\/wp-content\/uploads\/2018\/02\/main-qimg-2ad8320e151f2293275559d69586bd06.png\" alt=\"\" width=\"400\" height=\"273\" \/><\/p>\n<p class=\"ui_qtext_para\"><strong>Scenario 3: Deleting a node with 2 children.<\/strong><\/p>\n<p class=\"ui_qtext_para\">Before going to the deletion, lets understand how do we know if a binary tree is Binary Search Tree &#8211; The Inorder traversal of a Binary Search Tree is always sorted. Now lets understand what the below 2 terms mean in this case:<\/p>\n<ol>\n<li><strong>Predecessor:<\/strong> A predecessor of a node is the element which appears before that node in an Inorder traversal of a BST.<br \/>\n&#8211; In a BST, its the rightmost node of the left subtree.<\/li>\n<li><strong>Successor:<\/strong> A successor of a node is the element which appears after that node in an Inorder traversal of a BST.<br \/>\n&#8211; In a BST, its the leftmost node of the right subtree.Consider the following BST:<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-133 size-full\" src=\"http:\/\/www.datastructure.org\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.59.12-PM.png\" alt=\"\" width=\"3336\" height=\"1864\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.59.12-PM.png 3336w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.59.12-PM-300x168.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.59.12-PM-1024x572.png 1024w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.59.12-PM-768x429.png 768w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.59.12-PM-1536x858.png 1536w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.59.12-PM-2048x1144.png 2048w\" sizes=\"auto, (max-width: 3336px) 100vw, 3336px\" \/><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-127\" src=\"http:\/\/www.datastructure.org\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.34.54-PM.png\" alt=\"\" width=\"3334\" height=\"436\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.34.54-PM.png 3334w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.34.54-PM-300x39.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.34.54-PM-1024x134.png 1024w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.34.54-PM-768x100.png 768w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.34.54-PM-1536x201.png 1536w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.34.54-PM-2048x268.png 2048w\" sizes=\"auto, (max-width: 3334px) 100vw, 3334px\" \/><\/p>\n<p class=\"ui_qtext_para\"><strong>Deletion:<\/strong>\u00a0Now that you understand the terms predecessor and successor. When you want to delete a node with 2 children, follow the below steps:<\/p>\n<ol>\n<li>Replace the node to be deleted with either its predecessor or successor.<\/li>\n<li>Delete the respective predecessor or the successor from the tree.<\/li>\n<\/ol>\n<p>In the tree below, I am replacing the node to be deleted(17) with its successor(19), and then deleting the successor(19) from the tree.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-131 size-full\" src=\"http:\/\/www.datastructure.org\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.57.09-PM.png\" alt=\"\" width=\"3342\" height=\"1874\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.57.09-PM.png 3342w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.57.09-PM-300x168.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.57.09-PM-1024x574.png 1024w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.57.09-PM-768x431.png 768w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.57.09-PM-1536x861.png 1536w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-10.57.09-PM-2048x1148.png 2048w\" sizes=\"auto, (max-width: 3342px) 100vw, 3342px\" \/><strong><strong>Note that Inorder traversal after the deletion is still sorted retaining the property of a BST as shown below:<\/strong><\/strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-141 size-full\" src=\"http:\/\/www.datastructure.org\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-11.14.22-PM.png\" alt=\"\" width=\"2684\" height=\"378\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-11.14.22-PM.png 2684w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-11.14.22-PM-300x42.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-11.14.22-PM-1024x144.png 1024w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-11.14.22-PM-768x108.png 768w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-11.14.22-PM-1536x216.png 1536w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-23-at-11.14.22-PM-2048x288.png 2048w\" sizes=\"auto, (max-width: 2684px) 100vw, 2684px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We must always follow the below 2 points when deleting a node from Binary Search Tree: Delete the node. Retain the Binary Search Tree property. Consider the following scenarios that we encounter when deleting a node from a BST: Scenario 1: Deleting a leaf node or a childless node. This is certainly the easiest case &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[9,10,11,12,14,16,17],"class_list":["post-438","post","type-post","status-publish","format-standard","hentry","category-trees","tag-bst","tag-complexity","tag-datastructures","tag-delete","tag-interview","tag-node","tag-trees","entry"],"_links":{"self":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts\/438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/comments?post=438"}],"version-history":[{"count":1,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts\/438\/revisions"}],"predecessor-version":[{"id":492,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts\/438\/revisions\/492"}],"wp:attachment":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/media?parent=438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/categories?post=438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/tags?post=438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}