Monday 2 March 2015

4 - Forestry in Programming Class

This week, we learned about making a Tree object in python. A tree is essentially an object that has nodes but also leaves and children. Nodes are values in a tree that can either be a leaf, which is a terminating node without children, or a subtree, which has both parents and children. Nodes are connected by edges and you can determine the length of a sequence of nodes by adding the number of edges in said sequence. Trees can be used in data that involves several possible outcomes, figuring out different combinations of objects.

When we want to find out something like how many leaves a certain tree has, it is usually very difficult to do since trees can sometimes be nested lists. Recursion, what we started to learn a week or so ago, can help with this. I am beginning to see how recursion can be helpful in programming, especially with trees.

I participated in an in-class example with a couple of friends. It first started off with two people standing up in the first row; they were the "roots" of the in-class "tree". Then those two "roots" can choose to have one or two "children", which they would indicate by pointing to people in the row behind them. Eventually it got to me and I was able to choose my "children" and so on. Then, Danny asked us to do a "search" by asking our "children" if their birthdays fell on the day he called out. If both original roots didn't find anyone with said day, they would "return" false. We did this until we found a day that at least one person had a birthday on, the original "root" would "return" true. This group example showed us how recursion and searching worked in a tree, which helped me understand recursion and trees a lot better.

No comments:

Post a Comment