5 ms·
When you can't use recursion, what data structures/algorithms do you use in place of the common data structures that often use recursion? I'm thinking of stuff
by FLUX-YOU 11y ago
When you can't use recursion, what data structures/algorithms do you use in place of the common data structures that often use recursion? I'm thinking of stuff like trees and quicksort. Do they even need these things?
- pieceof314 11y agoLoops. A recursive algorithm can be transformed into an iterative looping algorithm that uses a fixed stack of memory. There's probably no change in the algorithms. They just use non-recursive versions.
- marcosdumay 11y agoWhat's the gain? If you turn your recursive algorithm into an iterative one with a stack, you are sill subject to all the same problems. They probably don't use trees and quicksort at all.
- deleted 11y ago[deleted]
- ktRolster 11y ago> What's the gain? You can prove that you won't have a stack overflow. You can calculate the maximum depth of the stack at compile time, not runtime.
- dclowd9901 11y agoCan't you also just pass a bit through the recursion that contains its depth, and halt the recursion if that number gets too high?
- ktRolster 11y agoYes, but that depends on making sure you get the programming logic correct, whereas with banning recursion outright, it can be tested automatically.
- marcosdumay 11y agoWell, you can either prove that you won't have a stack overflow at the original program, or you can not guarantee that your new code will run out of stack space. If you can calculate the size of the stack at compile time, you don't need to manually manage it.
- drakenot 11y agoI think that recursion can always be converted to iterative equivalents.
- adrianN 11y agoTypically you don't use dynamic allocation, making trees much more tedious to implement. The usual data structure is a stack allocated constant size array. But if you really have to use recursion, you can always simulate it with a loop and an explicit stack. This way it's also easy to bound the stack size.