You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At least to me, this makes a lot of sense. We fight recursion with recursion! First, we first output the node we are on and then we call `DFS_recursive(...)` on each of its children nodes. This method of tree traversal does what its name implies: it goes to the depths of the tree first before going through the rest of the branches. In this case, the ordering looks like:
@@ -56,7 +60,7 @@ At least to me, this makes a lot of sense. We fight recursion with recursion! Fi
Note that the in the code above, we are missing a crucial step: *checking to see if the node we are using actually exists!* Because we are using a vector to store all the nodes, we will be careful not to run into a case where we call `DFS_recursive(...)` on a node that has yet to be initialized; however, depending on the language we are using, we might need to be careful of this to avoid recursion errors!
63
+
Note that the in the code above, we are missing a crucial step: *checking to see if the node we are using actually exists!* Because we are using a vector to store all the nodes, we will be careful not to run into a case where we call `DFS_recursive(...)` on a node that has yet to be initialized; however, depending on the language we are using, we might need to be careful of this to avoid recursion errors!
60
64
61
65
Now, in this case the first element searched through is still the root of the tree. This type of tree traversal is known as *pre-order* DFS. We perform an action (output the ID) *before* searching through the children. If we shift the function around and place the data output at the end of the function, we can modify the order in which we search through the tree to be *post-order* and look something like this:
62
66
@@ -86,6 +90,8 @@ This has not been implemented in your chosen language, so here is the Julia code
86
90
{% sample lang="rs"%}
87
91
This has not been implemented in your chosen language, so here is the Julia code
@@ -128,7 +136,7 @@ This has not been implemented in your chosen language, so here is the Julia code
128
136
129
137
The order here seems to be some mix of the other 2 methods and works through the binary tree from left to right.
130
138
131
-
Now, at this point, it might seem that the only way to search through a recursive data structure is with recusion, but this is not necessarily the case! Rather surprisingly, we can perform a DFS non-recursively by using a stack, which are data structures that hold multiple elements, but only allow you to interact with the very last element you put in. The idea here is simple:
139
+
Now, at this point, it might seem that the only way to search through a recursive data structure is with recursion, but this is not necessarily the case! Rather surprisingly, we can perform a DFS non-recursively by using a stack, which are data structures that hold multiple elements, but only allow you to interact with the very last element you put in. The idea here is simple:
132
140
133
141
1. Put the root node in the stack
134
142
2. Take it out and put in its children
@@ -157,6 +165,9 @@ This has not been implemented in your chosen language, so here is the Julia code
157
165
[import:45-56, lang:"julia"](code/julia/Tree.jl)
158
166
{% sample lang="rs"%}
159
167
[import:17-24, lang:"rust"](code/rust/tree.rs)
168
+
{% sample lang="hs"%}
169
+
This has not been implemented in your chosen language, so here is the Julia code
170
+
[import:45-56, lang:"julia"](code/julia/Tree.jl)
160
171
{% endmethod %}
161
172
162
173
All this said, there are a few details about DFS that might not be idea, depending on the situation. For example, if we use DFS on an incredibly long tree, we will spend a lot of time going further and further down a single branch without searching the rest of the data structure. In addition, it is not the natural way humans would order a tree if asked to number all the nodes from top to bottom. I would argue a more natural traversal order would look something like this:
@@ -187,6 +198,8 @@ This has not been implemented in your chosen language, so here is the Julia code
0 commit comments