• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursion and lists?  RSS feed

 
peter pham
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have similar code that I stepped through in a debugger. After running the code I found that it built the desired tree, in this case a root node H with left child L and right child P. I want list of lists to contain all paths from root to leaf. I expected [H, L] and [H, P]. I discovered that statefulPath is not stateful; after a recursive stack frame pops, statefulPath still contains n! But that stack frame just popped! I expected to see statefulPath be [H] in the debugger and it was [H, L]! So I later have a list [H,L,P] which I don't want. How do I make the statefulPath list I want for my algorithm? Thanks
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got <= in that for loop?
Why are you mixing Java and pseudo‑code in the same block?
We cannot tell how you should make that list; start by telling us what the algorithm is.
 
peter pham
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Why have you got <= in that for loop?
Why are you mixing Java and pseudo‑code in the same block?
We cannot tell how you should make that list; start by telling us what the algorithm is.


because statefulPath is supposed to be exactly what the name entails, the state of path through recursion. Path is a reference, which is why I created a new list called statefulPath to simulate what i wanted. The algorithm is to store all paths from root to leaf into listOfLists. the only pseduocode I had was the condition for making a link, whick is irrelevant to the question I am asking.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!