Schwanzrekursion ist als rekursive Funktion definiert, bei der der rekursive Aufruf die letzte Anweisung ist, die von der Funktion ausgeführt wird. Nach dem Rekursionsaufruf muss also im Grunde nichts mehr ausgeführt werden.
Beispielsweise ist die folgende C++-Funktion print() endrekursiv.
C
// An example of tail recursive function> void> print(> int> n)> {> > if> (n <0)> > return> ;> > printf> (> '%d '> , n);> > // The last executed statement is recursive call> > print(n - 1);> }> |
>
>
C++
// An example of tail recursive function> static> void> print(> int> n)> {> > if> (n <0)> > return> ;> > cout <<> ' '> << n;> > > // The last executed statement is recursive call> > print(n - 1);> }> // This code is contributed by Aman Kumar> |
>
>
Java
// An example of tail recursive function> static> void> print(> int> n)> {> > if> (n <> 0> )> > return> ;> > System.out.print(> ' '> + n);> > // The last executed statement> > // is recursive call> > print(n -> 1> );> }> // This code is contributed by divyeh072019> |
>
>
Python3
# An example of tail recursive function> def> prints(n):> > if> (n <> 0> ):> > return> > print> (> str> (n), end> => ' '> )> > # The last executed statement is recursive call> > prints(n> -> 1> )> > # This code is contributed by Pratham76> > # improved by ashish2021> |
>
>
C#
// An example of tail recursive function> static> void> print(> int> n)> {> > if> (n <0)> > return> ;> > Console.Write(> ' '> + n);> > // The last executed statement> > // is recursive call> > print(n - 1);> }> // This code is contributed by divyeshrabadiya07> |
>
>
Javascript
> // An example of tail recursive function> function> print(n)> {> > if> (n <0)> > return> ;> > > document.write(> ' '> + n);> > > // The last executed statement> > // is recursive call> > print(n - 1);> }> // This code is contributed by Rajput-Ji> > |
>
>
Zeitkomplexität: An)
Hilfsraum: An)
Bedarf an Tail-Rekursion:
Die Tail-Rekursionsfunktionen gelten als besser als Nicht-Tail-Rekursionsfunktionen, da die Tail-Rekursion vom Compiler optimiert werden kann.
Compiler führen rekursive Prozeduren normalerweise mithilfe von a aus Stapel . Dieser Stapel besteht aus allen relevanten Informationen, einschließlich der Parameterwerte, für jeden rekursiven Aufruf. Wenn eine Prozedur aufgerufen wird, werden ihre Informationen angezeigt geschoben auf einen Stapel, und wenn die Funktion beendet wird, werden die Informationen angezeigt geknallt aus dem Stapel. Somit gilt für die nicht schwanzrekursiven Funktionen die Stapeltiefe (maximaler Stapelspeicherplatz, der zu jedem Zeitpunkt während der Kompilierung verwendet wird) ist mehr.
Die von Compilern verwendete Idee zur Optimierung endrekursiver Funktionen ist einfach: Da der rekursive Aufruf die letzte Anweisung ist, gibt es in der aktuellen Funktion nichts mehr zu tun, sodass das Speichern des Stapelrahmens der aktuellen Funktion keinen Nutzen hat (weitere Informationen finden Sie hier). Einzelheiten).
Kann eine nicht schwanzrekursive Funktion zur Optimierung als schwanzrekursiv geschrieben werden?
Betrachten Sie die folgende Funktion, um die Fakultät von n zu berechnen.
Es handelt sich um eine nicht schwanzrekursive Funktion. Obwohl es auf den ersten Blick wie ein rekursiver Schwanz aussieht. Wenn wir genauer hinschauen, können wir erkennen, dass der von fact(n-1) zurückgegebene Wert in verwendet wird Tatsache(n) . Also der Aufruf an Tatsache(n-1) ist nicht das Letzte, was getan wurde Tatsache(n) .
C++
#include> using> namespace> std;> // A NON-tail-recursive function. The function is not tail> // recursive because the value returned by fact(n-1) is used> // in fact(n) and call to fact(n-1) is not the last thing> // done by fact(n)> unsigned> int> fact(unsigned> int> n)> {> > if> (n <= 0)> > return> 1;> > return> n * fact(n - 1);> }> // Driver program to test above function> int> main()> {> > cout << fact(5);> > return> 0;> }> |
>
>
Java
class> GFG {> > // A NON-tail-recursive function.> > // The function is not tail> > // recursive because the value> > // returned by fact(n-1) is used> > // in fact(n) and call to fact(n-1)> > // is not the last thing done by> > // fact(n)> > static> int> fact(> int> n)> > {> > if> (n ==> 0> )> > return> 1> ;> > return> n * fact(n -> 1> );> > }> > // Driver program> > public> static> void> main(String[] args)> > {> > System.out.println(fact(> 5> ));> > }> }> // This code is contributed by Smitha.> |
>
>
Python3
# A NON-tail-recursive function.> # The function is not tail> # recursive because the value> # returned by fact(n-1) is used> # in fact(n) and call to fact(n-1)> # is not the last thing done by> # fact(n)> def> fact(n):> > if> (n> => => 0> ):> > return> 1> > return> n> *> fact(n> -> 1> )> # Driver program to test> # above function> if> __name__> => => '__main__'> :> > print> (fact(> 5> ))> # This code is contributed by Smitha.> |
>
>
C#
using> System;> class> GFG {> > // A NON-tail-recursive function.> > // The function is not tail> > // recursive because the value> > // returned by fact(n-1) is used> > // in fact(n) and call to fact(n-1)> > // is not the last thing done by> > // fact(n)> > static> int> fact(> int> n)> > {> > if> (n == 0)> > return> 1;> > return> n * fact(n - 1);> > }> > // Driver program to test> > // above function> > public> static> void> Main() { Console.Write(fact(5)); }> }> // This code is contributed by Smitha> |
>
>
PHP
// A NON-tail-recursive function. // The function is not tail // recursive because the value // returned by fact(n-1) is used in // fact(n) and call to fact(n-1) is // not the last thing done by fact(n) function fact( $n) { if ($n == 0) return 1; return $n * fact($n - 1); } // Driver Code echo fact(5); // This code is contributed by Ajit ?>> |
>
>
Javascript
> // A NON-tail-recursive function.> // The function is not tail> // recursive because the value> // returned by fact(n-1) is used> // in fact(n) and call to fact(n-1)> // is not the last thing done by> // fact(n)> function> fact(n)> {> > if> (n == 0)> > return> 1;> > > return> n * fact(n - 1);> }> // Driver code> document.write(fact(5));> // This code is contributed by divyeshrabadiya07> > |
>
>Ausgabe
120>
Zeitkomplexität: An)
Hilfsraum: An)
Die obige Funktion kann als schwanzrekursive Funktion geschrieben werden. Die Idee besteht darin, ein weiteres Argument zu verwenden und den Faktorwert im zweiten Argument zu akkumulieren. Wenn n 0 erreicht, wird der akkumulierte Wert zurückgegeben.
Unten ist die Implementierung mit einer tail-rekursiven Funktion.
C++
#include> using> namespace> std;> // A tail recursive function to calculate factorial> unsigned factTR(unsigned> int> n, unsigned> int> a)> {> > if> (n <= 1)> > return> a;> > return> factTR(n - 1, n * a);> }> // A wrapper over factTR> unsigned> int> fact(unsigned> int> n) {> return> factTR(n, 1); }> // Driver program to test above function> int> main()> {> > cout << fact(5);> > return> 0;> }> |
>
>
Java
// Java Code for Tail Recursion> class> GFG {> > // A tail recursive function> > // to calculate factorial> > static> int> factTR(> int> n,> int> a)> > {> > if> (n <=> 0> )> > return> a;> > return> factTR(n -> 1> , n * a);> > }> > // A wrapper over factTR> > static> int> fact(> int> n) {> return> factTR(n,> 1> ); }> > // Driver code> > static> public> void> main(String[] args)> > {> > System.out.println(fact(> 5> ));> > }> }> // This code is contributed by Smitha.> |
>
Finden Sie in String c++
>
Python3
# A tail recursive function> # to calculate factorial> def> fact(n, a> => 1> ):> > if> (n <> => 1> ):> > return> a> > return> fact(n> -> 1> , n> *> a)> # Driver program to test> # above function> print> (fact(> 5> ))> # This code is contributed> # by Smitha> # improved by Ujwal, ashish2021> |
>
>
C#
// C# Code for Tail Recursion> using> System;> class> GFG {> > // A tail recursive function> > // to calculate factorial> > static> int> factTR(> int> n,> int> a)> > {> > if> (n <= 0)> > return> a;> > return> factTR(n - 1, n * a);> > }> > // A wrapper over factTR> > static> int> fact(> int> n) {> return> factTR(n, 1); }> > // Driver code> > static> public> void> Main()> > {> > Console.WriteLine(fact(5));> > }> }> // This code is contributed by Ajit.> |
>
>
PHP
// A tail recursive function // to calculate factorial function factTR($n, $a) { if ($n <= 0) return $a; return factTR($n - 1, $n * $a); } // A wrapper over factTR function fact($n) { return factTR($n, 1); } // Driver program to test // above function echo fact(5); // This code is contributed // by Smitha ?>> |
>
>
Javascript
> // Javascript Code for Tail Recursion> // A tail recursive function> // to calculate factorial> function> factTR(n, a)> {> > if> (n <= 0)> > return> a;> > > return> factTR(n - 1, n * a);> }> > // A wrapper over factTR> function> fact(n)> {> > return> factTR(n, 1);> }> // Driver code> document.write(fact(5));> // This code is contributed by rameshtravel07> > > |
>
>Ausgabe
120>
Zeitkomplexität: An)
Hilfsraum: O(1)
Nächste Artikel zu diesem Thema:
- Eliminierung von Tail Calls
- QuickSort Tail Call-Optimierung (Reduzierung des Worst-Case-Speicherplatzes auf Log n)