Sunday, 16 February 2014

Week 6

During this week, we learned about the concept of trees. Trees are Python objects that consist of two sets: a set of nodes and a set of directed edges. One of the nodes is distinguished as the root; it is defined as being the only node with no parents. On the other hand, every non-root node has exactly one parent. While leafs are defined as nodes that have no children, internal nodes, on the other hand, are those that contain one or more children.

Paths can be formed between certain nodes in a Tree. A path is a sequence of nodes n(1), n(2), ..., n(k), where there is an edge from n(i) to n(i+1). The length of any path is defined as the number of edges in this path. A very important property of trees is the fact that there exists a single, unique path from the root to each node in the tree. For example, in the case of the root itself this path is just n(1) (if the root is n(1)), with length zero.

Another very important property of trees is the fact that paths cannot form any loops. The height of a tree is the maximum path length in a tree. Trees with a height of at least one contain subtrees, which are trees formed by any tree node together with its descendants and the edges leading to them. The arity of any specific tree is defined as the maximum number of children for any node in that tree.
During lecture, we wrote specific functions for the trees, including pre-order, post-order and in-order.

Moving on to a different subject, in this week's lab we were expected to transform list comprehensions into loops over iterables. In my opinion, this was a particularly straightforward task, and my partner and I were even able to finish the lab early for the first time since the beginning of the semester!

The last new thing we learned about this week were the two list comprehensions any and all. The first one can be applied to a list to check if any of the elements satisfy a given condition, while the latter can be used to get the bool of whether all elements inside the list satisfy a specific condition. These are particularly useful in order to write shorter and more comprehensible code, which is always more efficient!

No comments:

Post a Comment