{"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[pastacode lang=\u201dc\u201d manual=\u201dstruct%20Node%0A%7B%0A%20%20%20%20int%20key%3B%0A%20%20%20%20struct%20Node%20*left%2C%20*right%20%3B%0A%7D%3B\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201dbanner\u201d]\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=\u201dbanner\u201d]\n<p>Following is C++ implementation of the above algorithm:<\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20C%2B%2B%20program%20to%20find%20predecessor%20and%20successor%20in%20a%20BST%0A%23include%20%3Ciostream%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20BST%20Node%0Astruct%20Node%0A%7B%0A%20%20%20%20int%20key%3B%0A%20%20%20%20struct%20Node%20*left%2C%20*right%3B%0A%7D%3B%0A%20%0A%2F%2F%20This%20function%20finds%20predecessor%20and%20successor%20of%20key%20in%20BST.%0A%2F%2F%20It%20sets%20pre%20and%20suc%20as%20predecessor%20and%20successor%20respectively%0Avoid%20findPreSuc(Node*%20root%2C%20Node*%26%20pre%2C%20Node*%26%20suc%2C%20int%20key)%0A%7B%0A%20%20%20%20%2F%2F%20Base%20case%0A%20%20%20%20if%20(root%20%3D%3D%20NULL)%20%20return%20%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20key%20is%20present%20at%20root%0A%20%20%20%20if%20(root-%3Ekey%20%3D%3D%20key)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20the%20maximum%20value%20in%20left%20subtree%20is%20predecessor%0A%20%20%20%20%20%20%20%20if%20(root-%3Eleft%20!%3D%20NULL)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Node*%20tmp%20%3D%20root-%3Eleft%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20while%20(tmp-%3Eright)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20tmp%20%3D%20tmp-%3Eright%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20pre%20%3D%20tmp%20%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20the%20minimum%20value%20in%20right%20subtree%20is%20successor%0A%20%20%20%20%20%20%20%20if%20(root-%3Eright%20!%3D%20NULL)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Node*%20tmp%20%3D%20root-%3Eright%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20while%20(tmp-%3Eleft)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20tmp%20%3D%20tmp-%3Eleft%20%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20suc%20%3D%20tmp%20%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20return%20%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20If%20key%20is%20smaller%20than%20root\u2019s%20key%2C%20go%20to%20left%20subtree%0A%20%20%20%20if%20(root-%3Ekey%20%3E%20key)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20suc%20%3D%20root%20%3B%0A%20%20%20%20%20%20%20%20findPreSuc(root-%3Eleft%2C%20pre%2C%20suc%2C%20key)%20%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%20%2F%2F%20go%20to%20right%20subtree%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20pre%20%3D%20root%20%3B%0A%20%20%20%20%20%20%20%20findPreSuc(root-%3Eright%2C%20pre%2C%20suc%2C%20key)%20%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20create%20a%20new%20BST%20node%0ANode%20*newNode(int%20item)%0A%7B%0A%20%20%20%20Node%20*temp%20%3D%20%20new%20Node%3B%0A%20%20%20%20temp-%3Ekey%20%3D%20item%3B%0A%20%20%20%20temp-%3Eleft%20%3D%20temp-%3Eright%20%3D%20NULL%3B%0A%20%20%20%20return%20temp%3B%0A%7D%0A%20%0A%2F*%20A%20utility%20function%20to%20insert%20a%20new%20node%20with%20given%20key%20in%20BST%20*%2F%0ANode*%20insert(Node*%20node%2C%20int%20key)%0A%7B%0A%20%20%20%20if%20(node%20%3D%3D%20NULL)%20return%20newNode(key)%3B%0A%20%20%20%20if%20(key%20%3C%20node-%3Ekey)%0A%20%20%20%20%20%20%20%20node-%3Eleft%20%20%3D%20insert(node-%3Eleft%2C%20key)%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20node-%3Eright%20%3D%20insert(node-%3Eright%2C%20key)%3B%0A%20%20%20%20return%20node%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20key%20%3D%2065%3B%20%20%20%20%2F%2FKey%20to%20be%20searched%20in%20BST%0A%20%0A%20%20%20%2F*%20Let%20us%20create%20following%20BST%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%2050%0A%20%20%20%20%20%20%20%20%20%20%20%2F%20%20%20%20%20%5C%0A%20%20%20%20%20%20%20%20%20%2030%20%20%20%20%20%2070%0A%20%20%20%20%20%20%20%20%20%2F%20%20%5C%20%20%20%20%2F%20%20%5C%0A%20%20%20%20%20%20%2020%20%20%2040%20%2060%20%20%2080%20*%2F%0A%20%20%20%20Node%20*root%20%3D%20NULL%3B%0A%20%20%20%20root%20%3D%20insert(root%2C%2050)%3B%0A%20%20%20%20insert(root%2C%2030)%3B%0A%20%20%20%20insert(root%2C%2020)%3B%0A%20%20%20%20insert(root%2C%2040)%3B%0A%20%20%20%20insert(root%2C%2070)%3B%0A%20%20%20%20insert(root%2C%2060)%3B%0A%20%20%20%20insert(root%2C%2080)%3B%0A%20%0A%20%0A%20%20%20%20Node*%20pre%20%3D%20NULL%2C%20*suc%20%3D%20NULL%3B%0A%20%0A%20%20%20%20findPreSuc(root%2C%20pre%2C%20suc%2C%20key)%3B%0A%20%20%20%20if%20(pre%20!%3D%20NULL)%0A%20%20%20%20%20%20cout%20%3C%3C%20%22Predecessor%20is%20%22%20%3C%3C%20pre-%3Ekey%20%3C%3C%20endl%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20cout%20%3C%3C%20%22No%20Predecessor%22%3B%0A%20%0A%20%20%20%20if%20(suc%20!%3D%20NULL)%0A%20%20%20%20%20%20cout%20%3C%3C%20%22Successor%20is%20%22%20%3C%3C%20suc-%3Ekey%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20cout%20%3C%3C%20%22No%20Successor%22%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC++ Programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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}]}}