This week we learned about binary search trees which are different from regular trees. In a binary search tree the data is comparable and the data in the left subtree are less than node.data while the data in the right subtree are more than node.data. This makes a tree easier to traverse since it is sorted in a sense. For example, if you were looking for a node with a certain value you would know how to look for it from the root since the left subtree contains data that is lesser and the right subtree contains data that is greater. Hence you wouldn't waste time looking at all children in the tree.
We also learned about three different tree traversals called inorder, preorder, and postorder. These traversals done by recursive functions and the nodes in a tree are visited as follows in the image below:
1 represents the first node that is visited, 2 the second, and 3 the third.
As I mentioned last week, I found week seven's lab exercise to be much more difficult than previous labs. I am pleased to know that a fellow slogger also had difficulties with list_between and list_internal_between like I did and was able to solve his/her problem. I had the same issue of trying the write one line of code in my else block to make the two functions work. Fortunately, by seeing a TA at the CSC Help Centre, I realized that not all functions can work with just one line of code in an else block. By writing a few more lines of code I was able to make both of those functions work.

No comments:
Post a Comment