shivajikobardan
Junior Member
- Joined
- Nov 1, 2021
- Messages
- 107
Yes, recursion can be risky. For example node "19" has "1" listed as a child, AND IF "1" had "19" listed as a child (an arrow pointing up in the diagram, creating a loop in the data) this would lead to an infinite loop in the code, leading to a stack overflow and then the program would crash.What strikes me as strange in this code is that the definition of this function refers to itself. Seems like risky technique.
DFS function called, with parameter (node"7") {
for each (node"19", node"21", node"14") call DFS {
DFS function called, with parameter (node"19") {
| for each (node"1", node"12", node"31") call DFS {
| DFS function called, with parameter (node"1") {
| for () - no children
| print "1"
| }
| DFS function called, with parameter (node"12") {
| for () - no children
| print "12"
| }
| DFS function called, with parameter (node"31") {
| for () - no children
| print "31"
| }
| }
| print "19"
}
DFS function called, with parameter (node"21") {
for () - no children
print "21"
}
DFS function called, with parameter (node"14") {
...
}
}
print "7"
}
Yes, recursion can be risky. For example node "19" has "1" listed as a child, AND IF "1" had "19" listed as a child (an arrow pointing up in the diagram, creating a loop in the data) this would lead to an infinite loop in the code, leading to a stack overflow and then the program would crash.
While I agree that recursion can be risky it is also the most natural way to traverse trees.Yes, recursion can be risky. For example node "19" has "1" listed as a child, AND IF "1" had "19" listed as a child (an arrow pointing up in the diagram, creating a loop in the data) this would lead to an infinite loop in the code, leading to a stack overflow and then the program would crash.
Of course you could add code that prevents infinite loops, but that would increase run timeWhile I agree that recursion can be risky it is also the most natural way to traverse trees.
Regarding your questions about the stack ("where is stack...", "no code to pop the stack"): in most languages the parameter of the function calls are pushed automatically onto the call stack, and are automatically popped when the function exits. Usually it is the same stack where the local (sometime called "automatic") variables are kept. Your pseudo-code does not contain any local variables, but the "c" parameter is pushed onto the stack when "DFS(c)" is executed.View attachment 30939
I am surprised you called that the pseudocode is correct. It looks wrong from many aspects. I have done upto step 7...As after that it is just the
repetition of same steps...(forgot spelling of repitition or repetition whatever lol so had to copy it.)
class Node:
def __init__(self, id, children):
self.id = id
self.children = children
# Depth first search code
def DFS(n):
for c in n.children:
DFS(c)
print(n.id)
# Version that shows a string that represents the call stack
def DFS_with_stack(n, stack):
stack += " -> " + str(n.id)
for c in n.children:
DFS_with_stack(c, stack)
print(stack, " Output:", n.id)
###################################
# Set up data
# leaf nodes
n1=Node(1, [])
n12=Node(12, [])
n31=Node(31, [])
n23=Node(23, [])
n6=Node(6, [])
n21=Node(21, [])
# other nodes
n19=Node(19, [n1, n12, n31])
n14=Node(14, [n23, n6])
n7=Node(7, [n19, n21, n14])
###################################
DFS(n7)
print()
DFS_with_stack(n7, "")
1
12
31
19
21
23
6
14
7
-> 7 -> 19 -> 1 Output: 1
-> 7 -> 19 -> 12 Output: 12
-> 7 -> 19 -> 31 Output: 31
-> 7 -> 19 Output: 19
-> 7 -> 21 Output: 21
-> 7 -> 14 -> 23 Output: 23
-> 7 -> 14 -> 6 Output: 6
-> 7 -> 14 Output: 14
-> 7 Output: 7