A tree is composed of three parts: a root, children and leaves.
From the ground to the sky, the first part of a tree is the roots, then comes the trunk (no name for it in a binary tree), then it spreads out into branches (or children), and finally the leaves
This is an example of a tree where the leaves are digits and the children and root are mathematical symbols
(3 + (5 - 2))* 9
Thursday, 27 February 2014
Tuesday, 4 February 2014
Recursion
Recursion is a technique where the method will call itself and execute its code. A recursive method will continue to call itself until it reaches a "terminating condition" and it would start to work backwards
Although this sounds ridiculous, you could think of recursion like this. You have a large, plastic bag. You want to give your friend his gift. You get his gift from your bag, instead, you pull out another smaller, plastic bag and from this bag, you pull out another plastic bag (and no, those plastic bags are not for his friend) until you reach your incredibly fragile gift.
Or if you hate reading this and want to see what I am saying, here's a video you may remember from your childhood: http://youtu.be/PJg-HMlCR0M?t=3m15s
Although this example makes it sound like you do lots of meaningless work, it isn't. Recursion could become a more useful technique than implementing "for" or "while" loop. Would you want to use a loops, and accumulators and be less efficient?
The hardest part recursion for me would be to make sure that the function has a terminating condition. As demonstrated in class, if we didn't have this condition, our functions would exceed the memory our poor computers have given Python to use. You not only have to make a terminating condition, you have to have the right conditions or else the function ends prematurely or never ends at all due to some unforeseen case when you run a code.
Although this sounds ridiculous, you could think of recursion like this. You have a large, plastic bag. You want to give your friend his gift. You get his gift from your bag, instead, you pull out another smaller, plastic bag and from this bag, you pull out another plastic bag (and no, those plastic bags are not for his friend) until you reach your incredibly fragile gift.
Or if you hate reading this and want to see what I am saying, here's a video you may remember from your childhood: http://youtu.be/PJg-HMlCR0M?t=3m15s
Although this example makes it sound like you do lots of meaningless work, it isn't. Recursion could become a more useful technique than implementing "for" or "while" loop. Would you want to use a loops, and accumulators and be less efficient?
The hardest part recursion for me would be to make sure that the function has a terminating condition. As demonstrated in class, if we didn't have this condition, our functions would exceed the memory our poor computers have given Python to use. You not only have to make a terminating condition, you have to have the right conditions or else the function ends prematurely or never ends at all due to some unforeseen case when you run a code.
Subscribe to:
Posts (Atom)