{"id":300,"date":"2018-07-19T05:20:06","date_gmt":"2018-07-19T05:20:06","guid":{"rendered":"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=300"},"modified":"2018-12-12T10:30:48","modified_gmt":"2018-12-12T10:30:48","slug":"binary-search-trees-ii","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/chapter\/binary-search-trees-ii\/","title":{"rendered":"Binary Search Trees-II"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/ZUlIypDLp4U\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. We have understood the basic concept of an important ADT \u2013 the binary search tree ADT. In this module we will discuss some more operations of binary search tree ADT.<\/p>\r\n&nbsp;\r\n\r\n<strong>Learning Objectives<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe learning objectives of the module are as follows:\r\n\r\n&nbsp;\r\n\r\n\u2022 To illustrate the different types of Binary Search Tree Traversals\r\n\r\n\u2022 To discuss the construction of Binary Search Trees\r\n\r\n\u2022 To describe Insertion into Binary Search Trees\r\n\r\n\u2022 To explain Deletion from Binary Search Trees\r\n\r\n&nbsp;\r\n\r\n<strong>22.1\u00a0 Introduction<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As you may recall from our last module Binary Search Trees (BST) or Lexicographic Trees are a type of Binary Trees with a special organization of data. A binary search tree is a binary tree with a special property called the BST-property. This property states that for a tree or sub-tree rooted at X, all keys to the left of X are lesser than the key at X and all keys to the right of X are greater than the key at X. We also assume that the keys of a BST are pairwise distinct.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-303 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-168.png\" alt=\"\" width=\"359\" height=\"250\" \/>\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>22.2\u00a0 Traversals of Binary Search Trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us consider the example of a binary search tree given in Figure 22.1. Traversals of binary search tree is similar to the traversal of binary trees but here we know that the binary tree is organized is a special way.<\/p>\r\n&nbsp;\r\n\r\n<strong>22.2.1<\/strong>\u00a0<strong>Preorder Traversal<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We will first discuss the preorder traversal of a BST (Figure 22.2). Here we process the root (<strong>23<\/strong>) first. The root of the left subtree of 23 that is <strong>14<\/strong> is processed, followed by <strong>7<\/strong> root of left subtree of 14 is processed. Now since 7 has no left child, we process the right subtree of 7 that is <strong>12<\/strong>. Now we have completed processing node 7, that is processing of left subtree of 14. Now we need to process the right subtree of 14 \u2013 which in this example does not exist. Therefore we have completed processing node 14 or in other words the left subtree of the root node 23. Now we need to process the right subtree of the root 23. Now we process the root node <strong>67<\/strong>, then its left subtree <strong>34<\/strong>. Since 34 has no children, we process the right subtree of 67 that is<strong> 89<\/strong>. Now 89 has no left child so we process the right child of 89, that is <strong>90<\/strong>. Now we have completed traversing all the nodes. The preorder traversal is given in Figure 22.2.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-304 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-169.png\" alt=\"\" width=\"517\" height=\"282\" \/>\r\n\r\n<strong> 22.2.2 Postorder Traversal<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us discuss the postorder traversal of the BST (Figure 22.3). Here we first need to process the left subtree of the root (23). In order to process the root of the left subtree of 23 that is subtree rooted 14 in post order, we need to process its left subtree that is subtree rooted at 7. Here since 7 has no left child, we go on to process it\u2019s right subtree \u2013 here only one child <strong>12<\/strong>. Now we process node <strong>7<\/strong>. We have completed processing left subtree of node 14. Now we need to process the right subtree of 14 \u2013 which in this example does not exist. Therefore we have completed processing left and right subtree of node 14, therefore we need to process node <strong>14<\/strong>. On completion of processing node 14, we have completed processing left subtree of root node 23. Now we need to process the right subtree of the root 23 rooted at 67. Now we need to process the left subtree of 67 that is 34. Since 34 has no children, we process the node <strong>34.<\/strong> Now we need to process the right subtree of 67 rooted at 89. Now 89 has no left child so we process the right child of\u00a0<span style=\"text-align: initial;font-size: 1em\">89, that is <\/span><strong style=\"text-align: initial;font-size: 1em\">90<\/strong><span style=\"text-align: initial;font-size: 1em\">. Now we have completed processing left and right subtree of 89, so we process the root <\/span><strong style=\"text-align: initial;font-size: 1em\">89<\/strong><span style=\"text-align: initial;font-size: 1em\">. Now we have completed processing left and right subtree of the root 67, so we process the node <\/span><strong style=\"text-align: initial;font-size: 1em\">67<\/strong><span style=\"text-align: initial;font-size: 1em\">. Now we have completed processing left and right subtree of the root 23, so we process the root <\/span><strong style=\"text-align: initial;font-size: 1em\">23<\/strong><span style=\"text-align: initial;font-size: 1em\">. Now we have completed processing all the nodes of the tree. The postorder traversal is given in Figure 22.3.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-305 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-170.png\" alt=\"\" width=\"429\" height=\"237\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>22.2.3 Inorder Traversal<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us discuss the in-order traversal of the BST (Figure 22.4). Here we first need to process the left subtree of the root (23). In order to process the root of the left subtree of 23 that is subtree rooted 14 in in-order, we need to process its left subtree that is subtree rooted at 7. Here since 7 has no left child, we process the node <strong>7<\/strong>. We go on to process it\u2019s right subtree \u2013 here only one child <strong>12<\/strong>. We have completed processing left subtree of node 14. Now we process node <strong>14<\/strong>. Now we need to process the right subtree of 14 \u2013 which in this example does not exist. Therefore we have completed processing left and right subtree of node 14. On completion of processing node 14, we have completed processing left subtree of root node 23. Therefore we process the node <strong>23<\/strong>. Now we need to process the right subtree of the root 23 rooted at 67. Now we need to process the left subtree of 67 that is 34. Since 34 has no left child, we process the node <strong>34.<\/strong> We proceed in the same way by processing the left subtree, then root node and then right subtree until all the nodes are processed. The in-order traversal is given in Figure 22.4. The in-order traversal of a binary search tree produces a sequence of numbers which are in increasing order.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-306 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-171.png\" alt=\"\" width=\"456\" height=\"251\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">22.2.4 Right Node Left Traversal<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now for the BST another type of traversal has also been defined called the right node left traversal (Figure 22.5). This traversal is similar to inorder traversal except that instead of left subtree \u2013root- right subtree, the traversal is carried out as right subtree \u2013node-left subtree. This type of traversal is important for a BST because this produces a descending sequence.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-307 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-172.png\" alt=\"\" width=\"493\" height=\"273\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>22.3\u00a0 Construction of a Binary Search Tree<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us consider the construction of BSTs. There are two things to remember. First the tree that is constructed must obey the binary search property. Second we need to search the BST to find the correct position in which is insert a node. That is we need to maintain the key property and ensure that the smaller values are in the left sub-tree and the larger values in right sub-tree for every node in the tree. We also need to remember for the same keys, different BSTs will be constructed if the keys are used for construction in a different order.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now the base case is when the tree is empty. In this case, we create new node for the item and make it the root of the BST. In the recursive case, if key (node to be added next) &lt; root's value, add to the left subtree, otherwise add to the right subtree. Remember that all BST insertions take place at a leaf or a leaflike node. A leaflike node is a node that has one null subtree.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In order to insert an element e, we need to find the node <em>n<\/em> that should be <em>e<\/em> 's parent, and set <em>n<\/em> 's child to be a newly created node containing e. Now here we use the search operation in order to find the parent node <em>n<\/em>. If the element or key is smaller than the root we are at, we examine the left subtree and if the elemnt is greater, we examine the right subtree in each case. When we can no longer proceed, we are at a leaf; this is the position at which to add the element e.<\/p>\r\n&nbsp;\r\n\r\n<strong>22.3.1<\/strong>\u00a0\u00a0\u00a0 <strong>Algorithm<\/strong>\r\n\r\n&nbsp;\r\n\r\nPerform search for value X\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">Search will end at node Y (if X not in tree). If the element to be added is a duplicate which is generally\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 not\u00a0 \u00a0allowed in BST we can indicate that insertion is not possible.<\/span>\r\n<ul>\r\n \t<li><span style=\"text-align: initial;font-size: 1em\">If X &lt; Y, we need to insert new leaf X as new left subtree of the node Y<\/span><\/li>\r\n \t<li><span style=\"text-align: initial;font-size: 1em\">If X &gt; Y, we need to insert new leaf X as new right subtree of the node Y<\/span><\/li>\r\n<\/ul>\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>22.3.2<\/strong>\u00a0\u00a0<strong>Observations<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The search operation will be O(log(n) ) only for balanced trees. We have defined balanced trees as trees where for each node the height of the left and right subtrees differ by at most one. Now insertions into BSTs may unbalance the tree. In fact when we insert the keys in ascending or descending order we will land up with skewed trees. Insertion always places a new item near the periphery (that is at the bottom of the tree) of the BST while ensuring that the BST property is maintained. Searching of the binary search tree is used to locate the insertion point. As in a search operation, starting at the root we probe down the tree till we find a node who\u2019s left or right sub-tree is empty. Now we have found the place for the new value. The insertion is based on comparisons of the new item and values of nodes in the BST. Please note that elements in nodes must be comparable otherwise we cannot construct a BST.<\/p>\r\n&nbsp;\r\n\r\n<strong>22.3.3 Detailed Cases<\/strong>\r\n\r\n&nbsp;\r\n\r\nCase 1: Let us first consider the case when the tree is empty\r\n<ul>\r\n \t<li>Here we set the root to a new node containing the item - <strong><em>create a root<\/em><\/strong> <strong>node<\/strong> <strong>with the new key<\/strong><\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\nCase 2: Now let us assume that the tree is not empty <em style=\"text-align: initial;font-size: 1em\">Here we compare <\/em><span style=\"text-align: initial;font-size: 1em\">key with the top node<\/span>\r\n\r\n&nbsp;\r\n\r\nif key = node key\r\n\r\nThe node already exists\r\n\r\nelse if key &gt;\u00a0 node key\r\n\r\n<em>compare <\/em>key with the right subtree:\r\n\r\nif\u00a0 subtree is empty create a leaf node\r\n\r\nelse add key in right subtree\r\n\r\nelse key &lt;\u00a0 node key\r\n\r\n<em>compare <\/em>key with the left subtree:\r\n\r\nif the subtree is empty create a leaf node\r\n\r\nelse add key to the left subtree\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">The steps here include the search procedure to find the correct location for insertion as well as the actual insertion.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The trace of the recursive BST insert algorithm is given in Figure 22.6. Now let us assume that we want to insert node 19 into the existing BST rooted at 23. Now we find 19 &lt; 23, so we go left, where we find 19&gt; 14 so we go right. Now we find that the right subtree of 14 is empty so we insert there. The BST after insertion is shown in Figure 22.6.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-308 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-173.png\" alt=\"\" width=\"619\" height=\"334\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>22.3.4 Construction\/Insertion of a Binary Search Tree - Example<\/strong>\r\n\r\n&nbsp;\r\n\r\nAdd the elements <strong>23, 67, 14, 89, 14, 7, 90, 12<\/strong> to form a Binary Search Tree (Figure 22.7).\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-309 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-174.png\" alt=\"\" width=\"575\" height=\"362\" \/>\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 1.Initially\u00a0 BST is Empty-<em>create a root<\/em> node with the new key 23 (Figure 22.7 (a)).\r\n<p style=\"text-align: justify\">2.Now we need to insert 67, which is &gt;23, we have an empty right subtree so we directly insert it as right subtree of 23 (Figure 22.7 (b)).<\/p>\r\n<p style=\"text-align: justify\">3.Next we insert node 14, which is &lt;23, we have an empty left subtree so we directly insert it as left subtree of 23 (Figure 22.7 (c)).<\/p>\r\n<p style=\"text-align: justify\">4.Next we insert node 89, 89&gt;23, so we go right, but right subtree not empty, so we check 89 with root of right subtree 67. We find 89&gt;67, so we go right. Now we find an empty right subtree and insert 89 there (Figure 22.7 (d)).<\/p>\r\n<p style=\"text-align: justify\">5.Next we need to insert node 34. 34&gt;23, so we go right, but right subtree not empty, so we check 89 with root of right subtree 67. We find 34&lt;67, so we go left. Now we find an empty left subtree and insert 34 there (Figure 22.7 (e)).<\/p>\r\n<p style=\"text-align: justify\">6.Next we need to insert node 7. 7&lt;23, so we go left, but left subtree not empty, so we check 7 with root of left subtree 14. We find 7&lt;14, so we go left. Now we find an empty left subtree and insert 7 there (Figure 22.7 (f)).<\/p>\r\n<p style=\"text-align: justify\">7.Next we need to insert node 90. 90&gt;23, so we go right, but right subtree not empty, so we check 90 with root of right subtree 67. We find 90&gt;67, so we go right. But right subtree not empty, so we check 90 with root of right subtree 89. We find 90&gt;89, so we go right. Now we find an empty right subtree and insert 90 there (Figure 22.7 (g)).<\/p>\r\n<p style=\"text-align: justify\">8.Next we need to insert node 12. 12&lt;23, so we go left, but leftt subtree not empty, so we check 12 with root of left subtree 14. We find 12&lt;14, so we go left. But left subtree not empty, so we check 12 with root of left subtree 7. We find 12&gt;7, so we go right. Now we find an empty right subtree and insert 12 there (Figure 22.7 (h)).<\/p>\r\n&nbsp;\r\n\r\n<strong>22.3.5 Analysis<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The time taken for initialization will be of the <em>O<\/em>(1). The while loop searches for place to insert <em>the key<\/em>, while maintaining the parent. This <em>O<\/em>(<em>h<\/em>) time where h is the height of the tree. The actual creation of the node for insertion of the value takes <em>O<\/em>(1). Therefore total time taken for insertion of a node will be of <em>O<\/em>(<em>h<\/em>).<\/p>\r\n&nbsp;\r\n\r\n<strong>22.4 Removing or Deleting from Binary Search Tree<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">When removing a node from a BST, we need to ensure that the binary search tree property is maintained. Therefore during deletion, we need to search for the node, remove it, and do some reorganization to maintain the binary search tree property. There are many cases two be considered, we may be removing an element which is a leaf, removing an element with one child, removing an element having both the children or we may be deleting the root itself. Again we can have both recursive and an iterative implementation. If the node being deleted is a leaf then deletion is a straightforward procedure of just making the associated parent node point to NULL. If the node being deleted has only one child then the parent node of the node being deleted will have to point to the child node of the node being deleted. If the node being deleted has both the children then we first need to find the <strong>inorder successor<\/strong> of the node being deleted and replace the node being deleted with this node. (The inorder successor of a node can be obtained by taking the right node of the current\u00a0<span style=\"text-align: initial;font-size: 1em\">node and traversing in the left till we reach the left most node whose right subtree is null) We can also take inorder predecessor instead of inorder successor. The total time taken for deletion is of the <\/span><em style=\"text-align: initial;font-size: 1em\">O<\/em><span style=\"text-align: initial;font-size: 1em\">(<\/span><em style=\"text-align: initial;font-size: 1em\">h<\/em><span style=\"text-align: initial;font-size: 1em\">).<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">The algorithm removes a specified item from the BST and adjusts the tree. It uses a binary search to locate the target item that is starting at the root it probes down the tree till it finds the target or reaches a leaf node (target not in the tree). The removal of a node must not leave a \u2018gap\u2019 in the tree.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">22.4.1 Algorithm<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">1.\u00a0<\/strong><span style=\"text-align: initial;font-size: 1em\">if the tree is empty return false<\/span><\/p>\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">2.\u00a0<\/strong><span style=\"text-align: initial;font-size: 1em\">Attempt to locate the node x using the binary search\u00a0 algorithm<\/span><\/p>\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 if the node is not found return false else the node is found, remove it<\/strong><\/p>\r\n<p style=\"text-align: justify\"><strong style=\"text-align: justify;font-size: 1em\">3.\u00a0<\/strong><strong style=\"text-align: justify;font-size: 1em\">(a) Case 1:<\/strong><span style=\"text-align: justify;font-size: 1em\"> if <\/span><em style=\"text-align: justify;font-size: 1em\">x<\/em><span style=\"text-align: justify;font-size: 1em\"> has no children then remove <\/span><em style=\"text-align: justify;font-size: 1em\">x (<\/em><span style=\"text-align: justify;font-size: 1em\">The node to be deleted has no children. In this case, all we need to do is delete the node)<\/span><\/p>\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">if the node has 2 empty subtrees<\/strong><\/p>\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">replace the link in the parent of x which was pointing to x with null<\/strong><\/p>\r\n<p style=\"text-align: justify\"><strong style=\"text-align: justify;font-size: 1em\">\u00a0 (b) C<\/strong><strong style=\"text-align: justify;font-size: 1em\">ase 2:<\/strong><span style=\"text-align: justify;font-size: 1em\"> if <\/span><em style=\"text-align: justify;font-size: 1em\">x<\/em><span style=\"text-align: justify;font-size: 1em\"> has one child (left) then make <\/span><em style=\"text-align: justify;font-size: 1em\">parent<\/em><span style=\"text-align: justify;font-size: 1em\">[<\/span><em style=\"text-align: justify;font-size: 1em\">x<\/em><span style=\"text-align: justify;font-size: 1em\">] point to existing child of x. The node to be deleted has only a left subtree. We delete the node and attach the left subtree to the deleted node\u2019s parent.<\/span><\/p>\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">\u00a0if the node has no right child (only left child)<\/strong><\/p>\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">\u00a0link the parent of the target to the left (non-empty) subtree<\/strong><\/p>\r\n<p style=\"text-align: justify\"><strong style=\"font-size: 1em\">(c)\u00a0 Case 3:<\/strong><span style=\"font-size: 1em\"> if <\/span><em style=\"font-size: 1em\">x<\/em><span style=\"font-size: 1em\"> has one child (right) then make <\/span><em style=\"font-size: 1em\">parent<\/em><span style=\"font-size: 1em\">[<\/span><em style=\"font-size: 1em\">x<\/em><span style=\"font-size: 1em\">] point to existing child of x. The node to be deleted has only a right subtree. We delete the node and attach the right subtree to the deleted node\u2019s parent.<\/span><\/p>\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">if the node has no left child (only right child)<\/strong><\/p>\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">link the parent of the node to the right (non-empty) subtree<\/strong><\/p>\r\n<p style=\"text-align: justify\"><strong style=\"font-size: 1em\">(d)\u00a0 <\/strong><strong style=\"font-size: 1em\">Case 4: <\/strong><span style=\"font-size: 1em\">if <\/span><em style=\"font-size: 1em\">x<\/em><span style=\"font-size: 1em\"> has two children (subtrees) then swap <\/span><em style=\"font-size: 1em\">x<\/em><span style=\"font-size: 1em\"> with its in order successor - perform case 0 or case 1 to delete it. The node to be deleted has two subtrees. Rather than simply delete the node, we try to maintain the existing structure as much as possible by finding data to take the place of the deleted data. This can be done in one of two ways. We can find the largest node in the deleted node\u2019s left subtree and move its data to replace the deleted node\u2019s data. We can find the smallest node on the deleted node\u2019s right subtree and move its data to replace the deleted node\u2019s data. Either of these moves preserves the binary search property of the tree. Moreover the property of inorder successor (or predecessor) ensures that this node will have either no child or at most one child. The inorder successor (respectively,\u00a0<\/span><span style=\"font-size: 1em;text-align: initial\">the predecessor) of a key k in a search tree is the smallest (respectively, the largest) key that belongs to the tree and that is strictly greater than (respectively, less than) k. The idea for finding the successor of a given node x is that if x has right child, then the successor is the minimum in the right subtree of x otherwise, the successor is the parent of the farthest node that can be reached from x by following only right branches backward. The predecessor can be found similarly with the roles of left and right exchanged and with the roles of maximum and minimum exchanged.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nBefore we go further let us describe the algorithm to find the inorder successor of a node x.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The successor (x ) = y, is such that key [y] is the smallest key <em>&gt;<\/em> key [x]. Here there are two cases<\/p>\r\n<p style=\"text-align: justify\">\u2022 Case 1: right (x) is non empty<\/p>\r\n<p style=\"text-align: justify\">\u2013\u00a0\u00a0 successor (x ) = the minimum in right (x)<\/p>\r\n<p style=\"text-align: justify\">\u2022 Case 2: right (x) is empty<\/p>\r\n<p style=\"text-align: justify\">\u00a0\u2013 go up the tree until the current node is a left child: successor (x ) is the parent of the current node<\/p>\r\n<p style=\"text-align: justify\">\u00a0\u2013 if you cannot go further (and you reached the root): x is the largest element<\/p>\r\n<p style=\"text-align: justify\">The algorithm is given in Figure 22.8.<\/p>\r\n<img class=\"alignnone size-full wp-image-310 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-175.png\" alt=\"\" width=\"386\" height=\"213\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>if the node has a left and a right subtree<\/strong>\r\n<ul>\r\n \t<li><strong>replace the node's value with the minimum value in the right subtree (inorder successor)<\/strong><\/li>\r\n \t<li><strong>delete the minimum node in the right subtree<\/strong><\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">Instead of using the inorder successor as we described above, we can use the inorder predecessor defined in a similar way.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">We have explained the concept of the deletion algorithm and detailed the four cases we consider during deletion. We use the Tree Successor algorithm to find the inorder successor when the node to be deleted has both left and right children. We assume that the parent of a node is available. Now let us assume that we want to delete the node z from the binary search tree. If z has no children, then we will just replace z by nil. If z has only one child, then we will promote the unique child to z\u2019s place. If z has two children, then we will identify z\u2019s successor. Call it y. The successor y either is a leaf or has only the right child. We promote y to z\u2019s place. Now the deletion of y from its original place will result in deletion of the first three cases only. The code for the above is given in Figure 22.9.<\/p>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-311 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-176.png\" alt=\"\" width=\"615\" height=\"548\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>22.4.2 Deletion from BST \u2013 Example<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us discuss each of the 4 cases of deletion with examples. Let us consider Case 1 where the node to be deleted is a leaf that is has no children. Let us consider deletion of node 34 (Figure 22.10). We need to carry out the search operation (as was done for insertion) to find 34. Now parent of 34 that is 67 would have 34 as its left child. The parent 67\u2019s left child is now made Null.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-312 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-177.png\" alt=\"\" width=\"519\" height=\"301\" \/>\r\n<p style=\"text-align: justify\">Now let us consider Case 2 where the node to be deleted has one child, in this only left child. Let us consider deletion of node 12 (Figure 22.11). We need to carry out the search operation (as was done for insertion) to find 12. Now 12 has only left child that is 10. Node 12 is deleted and 12\u2019s parent 7\u2019s right child is now the left child of 12 that is 10.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-313 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-178.png\" alt=\"\" width=\"551\" height=\"277\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us consider Case 3 where the node to be deleted has one child, in this only right child. Let us consider deletion of node 90 (Figure 22.12). We need to carry out the search operation (as was done for insertion) to find 90. Now 90 has only a right child that is 99. Node 90 is deleted and 90\u2019s parent 89\u2019s right child is now the right child of 90 that is 99.<\/p>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-314 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-179.png\" alt=\"\" width=\"565\" height=\"312\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us consider Case 4 where the node to be deleted has both the children. Let us consider deletion of node 67 (Figure 22.13). We need to carry out the search operation (as was done for insertion) to find 67. Now 67 has both left (34) and right (89)\u00a0 children. First we need to find the inorder successor of 67, which is 81. Now we replace 67 in tree with it\u2019s inorder successor 81. In order to do this we first need to delete 81 from its place in the tree. By definition of inorder successor, 81 can have at most one child. In our case it has no children. Therefore to delete 81 we make the left child of its parent (89) as Null. To replace 67 with 81, the parent of 67 that is 23\u2019s right child should now point to 81. Moreover 81\u2019s left pointer should be made to point to 34 (67\u2019s left child) and 81\u2019s right pointer should be made to point to 89 (67\u2019s right child). This completes the deletion process.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-315 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-180.png\" alt=\"\" width=\"553\" height=\"292\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n<ul>\r\n \t<li>Illustrated the different type of Binary Search Tree Traversals with Examples<\/li>\r\n \t<li>Discussed the Construction of Binary Search Trees<\/li>\r\n \t<li><span style=\"font-size: 1em\">Described the Insertion operation of Binary Search Trees<\/span><\/li>\r\n \t<li><span style=\"font-size: 1em\">Explained the Deletion Operation of Binary Search Trees<\/span><\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Binary Search Trees-II<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/ZUlIypDLp4U\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<img class=\"size-full wp-image-317 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-181.png\" alt=\"\" width=\"640\" height=\"353\" \/>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-318 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-182.png\" alt=\"\" width=\"657\" height=\"324\" \/>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/ZUlIypDLp4U\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. We have understood the basic concept of an important ADT \u2013 the binary search tree ADT. In this module we will discuss some more operations of binary search tree ADT.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Learning Objectives<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The learning objectives of the module are as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 To illustrate the different types of Binary Search Tree Traversals<\/p>\n<p>\u2022 To discuss the construction of Binary Search Trees<\/p>\n<p>\u2022 To describe Insertion into Binary Search Trees<\/p>\n<p>\u2022 To explain Deletion from Binary Search Trees<\/p>\n<p>&nbsp;<\/p>\n<p><strong>22.1\u00a0 Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As you may recall from our last module Binary Search Trees (BST) or Lexicographic Trees are a type of Binary Trees with a special organization of data. A binary search tree is a binary tree with a special property called the BST-property. This property states that for a tree or sub-tree rooted at X, all keys to the left of X are lesser than the key at X and all keys to the right of X are greater than the key at X. We also assume that the keys of a BST are pairwise distinct.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-303 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-168.png\" alt=\"\" width=\"359\" height=\"250\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-168.png 359w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-168-300x209.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-168-65x45.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-168-225x157.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-168-350x244.png 350w\" sizes=\"auto, (max-width: 359px) 100vw, 359px\" \/><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>22.2\u00a0 Traversals of Binary Search Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us consider the example of a binary search tree given in Figure 22.1. Traversals of binary search tree is similar to the traversal of binary trees but here we know that the binary tree is organized is a special way.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>22.2.1<\/strong>\u00a0<strong>Preorder Traversal<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We will first discuss the preorder traversal of a BST (Figure 22.2). Here we process the root (<strong>23<\/strong>) first. The root of the left subtree of 23 that is <strong>14<\/strong> is processed, followed by <strong>7<\/strong> root of left subtree of 14 is processed. Now since 7 has no left child, we process the right subtree of 7 that is <strong>12<\/strong>. Now we have completed processing node 7, that is processing of left subtree of 14. Now we need to process the right subtree of 14 \u2013 which in this example does not exist. Therefore we have completed processing node 14 or in other words the left subtree of the root node 23. Now we need to process the right subtree of the root 23. Now we process the root node <strong>67<\/strong>, then its left subtree <strong>34<\/strong>. Since 34 has no children, we process the right subtree of 67 that is<strong> 89<\/strong>. Now 89 has no left child so we process the right child of 89, that is <strong>90<\/strong>. Now we have completed traversing all the nodes. The preorder traversal is given in Figure 22.2.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-304 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-169.png\" alt=\"\" width=\"517\" height=\"282\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-169.png 517w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-169-300x164.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-169-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-169-225x123.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-169-350x191.png 350w\" sizes=\"auto, (max-width: 517px) 100vw, 517px\" \/><\/p>\n<p><strong> 22.2.2 Postorder Traversal<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us discuss the postorder traversal of the BST (Figure 22.3). Here we first need to process the left subtree of the root (23). In order to process the root of the left subtree of 23 that is subtree rooted 14 in post order, we need to process its left subtree that is subtree rooted at 7. Here since 7 has no left child, we go on to process it\u2019s right subtree \u2013 here only one child <strong>12<\/strong>. Now we process node <strong>7<\/strong>. We have completed processing left subtree of node 14. Now we need to process the right subtree of 14 \u2013 which in this example does not exist. Therefore we have completed processing left and right subtree of node 14, therefore we need to process node <strong>14<\/strong>. On completion of processing node 14, we have completed processing left subtree of root node 23. Now we need to process the right subtree of the root 23 rooted at 67. Now we need to process the left subtree of 67 that is 34. Since 34 has no children, we process the node <strong>34.<\/strong> Now we need to process the right subtree of 67 rooted at 89. Now 89 has no left child so we process the right child of\u00a0<span style=\"text-align: initial;font-size: 1em\">89, that is <\/span><strong style=\"text-align: initial;font-size: 1em\">90<\/strong><span style=\"text-align: initial;font-size: 1em\">. Now we have completed processing left and right subtree of 89, so we process the root <\/span><strong style=\"text-align: initial;font-size: 1em\">89<\/strong><span style=\"text-align: initial;font-size: 1em\">. Now we have completed processing left and right subtree of the root 67, so we process the node <\/span><strong style=\"text-align: initial;font-size: 1em\">67<\/strong><span style=\"text-align: initial;font-size: 1em\">. Now we have completed processing left and right subtree of the root 23, so we process the root <\/span><strong style=\"text-align: initial;font-size: 1em\">23<\/strong><span style=\"text-align: initial;font-size: 1em\">. Now we have completed processing all the nodes of the tree. The postorder traversal is given in Figure 22.3.<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-305 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-170.png\" alt=\"\" width=\"429\" height=\"237\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-170.png 429w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-170-300x166.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-170-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-170-225x124.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-170-350x193.png 350w\" sizes=\"auto, (max-width: 429px) 100vw, 429px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>22.2.3 Inorder Traversal<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us discuss the in-order traversal of the BST (Figure 22.4). Here we first need to process the left subtree of the root (23). In order to process the root of the left subtree of 23 that is subtree rooted 14 in in-order, we need to process its left subtree that is subtree rooted at 7. Here since 7 has no left child, we process the node <strong>7<\/strong>. We go on to process it\u2019s right subtree \u2013 here only one child <strong>12<\/strong>. We have completed processing left subtree of node 14. Now we process node <strong>14<\/strong>. Now we need to process the right subtree of 14 \u2013 which in this example does not exist. Therefore we have completed processing left and right subtree of node 14. On completion of processing node 14, we have completed processing left subtree of root node 23. Therefore we process the node <strong>23<\/strong>. Now we need to process the right subtree of the root 23 rooted at 67. Now we need to process the left subtree of 67 that is 34. Since 34 has no left child, we process the node <strong>34.<\/strong> We proceed in the same way by processing the left subtree, then root node and then right subtree until all the nodes are processed. The in-order traversal is given in Figure 22.4. The in-order traversal of a binary search tree produces a sequence of numbers which are in increasing order.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-306 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-171.png\" alt=\"\" width=\"456\" height=\"251\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-171.png 456w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-171-300x165.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-171-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-171-225x124.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-171-350x193.png 350w\" sizes=\"auto, (max-width: 456px) 100vw, 456px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">22.2.4 Right Node Left Traversal<\/strong><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now for the BST another type of traversal has also been defined called the right node left traversal (Figure 22.5). This traversal is similar to inorder traversal except that instead of left subtree \u2013root- right subtree, the traversal is carried out as right subtree \u2013node-left subtree. This type of traversal is important for a BST because this produces a descending sequence.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-307 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-172.png\" alt=\"\" width=\"493\" height=\"273\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-172.png 493w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-172-300x166.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-172-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-172-225x125.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-172-350x194.png 350w\" sizes=\"auto, (max-width: 493px) 100vw, 493px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>22.3\u00a0 Construction of a Binary Search Tree<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us consider the construction of BSTs. There are two things to remember. First the tree that is constructed must obey the binary search property. Second we need to search the BST to find the correct position in which is insert a node. That is we need to maintain the key property and ensure that the smaller values are in the left sub-tree and the larger values in right sub-tree for every node in the tree. We also need to remember for the same keys, different BSTs will be constructed if the keys are used for construction in a different order.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now the base case is when the tree is empty. In this case, we create new node for the item and make it the root of the BST. In the recursive case, if key (node to be added next) &lt; root&#8217;s value, add to the left subtree, otherwise add to the right subtree. Remember that all BST insertions take place at a leaf or a leaflike node. A leaflike node is a node that has one null subtree.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In order to insert an element e, we need to find the node <em>n<\/em> that should be <em>e<\/em> &#8216;s parent, and set <em>n<\/em> &#8216;s child to be a newly created node containing e. Now here we use the search operation in order to find the parent node <em>n<\/em>. If the element or key is smaller than the root we are at, we examine the left subtree and if the elemnt is greater, we examine the right subtree in each case. When we can no longer proceed, we are at a leaf; this is the position at which to add the element e.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>22.3.1<\/strong>\u00a0\u00a0\u00a0 <strong>Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Perform search for value X<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">Search will end at node Y (if X not in tree). If the element to be added is a duplicate which is generally\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 not\u00a0 \u00a0allowed in BST we can indicate that insertion is not possible.<\/span><\/p>\n<ul>\n<li><span style=\"text-align: initial;font-size: 1em\">If X &lt; Y, we need to insert new leaf X as new left subtree of the node Y<\/span><\/li>\n<li><span style=\"text-align: initial;font-size: 1em\">If X &gt; Y, we need to insert new leaf X as new right subtree of the node Y<\/span><\/li>\n<\/ul>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>22.3.2<\/strong>\u00a0\u00a0<strong>Observations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The search operation will be O(log(n) ) only for balanced trees. We have defined balanced trees as trees where for each node the height of the left and right subtrees differ by at most one. Now insertions into BSTs may unbalance the tree. In fact when we insert the keys in ascending or descending order we will land up with skewed trees. Insertion always places a new item near the periphery (that is at the bottom of the tree) of the BST while ensuring that the BST property is maintained. Searching of the binary search tree is used to locate the insertion point. As in a search operation, starting at the root we probe down the tree till we find a node who\u2019s left or right sub-tree is empty. Now we have found the place for the new value. The insertion is based on comparisons of the new item and values of nodes in the BST. Please note that elements in nodes must be comparable otherwise we cannot construct a BST.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>22.3.3 Detailed Cases<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Case 1: Let us first consider the case when the tree is empty<\/p>\n<ul>\n<li>Here we set the root to a new node containing the item &#8211; <strong><em>create a root<\/em><\/strong> <strong>node<\/strong> <strong>with the new key<\/strong><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Case 2: Now let us assume that the tree is not empty <em style=\"text-align: initial;font-size: 1em\">Here we compare <\/em><span style=\"text-align: initial;font-size: 1em\">key with the top node<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>if key = node key<\/p>\n<p>The node already exists<\/p>\n<p>else if key &gt;\u00a0 node key<\/p>\n<p><em>compare <\/em>key with the right subtree:<\/p>\n<p>if\u00a0 subtree is empty create a leaf node<\/p>\n<p>else add key in right subtree<\/p>\n<p>else key &lt;\u00a0 node key<\/p>\n<p><em>compare <\/em>key with the left subtree:<\/p>\n<p>if the subtree is empty create a leaf node<\/p>\n<p>else add key to the left subtree<\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">The steps here include the search procedure to find the correct location for insertion as well as the actual insertion.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The trace of the recursive BST insert algorithm is given in Figure 22.6. Now let us assume that we want to insert node 19 into the existing BST rooted at 23. Now we find 19 &lt; 23, so we go left, where we find 19&gt; 14 so we go right. Now we find that the right subtree of 14 is empty so we insert there. The BST after insertion is shown in Figure 22.6.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-308 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-173.png\" alt=\"\" width=\"619\" height=\"334\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-173.png 619w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-173-300x162.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-173-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-173-225x121.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-173-350x189.png 350w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>22.3.4 Construction\/Insertion of a Binary Search Tree &#8211; Example<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Add the elements <strong>23, 67, 14, 89, 14, 7, 90, 12<\/strong> to form a Binary Search Tree (Figure 22.7).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-309 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-174.png\" alt=\"\" width=\"575\" height=\"362\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-174.png 575w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-174-300x189.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-174-65x41.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-174-225x142.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-174-350x220.png 350w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 1.Initially\u00a0 BST is Empty-<em>create a root<\/em> node with the new key 23 (Figure 22.7 (a)).<\/p>\n<p style=\"text-align: justify\">2.Now we need to insert 67, which is &gt;23, we have an empty right subtree so we directly insert it as right subtree of 23 (Figure 22.7 (b)).<\/p>\n<p style=\"text-align: justify\">3.Next we insert node 14, which is &lt;23, we have an empty left subtree so we directly insert it as left subtree of 23 (Figure 22.7 (c)).<\/p>\n<p style=\"text-align: justify\">4.Next we insert node 89, 89&gt;23, so we go right, but right subtree not empty, so we check 89 with root of right subtree 67. We find 89&gt;67, so we go right. Now we find an empty right subtree and insert 89 there (Figure 22.7 (d)).<\/p>\n<p style=\"text-align: justify\">5.Next we need to insert node 34. 34&gt;23, so we go right, but right subtree not empty, so we check 89 with root of right subtree 67. We find 34&lt;67, so we go left. Now we find an empty left subtree and insert 34 there (Figure 22.7 (e)).<\/p>\n<p style=\"text-align: justify\">6.Next we need to insert node 7. 7&lt;23, so we go left, but left subtree not empty, so we check 7 with root of left subtree 14. We find 7&lt;14, so we go left. Now we find an empty left subtree and insert 7 there (Figure 22.7 (f)).<\/p>\n<p style=\"text-align: justify\">7.Next we need to insert node 90. 90&gt;23, so we go right, but right subtree not empty, so we check 90 with root of right subtree 67. We find 90&gt;67, so we go right. But right subtree not empty, so we check 90 with root of right subtree 89. We find 90&gt;89, so we go right. Now we find an empty right subtree and insert 90 there (Figure 22.7 (g)).<\/p>\n<p style=\"text-align: justify\">8.Next we need to insert node 12. 12&lt;23, so we go left, but leftt subtree not empty, so we check 12 with root of left subtree 14. We find 12&lt;14, so we go left. But left subtree not empty, so we check 12 with root of left subtree 7. We find 12&gt;7, so we go right. Now we find an empty right subtree and insert 12 there (Figure 22.7 (h)).<\/p>\n<p>&nbsp;<\/p>\n<p><strong>22.3.5 Analysis<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The time taken for initialization will be of the <em>O<\/em>(1). The while loop searches for place to insert <em>the key<\/em>, while maintaining the parent. This <em>O<\/em>(<em>h<\/em>) time where h is the height of the tree. The actual creation of the node for insertion of the value takes <em>O<\/em>(1). Therefore total time taken for insertion of a node will be of <em>O<\/em>(<em>h<\/em>).<\/p>\n<p>&nbsp;<\/p>\n<p><strong>22.4 Removing or Deleting from Binary Search Tree<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When removing a node from a BST, we need to ensure that the binary search tree property is maintained. Therefore during deletion, we need to search for the node, remove it, and do some reorganization to maintain the binary search tree property. There are many cases two be considered, we may be removing an element which is a leaf, removing an element with one child, removing an element having both the children or we may be deleting the root itself. Again we can have both recursive and an iterative implementation. If the node being deleted is a leaf then deletion is a straightforward procedure of just making the associated parent node point to NULL. If the node being deleted has only one child then the parent node of the node being deleted will have to point to the child node of the node being deleted. If the node being deleted has both the children then we first need to find the <strong>inorder successor<\/strong> of the node being deleted and replace the node being deleted with this node. (The inorder successor of a node can be obtained by taking the right node of the current\u00a0<span style=\"text-align: initial;font-size: 1em\">node and traversing in the left till we reach the left most node whose right subtree is null) We can also take inorder predecessor instead of inorder successor. The total time taken for deletion is of the <\/span><em style=\"text-align: initial;font-size: 1em\">O<\/em><span style=\"text-align: initial;font-size: 1em\">(<\/span><em style=\"text-align: initial;font-size: 1em\">h<\/em><span style=\"text-align: initial;font-size: 1em\">).<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">The algorithm removes a specified item from the BST and adjusts the tree. It uses a binary search to locate the target item that is starting at the root it probes down the tree till it finds the target or reaches a leaf node (target not in the tree). The removal of a node must not leave a \u2018gap\u2019 in the tree.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">22.4.1 Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">1.\u00a0<\/strong><span style=\"text-align: initial;font-size: 1em\">if the tree is empty return false<\/span><\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">2.\u00a0<\/strong><span style=\"text-align: initial;font-size: 1em\">Attempt to locate the node x using the binary search\u00a0 algorithm<\/span><\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 if the node is not found return false else the node is found, remove it<\/strong><\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: justify;font-size: 1em\">3.\u00a0<\/strong><strong style=\"text-align: justify;font-size: 1em\">(a) Case 1:<\/strong><span style=\"text-align: justify;font-size: 1em\"> if <\/span><em style=\"text-align: justify;font-size: 1em\">x<\/em><span style=\"text-align: justify;font-size: 1em\"> has no children then remove <\/span><em style=\"text-align: justify;font-size: 1em\">x (<\/em><span style=\"text-align: justify;font-size: 1em\">The node to be deleted has no children. In this case, all we need to do is delete the node)<\/span><\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">if the node has 2 empty subtrees<\/strong><\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">replace the link in the parent of x which was pointing to x with null<\/strong><\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: justify;font-size: 1em\">\u00a0 (b) C<\/strong><strong style=\"text-align: justify;font-size: 1em\">ase 2:<\/strong><span style=\"text-align: justify;font-size: 1em\"> if <\/span><em style=\"text-align: justify;font-size: 1em\">x<\/em><span style=\"text-align: justify;font-size: 1em\"> has one child (left) then make <\/span><em style=\"text-align: justify;font-size: 1em\">parent<\/em><span style=\"text-align: justify;font-size: 1em\">[<\/span><em style=\"text-align: justify;font-size: 1em\">x<\/em><span style=\"text-align: justify;font-size: 1em\">] point to existing child of x. The node to be deleted has only a left subtree. We delete the node and attach the left subtree to the deleted node\u2019s parent.<\/span><\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">\u00a0if the node has no right child (only left child)<\/strong><\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">\u00a0link the parent of the target to the left (non-empty) subtree<\/strong><\/p>\n<p style=\"text-align: justify\"><strong style=\"font-size: 1em\">(c)\u00a0 Case 3:<\/strong><span style=\"font-size: 1em\"> if <\/span><em style=\"font-size: 1em\">x<\/em><span style=\"font-size: 1em\"> has one child (right) then make <\/span><em style=\"font-size: 1em\">parent<\/em><span style=\"font-size: 1em\">[<\/span><em style=\"font-size: 1em\">x<\/em><span style=\"font-size: 1em\">] point to existing child of x. The node to be deleted has only a right subtree. We delete the node and attach the right subtree to the deleted node\u2019s parent.<\/span><\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">if the node has no left child (only right child)<\/strong><\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">link the parent of the node to the right (non-empty) subtree<\/strong><\/p>\n<p style=\"text-align: justify\"><strong style=\"font-size: 1em\">(d)\u00a0 <\/strong><strong style=\"font-size: 1em\">Case 4: <\/strong><span style=\"font-size: 1em\">if <\/span><em style=\"font-size: 1em\">x<\/em><span style=\"font-size: 1em\"> has two children (subtrees) then swap <\/span><em style=\"font-size: 1em\">x<\/em><span style=\"font-size: 1em\"> with its in order successor &#8211; perform case 0 or case 1 to delete it. The node to be deleted has two subtrees. Rather than simply delete the node, we try to maintain the existing structure as much as possible by finding data to take the place of the deleted data. This can be done in one of two ways. We can find the largest node in the deleted node\u2019s left subtree and move its data to replace the deleted node\u2019s data. We can find the smallest node on the deleted node\u2019s right subtree and move its data to replace the deleted node\u2019s data. Either of these moves preserves the binary search property of the tree. Moreover the property of inorder successor (or predecessor) ensures that this node will have either no child or at most one child. The inorder successor (respectively,\u00a0<\/span><span style=\"font-size: 1em;text-align: initial\">the predecessor) of a key k in a search tree is the smallest (respectively, the largest) key that belongs to the tree and that is strictly greater than (respectively, less than) k. The idea for finding the successor of a given node x is that if x has right child, then the successor is the minimum in the right subtree of x otherwise, the successor is the parent of the farthest node that can be reached from x by following only right branches backward. The predecessor can be found similarly with the roles of left and right exchanged and with the roles of maximum and minimum exchanged.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>Before we go further let us describe the algorithm to find the inorder successor of a node x.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The successor (x ) = y, is such that key [y] is the smallest key <em>&gt;<\/em> key [x]. Here there are two cases<\/p>\n<p style=\"text-align: justify\">\u2022 Case 1: right (x) is non empty<\/p>\n<p style=\"text-align: justify\">\u2013\u00a0\u00a0 successor (x ) = the minimum in right (x)<\/p>\n<p style=\"text-align: justify\">\u2022 Case 2: right (x) is empty<\/p>\n<p style=\"text-align: justify\">\u00a0\u2013 go up the tree until the current node is a left child: successor (x ) is the parent of the current node<\/p>\n<p style=\"text-align: justify\">\u00a0\u2013 if you cannot go further (and you reached the root): x is the largest element<\/p>\n<p style=\"text-align: justify\">The algorithm is given in Figure 22.8.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-310 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-175.png\" alt=\"\" width=\"386\" height=\"213\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-175.png 386w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-175-300x166.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-175-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-175-225x124.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-175-350x193.png 350w\" sizes=\"auto, (max-width: 386px) 100vw, 386px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>if the node has a left and a right subtree<\/strong><\/p>\n<ul>\n<li><strong>replace the node&#8217;s value with the minimum value in the right subtree (inorder successor)<\/strong><\/li>\n<li><strong>delete the minimum node in the right subtree<\/strong><\/li>\n<\/ul>\n<p style=\"text-align: justify\">Instead of using the inorder successor as we described above, we can use the inorder predecessor defined in a similar way.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We have explained the concept of the deletion algorithm and detailed the four cases we consider during deletion. We use the Tree Successor algorithm to find the inorder successor when the node to be deleted has both left and right children. We assume that the parent of a node is available. Now let us assume that we want to delete the node z from the binary search tree. If z has no children, then we will just replace z by nil. If z has only one child, then we will promote the unique child to z\u2019s place. If z has two children, then we will identify z\u2019s successor. Call it y. The successor y either is a leaf or has only the right child. We promote y to z\u2019s place. Now the deletion of y from its original place will result in deletion of the first three cases only. The code for the above is given in Figure 22.9.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-311 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-176.png\" alt=\"\" width=\"615\" height=\"548\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-176.png 615w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-176-300x267.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-176-65x58.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-176-225x200.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-176-350x312.png 350w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>22.4.2 Deletion from BST \u2013 Example<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us discuss each of the 4 cases of deletion with examples. Let us consider Case 1 where the node to be deleted is a leaf that is has no children. Let us consider deletion of node 34 (Figure 22.10). We need to carry out the search operation (as was done for insertion) to find 34. Now parent of 34 that is 67 would have 34 as its left child. The parent 67\u2019s left child is now made Null.<\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-312 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-177.png\" alt=\"\" width=\"519\" height=\"301\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-177.png 519w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-177-300x174.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-177-65x38.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-177-225x130.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-177-350x203.png 350w\" sizes=\"auto, (max-width: 519px) 100vw, 519px\" \/><\/p>\n<p style=\"text-align: justify\">Now let us consider Case 2 where the node to be deleted has one child, in this only left child. Let us consider deletion of node 12 (Figure 22.11). We need to carry out the search operation (as was done for insertion) to find 12. Now 12 has only left child that is 10. Node 12 is deleted and 12\u2019s parent 7\u2019s right child is now the left child of 12 that is 10.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-313 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-178.png\" alt=\"\" width=\"551\" height=\"277\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-178.png 551w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-178-300x151.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-178-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-178-225x113.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-178-350x176.png 350w\" sizes=\"auto, (max-width: 551px) 100vw, 551px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us consider Case 3 where the node to be deleted has one child, in this only right child. Let us consider deletion of node 90 (Figure 22.12). We need to carry out the search operation (as was done for insertion) to find 90. Now 90 has only a right child that is 99. Node 90 is deleted and 90\u2019s parent 89\u2019s right child is now the right child of 90 that is 99.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-314 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-179.png\" alt=\"\" width=\"565\" height=\"312\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-179.png 565w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-179-300x166.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-179-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-179-225x124.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-179-350x193.png 350w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us consider Case 4 where the node to be deleted has both the children. Let us consider deletion of node 67 (Figure 22.13). We need to carry out the search operation (as was done for insertion) to find 67. Now 67 has both left (34) and right (89)\u00a0 children. First we need to find the inorder successor of 67, which is 81. Now we replace 67 in tree with it\u2019s inorder successor 81. In order to do this we first need to delete 81 from its place in the tree. By definition of inorder successor, 81 can have at most one child. In our case it has no children. Therefore to delete 81 we make the left child of its parent (89) as Null. To replace 67 with 81, the parent of 67 that is 23\u2019s right child should now point to 81. Moreover 81\u2019s left pointer should be made to point to 34 (67\u2019s left child) and 81\u2019s right pointer should be made to point to 89 (67\u2019s right child). This completes the deletion process.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-315 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-180.png\" alt=\"\" width=\"553\" height=\"292\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-180.png 553w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-180-300x158.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-180-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-180-225x119.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-180-350x185.png 350w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<ul>\n<li>Illustrated the different type of Binary Search Tree Traversals with Examples<\/li>\n<li>Discussed the Construction of Binary Search Trees<\/li>\n<li><span style=\"font-size: 1em\">Described the Insertion operation of Binary Search Trees<\/span><\/li>\n<li><span style=\"font-size: 1em\">Explained the Deletion Operation of Binary Search Trees<\/span><\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Binary Search Trees-II<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/ZUlIypDLp4U\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-317 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-181.png\" alt=\"\" width=\"640\" height=\"353\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-181.png 640w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-181-300x165.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-181-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-181-225x124.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-181-350x193.png 350w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-318 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-182.png\" alt=\"\" width=\"657\" height=\"324\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-182.png 657w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-182-300x148.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-182-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-182-225x111.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-182-350x173.png 350w\" sizes=\"auto, (max-width: 657px) 100vw, 657px\" \/><\/p>\n","protected":false},"author":3,"menu_order":22,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-t-v-geetha"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-300","chapter","type-chapter","status-publish","hentry","contributor-dr-t-v-geetha"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":11,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/300\/revisions"}],"predecessor-version":[{"id":948,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/300\/revisions\/948"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/300\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/media?parent=300"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapter-type?post=300"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/contributor?post=300"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/license?post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}