Sorted order printing of a given array that represents a BST

16 Jun, 2022
Given an array that stores a complete Binary Search Tree, write a function that efficiently prints the given array in ascending order. For example, given an array [4, 2, 5, 1, 3], the function should print 1, 2, 3, 4, 5 
Solution:
Inorder traversal of BST prints it in ascending order. The only trick is to modify recursion termination condition in standard Inorder Tree Traversal.
Implementation: 
C++
C
Java
Python3
C#
PHP
Javascript
// C++ Code for Sorted order printing of a// given array that represents a BST#include<bits/stdc++.h>using namespace std; void printSorted(int arr[], int start, int end){        if(start > end)        return;         // print left subtree    printSorted(arr, start*2 + 1, end);         // print root    cout << arr[start] << " ";         // print right subtree    printSorted(arr, start*2 + 2, end);} int main(){    int arr[] = {4, 2, 5, 1, 3};    int arr_size = sizeof(arr)/sizeof(int);    printSorted(arr, 0, arr_size-1);    getchar();    return 0;} // This code is contributed by Akanksha Rai
// C Code for Sorted order printing of a// given array that represents a BST#include<stdio.h> void printSorted(int arr[], int start, int end){      if(start > end)    return;   // print left subtree  printSorted(arr, start*2 + 1, end);   // print root  printf("%d  ", arr[start]);   // print right subtree  printSorted(arr, start*2 + 2, end); } int main(){  int arr[] = {4, 2, 5, 1, 3};  int arr_size = sizeof(arr)/sizeof(int);  printSorted(arr, 0, arr_size-1);  getchar();  return 0;}
// JAVA Code for Sorted order printing of a// given array that represents a BSTclass GFG{     private static void printSorted(int[] arr, int start,                                        int end) {        if(start > end)            return;                     // print left subtree        printSorted(arr, start*2 + 1, end);                     // print root        System.out.print(arr[start] + " ");                     // print right subtree        printSorted(arr, start*2 + 2, end);        }             // driver program to test above function    public static void main(String[] args) {        int arr[] = {4, 2, 5, 1, 3};                     printSorted(arr, 0, arr.length-1);    }}     // This code is contributed by Arnav Kr. Mandal.
# Python3 Code for Sorted order printing of a# given array that represents a BSTdef printSorted(arr, start, end):    if start > end:        return         # print left subtree    printSorted(arr, start * 2 + 1, end)         # print root    print(arr[start], end = " ")         # print right subtree    printSorted(arr, start * 2 + 2, end) # Driver Code   if __name__ == '__main__':    arr = [4, 2, 5, 1, 3]    arr_size = len(arr)    printSorted(arr, 0, arr_size - 1) # This code is contributed by PranchalK
// C# Code for Sorted order printing// of a given array that represents a BSTusing System; class GFG{static private void printSorted(int []arr,                                int start,                                int end){    if(start > end)        return;             // print left subtree    printSorted(arr, start * 2 + 1, end);             // print root    Console.Write(arr[start] + " ");             // print right subtree    printSorted(arr, start * 2 + 2, end);    }     // Driver Codestatic public void Main(String []args){    int []arr= {4, 2, 5, 1, 3};             printSorted(arr, 0, arr.Length - 1);}}     // This code is contributed by Arnab Kundu
<?php// PHP Code for Sorted order printing of a// given array that represents a BST function printSorted($arr, $start, $end){    if($start > $end)        return;             // print left subtree    printSorted($arr, $start * 2 + 1, $end);             // print root    echo($arr[$start] . " ");             // print right subtree    printSorted($arr, $start * 2 + 2, $end);}     // Driver Code$arr = array(4, 2, 5, 1, 3);     printSorted($arr, 0, sizeof($arr) - 1); // This code is contributed by Code_Mech.
<script> // Javascript Code for Sorted order printing of a// given array that represents a BSTfunction printSorted(arr, start, end){    if (start > end)        return;             // Print var left subtree    printSorted(arr, start * 2 + 1, end);             // Print var root    document.write(arr[start] + " ");             // Print var right subtree    printSorted(arr, start * 2 + 2, end);}     // Driver codevar arr = [4, 2, 5, 1, 3];     printSorted(arr, 0, arr.length - 1); // This code is contributed by shikhasingrajput </script>
Output: 
1 2 3 4 5 
Time Complexity: O(n)
Sorted order printing of a given array that represents a BST | GeeksforGeeks - YouTubeGeeksforGeeks531K subscribersSorted order printing of a given array that represents a BST | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 4:19•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=Z_5OpW49Rik" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Please write comments if you find the above solution incorrect, or find better ways to solve the same problem.
andrew1234
PranchalKatiyar
Code_Mech
Akanksha_Rai
shikhasingrajput
hardikkoriintern
Binary Search Tree
Binary Search Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.