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?

5
  • what about timestamp Commented May 8, 2015 at 8:14
  • A local variable in a recursive method would be unique to that particular invocation. Commented May 8, 2015 at 8: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? Commented May 8, 2015 at 8: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. Commented May 8, 2015 at 8:20
  • Rather than trying and playing with such stuff, you had better try and design a relevant data structure, don't you think? Commented May 8, 2015 at 8:24

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

....

Comments

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

Comments

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.

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.