CST 370 - Week 2 - Recursion is fun

We covered a lot this week, but particularly the Tower of Hanoi is pretty interesting (beyond its appearance in the recent Planet of the Apes movie). The puzzle is solved by recursion. Initially solving the puzzle seems impossible, but by solving a smaller sub problem first you find the rest of the solution falls into place quite naturally.

I have a lot of experience writing web applications, and it really made me think about nested component trees in a complex DOM Structure. For example let's say we wanted to find a simple piece of text. If you want to search to find that specific word, a regular for loop is not useful because you never know how deep the branches go (DOM can be very very nested). The solution to that is a recursive function. You just write two pieces of logic: "Check this element, and if it has descendants, tell them to run this exact same check." This allows for your logic to quickly drill down the DOM and find the text you're looking for rather than anticipating the depth of the elements.

This type of recursion is seemingly all over, where solving for multiple sub problems allows for a bigger problem to be solved.

Comments

Popular posts from this blog

CST 363 - Week 3 - SEE-QWILL or ES-SKEW-EL

CST 338 - Week 4 - Legally Distinct Learning Journal

CST 300 - Week 4 - Writing With Purpose