{"id":27253,"date":"2018-01-05T22:08:37","date_gmt":"2018-01-05T16:38:37","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27253"},"modified":"2018-01-05T22:08:37","modified_gmt":"2018-01-05T16:38:37","slug":"sorted-order-printing-given-array-represents-bst","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/sorted-order-printing-given-array-represents-bst\/","title":{"rendered":"Sorted order printing of a given array that represents a BST"},"content":{"rendered":"<p>Given an array that stores a complete Binary Search Tree, write a function that efficiently prints the given array in ascending order.<span id=\"more-9514\"><\/span><\/p>\n<p>For example, given an array [4, 2, 5, 1, 3], the function should print 1, 2, 3, 4, 5<\/p>\n<p><strong>Solution:<\/strong><br \/>\nInorder traversal of BST prints it in ascending order. The only trick is to modify recursion termination condition in <a href=\"http:\/\/www.geeksforgeeks.org\/?p=618\" target=\"_blank\" rel=\"noopener\">standard Inorder Tree Traversal<\/a>.<\/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\/>void printSorted(int arr[], int start, int end)<br\/>{     <br\/>  if(start &gt; end)<br\/>    return;<br\/> <br\/>  \/\/ print left subtree<br\/>  printSorted(arr, start*2 + 1, end);<br\/> <br\/>  \/\/ print root<br\/>  printf(&quot;%d  &quot;, arr[start]);<br\/> <br\/>  \/\/ print right subtree<br\/>  printSorted(arr, start*2 + 2, end);  <br\/>}<br\/> <br\/>int main()<br\/>{<br\/>  int arr[] = {4, 2, 5, 1, 3};<br\/>  int arr_size = sizeof(arr)\/sizeof(int);<br\/>  printSorted(arr, 0, arr_size-1);<br\/>  getchar();<br\/>  return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Time Complexity:<\/strong> O(n)<\/p>\n<p>Please write comments if you find the above solution incorrect, or find better ways to solve the same problem.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Sorted order printing of a given array that represents a BST &#8211; Binary Search Tree &#8211; Given an array that stores a complete Binary Search Tree,<\/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],"tags":[80546,80539,70263,70669,70300,70284,70277,73310,73309,80557,70268,80573,80818,70285,80710,81264],"class_list":["post-27253","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-binary-search-tree","tag-application-of-binary-tree","tag-binary-search-algorithm-c","tag-binary-search-tree","tag-binary-search-tree-applications","tag-binary-search-tree-in-c","tag-binary-search-tree-in-data-structure","tag-binary-search-tree-java","tag-binary-search-tree-program-in-c","tag-binary-search-tree-traversal","tag-binary-search-tree-with-example","tag-binary-tree","tag-binary-tree-search-java","tag-binary-tree-simulator","tag-binary-tree-traversal","tag-binary-tree-traversal-program-in-c","tag-pre-order-traversal-algorithm"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27253","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=27253"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27253\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}