Dada una array que almacena un árbol de búsqueda binario completo, escriba una función que imprima eficientemente la array dada en orden ascendente.
Por ejemplo, dada una array [4, 2, 5, 1, 3], la función debe imprimir 1, 2, 3, 4, 5
Solución:
El recorrido en orden de BST lo imprime en orden ascendente. El único truco es modificar la condición de terminación de recursividad en Inorder Tree Traversal estándar .
Implementación:
C++
// 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
// 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
// JAVA Code for Sorted order printing of a // given array that represents a BST class 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
# Python3 Code for Sorted order printing of a # given array that represents a BST def 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#
// C# Code for Sorted order printing // of a given array that represents a BST using 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 Code static 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 // 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.
Javascript
<script> // Javascript Code for Sorted order printing of a // given array that represents a BST function 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 code var arr = [4, 2, 5, 1, 3]; printSorted(arr, 0, arr.length - 1); // This code is contributed by shikhasingrajput </script>
Producción:
1 2 3 4 5
Complejidad de tiempo: O(n)
Escriba comentarios si encuentra que la solución anterior es incorrecta o encuentra mejores formas de resolver el mismo problema.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA