{"id":27092,"date":"2018-01-03T21:26:56","date_gmt":"2018-01-03T15:56:56","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27092"},"modified":"2018-01-03T21:26:56","modified_gmt":"2018-01-03T15:56:56","slug":"c-programming-inorder-predecessor-successor-given-key-bst","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-inorder-predecessor-successor-given-key-bst\/","title":{"rendered":"C++ Programming Inorder predecessor and successor for a given key in BST"},"content":{"rendered":"<p>I recently encountered with a question in an interview at e-commerce company. The interviewer asked the following question:<\/p>\n<p>There is BST given with root node with key part as integer only. The structure of each node is as follows:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">struct Node<br\/>{<br\/>    int key;<br\/>    struct Node *left, *right ;<br\/>};<\/code><\/pre> <\/div>\n<p>You need to find the inorder successor and predecessor of a given key. In case the given key is not found in BST, then return the two values within which this key will lie.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Following is the algorithm to reach the desired result. Its a recursive method:<\/p>\n<pre>Input: root node, key\r\noutput: predecessor node, successor node\r\n\r\n1. If root is NULL\r\n      then return\r\n2. if key is found then\r\n    a. If its left subtree is not null\r\n        Then predecessor will be the right most \r\n        child of left subtree or left child itself.\r\n    b. If its right subtree is not null\r\n        The successor will be the left most child \r\n        of right subtree or right child itself.\r\n    return\r\n3. If key is smaller then root node\r\n        set the successor as root\r\n        search recursively into left subtree\r\n    else\r\n        set the predecessor as root\r\n        search recursively into right subtree\r\n<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p>Following is C++ implementation of the above algorithm:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++ Programming<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ C++ program to find predecessor and successor in a BST<br\/>#include &lt;iostream&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ BST Node<br\/>struct Node<br\/>{<br\/>    int key;<br\/>    struct Node *left, *right;<br\/>};<br\/> <br\/>\/\/ This function finds predecessor and successor of key in BST.<br\/>\/\/ It sets pre and suc as predecessor and successor respectively<br\/>void findPreSuc(Node* root, Node*&amp; pre, Node*&amp; suc, int key)<br\/>{<br\/>    \/\/ Base case<br\/>    if (root == NULL)  return ;<br\/> <br\/>    \/\/ If key is present at root<br\/>    if (root-&gt;key == key)<br\/>    {<br\/>        \/\/ the maximum value in left subtree is predecessor<br\/>        if (root-&gt;left != NULL)<br\/>        {<br\/>            Node* tmp = root-&gt;left;<br\/>            while (tmp-&gt;right)<br\/>                tmp = tmp-&gt;right;<br\/>            pre = tmp ;<br\/>        }<br\/> <br\/>        \/\/ the minimum value in right subtree is successor<br\/>        if (root-&gt;right != NULL)<br\/>        {<br\/>            Node* tmp = root-&gt;right ;<br\/>            while (tmp-&gt;left)<br\/>                tmp = tmp-&gt;left ;<br\/>            suc = tmp ;<br\/>        }<br\/>        return ;<br\/>    }<br\/> <br\/>    \/\/ If key is smaller than root&#039;s key, go to left subtree<br\/>    if (root-&gt;key &gt; key)<br\/>    {<br\/>        suc = root ;<br\/>        findPreSuc(root-&gt;left, pre, suc, key) ;<br\/>    }<br\/>    else \/\/ go to right subtree<br\/>    {<br\/>        pre = root ;<br\/>        findPreSuc(root-&gt;right, pre, suc, key) ;<br\/>    }<br\/>}<br\/> <br\/>\/\/ A utility function to create a new BST node<br\/>Node *newNode(int item)<br\/>{<br\/>    Node *temp =  new Node;<br\/>    temp-&gt;key = item;<br\/>    temp-&gt;left = temp-&gt;right = NULL;<br\/>    return temp;<br\/>}<br\/> <br\/>\/* A utility function to insert a new node with given key in BST *\/<br\/>Node* insert(Node* node, int key)<br\/>{<br\/>    if (node == NULL) return newNode(key);<br\/>    if (key &lt; node-&gt;key)<br\/>        node-&gt;left  = insert(node-&gt;left, key);<br\/>    else<br\/>        node-&gt;right = insert(node-&gt;right, key);<br\/>    return node;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    int key = 65;    \/\/Key to be searched in BST<br\/> <br\/>   \/* Let us create following BST<br\/>              50<br\/>           \/     \\<br\/>          30      70<br\/>         \/  \\    \/  \\<br\/>       20   40  60   80 *\/<br\/>    Node *root = NULL;<br\/>    root = insert(root, 50);<br\/>    insert(root, 30);<br\/>    insert(root, 20);<br\/>    insert(root, 40);<br\/>    insert(root, 70);<br\/>    insert(root, 60);<br\/>    insert(root, 80);<br\/> <br\/> <br\/>    Node* pre = NULL, *suc = NULL;<br\/> <br\/>    findPreSuc(root, pre, suc, key);<br\/>    if (pre != NULL)<br\/>      cout &lt;&lt; &quot;Predecessor is &quot; &lt;&lt; pre-&gt;key &lt;&lt; endl;<br\/>    else<br\/>      cout &lt;&lt; &quot;No Predecessor&quot;;<br\/> <br\/>    if (suc != NULL)<br\/>      cout &lt;&lt; &quot;Successor is &quot; &lt;&lt; suc-&gt;key;<br\/>    else<br\/>      cout &lt;&lt; &quot;No Successor&quot;;<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Predecessor is 60\r\nSuccessor is 70<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming Inorder predecessor and successor for a given key in BST &#8211; I recently encountered with a question in an interview at e-commerce company. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80126,83515,1],"tags":[70715,80871,80872,80875,80858,80861,80797,80876,80864,80868,80873,80859,80860,80867,80862,80877],"class_list":["post-27092","post","type-post","status-publish","format-standard","hentry","category-binary-search-tree","category-c-programming-3","category-coding","tag-complexity-of-binary-search-tree","tag-define-predecessor","tag-delete-a-node-in-binary-search-tree","tag-difference-between-binary-tree-and-binary-search-tree","tag-inorder-predecessor","tag-inorder-traversal-binary-tree","tag-inorder-traversal-example","tag-inorder-traversal-of-bst","tag-insertion-in-binary-search-tree","tag-insertion-in-binary-tree","tag-meaning-of-predecessor","tag-pre-order-binary-tree","tag-predecessor-and-successor","tag-successor-and-predecessor","tag-successor-and-predecessor-in-binary-search-tree","tag-what-is-predecessor"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27092"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27092\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}