So this week we delved even deeper into recursion. We learned about trees, leaves, edges and roots. Before you go and think it's about plants (for those of you who didn't attend lecture and think we suddenly turned into a biology course), it's actually about data structures.
If you think about it, we've actually had an encounter with trees last week in the labs, with the SearchList. The 0th element was the root, the 1st and 2nd elements were its leaves. Each of the elements could have had either more leaves (up to 2) or None (and None was a base case). Maybe this was Danny's way of preparing us just in case some of us actually thought CSC148 was turning into a biology course.
But all in all, summing trees up. Trees are made of nodes, and there's always a node that's referred to as a root (the one node with no parents). It may or may not have children (and the children are referred to as leaves), and parents are connected to children via edges. In the diagram below, we can see that 1 is the root, and 2, 3, 4 are its children. All values are nodes, and each of the arrows connecting the nodes are the edges. All trees have subtrees.
Through learning about trees, we also got some practice with list comprehensions. I thought it was about time we had to use them because there were so many more things you can do with less code, and they were so fast and fun to use.
If you think about it, we've actually had an encounter with trees last week in the labs, with the SearchList. The 0th element was the root, the 1st and 2nd elements were its leaves. Each of the elements could have had either more leaves (up to 2) or None (and None was a base case). Maybe this was Danny's way of preparing us just in case some of us actually thought CSC148 was turning into a biology course.
But all in all, summing trees up. Trees are made of nodes, and there's always a node that's referred to as a root (the one node with no parents). It may or may not have children (and the children are referred to as leaves), and parents are connected to children via edges. In the diagram below, we can see that 1 is the root, and 2, 3, 4 are its children. All values are nodes, and each of the arrows connecting the nodes are the edges. All trees have subtrees.
(Source: http://new.math.uiuc.edu/math198/MA198-2009/lee522/pix/frame_tree_diagram.JPG)
Through learning about trees, we also got some practice with list comprehensions. I thought it was about time we had to use them because there were so many more things you can do with less code, and they were so fast and fun to use.
(Source: YouTube.com)
Athene: What do you think about List Comprehensions, George?
HotShotGG: I THINK IT'S AWESOME! IT'S FAST, IT'S FUN.
HotShotGG: I THINK IT'S AWESOME! IT'S FAST, IT'S FUN.
Also, before I forget (new list comprehension functions we encountered in the lab):
Filter is a function of list comprehensions that evaluate an iterable (had to use tuples for this in lab), that applies the function to each item in the iterable and eliminates all elements for which the function evaluates to False.
Zip takes two lists, and pairs the ith element together from each of the list and turns it into a tuple. The tuple then gets appended to its own list and becomes the ith element of that list. (remember you have to assign it to a variable so you can call it again.)
P.S.:
Pre-Order = Root first > Preorder left subtree > Preorder right subtree
In-Order = Left subtree first > Root > Right subtree
Post-Order = Right subtree first > Root > Left subtree
ONE LAST THING THAT CONFUSED ME THIS WEEK:
And I finally understand after some example code testing:
You can concatenate values onto a list if the first value in your ([a] + b + c) expression is a list. O:! I was wondering why some random line of code with all plus signs was returning a list then I found the magic [] brackets for a list ._. ...
P.P.S: If you're looking for a TL;DR, sorry this is already a tldr version of this week >_<
No comments:
Post a Comment