|
22 | 22 | - initialize:
|
23 | 23 | - variables to hold the start & dest nodes to `None`
|
24 | 24 | - the stack of nodes to search to `[root]`
|
25 |
| - - while 1 or both of the start/dest node variables is `None`: |
26 |
| - - pop the top node off the stack. If it's the start or dest node, set the relevant variable to it |
27 |
| - - at this point I'll need to do something to figure out the directions to get to this node from the root... I should probably record that each iteration |
28 |
| - - actually, how could I do this... I was originally thinking I could maintain a record of `L`s and `R`s, and add to/remove from it based on whether the current node is the left or right child of its parent node... but the nodes don't store references to their parents, and since I'll be popping nodes off the stack to get the next one, the parent won't necessarily have been the previous node we checked. |
29 |
| - - I could set an attr on each node I check that references its parent, and then once I find the start/dest node, backtrack through those. Or I could do a similar thing with a dict that maps nodes to their parents. But either of those would add additional memory usage, which isn't ideal. |
30 |
| - - I was also starting to think that DFS might be better conceptualized as recursive, since when we hit the "bottom" of the tree and run out of child nodes to check, we go "back up a level" like recursion does with a call stack. And since I could recurse all the way down, say, the left branch before starting the right branch (at each given level), then I think I *could* maintain a stack of directions, and push/pop from it as I go down and back up the tree... but I think the problem with the recursive idea is I'm not sure I could easily continue looking for the start/dest node once I've found the other... though actually I guess I could either set those start & dest variables as attrs of the class instance instead of inside the function... |
31 |
| - - I'm running out of time to finish this problem today, so I'll just go with the dict solution for now and maybe try out the recursive version if I have time later. |
32 |
| - - let's say I now have a dict of {node: parent}. I think I'll write a helper function that traces back through the parents dict to create the path from the root to that node. This will probably be a while loop based on encountering the root |
33 |
| - - the recursive version is seeming more and more preferable... I want to get this problem done by EOD so I think I'm just gonna wing it rather than continue on with these thoughts |
| 25 | + - while 1 or both of the start/dest node variables is `None`: |
| 26 | + - pop the top node off the stack. If it's the start or dest node, set the relevant variable to it |
| 27 | + - at this point I'll need to do something to figure out the directions to get to this node from the root... I should probably record that each iteration |
| 28 | + - actually, how could I do this... I was originally thinking I could maintain a record of `L`s and `R`s, and add to/remove from it based on whether the current node is the left or right child of its parent node... but the nodes don't store references to their parents, and since I'll be popping nodes off the stack to get the next one, the parent won't necessarily have been the previous node we checked. |
| 29 | + - I could set an attr on each node I check that references its parent, and then once I find the start/dest node, backtrack through those. Or I could do a similar thing with a dict that maps nodes to their parents. But either of those would add additional memory usage, which isn't ideal. |
| 30 | + - I was also starting to think that DFS might be better conceptualized as recursive, since when we hit the "bottom" of the tree and run out of child nodes to check, we go "back up a level" like recursion does with a call stack. And since I could recurse all the way down, say, the left branch before starting the right branch (at each given level), then I think I *could* maintain a stack of directions, and push/pop from it as I go down and back up the tree... but I think the problem with the recursive idea is I'm not sure I could easily continue looking for the start/dest node once I've found the other... though actually I guess I could either set those start & dest variables as attrs of the class instance instead of inside the function... |
| 31 | + - I'm running out of time to finish this problem today, so I'll just go with the dict solution for now and maybe try out the recursive version if I have time later. |
| 32 | + - let's say I now have a dict of {node: parent}. I think I'll write a helper function that traces back through the parents dict to create the path from the root to that node. This will probably be a while loop based on encountering the root |
| 33 | + - the recursive version is seeming more and more preferable... I want to get this problem done by EOD so I think I'm just gonna wing it rather than continue on with these thoughts |
34 | 34 |
|
35 | 35 | ## Refining the problem, round 2 thoughts
|
36 | 36 |
|
|
0 commit comments