During week 7, we learned about linked structures, using the class BTNode as the example. A binary tree (BT) is subcategory of trees. While a root in a tree structure may have infinitely many childrens, all contained within a list, a root in a binary tree may have up to two children. One of the examples we examined that made use of a binary tree was binary arithmetic expressions. Each of its root contains a "data" that can be used to evaluate its two children. For example, a root with '+', left node '3', and right node '4' would represent '3+4'. A more complex case would require recursion. Such as one with a root '+', left node '*', right node '7'. The left node (now the root), has a left node '3' and right node '4'. In order for this binary tree to make sense, each child node that also has children must evaluate its children and return the evaluated value up the tree so that its parent may evaluate that value. Doing this we would represent the expression '(3*4)+7', with the '(3*4)' evaluated first and returned as the left node. The final evaluation would look like '12+7'.
I didn't really get what preorder, inorder, and postorder was about. I will try to review them in the future. I found the idea of binary search trees to be rather convenient and interesting.
The parenthesize function in the lab was almost identical to the evaluate function we developed in class. I actually found this to be helpful in solidifying my understanding of recursion using binary trees. I found list_between and list_internal_between to be extremely difficult. I ran out of time during the lab to finish it, and decided to give up on it for the time being.
Coming back to this, I finally figured out what I was doing wrong in list_between and list_internal_between. My biggest mistake was to trying to implement the else block in one line like I did for every other recursive function. This has expanded my view on implementing recursive functions. I had always tried to stick to a single line per if, elif, and else for recursion because recursively calling the function would reset any sort of setting, counters and the likes in the function.
Having learned this, I think that I should try more ways to implement recursive functions, even if I initially think they are wrong. If I try enough ways to implement one, I will learn something from it even if I fail to correctly implement it the first few times.
No comments:
Post a Comment