Friday, 14 March 2014

Week 9 - Working with Binary Search Trees

This week has been all about binary search trees, from lecture to the lab and even the exercise. Binary trees, as mention earlier, have up to two children per node, and the child to the left of the node has a value less than the node, whereas the child to the right of the node has a value that is greater than the node. Of course, the children can also be other binary search trees.

The key point I want to mention is something that came up over the course of doing the exercise. I will not say too much, as the deadline has been extended, but it is about the recursive logic. For the first part, I reached the point of understanding that the preorder traversal will give the nodes of the tree while the inorder traversal would help me identify the subtrees. Initially I thought that the node from the preorder tree should be removed from the inorder traversal, and doing so recursively worked in certain situations, but created a mess later on. After some thinking I realized that I would have to create new lists at every point to ensure that there was no mismatch. As for part B, all I can say for that is that it has taught me to thoroughly count brackets...

The lab went really well so not much to mention here, other than abut the iterative method that we were supposed to come up with. I know the point of this was to teach us to represent the tree as a ADT which we could work with at various points of execution but I came up with a method to solving this which I believe works well or even better than the solution mentioned, at least for small trees. Seeing as this part did not mention that we could not edit any other functions, all I did was add a line to the __init__ method, where I copied the list into a variable and then in the method i just iterated over that list using a for loop and a counter, incrementing the counter every time my condition was met. I liked that this approach made use of the list of nodes (which can be easily created, if not explicitly given) and just basic iterative logic; looping over a list with a for loop, checking for a condition to be met. While it was not the method that was desired, it is at least worth considering.

No comments:

Post a Comment