2

Is it possible to create a variable that is unique to that level of recursion so that whatever happens to it will remain that way, regardless of every other recursive step? And can this be done in a way that I can retrieve the value in the variable in each recursive step after the recursion is finished?

Let me use an example to visualize the problem. Let's say I've got a tree. I want to record the depth levels of every node, and organize it so that all the nodes at each depth level are bundled in the same array or list. So I'll need a different array/list for each depth level.

I'm traversing the tree recursively. At the beginning of each recursion, I'm creating a new list. It's the same line, so every recursion creates a list variable with the same name. I am processing a node in depth X. I add this node to the list, then proceed (recursively) to a child node at depth X + 1. I add this child node to a new list. So on and so forth until all the nodes in the tree belong to some list.

After the recursion is finished, I expect to have a number of lists equal to the height of the tree (1 list per depth, containing all the nodes in that depth), and a way to access all the nodes in all the lists.

Is this possible?

Manuel
  • 2,143
  • 5
  • 20
  • 22
  • what about `timestamp` – Rustam May 08 '15 at 08:14
  • A local variable in a recursive method would be unique to that particular invocation. – akhil_mittal May 08 '15 at 08:14
  • akhil, I fear that in your case, the variable in the previous recursion would be overwritten or lost in some way. Am I right in thinking this? – Manuel May 08 '15 at 08:18
  • You just need to declare a list of list before calling the recursive method and pass the list along to the recursive method as a parameter. After returning from the method call, the list should still have all the information you nee. – Tony Vu May 08 '15 at 08:20
  • Rather than trying and playing with such stuff, you had better try and design a relevant data structure, don't you think? – fge May 08 '15 at 08:24

7 Answers7

1

Store the values in a List, appending each new value to the list. The index of the value in the list will be the recursion depth at which the value was added.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
1

You probably need a Map<Integer, List<?>> where the key is the depth, and the value is the list to which items are placed for that depth. Map is the most general object here because it can be used in a depth-first or breath-first search.

This Map needs to be either part of the recursive method signature, or a instance variable accessible from the recursive method.

Brett Walker
  • 3,566
  • 1
  • 18
  • 36
1

You don't know how many levels you have so you cannot make the lists before that. But you can use a Map. For example Map<Integer,List<Node>>. Pass it to the recurrsive function and use the current level as key.

In the end you will have map like:

1: list of nodes

2: list of nodes

....

Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
1

You can maintain a local variable inside method which will remain the same !

Consider basic example of Factorial With Recursion

public int calculateFactorial(int number){ //User will call this method
    return factPreservingOriginalValue(number,number);
         |
         |
         -----> Internally you call a method to maintain original value
}

Another Method

public int factPreservingOriginalValue(int orig,int number){ // orig will remain same
          if(number==0)
               return 1;
          return number*factWithTempVariable(orig,number-1);
}

You can use this orig value to know the depth at any point of time while traversing the tree

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
0

In a recursion you don't know your "level of recursion" but you could easily have a map Map<int, List<...> that is made available e.g. as an argument to your recursion, where you can store a list of the nodes for each level.

Jan
  • 2,060
  • 2
  • 29
  • 34
0

You need to create an array of collections of nodes, then, when adding current n-depth node to the structure, just add it to the n-th collection. Here you can find more info on how to create an array of lists.

Community
  • 1
  • 1
CptBartender
  • 1,235
  • 8
  • 22
0

You can use POJO:

  • Create new java bean Object at the start of recursion
  • Store your unique recursion value
  • Add that Object to List at the end of recursion
  • Process List as needed after the recursion is finished
Rajesh
  • 2,135
  • 1
  • 12
  • 14