answersLogoWhite

0


Best Answer

If there is n consecutive execution in a recursive function, then the function will return the value to itself either 1 or n-1 times.

but in normal recursion it returns n times the value to that function.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

Tail recursion is a term used in computer programming, whereby the final statement in a function is a recursive call to the same function. Function calls are expensive in terms of performance as well as memory consumption, but when the final statement is a recursive call there is an opportunity for optimisation.

Program execution normally flows from one statement to the next, but all non-trivial programs will inevitably break the flow of execution with conditional statements (if), jumps (goto), loops (for and while) and procedure calls (function calls). In order to achieve these "branches", the CPU's next instruction pointer is simply replaced with the address of the appropriate instruction and execution continues from that point on. However, function calls need to be treated differently because there has to be a clear return path back to the point of the call.

When a function call is encountered, the CPU's current state must be saved so that it can be restored when the function returns. There also has to be some means of telling the called function where to resume execution of the current function when it returns. Also, the caller needs some means of passing arguments to the called function. All of this is achieved through a special area of memory known as the procedure call stack (often shortened to the call stack, or simply the stack). Being a stack, values can be popped off in the reverse order they were pushed on, thus providing a simple means of restoring the current function's state as well as a means of passing information to and from functions (arguments and return values).

However, this functionality comes at a cost in terms of performance. Functions are a programming aid, making it easier to read and maintain source code. But in order to achieve the highest performance, the compiler uses complex algorithms to determine if a function call is actually needed or not. If the function is fairly simple (one that performs a very simple operation and returns a value), it will often inject the code directly in place of the function call, thus eliminating the function call entirely. This technique is known as inline expansion and allows programmers to make use of functions without worrying too much about the performance impact. While inline expansion helps performance by eliminating unnecessary function calls, it can also have a negative impact because it also increases code size (large code tends to run slower than short code).

Recursive functions are often difficult if not impossible to expand inline. If the function is fairly trivial, a good compiler may be able to expand a few of the recursions, but ultimately it must draw the line somewhere otherwise code growth begins to impact the performance. However, when the final statement of a function is a call to the same function (a tail recursion), there is an obvious opportunity for optimisation.

Tail recursion does not need to maintain state because as soon as the called instance of the function returns, the current instance also returns. A good compiler will optimise away the function call simply by modifying the current instance's arguments and jumping back to the beginning -- re-invoking the same instance without making an unnecessary function call.

Although many algorithms are naturally recursive (sorting algorithms in particular), those that use tail recursion can be manually modified to become iterative algorithms (loops). However, this can often result in code that is more complex to both read and maintain. But since a good compiler can do this for us, we can take advantage of the simplicity of a recursive call and let the compiler do the hard work for us by injecting the necessary code to render the algorithm iterative.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Tail recursion is a term used in computer programming, whereby the final statement in a function is a recursive call to the same function. Function calls are expensive in terms of performance as well as memory consumption, but when the final statement is a recursive call there is an opportunity for optimisation.

Program execution normally flows from one statement to the next, but all non-trivial programs will inevitably break the flow of execution with conditional statements (if), jumps (goto), loops (for and while) and procedure calls (function calls). In order to achieve these "branches", the CPU's next instruction pointer is simply replaced with the address of the appropriate instruction and execution continues from that point on. However, function calls need to be treated differently because there has to be a clear return path back to the point of the call.


When a function call is encountered, the CPU's current state must be saved so that it can be restored when the function returns. There also has to be some means of telling the called function where to resume execution of the current function when it returns. Also, the caller needs some means of passing arguments to the called function. All of this is achieved through a special area of memory known as the procedure call stack (often shortened to the call stack, or simply the stack). Being a stack, values can be popped off in the reverse order they were pushed on, thus providing a simple means of restoring the current function's state as well as a means of passing information to and from functions (arguments and return values).


However, this functionality comes at a cost in terms of performance. Functions are a programming aid, making it easier to read and maintain source code. But in order to achieve the highest performance, the compiler uses complex algorithms to determine if a function call is actually needed or not. If the function is fairly simple (one that performs a very simple operation and returns a value), it will often inject the code directly in place of the function call, thus eliminating the function call entirely. This technique is known as inline expansion and allows programmers to make use of functions without worrying too much about the performance impact. While inline expansion helps performance by eliminating unnecessary function calls, it can also have a negative impact because it also increases code size (large code tends to run slower than short code).


Recursive functions are often difficult if not impossible to expand inline. If the function is fairly trivial, a good compiler may be able to expand a few of the recursions, but ultimately it must draw the line somewhere otherwise code growth begins to impact the performance. However, when the final statement of a function is a call to the same function (a tail recursion), there is an obvious opportunity for optimisation.


Tail recursion does not need to maintain state because as soon as the called instance of the function returns, the current instance also returns. A good compiler will optimise away the function call simply by modifying the current instance's arguments and jumping back to the beginning -- re-invoking the same instance without making an unnecessary function call.


Although many algorithms are naturally recursive (sorting algorithms in particular), those that use tail recursion can be manually modified to become iterative algorithms (loops). However, this can often result in code that is more complex to both read and maintain. But since a good compiler can do this for us, we can take advantage of the simplicity of a recursive call and let the compiler do the hard work for us by injecting the necessary code to render the algorithm iterative.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is tail recursion in data structure?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the difference between left recursion and right recursion in a grammar?

Recursion is what it's called when a function calls itself. When a function calls itself immediately before returning, it's called tail recursion. Tail recursion can be more efficiently written as iteration. In fact a good compiler will recognize tail recursion and compile it as iteration. There is no such thing as left or right recursion in C programming.


What is a high speed data searching and sorting in data structure algorithm?

Binary Search is the high speed data searching.Here in each recursion the is divided in two equal halves so that execution becomes easier.


What data structures are used for implementing backtracking branch and bound?

Recursion is used for backtracking


Which data structure would you most likely see in a nonrecursive implementation of a recursive algorithm?

Some sort of structure that lets you store a list of pending jobs - for example, an array, but it would have to be a resizable array. Some type of collection might be more appropriate. As you process one level of recursion, you add the "children" (which have yet to be processed) to the collection of pending jobs; once the "parent" is done processing, you remove it from the collection.


What is advantages of recursion in a data structure?

Recursive procedures are huge memory hogs. Also, they're a nightmare to debug. Finally, it's pretty rare to find an application that actually needs recursion as opposed to a simpler, more friendly methodolgy.

Related questions

Which data structure is used for recursion?

this question depends on what the recursion is being used for..... sumit kumar srivastava 9455587002


What is the difference between left recursion and right recursion in a grammar?

Recursion is what it's called when a function calls itself. When a function calls itself immediately before returning, it's called tail recursion. Tail recursion can be more efficiently written as iteration. In fact a good compiler will recognize tail recursion and compile it as iteration. There is no such thing as left or right recursion in C programming.


What is a high speed data searching and sorting in data structure algorithm?

Binary Search is the high speed data searching.Here in each recursion the is divided in two equal halves so that execution becomes easier.


Can heap implement recursion?

Heap is a data-structure, it cannot implement anything. On the other hand, it is true that: 1. Recursive routines might use heap. 2. You can use dynamic memory allocation (heap), to implement a stack; and use the stack to implement recursion.


How many types of recursion are there in c language?

Recursion in c language is a method where the function calls itself, within or outside the scope. Using Recursion, complicated problems can be divided into smaller parts so that solving them becomes more manageable. The recursion technique is available in Java, JavaScript, and C++.serves the same purpose. The type of Recursion in C • Direct Recursion • Indirect Recursion. Direct Recursion Recursion can call the function n-number of times. In the case of direct Recursion, the function calls itself inside the same position or in the local scope Direct Recursion problems are the Fibonacci series, a program to print 50 natural numbers. Indirect Recursion In the case of Indirect Recursion, a function X calls function Y, and function Y calls any function Z. Under certain conditions, function Z calls function A. In this case, function A is indirectly related to function Z. Indirect Recursion is also known as mutual Recursion, as more than one function runs a program. It is a two-step recursive function call process for making a recursive function call. Below mentioned are also type of Recursion: Tail Recursion No Tail/Head Recursion Linear Recursion Tree Recursion Tail Recursion A function is said to be tail recursion if it calls itself and also calls the last or the previous statement executed in the process. Head Recursion A function is said to be Head Recursion if it calls itself and also calls the first or the beginning statement executed in the process. Linear Recursion A function is said to be a linear recursive function if it makes a single call to itself each time the procedure executes itself and grows linearly depending on the size of the problem. Tree Recursion Tree Recursion is different from linear Recursion. Rather than making only one call to itself, that function makes more than one recursive call to the process within the recursive function. Following are the steps to solve the recursive problem in C: Step 1: Create a function and assign the work a part should do. Step 2: Select the subproblem and assume that the function already works on the problem. Step 3: Get the answer to the subproblem and use it to resolve the main issue. Step 4: The 90% of the problem defined is solved.


What data structures are used for implementing backtracking branch and bound?

Recursion is used for backtracking


Which data structure would you most likely see in a nonrecursive implementation of a recursive algorithm?

Some sort of structure that lets you store a list of pending jobs - for example, an array, but it would have to be a resizable array. Some type of collection might be more appropriate. As you process one level of recursion, you add the "children" (which have yet to be processed) to the collection of pending jobs; once the "parent" is done processing, you remove it from the collection.


What is advantages of recursion in a data structure?

Recursive procedures are huge memory hogs. Also, they're a nightmare to debug. Finally, it's pretty rare to find an application that actually needs recursion as opposed to a simpler, more friendly methodolgy.


What are the subject-matters of data structure?

types of data structure types of data structure


How do you amend a data structure?

How do you amend a data structure?


What do you mean by base case recursive case binding time runtime stack and tail recursion?

These terms are found in Recursion.1.Base Case:it is the case in recursion where the answer is known,or we can say the termination condition for a recursion to unwind back.For example to find Factorial of num using recursion: int Fact(int num){ if(num==1 num==0)//base casereturn 1;else // recursive case: return num*Fact(num-1);} 2.Recursive case:It is the case whcih brings us to the closer answer. Run Time Stack:It is a system stack us to save the frame stack of a function every recursion or every call.This frame stack consists of the return address,local variables and return value if any. Tail Recursion:The case where the function consist of single recursive call and it is the last statement to be executed.A tail Recursion can be replace by iteration. The above function consists of tail recursion case.where as the below function does not. void binary(int start,int end,int el){int mid;if(end>start){mid=(start+end)/2;if(el==ar[mid])return mid;else{if(el>ar[mid])binary(mid+1,end,ele);elsebinary(start,mid-11,ele);


What is the recursive solution in data structure?

You cannot have recursion within a data structure: struct foo { int x; foo y; // compiler error }; This has to fail; there is no end point to the recursion. If a foo is member of a foo, then the member foo also requires a member foo, and so on to infinity... If a structure needs to refer to another instance of itself, we can use a member pointer: struct foo { int x; foo* y; // ok }; A pointer works because all pointers are the same length regardless of the pointer's type (the type being referred to). Using member pointers like this is fundamental to many data structures, such as linked lists: struct node { int data; node* prev; // previous node in the sequence (may be NULL) node* next; // next node in the sequence (may be NULL) };