The reason for it being called a tree is it being a metaphor for several things. Generally, you begin at the root with a value, it and carries on through left and right "sub-trees" which also contain their own left and right sub-trees until a NoneType node is reached, to finally end the tree. Trees and their implementations can vary extremely. Below is an example of a Tree object's initialization given to us in the readings.
Cargo can be of any type, the parameter is unspecific for this reason. For example, a Tree cargo could be an integer like so: Tree(3). It could also be a string, Tree('x'). As you may notice, there is no need to specify a left or right sub-tree. By default, they are set to None. The above examples would be shown like so in the Python shell.
Why use a tree in the first place, you may ask? Well, they're a very convenient way to store information. Traversing through a tree in a particular manner can return a result most helpful to the user. You could, for example, print a tree preorder, where the contents of the root appear before the contents of the children. In this case, the contents of the left tree is printed before the contents of the right tree. So the case prints from root, to left tree, to right tree.
Voila! Now if anyone wanted a preorder representation of their expression tree (yes, trees can be manipulated to deal with various expressions in various ways!)
I think they can be cool, for lack of a better word, but understanding how to use them the way I want to use them for A2 and E3 is killing me. I also regret to say that I don't know if I'll be able to figure E3 out in time. Aside from it being completely confusing to me, I do not have much time to focus on CSC148 this week. So cheers, until next time.
No comments:
Post a Comment