String reversal using recursion

  • Thread starter Thread starter gfd43tg
  • Start date Start date
  • Tags Tags
    Recursion String
Click For Summary

Discussion Overview

The discussion revolves around the implementation of a recursive function to reverse a one-dimensional string array. Participants explore the structure of the recursion tree, the behavior of the recursive calls, and the efficiency of different approaches to string reversal.

Discussion Character

  • Homework-related
  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their implementation of a recursive function and expresses difficulty in visualizing the recursion tree.
  • Another participant explains that each level of recursion corresponds to an element in the string, leading to a null string at the deepest level before returning characters in reverse order.
  • A participant questions how to correctly represent the tree of recursive calls, comparing it to a previously understood Fibonacci sequence structure.
  • One participant suggests an alternative method for reversing a string that involves splitting the string into halves, which may be more efficient in terms of stack space.
  • There is a discussion about the representation of nodes in the recursion tree and whether it should reflect the calls or the returns, with some participants noting that the stack does not form a traditional tree structure.
  • Another participant emphasizes the importance of understanding the call stack rather than visualizing it as a tree, noting that recursion involves nesting local variables on the stack.

Areas of Agreement / Disagreement

Participants express differing views on how to visualize the recursion tree and whether it should represent calls or returns. There is no consensus on the best way to depict the recursive structure or the efficiency of the proposed methods.

Contextual Notes

Some participants mention limitations in their understanding of recursion trees and the specifics of how the call stack operates. There is also a suggestion that the question may not have been fully covered in prior discussions.

gfd43tg
Gold Member
Messages
949
Reaction score
48

Homework Statement


Write a function called reverse that uses recursion to reverse any one-dimensional string array.

Homework Equations


The Attempt at a Solution


Code:
function out = reverse(str)
if length(str) == 0
    out = str;
else
    out = [str(end) reverse(str(1:end-1))];
end

I am having difficulty understanding how this recursion tree will look. I am using a break point to understand the behavior, and it seems like every iteration it is taking off the last letter of the string until the string is empty, then it is filling it back in reverse order. But I can't put that into the form of a tree.
ImageUploadedByPhysics Forums1407566948.526315.jpg
 
Physics news on Phys.org
There's a level of recursion for every element in the string (which consumes a lot of stack space). At the final depth of recursion, the function returns a null string. The next level up, returns the 1st element, the next level up returns the 2nd element followed by the 1st element, then 3rd element followed by (2nd, 1st), ...
 
How should the tree of recursive calls look? Does my root node have correct children? I'm trying to do this based off a fibonacci sequence that I wrote recursively where each node had two children with fibnum(n-1) and fibnum(n-2), but that was easier to see on a tree. Now instead of a sum I am appending the end to the recursive function, so I am confused about how the tree should appear.
 
It doesn't look right to me. Anyway, there is a trick you don't know about. How would you solve this problem: "Find a recursive procedure to print a string one character at a time." Try to solve that and then find an extremely simple way to modify it to print the string in reverse.
 
The tree would look something like this:

Code:
//
     node
  end-0  node
      end-1  node
         end-2  node
           ...
                 2  node
                   1  node
                     0  null

Side note, recursively splitting the string into halves would be better (less stack space):
Code:
std::string rvrs(std::string str)
{
size_t m, n;
    if(str.size() < 2)
        return(str);
    m = str.size()/2;
    n = str.size()-m;
    std::string left  = rvrs(str.substr(0, m));
    std::string right = rvrs(str.substr(m, n));
    return(right + left);
}
 
What you are labeling as node should be reverse(str(1:end-1)), or do the indices change there as well? Like the 2nd layer should be

Code:
 reverse(str)
         str(end) reverse(str(1:end-1))
               str(end-1) reverse(str(1:end-2))
                     ...
                                        str(1)    reverse(str(1:1))
 
Maylis said:
What you are labeling as node should be reverse(str(1:end-1)), or do the indices change there as well?
Is the goal here to show what the tree looks like during calls or during returns? What I posted is a tree of returned values. The stack isn't really a tree, it's just a set of pointers to concatenated strings, so there's no "left" or "right" on the stack, but only in the concatenation operation performed within the recursive function.

The example code I posted does end up with left and right pointers on the stack, but no nodes, and it's a depth first, left first sequence, so again, it's not really a tree structure.
 
I was under the impression that the tree looks identical to the calls as the returns, with the exception of how the tree is traversed. Calls are pre-order and returns are post-order, no??

In general I don't understand how to do the trees. My other thread I believe the tree is correct, with the likely exception of the route of traversal. Technically this question doesn't ask anything as far as how many recursive calls are made, I was just wondering out of my own understanding. Perhaps there is a reason that it wasn't asked, it might be something we didn't cover.
 
In my opinion, the goal here should be looking at what is on the "call stack". Recursion nests local variables onto the stack, which normally is not considered to a tree structure (as far as I know). A call stack, may optionally include a linked list of "frame pointers", but that's optional (many compilers allow frame pointers to be omitted).

http://en.wikipedia.org/wiki/Call_stack
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
55
Views
7K
  • · Replies 6 ·
Replies
6
Views
20K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K