{"id":27408,"date":"2018-02-05T21:44:49","date_gmt":"2018-02-05T16:14:49","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27408"},"modified":"2018-02-05T21:44:49","modified_gmt":"2018-02-05T16:14:49","slug":"c-program-print-bst-keys-given-range","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-program-print-bst-keys-given-range\/","title":{"rendered":"C Program Print BST keys in the given range"},"content":{"rendered":"<p>Given two values k1 and k2 (where k1 &lt; k2) and a root pointer to a Binary Search Tree. Print all the keys of tree in range k1 to k2. i.e. print all x such that k1&lt;=x&lt;=k2 and x is a key of given BST. Print all the keys in increasing order.<span id=\"more-11103\"><\/span><\/p>\n<p>For example, if k1 = 10 and k2 = 22, then your function should print 12, 20 and 22.<\/p>\n<p><strong>Algorithm:<\/strong><br \/>\n1) If value of root\u2019s key is greater than k1, then recursively call in left subtree.<br \/>\n2) If value of root\u2019s key is in range, then print the root\u2019s key.<br \/>\n3) If value of root\u2019s key is smaller than k2, then recursively call in right subtree.<\/p>\n<p><strong>Implementation:<\/strong><\/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\/> <br\/>\/* A tree node structure *\/<br\/>struct node<br\/>{<br\/>  int data;<br\/>  struct node *left;<br\/>  struct node *right;<br\/>};<br\/> <br\/>\/* The functions prints all the keys which in the given range [k1..k2].<br\/>    The function assumes than k1 &lt; k2 *\/<br\/>void Print(struct node *root, int k1, int k2)<br\/>{<br\/>   \/* base case *\/<br\/>   if ( NULL == root )<br\/>      return;<br\/> <br\/>   \/* Since the desired o\/p is sorted, recurse for left subtree first<br\/>      If root-&gt;data is greater than k1, then only we can get o\/p keys<br\/>      in left subtree *\/<br\/>   if ( k1 &lt; root-&gt;data )<br\/>     Print(root-&gt;left, k1, k2);<br\/> <br\/>   \/* if root&#039;s data lies in range, then prints root&#039;s data *\/<br\/>   if ( k1 &lt;= root-&gt;data &amp;&amp; k2 &gt;= root-&gt;data )<br\/>     printf(&quot;%d &quot;, root-&gt;data );<br\/> <br\/>  \/* If root-&gt;data is smaller than k2, then only we can get o\/p keys<br\/>      in right subtree *\/<br\/>   if ( k2 &gt; root-&gt;data )<br\/>     Print(root-&gt;right, k1, k2);<br\/>}<br\/> <br\/>\/* Utility function to create a new Binary Tree node *\/<br\/>struct node* newNode(int data)<br\/>{<br\/>  struct node *temp = new struct node;<br\/>  temp-&gt;data = data;<br\/>  temp-&gt;left = NULL;<br\/>  temp-&gt;right = NULL;<br\/> <br\/>  return temp;<br\/>}<br\/> <br\/>\/* Driver function to test above functions *\/<br\/>int main()<br\/>{<br\/>  struct node *root = new struct node;<br\/>  int k1 = 10, k2 = 25;<br\/> <br\/>  \/* Constructing tree given in the above figure *\/<br\/>  root = newNode(20);<br\/>  root-&gt;left = newNode(8);<br\/>  root-&gt;right = newNode(22);<br\/>  root-&gt;left-&gt;left = newNode(4);<br\/>  root-&gt;left-&gt;right = newNode(12);<br\/> <br\/>  Print(root, k1, k2);<br\/> <br\/>  getchar();<br\/>  return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>12 20 22<\/pre>\n<p><strong>Time Complexity:<\/strong> O(n) where n is the total number of keys in tree.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Program Print BST keys in the given range &#8211; Binary Search Tree &#8211; Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree.\n<\/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":[81560,81569,81568,81558,81559,81565,81561,81563,81562,81564],"class_list":["post-27408","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-binary-search-tree","category-c-programming","category-coding","tag-binary-search-tree-print-java","tag-binary-search-tree-range-count","tag-binary-search-tree-range-query","tag-count-bst-nodes-that-lie-in-a-given-range","tag-count-bst-subtrees-that-lie-in-given-range","tag-print-a-binary-search-tree-in-sorted-order","tag-print-binary-search-tree-c","tag-remove-bst-keys-outside-the-given-range","tag-search-range-in-binary-search-tree-leetcode","tag-what-is-the-minimum-height-of-binary-tree-with-n-nodes"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27408","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=27408"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27408\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}