{"id":27309,"date":"2018-01-20T21:23:33","date_gmt":"2018-01-20T15:53:33","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27309"},"modified":"2018-01-20T21:23:33","modified_gmt":"2018-01-20T15:53:33","slug":"c-program-inorder-successor-binary-search-tree","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-program-inorder-successor-binary-search-tree\/","title":{"rendered":"C Program-Inorder Successor in Binary Search Tree"},"content":{"rendered":"<p>In Binary Tree, Inorder successor of a node is the next node in Inorder traversal of the Binary Tree. Inorder Successor is NULL for the last node in Inoorder traversal.<span id=\"more-9999\"><\/span><br \/>\nIn Binary Search Tree, Inorder Successor of an input node can also be defined as the node with the smallest key greater than the key of input node. So, it is sometimes important to find next node in sorted order.<\/p>\n<p>In the above diagram, inorder successor of 8 is 10, inorder successor of 10 is 12 and inorder successor of 14 is 20.<\/p>\n<p><strong>Recommended: Please solve it on &#8220;PRACTICE&#8221; first, before moving on to the solution.<\/strong><\/p>\n<p><strong>Method 1 (Uses Parent Pointer)<\/strong><br \/>\nIn this method, we assume that every node has parent pointer.<\/p>\n<p>The Algorithm is divided into two cases on the basis of right subtree of the input node being empty or not.<\/p>\n<p>Input: node, root \/\/ node is the node whose Inorder successor is needed.<br \/>\noutput: succ \/\/ succ is Inorder successor of node.<\/p>\n<p><strong>1)<\/strong> If right subtree of node is not NULL, then succ lies in right subtree. Do following.<br \/>\nGo to right subtree and return the node with minimum key value in right subtree.<br \/>\n<strong>2)<\/strong> If right sbtree of node is NULL, then succ is one of the ancestors. Do following.<br \/>\nTravel up using the parent pointer until you see a node which is left child of it\u2019s parent. The parent of such a node is the succ.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Implementation<\/strong><br \/>\nNote that the function to find InOrder Successor is highlighted (with gray background) in below code.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Programming<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include &lt;stdio.h&gt;<br\/>#include &lt;stdlib.h&gt;<br\/>  <br\/>\/* A binary tree node has data, pointer to left child<br\/>   and a pointer to right child *\/<br\/>struct node<br\/>{<br\/>    int data;<br\/>    struct node* left;<br\/>    struct node* right;<br\/>    struct node* parent;<br\/>};<br\/> <br\/>struct node * minValue(struct node* node); <br\/> <br\/>struct node * inOrderSuccessor(struct node *root, struct node *n)<br\/>{<br\/>  \/\/ step 1 of the above algorithm <br\/>  if( n-&gt;right != NULL )<br\/>    return minValue(n-&gt;right);<br\/> <br\/>  \/\/ step 2 of the above algorithm<br\/>  struct node *p = n-&gt;parent;<br\/>  while(p != NULL &amp;&amp; n == p-&gt;right)<br\/>  {<br\/>     n = p;<br\/>     p = p-&gt;parent;<br\/>  }<br\/>  return p;<br\/>}<br\/> <br\/>\/* Given a non-empty binary search tree, return the minimum data  <br\/>    value found in that tree. Note that the entire tree does not need<br\/>    to be searched. *\/<br\/>struct node * minValue(struct node* node) {<br\/>  struct node* current = node;<br\/>  <br\/>  \/* loop down to find the leftmost leaf *\/<br\/>  while (current-&gt;left != NULL) {<br\/>    current = current-&gt;left;<br\/>  }<br\/>  return current;<br\/>}<br\/> <br\/>\/* Helper function that allocates a new node with the given data and <br\/>    NULL left and right pointers. *\/<br\/>struct node* newNode(int data)<br\/>{<br\/>  struct node* node = (struct node*)<br\/>                       malloc(sizeof(struct node));<br\/>  node-&gt;data   = data;<br\/>  node-&gt;left   = NULL;<br\/>  node-&gt;right  = NULL;<br\/>  node-&gt;parent = NULL;<br\/>   <br\/>  return(node);<br\/>}<br\/> <br\/>\/* Give a binary search tree and a number, inserts a new node with    <br\/>    the given number in the correct place in the tree. Returns the new<br\/>    root pointer which the caller should then use (the standard trick to <br\/>    avoid using reference parameters). *\/<br\/>struct node* insert(struct node* node, int data)<br\/>{<br\/>  \/* 1. If the tree is empty, return a new,<br\/>      single node *\/<br\/>  if (node == NULL)<br\/>    return(newNode(data));<br\/>  else<br\/>  {<br\/>    struct node *temp;  <br\/> <br\/>    \/* 2. Otherwise, recur down the tree *\/<br\/>    if (data &lt;= node-&gt;data)<br\/>    {    <br\/>         temp = insert(node-&gt;left, data);<br\/>         node-&gt;left  = temp;<br\/>         temp-&gt;parent= node;<br\/>    }<br\/>    else<br\/>    {<br\/>        temp = insert(node-&gt;right, data);<br\/>        node-&gt;right = temp;<br\/>        temp-&gt;parent = node;<br\/>    }    <br\/>  <br\/>    \/* return the (unchanged) node pointer *\/<br\/>    return node;<br\/>  }<br\/>} <br\/>  <br\/>\/* Driver program to test above functions*\/<br\/>int main()<br\/>{<br\/>  struct node* root = NULL, *temp, *succ, *min;<br\/> <br\/>  \/\/creating the tree given in the above diagram<br\/>  root = insert(root, 20);<br\/>  root = insert(root, 8);<br\/>  root = insert(root, 22);<br\/>  root = insert(root, 4);<br\/>  root = insert(root, 12);<br\/>  root = insert(root, 10);  <br\/>  root = insert(root, 14);    <br\/>  temp = root-&gt;left-&gt;right-&gt;right;<br\/> <br\/>  succ =  inOrderSuccessor(root, temp);<br\/>  if(succ !=  NULL)<br\/>    printf(&quot;\\n Inorder Successor of %d is %d &quot;, temp-&gt;data, succ-&gt;data);    <br\/>  else<br\/>    printf(&quot;\\n Inorder Successor doesn&#039;t exit&quot;);<br\/> <br\/>  getchar();<br\/>  return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output of the above program:<\/strong><br \/>\n<em>Inorder Successor of 14 is 20<\/em><\/p>\n<p><strong>Time Complexity:<\/strong> O(h) where h is height of tree.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Method 2 (Search from root) <\/strong><br \/>\nParent pointer is NOT needed in this algorithm. The Algorithm is divided into two cases on the basis of right subtree of the input node being empty or not.<\/p>\n<p>Input: <em>node, root<\/em> \/\/ <em>node <\/em>is the node whose Inorder successor is needed.<br \/>\noutput: <em>succ <\/em>\/\/ <em>succ <\/em>is Inorder successor of <em>node<\/em>.<\/p>\n<p><strong>1)<\/strong> If right subtree of <em>node <\/em>is not <em>NULL<\/em>, then <em>succ <\/em>lies in right subtree. Do following.<br \/>\nGo to right subtree and return the node with minimum key value in right subtree.<br \/>\n<strong>2) <\/strong>If right sbtree of <em>node <\/em>is NULL, then start from root and us search like technique. Do following.<br \/>\nTravel down the tree, if a node\u2019s data is greater than root\u2019s data then go right side, otherwise go to left side.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Programming<\/span> <\/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 * inOrderSuccessor(struct node *root, struct node *n)<br\/>{<br\/>    \/\/ step 1 of the above algorithm<br\/>    if( n-&gt;right != NULL )<br\/>        return minValue(n-&gt;right);<br\/> <br\/>    struct node *succ = NULL;<br\/> <br\/>    \/\/ Start from root and search for successor down the tree<br\/>    while (root != NULL)<br\/>    {<br\/>        if (n-&gt;data &lt; root-&gt;data)<br\/>        {<br\/>            succ = root;<br\/>            root = root-&gt;left;<br\/>        }<br\/>        else if (n-&gt;data &gt; root-&gt;data)<br\/>            root = root-&gt;right;<br\/>        else<br\/>           break;<br\/>    }<br\/> <br\/>    return succ;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Program &#8211; In order Successor in Binary Search Tree &#8211; Binary Search Tree &#8211; In order Successor is NULL for the last node in In order traversal.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,80126,69866,1],"tags":[81419,81420,81412,81414,81413,81418,81415,80862,81417,81416],"class_list":["post-27309","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-binary-search-tree","category-c-programming","category-coding","tag-inorder-successor-in-bst","tag-inorder-successor-in-bst-java","tag-inorder-successor-in-bst-leetcode","tag-inorder-successor-without-parent-pointer","tag-inorder-traversal-in-bst","tag-postorder-successor","tag-preorder-successor","tag-successor-and-predecessor-in-binary-search-tree","tag-tree-predecessor-pseudocode","tag-what-is-successor-and-predecessor"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27309","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=27309"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27309\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}