Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
There are several ways to turn a recursive algorithm into an iterative one or at least avoid to do the recursive calls in your own code. In the specific example of yours, where your traverse a tre...
Answer
#3: Post edited
- There are several ways to turn a recursive algorithm into an iterative one or at least avoid to do the recursive calls in your own code. In the specific example of yours, where your traverse a tree, some possibilities are:
- * You can create a stack data structure explicitly that corresponds to the stack that is otherwise built implicitly by the recursive calls. Whenever in the recursive algorithm you would do a recursive call, you push the current set of variables onto the stack. Then the previous value of the variables is saved on the stack and you can use the variables again as you would within the next recursive call. When in the original algorithm you return from a recursive call, you can restore the values from the top of the stack.
- * You can convert the tree into a sequential structure, like a list or an array and then iterate over the array. If the tree comes from a library, it is likely that the library itself offers something like a "tree_to_list" or "tree_to_array" function. But, even if that is not the case, such a conversion from tree to array is simple: You create an array that holds the tree nodes. You initialize the array with with the root node of the tree. Then, in a loop, you iterate over the array indices and for each node you append the children of the node to the arrary (which gets longer while you encounter new nodes with children). This way you will get all the tree nodes into the array without recursion.
- * You can use a tree-iterator object that allows you to traverse the tree. The tree-iterator represents a position within the tree. Your algorithm then initializes the iterator to point to the first position of the tree, and, in a loop, operates at the position the iterator represents and afterwards "increments" the iterator. Then, the recursive nature of tree traversal is hidden inside the tree-iterator object (which typically will internally have some stack like structure that is used to implement "incrementing" the iterator). This way, recursion still happens (inside the iterator), but is not apparent from your algorithm.
* If your tree library offers a "map" function for trees this also allows you to keep the recursion off your own code: You define a function that shall be applied to each node in the tree (in your case the function would replace the "a"s), and then call the map function which does the iteration for you and will, for each node it finds, call your function for that node.
- There are several ways to turn a recursive algorithm into an iterative one or at least avoid to do the recursive calls in your own code. In the specific example of yours, where your traverse a tree, some possibilities are:
- * You can create a stack data structure explicitly that corresponds to the stack that is otherwise built implicitly by the recursive calls. Whenever in the recursive algorithm you would do a recursive call, you push the current set of variables onto the stack. Then the previous value of the variables is saved on the stack and you can use the variables again as you would within the next recursive call. When in the original algorithm you return from a recursive call, you can restore the values from the top of the stack.
- * You can convert the tree into a sequential structure, like a list or an array and then iterate over the array. If the tree comes from a library, it is likely that the library itself offers something like a "tree_to_list" or "tree_to_array" function. But, even if that is not the case, such a conversion from tree to array is simple: You create an array that holds the tree nodes. You initialize the array with with the root node of the tree. Then, in a loop, you iterate over the array indices and for each node you append the children of the node to the arrary (which gets longer while you encounter new nodes with children). This way you will get all the tree nodes into the array without recursion.
- * You can use a tree-iterator object that allows you to traverse the tree. The tree-iterator represents a position within the tree. Your algorithm then initializes the iterator to point to the first position of the tree, and, in a loop, operates at the position the iterator represents and afterwards "increments" the iterator. Then, the recursive nature of tree traversal is hidden inside the tree-iterator object (which typically will internally have some stack like structure that is used to implement "incrementing" the iterator). This way, recursion still happens (inside the iterator), but is not apparent from your algorithm.
- * If your tree library offers a "map" function for trees this also allows you to keep the recursion off your own code: You define a function that shall be applied to each node in the tree (in your case the function would replace the "a"s), and then call the map function which does the tree traversal for you and will, for each node it finds, call your function for that node.
#2: Post edited
- There are several ways to turn a recursive algorithm into an iterative one or at least avoid to do the recursive calls in your own code. In the specific example of yours, where your traverse a tree, some possibilities are:
- * You can create a stack data structure explicitly that corresponds to the stack that is otherwise built implicitly by the recursive calls. Whenever in the recursive algorithm you would do a recursive call, you push the current set of variables onto the stack. Then the previous value of the variables is saved on the stack and you can use the variables again as you would within the next recursive call. When in the original algorithm you return from a recursive call, you can restore the values from the top of the stack.
- * You can convert the tree into a sequential structure, like a list or an array and then iterate over the array. If the tree comes from a library, it is likely that the library itself offers something like a "tree_to_list" or "tree_to_array" function. But, even if that is not the case, such a conversion from tree to array is simple: You create an array that holds the tree nodes. You initialize the array with with the root node of the tree. Then, in a loop, you iterate over the array indices and for each node you append the children of the node to the arrary (which gets longer while you encounter new nodes with children). This way you will get all the tree nodes into the array without recursion.
* You can use a tree-iterator object that allows you to traverse the tree. The tree-iterator represents a position within the tree. Your algorithm then initializes the iterator to point to the first position of the tree, and, in a loop, operates at the position the iterator represents and afterwards "increments" the iterator. Then, the recursive nature of tree traversal hidden inside the tree-iterator object (which typically will internally have some stack like structure that is used to implement "incrementing" the iterator). This way, recursion still happens (inside the iterator), but is not apparent from your algorithm.- * If your tree library offers a "map" function for trees this also allows you to keep the recursion off your own code: You define a function that shall be applied to each node in the tree (in your case the function would replace the "a"s), and then call the map function which does the iteration for you and will, for each node it finds, call your function for that node.
- There are several ways to turn a recursive algorithm into an iterative one or at least avoid to do the recursive calls in your own code. In the specific example of yours, where your traverse a tree, some possibilities are:
- * You can create a stack data structure explicitly that corresponds to the stack that is otherwise built implicitly by the recursive calls. Whenever in the recursive algorithm you would do a recursive call, you push the current set of variables onto the stack. Then the previous value of the variables is saved on the stack and you can use the variables again as you would within the next recursive call. When in the original algorithm you return from a recursive call, you can restore the values from the top of the stack.
- * You can convert the tree into a sequential structure, like a list or an array and then iterate over the array. If the tree comes from a library, it is likely that the library itself offers something like a "tree_to_list" or "tree_to_array" function. But, even if that is not the case, such a conversion from tree to array is simple: You create an array that holds the tree nodes. You initialize the array with with the root node of the tree. Then, in a loop, you iterate over the array indices and for each node you append the children of the node to the arrary (which gets longer while you encounter new nodes with children). This way you will get all the tree nodes into the array without recursion.
- * You can use a tree-iterator object that allows you to traverse the tree. The tree-iterator represents a position within the tree. Your algorithm then initializes the iterator to point to the first position of the tree, and, in a loop, operates at the position the iterator represents and afterwards "increments" the iterator. Then, the recursive nature of tree traversal is hidden inside the tree-iterator object (which typically will internally have some stack like structure that is used to implement "incrementing" the iterator). This way, recursion still happens (inside the iterator), but is not apparent from your algorithm.
- * If your tree library offers a "map" function for trees this also allows you to keep the recursion off your own code: You define a function that shall be applied to each node in the tree (in your case the function would replace the "a"s), and then call the map function which does the iteration for you and will, for each node it finds, call your function for that node.
#1: Initial revision
There are several ways to turn a recursive algorithm into an iterative one or at least avoid to do the recursive calls in your own code. In the specific example of yours, where your traverse a tree, some possibilities are: * You can create a stack data structure explicitly that corresponds to the stack that is otherwise built implicitly by the recursive calls. Whenever in the recursive algorithm you would do a recursive call, you push the current set of variables onto the stack. Then the previous value of the variables is saved on the stack and you can use the variables again as you would within the next recursive call. When in the original algorithm you return from a recursive call, you can restore the values from the top of the stack. * You can convert the tree into a sequential structure, like a list or an array and then iterate over the array. If the tree comes from a library, it is likely that the library itself offers something like a "tree_to_list" or "tree_to_array" function. But, even if that is not the case, such a conversion from tree to array is simple: You create an array that holds the tree nodes. You initialize the array with with the root node of the tree. Then, in a loop, you iterate over the array indices and for each node you append the children of the node to the arrary (which gets longer while you encounter new nodes with children). This way you will get all the tree nodes into the array without recursion. * You can use a tree-iterator object that allows you to traverse the tree. The tree-iterator represents a position within the tree. Your algorithm then initializes the iterator to point to the first position of the tree, and, in a loop, operates at the position the iterator represents and afterwards "increments" the iterator. Then, the recursive nature of tree traversal hidden inside the tree-iterator object (which typically will internally have some stack like structure that is used to implement "incrementing" the iterator). This way, recursion still happens (inside the iterator), but is not apparent from your algorithm. * If your tree library offers a "map" function for trees this also allows you to keep the recursion off your own code: You define a function that shall be applied to each node in the tree (in your case the function would replace the "a"s), and then call the map function which does the iteration for you and will, for each node it finds, call your function for that node.