2 ms·
"To understand recursion, first you must understand recursion" ...so said many a professor, instructor, and developer I've known over the years (I first heard i
by warrenm 4y ago
"To understand recursion, first you must understand recursion" ...so said many a professor, instructor, and developer I've known over the years (I first heard it from Dale Bryant, CS prof at HVCC[0] back in ~2000)
Recursion is the perfect solution to a handful of problems (eg tree traversal)
...so long as you have the memory to do it!
Back in highschool, I wrote a linear (ie iterative) octree traverser in C++
It was about 250 lines (not counting comments and whitespace ... printed it was about 10 pages (50 lines to the page on that old printer!)
And it worked! But I did it that way because I hadn't seen recursion except as an example of solving the Fibonacci sequence (for which it is distinctly ill-prepared!)
When I learned about recursion properly a few years later, I rewrote that ~250 line function down to under one page printed (with ample whitespace and comments)...it was maybe 30 lines long in its recursive form
That all said ... solving for recurrence relations is 100% "worth it" IF (and only if) the recursive runtime and/or memory usage is "too much". How "much" is "too much"? That's going to depend on the problem at hand.
Linear/iterative solutions are [almost] always faster than their recursive cousins ... but they're often not nearly as intuitive
------------
[0] https://hvcc.edu https://hvcc.edu