0

I would like to draw a flow chart for a recursive call function whose return value is used, the real function is much more complex, but very similar on the basic idea as following:

List getDigit(Integer arg) {
   List<Integer> result = new ArrayList<Integer>();
   result.add(arg);
   if (arg != 0) {
      result.addAll(this.getDigit(arg/10));
   }
   return result;
} 

1 Answer 1

3

It really depends on what level of detail you are looking to diagram.

If the detail is very coarse, you might not need to represent the recursion at all. You'd just represent it as a calculation step, with the overall inputs and outputs shown.

At the next finer level of detail, you could perhaps represent it as simple iteration - what I mean is not an exact representation of the algorithm, but something that gives the gist of what it does. You might also find that your use of recursion can be converted to simple iteration anyway - in other words, the algorithm itself can be aligned with an easier diagrammatic representation.

If absolute detail of the recursive nature of the algorithm is required, you'd have to model the existence of a stack, and the iterative fetching and storing of intermediary data in this stack. In other words, making explicit everything that is otherwise implicit in a language that allows recursive calls.

3
  • I agree with that 3 tips, and in order to give absolute detail for an hierachical data structure and algorithm (a special DAG), I am struggling with how to express result.addAll(this.getDigit()) in flow chart. I found an example, where the recursive bottom 3 layers are drawn with assumed function arguments. This is not available for hierachical data, because they are nodes in a special DAG. Commented May 5, 2023 at 12:17
  • @Tiina, you'd firstly have to perform the necessary calculations of each step, then you'd have to represent a call in terms of pushing the existing local variables to a stack for storage, then re-setting up the local variables for the next calculation, then iterating the calculation again (but using the new settings of the variables). After the recursion bottoms out, you'd have to represent the unwind by another loop which proceeds to fetch from the stack and accumulate the results. (1/2) Commented May 5, 2023 at 15:06
  • Obviously this assumes calls go straight to the bottom and then back to the top, one swoop in each direction. In more complicated algorithms, you might have to bounce back and forth between these two iterators. The detail of course depends on the specific algorithm. (2/2) Commented May 5, 2023 at 15:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.