{"id":278,"date":"2018-07-19T05:04:27","date_gmt":"2018-07-19T05:04:27","guid":{"rendered":"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=278"},"modified":"2018-12-12T10:13:04","modified_gmt":"2018-12-12T10:13:04","slug":"binary-search-trees-i","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/chapter\/binary-search-trees-i\/","title":{"rendered":"Binary Search Trees-I"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/1MH4fbMx7Bc\" 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 Tree ADT. In this module we will discuss one very important type of binary tree that is 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 understand the concept of Binary Search Trees\r\n\r\n\u2022 To discuss the Binary Search Tree Search\r\n\r\n\u2022 To describe Traversal of Binary Search Trees \u2013 the In-order traversal\r\n\r\n\u2022 To explain the operations of finding the Successor and Predecessor of a node in a Binary Search Tree\r\n\r\n&nbsp;\r\n\r\n<strong>21.1\u00a0 Introduction<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Binary Search Trees (BST) or Lexicographic Trees are a type of Binary Trees with a special organization of data. This organization of the nodes of the binary tree makes searching for an element faster of the O(log n) and for some types of balanced binary search trees (we will discuss this in later modules) insertions and deletions also has a time complexity of the O(log n). However in general insertions and deletions in BST have a complexity of the O(h) where h is the height of the tree. The properties of BST are defined in terms of the maximum number of leaves, maximum number of nodes N and the average depth for the N nodes.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">A binary search tree is a binary tree with a special property called the BST-property, which is defined as follows:<\/p>\r\n&nbsp;\r\n\r\n\u2022\u00a0 For a tree or sub-tree rooted at X,\r\n\r\n\u2013\u00a0 All keys to the left of X are lesser than the key at X\r\n\r\n\u2013\u00a0 All keys to the right of X are greater than the key at X\r\n\r\n\u2022\u00a0 This property is true for every sub-tree of the BST\r\n\r\n\u2022\u00a0 We can assume that the keys of a BST are pairwise distinct\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In other words all items in the left subtree are less than the root K, and all items in the right subtree are greater than the root K (Figure 21.1).<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone wp-image-281 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-150.png\" alt=\"\" width=\"401\" height=\"233\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>21.2 Binary Search Tree Property<\/strong>\r\n\r\n&nbsp;\r\n\r\nAll the stored keys in a binary search tree must satisfy the <em>binary search tree<\/em> property.\r\n\r\nAll nodes K in left sub-tree of <em>R<\/em>, will be such that <em>key<\/em>[<em>K<\/em>] \u00a3 <em>key<\/em>[<em>R<\/em>].\r\n\r\nSimilarly all nodes in right sub-tree of <em>R<\/em>, will be such that <em>key<\/em>[<em>K<\/em>] \u00b3 <em>key<\/em>[<em>R<\/em>].\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In other words in a BST, the value stored at the root of a sub-tree is <em>greater<\/em> than any value in its left subtree and <em>less<\/em> than any value in its right subtree (Figure 21.2).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-282 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-151.png\" alt=\"\" width=\"403\" height=\"347\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>21.3 Other properties of Binary Search Trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The minimum node is identified as the leftmost node, i.e. the farthest node you can reach by following only left branches. Similarly the maximum node is identified as the rightmost node, i.e. the farthest node you can reach by following only right branches.\u00a0<span style=\"text-align: initial;font-size: 1em\">Hence the smallest element in a binary search tree is the <\/span><strong style=\"text-align: initial;font-size: 1em\">leftmost element<\/strong><span style=\"text-align: initial;font-size: 1em\"> and the\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">.largest element is the <\/span><strong style=\"text-align: initial;font-size: 1em\">rightmost element<\/strong><span style=\"text-align: initial;font-size: 1em\"> (Figure 21.3). The same set of keys can result in different BSTs depending on the order in which the keys are inserted into the BST (Figure 21.4). The search time of a node in a tree depends on its depth. In the case of a binary search tree, the maximum depth in the skewed case with the least or the largest key at the root is N where N is the number of nodes. However the minimum depth is logN.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-283 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-152.png\" alt=\"\" width=\"534\" height=\"344\" \/>\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-284 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-153.png\" alt=\"\" width=\"534\" height=\"349\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us now see BST examples and examples that are not BSTs (Figure 21.5). The first example shows a BST with keys being names so here the lexicographic order is relevant while the next example shows a BST with keys being numbers and hence numeric order is followed. The third example shows a tree that is not a BST since the left sub-tree is not binary and the right sub-tree does not satisfy the binary search property.<\/p>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-285 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-154.png\" alt=\"\" width=\"591\" height=\"291\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Sometimes duplicate entries are allowed in binary search trees in which case they are always placed in the right subtree (Figure 21.6).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-286 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-155.png\" alt=\"\" width=\"436\" height=\"259\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Binary Search Trees data structures that can support dynamic set operations like search, minimum, maximum, predecessor, successor, insert, and delete and hence are appropriate for building dictionaries and priority queues. All the basic operations associated with binary search trees have maximum complexity of the <em>O<\/em>(<em>h<\/em>) where h is the height h of the tree.<\/p>\r\n&nbsp;\r\n\r\n<strong>21.4 Ordered Dictionaries<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">When binary search trees are used to represent dictionaries the keys are assumed to come from a total order. Remember that a total order relation satisfies the reflexive, anti-symmetric and transitive properties. Examples of total order relations include \u2264 and \u2265 for numerical order and we can define a &lt; b if a comes before b in the lexicographic order. Some of the operations associated with the dictionary are:<\/p>\r\n&nbsp;\r\n\r\n\u2022\u00a0 first(): the first entry in the ordering of the dictionary\r\n\r\n\u2022\u00a0 last(): last entry in the ordering of the dictionary\r\n\r\n\u2022\u00a0 successors(k): iterator of entries with keys greater than or equal to k; where the order is increasing\r\n\r\n\u2022\u00a0 predecessors(k): iterator of entries with keys less than or equal to k where the order is decreasing\r\n\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">21.5\u00a0 Binary Search Tree (BST) ADTs<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">The ADT for a binary search tree is similar to the ADT for a general linear list with the same operations. However searching a BST is more efficient than searching a linear list. While a general linear list uses sequential searching, BSTs use a version of binary search.<\/span><\/p>\r\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">BSTs can be implemented using either arrays or linked lists. However, linked list structures are more common and more efficient. The implementation uses nodes with two pointers, left and right with the node containing the data element (Figure 21.7)<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-287 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-156.png\" alt=\"\" width=\"555\" height=\"253\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The Binary search tree is represented by a linked data structure of nodes. Here <em>root<\/em>(<em>T<\/em>) points to the root of tree<em> T<\/em>. In one implementation each node contains the fields \u2013<em>key, left<\/em> - pointer to left child, <em>right<\/em> <em>\u2013<\/em> pointer to right child and <em>p<\/em> <em>\u2013<\/em> pointer to parent. <em>p<\/em>[<em>root<\/em>[T]] = NIL (optional).<\/p>\r\n&nbsp;\r\n\r\nSome of the typical operations of the ADT binary search tree are\r\n<ul>\r\n \t<li><strong>Retrieving<\/strong> the item with a given search key from a binary search tree<\/li>\r\n \t<li style=\"text-align: justify\"><strong>Traversing<\/strong> the items in a binary search tree in preorder, in-order, or post-order<\/li>\r\n \t<li style=\"text-align: justify\"><strong>Inserting<\/strong> a new item into a binary search tree such that BST property is maintained<\/li>\r\n \t<li style=\"text-align: justify\"><strong>Deleting<\/strong> the item with a given search key from a binary search tree such that BST property is maintained<\/li>\r\n<\/ul>\r\nBST allows us to implement the following accessing operations\r\n<ul>\r\n \t<li>Search(element) \u2013 search for a particular element in the BST<\/li>\r\n \t<li>add(element) \u2013 add or insert an element in its proper place in the BST<\/li>\r\n \t<li style=\"text-align: justify\">getHeight \u2013 Get the height of the BST<\/li>\r\n \t<li style=\"text-align: justify\">getMin, getMax \u2013 get the minimum \/ maximum key from the BST<\/li>\r\n \t<li style=\"text-align: justify\">removeMin, removeMax - remove the minimum \/ maximum key from the BST and restore the BST\u00a0 property<\/li>\r\n \t<li style=\"text-align: justify\">remove(element) \u2013 remove the element from the BST and restore the BST property<\/li>\r\n \t<li style=\"text-align: justify\">printInOrder, printPreOrder, printPostOrder \u2013 traverse the BST and print the elements in the required order<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">21.6\u00a0 Searching BST<\/strong>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">There are basically three search algorithms possible in a BST - finding the smallest node, finding the largest node and finding a requested node (BST search). For example in Figure 21.8 suppose we are searching for a node. If we are searching for 15, then we are done. If we are searching for a key &lt; 15, then we should search in the left subtree. If we are searching for a key &gt; 15, then we should search in the right subtree.<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-288 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-157.png\" alt=\"\" width=\"256\" height=\"249\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>21.6.1 Finding Minimum and Maximum<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In order to find the minimum element in the BST, we follow <em>left<\/em> children until we reach null. Similarly in order to find the maximum element in the BST, we follow <em>right<\/em> children until we reach null (Figure 21.9).<\/p>\r\n<img class=\"alignnone size-full wp-image-289 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-158.png\" alt=\"\" width=\"461\" height=\"250\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The binary-search-tree property guarantees that the minimum key is located at the left-most node and the maximum key is located at the right-most node. The algorithm\u00a0<span style=\"font-size: 1em;text-align: initial\">to find these elements is given in Figure 21.10 where x is a pointer to a tree node.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-290 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-159.png\" alt=\"\" width=\"581\" height=\"138\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Binary Search algorithm of an array of <em>sorted<\/em> items reduces the search space by one half after each comparison (Figure 21.11). Many of the other operations associated with BST use the search operation.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-291 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-160.png\" alt=\"\" width=\"502\" height=\"402\" \/>\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-292 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-161.png\" alt=\"\" width=\"405\" height=\"297\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>Figure 21.12 Searching for the Element 9 <\/strong><\/p>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">As shown in Figure 21.12 when we want to search for 9, we compare 9 with 13 (root); and go to left subtree. Then we compare 9 with 7; and go to right subtree, then we compare 9 with 8 and again go to right subtree. Then we compare 9 with 10 and go to left subtree. Finally we compare 9 with 9 and we find the element we searched for. At this point if we do not find the element then the element is not in the BST.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us understand both the recursive and iterative algorithms for searching an element in the BST. The recursive algorithm is easier to understand as it look for the key and if the key to be found is less that root we call Tree-Search of the left subtree else if the key is greater than the root we call Tree-Search of the right subtree. We recursively do this till we find the key or the key is not in tree. In the iterative case we use a pointer x (pointer to the tree node) to traverse the tree (Figure 21.13). The iterative tree search is more efficient on most computers. The running time is O(h) where h is the height of the tree.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-293 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-162.png\" alt=\"\" width=\"600\" height=\"214\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>21.7 In order Traversal of a BST<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A very interesting property of a BST is that if we apply the inorder traversal, the elements that are visited are sorted in ascending order. When we print the elements of a binary search tree in the order of the recursive in-order traversal (or walk) of the tree, the binary-search-tree property ensures that the keys are printed in increasing order. The recursive algorithm is given in Figure 21.14 where x is the node from which we start the traversal. The Inorder traversal of a BST is similar to the in-order traversal of a binary tree described in the previous module.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-294 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-163.png\" alt=\"\" width=\"444\" height=\"273\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong> 21.7.1 Successor and Predecessor of a Node<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The successor and predecessor of a node is defined in terms of the in-order traversal of the binary search tree. Successor of node <em>x<\/em> is defined as the node <em>y<\/em> such that the <em>key of y<\/em> is the smallest key (left most node) that is greater than <em>key<\/em>[<em>x<\/em>]. The successor of the largest key is NIL. In this case searching for the successor needs to consider two cases. If node <em>x<\/em> has a non-empty right subtree, then <em>x<\/em>\u2019s successor is the minimum key in the right subtree of <em>x<\/em>. However if the node <em>x<\/em> has an empty right subtree, we move up the tree (move up through right children). Now we encounter keys that are smaller than x. We need to first find the lowest ancestor of x. If node x has an empty right subtree and x has a successor y, then y is the lowest ancestor of x whose left child is also an ancestor of x.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-295 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-164.png\" alt=\"\" width=\"617\" height=\"337\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us consider Case(a) of Figure 21.15. Suppose we want to find successor of 56. Now 56 has a non-empty right subtree and hence the successor of 56 is the minimum value in the right subtree \u2013 in this case 190. Now considering Case(b), 10 whose successor we want to find has an empty right subtree. Therefore we move left up the tree and find its successor 13. The algorithm for finding the successor is given in Figure 21.16 and for finding predecessor which is symmetric is given in Figure<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n21.17. Both the algorithms have a running time of <em>O<\/em>(<em>h<\/em>) where h is the height of the tree.\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-296 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-165.png\" alt=\"\" width=\"383\" height=\"563\" \/>\r\n\r\n<strong>Summary<\/strong>\r\n<ul>\r\n \t<li>Explained the concept of Binary Search Trees<\/li>\r\n \t<li>Discussed Searching operation of Binary Search Trees<\/li>\r\n \t<li>Described In order Traversal of Binary Search Trees<\/li>\r\n \t<li>Explained the Successor and Predecessor operations associated with Binary Search Trees<\/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-I<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/1MH4fbMx7Bc\" 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\r\n<img class=\"size-full wp-image-297 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-166.png\" alt=\"\" width=\"687\" height=\"415\" \/>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-298 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-167.png\" alt=\"\" width=\"661\" height=\"329\" \/>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/1MH4fbMx7Bc\" 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 Tree ADT. In this module we will discuss one very important type of binary tree that is 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 understand the concept of Binary Search Trees<\/p>\n<p>\u2022 To discuss the Binary Search Tree Search<\/p>\n<p>\u2022 To describe Traversal of Binary Search Trees \u2013 the In-order traversal<\/p>\n<p>\u2022 To explain the operations of finding the Successor and Predecessor of a node in a Binary Search Tree<\/p>\n<p>&nbsp;<\/p>\n<p><strong>21.1\u00a0 Introduction<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Binary Search Trees (BST) or Lexicographic Trees are a type of Binary Trees with a special organization of data. This organization of the nodes of the binary tree makes searching for an element faster of the O(log n) and for some types of balanced binary search trees (we will discuss this in later modules) insertions and deletions also has a time complexity of the O(log n). However in general insertions and deletions in BST have a complexity of the O(h) where h is the height of the tree. The properties of BST are defined in terms of the maximum number of leaves, maximum number of nodes N and the average depth for the N nodes.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A binary search tree is a binary tree with a special property called the BST-property, which is defined as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0 For a tree or sub-tree rooted at X,<\/p>\n<p>\u2013\u00a0 All keys to the left of X are lesser than the key at X<\/p>\n<p>\u2013\u00a0 All keys to the right of X are greater than the key at X<\/p>\n<p>\u2022\u00a0 This property is true for every sub-tree of the BST<\/p>\n<p>\u2022\u00a0 We can assume that the keys of a BST are pairwise distinct<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In other words all items in the left subtree are less than the root K, and all items in the right subtree are greater than the root K (Figure 21.1).<\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-281 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-150.png\" alt=\"\" width=\"401\" height=\"233\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-150.png 356w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-150-300x174.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-150-65x38.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-150-225x131.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-150-350x204.png 350w\" sizes=\"auto, (max-width: 401px) 100vw, 401px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>21.2 Binary Search Tree Property<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>All the stored keys in a binary search tree must satisfy the <em>binary search tree<\/em> property.<\/p>\n<p>All nodes K in left sub-tree of <em>R<\/em>, will be such that <em>key<\/em>[<em>K<\/em>] \u00a3 <em>key<\/em>[<em>R<\/em>].<\/p>\n<p>Similarly all nodes in right sub-tree of <em>R<\/em>, will be such that <em>key<\/em>[<em>K<\/em>] \u00b3 <em>key<\/em>[<em>R<\/em>].<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In other words in a BST, the value stored at the root of a sub-tree is <em>greater<\/em> than any value in its left subtree and <em>less<\/em> than any value in its right subtree (Figure 21.2).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-282 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-151.png\" alt=\"\" width=\"403\" height=\"347\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-151.png 403w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-151-300x258.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-151-65x56.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-151-225x194.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-151-350x301.png 350w\" sizes=\"auto, (max-width: 403px) 100vw, 403px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>21.3 Other properties of Binary Search Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The minimum node is identified as the leftmost node, i.e. the farthest node you can reach by following only left branches. Similarly the maximum node is identified as the rightmost node, i.e. the farthest node you can reach by following only right branches.\u00a0<span style=\"text-align: initial;font-size: 1em\">Hence the smallest element in a binary search tree is the <\/span><strong style=\"text-align: initial;font-size: 1em\">leftmost element<\/strong><span style=\"text-align: initial;font-size: 1em\"> and the\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">.largest element is the <\/span><strong style=\"text-align: initial;font-size: 1em\">rightmost element<\/strong><span style=\"text-align: initial;font-size: 1em\"> (Figure 21.3). The same set of keys can result in different BSTs depending on the order in which the keys are inserted into the BST (Figure 21.4). The search time of a node in a tree depends on its depth. In the case of a binary search tree, the maximum depth in the skewed case with the least or the largest key at the root is N where N is the number of nodes. However the minimum depth is logN.<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-283 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-152.png\" alt=\"\" width=\"534\" height=\"344\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-152.png 534w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-152-300x193.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-152-65x42.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-152-225x145.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-152-350x225.png 350w\" sizes=\"auto, (max-width: 534px) 100vw, 534px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-284 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-153.png\" alt=\"\" width=\"534\" height=\"349\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-153.png 534w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-153-300x196.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-153-65x42.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-153-225x147.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-153-350x229.png 350w\" sizes=\"auto, (max-width: 534px) 100vw, 534px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us now see BST examples and examples that are not BSTs (Figure 21.5). The first example shows a BST with keys being names so here the lexicographic order is relevant while the next example shows a BST with keys being numbers and hence numeric order is followed. The third example shows a tree that is not a BST since the left sub-tree is not binary and the right sub-tree does not satisfy the binary search property.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-285 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-154.png\" alt=\"\" width=\"591\" height=\"291\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-154.png 591w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-154-300x148.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-154-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-154-225x111.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-154-350x172.png 350w\" sizes=\"auto, (max-width: 591px) 100vw, 591px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Sometimes duplicate entries are allowed in binary search trees in which case they are always placed in the right subtree (Figure 21.6).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-286 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-155.png\" alt=\"\" width=\"436\" height=\"259\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-155.png 436w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-155-300x178.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-155-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-155-225x134.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-155-350x208.png 350w\" sizes=\"auto, (max-width: 436px) 100vw, 436px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Binary Search Trees data structures that can support dynamic set operations like search, minimum, maximum, predecessor, successor, insert, and delete and hence are appropriate for building dictionaries and priority queues. All the basic operations associated with binary search trees have maximum complexity of the <em>O<\/em>(<em>h<\/em>) where h is the height h of the tree.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>21.4 Ordered Dictionaries<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When binary search trees are used to represent dictionaries the keys are assumed to come from a total order. Remember that a total order relation satisfies the reflexive, anti-symmetric and transitive properties. Examples of total order relations include \u2264 and \u2265 for numerical order and we can define a &lt; b if a comes before b in the lexicographic order. Some of the operations associated with the dictionary are:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0 first(): the first entry in the ordering of the dictionary<\/p>\n<p>\u2022\u00a0 last(): last entry in the ordering of the dictionary<\/p>\n<p>\u2022\u00a0 successors(k): iterator of entries with keys greater than or equal to k; where the order is increasing<\/p>\n<p>\u2022\u00a0 predecessors(k): iterator of entries with keys less than or equal to k where the order is decreasing<\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">21.5\u00a0 Binary Search Tree (BST) ADTs<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">The ADT for a binary search tree is similar to the ADT for a general linear list with the same operations. However searching a BST is more efficient than searching a linear list. While a general linear list uses sequential searching, BSTs use a version of binary search.<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">BSTs can be implemented using either arrays or linked lists. However, linked list structures are more common and more efficient. The implementation uses nodes with two pointers, left and right with the node containing the data element (Figure 21.7)<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-287 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-156.png\" alt=\"\" width=\"555\" height=\"253\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-156.png 555w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-156-300x137.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-156-65x30.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-156-225x103.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-156-350x160.png 350w\" sizes=\"auto, (max-width: 555px) 100vw, 555px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The Binary search tree is represented by a linked data structure of nodes. Here <em>root<\/em>(<em>T<\/em>) points to the root of tree<em> T<\/em>. In one implementation each node contains the fields \u2013<em>key, left<\/em> &#8211; pointer to left child, <em>right<\/em> <em>\u2013<\/em> pointer to right child and <em>p<\/em> <em>\u2013<\/em> pointer to parent. <em>p<\/em>[<em>root<\/em>[T]] = NIL (optional).<\/p>\n<p>&nbsp;<\/p>\n<p>Some of the typical operations of the ADT binary search tree are<\/p>\n<ul>\n<li><strong>Retrieving<\/strong> the item with a given search key from a binary search tree<\/li>\n<li style=\"text-align: justify\"><strong>Traversing<\/strong> the items in a binary search tree in preorder, in-order, or post-order<\/li>\n<li style=\"text-align: justify\"><strong>Inserting<\/strong> a new item into a binary search tree such that BST property is maintained<\/li>\n<li style=\"text-align: justify\"><strong>Deleting<\/strong> the item with a given search key from a binary search tree such that BST property is maintained<\/li>\n<\/ul>\n<p>BST allows us to implement the following accessing operations<\/p>\n<ul>\n<li>Search(element) \u2013 search for a particular element in the BST<\/li>\n<li>add(element) \u2013 add or insert an element in its proper place in the BST<\/li>\n<li style=\"text-align: justify\">getHeight \u2013 Get the height of the BST<\/li>\n<li style=\"text-align: justify\">getMin, getMax \u2013 get the minimum \/ maximum key from the BST<\/li>\n<li style=\"text-align: justify\">removeMin, removeMax &#8211; remove the minimum \/ maximum key from the BST and restore the BST\u00a0 property<\/li>\n<li style=\"text-align: justify\">remove(element) \u2013 remove the element from the BST and restore the BST property<\/li>\n<li style=\"text-align: justify\">printInOrder, printPreOrder, printPostOrder \u2013 traverse the BST and print the elements in the required order<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">21.6\u00a0 Searching BST<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">There are basically three search algorithms possible in a BST &#8211; finding the smallest node, finding the largest node and finding a requested node (BST search). For example in Figure 21.8 suppose we are searching for a node. If we are searching for 15, then we are done. If we are searching for a key &lt; 15, then we should search in the left subtree. If we are searching for a key &gt; 15, then we should search in the right subtree.<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-288 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-157.png\" alt=\"\" width=\"256\" height=\"249\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-157.png 256w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-157-65x63.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-157-225x219.png 225w\" sizes=\"auto, (max-width: 256px) 100vw, 256px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>21.6.1 Finding Minimum and Maximum<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In order to find the minimum element in the BST, we follow <em>left<\/em> children until we reach null. Similarly in order to find the maximum element in the BST, we follow <em>right<\/em> children until we reach null (Figure 21.9).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-289 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-158.png\" alt=\"\" width=\"461\" height=\"250\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-158.png 461w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-158-300x163.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-158-65x35.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-158-225x122.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-158-350x190.png 350w\" sizes=\"auto, (max-width: 461px) 100vw, 461px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The binary-search-tree property guarantees that the minimum key is located at the left-most node and the maximum key is located at the right-most node. The algorithm\u00a0<span style=\"font-size: 1em;text-align: initial\">to find these elements is given in Figure 21.10 where x is a pointer to a tree node.<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-290 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-159.png\" alt=\"\" width=\"581\" height=\"138\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-159.png 581w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-159-300x71.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-159-65x15.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-159-225x53.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-159-350x83.png 350w\" sizes=\"auto, (max-width: 581px) 100vw, 581px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Binary Search algorithm of an array of <em>sorted<\/em> items reduces the search space by one half after each comparison (Figure 21.11). Many of the other operations associated with BST use the search operation.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-291 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-160.png\" alt=\"\" width=\"502\" height=\"402\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-160.png 502w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-160-300x240.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-160-65x52.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-160-225x180.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-160-350x280.png 350w\" sizes=\"auto, (max-width: 502px) 100vw, 502px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-292 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-161.png\" alt=\"\" width=\"405\" height=\"297\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-161.png 405w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-161-300x220.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-161-65x48.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-161-225x165.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-161-350x257.png 350w\" sizes=\"auto, (max-width: 405px) 100vw, 405px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>Figure 21.12 Searching for the Element 9 <\/strong><\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">As shown in Figure 21.12 when we want to search for 9, we compare 9 with 13 (root); and go to left subtree. Then we compare 9 with 7; and go to right subtree, then we compare 9 with 8 and again go to right subtree. Then we compare 9 with 10 and go to left subtree. Finally we compare 9 with 9 and we find the element we searched for. At this point if we do not find the element then the element is not in the BST.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us understand both the recursive and iterative algorithms for searching an element in the BST. The recursive algorithm is easier to understand as it look for the key and if the key to be found is less that root we call Tree-Search of the left subtree else if the key is greater than the root we call Tree-Search of the right subtree. We recursively do this till we find the key or the key is not in tree. In the iterative case we use a pointer x (pointer to the tree node) to traverse the tree (Figure 21.13). The iterative tree search is more efficient on most computers. The running time is O(h) where h is the height of the tree.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-293 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-162.png\" alt=\"\" width=\"600\" height=\"214\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-162.png 600w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-162-300x107.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-162-65x23.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-162-225x80.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-162-350x125.png 350w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>21.7 In order Traversal of a BST<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A very interesting property of a BST is that if we apply the inorder traversal, the elements that are visited are sorted in ascending order. When we print the elements of a binary search tree in the order of the recursive in-order traversal (or walk) of the tree, the binary-search-tree property ensures that the keys are printed in increasing order. The recursive algorithm is given in Figure 21.14 where x is the node from which we start the traversal. The Inorder traversal of a BST is similar to the in-order traversal of a binary tree described in the previous module.<\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-294 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-163.png\" alt=\"\" width=\"444\" height=\"273\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-163.png 444w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-163-300x184.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-163-65x40.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-163-225x138.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-163-350x215.png 350w\" sizes=\"auto, (max-width: 444px) 100vw, 444px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong> 21.7.1 Successor and Predecessor of a Node<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The successor and predecessor of a node is defined in terms of the in-order traversal of the binary search tree. Successor of node <em>x<\/em> is defined as the node <em>y<\/em> such that the <em>key of y<\/em> is the smallest key (left most node) that is greater than <em>key<\/em>[<em>x<\/em>]. The successor of the largest key is NIL. In this case searching for the successor needs to consider two cases. If node <em>x<\/em> has a non-empty right subtree, then <em>x<\/em>\u2019s successor is the minimum key in the right subtree of <em>x<\/em>. However if the node <em>x<\/em> has an empty right subtree, we move up the tree (move up through right children). Now we encounter keys that are smaller than x. We need to first find the lowest ancestor of x. If node x has an empty right subtree and x has a successor y, then y is the lowest ancestor of x whose left child is also an ancestor of x.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-295 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-164.png\" alt=\"\" width=\"617\" height=\"337\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-164.png 617w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-164-300x164.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-164-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-164-225x123.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-164-350x191.png 350w\" sizes=\"auto, (max-width: 617px) 100vw, 617px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us consider Case(a) of Figure 21.15. Suppose we want to find successor of 56. Now 56 has a non-empty right subtree and hence the successor of 56 is the minimum value in the right subtree \u2013 in this case 190. Now considering Case(b), 10 whose successor we want to find has an empty right subtree. Therefore we move left up the tree and find its successor 13. The algorithm for finding the successor is given in Figure 21.16 and for finding predecessor which is symmetric is given in Figure<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>21.17. Both the algorithms have a running time of <em>O<\/em>(<em>h<\/em>) where h is the height of the tree.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-296 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-165.png\" alt=\"\" width=\"383\" height=\"563\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-165.png 383w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-165-204x300.png 204w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-165-65x96.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-165-225x331.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-165-350x514.png 350w\" sizes=\"auto, (max-width: 383px) 100vw, 383px\" \/><\/p>\n<p><strong>Summary<\/strong><\/p>\n<ul>\n<li>Explained the concept of Binary Search Trees<\/li>\n<li>Discussed Searching operation of Binary Search Trees<\/li>\n<li>Described In order Traversal of Binary Search Trees<\/li>\n<li>Explained the Successor and Predecessor operations associated with Binary Search Trees<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Binary Search Trees-I<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/1MH4fbMx7Bc\" 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<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-297 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-166.png\" alt=\"\" width=\"687\" height=\"415\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-166.png 687w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-166-300x181.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-166-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-166-225x136.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-166-350x211.png 350w\" sizes=\"auto, (max-width: 687px) 100vw, 687px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-298 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-167.png\" alt=\"\" width=\"661\" height=\"329\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-167.png 661w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-167-300x149.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-167-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-167-225x112.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-167-350x174.png 350w\" sizes=\"auto, (max-width: 661px) 100vw, 661px\" \/><\/p>\n","protected":false},"author":3,"menu_order":21,"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-278","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\/278","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":8,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/278\/revisions"}],"predecessor-version":[{"id":945,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/278\/revisions\/945"}],"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\/278\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/media?parent=278"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapter-type?post=278"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/contributor?post=278"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/license?post=278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}