Sunday, March 8, 2015

Week 7 Impressions: Binary Trees

During week 7 for this course, we continued learning about Trees, this time Binary Trees. Binary trees are a specific type of trees in which each node can have at most two children (left and right). Binary trees can be have ordering conditions. This is very useful, since with an ordering condition, a binary tree is more efficient and easier to search through. If it is known that the higher values are on the right of a node and lower values are on the right, then searching through the tree is not only easier, it's also more efficient.  Also, using a binary tree, we can create a code so that the program searches through the tree without having to stop at and search through unnecessary nodes. For example, if we are searching for 8 in a binary tree and self.data (or the value at the first node) is 4, then we know that the code does not have to stop at the node on the left, since it would be less than 4, and 8 is obviously greater than 4. Using a binary tree in this manner, makes searching through large amounts of data more quick and efficient. Additionally there are more than one way of searching through a binary tree: inorder, preorder, and postorder.

Inorder searching is a way of searching through binary trees, where it visits the left subtree inorder, then visits the node itself, then visits the right subtree inorder. This code is recursive. Preorder, on the other hand, is different, since the node itself is visited first, then the left subtree in preorder, then the right subtree in preorder. Finally, post-order searching is when the left subtree is visited first in postorder, then the right subtree in postorder, then the node itself. In all cases, the left subtree is always visited before the right subtree. 

Furthermore, in last week's lab, we created recursive code for a binary tree. This was difficult at first, especially writing the recursive part of the code. In order to create recursive code, you must first understand the most basic and simplest example, which does not require recursive code. This part usually the easiest since it does not require any recursion and is just a simple 'if' statement. Afterwards, you must think of the most simplest example that does require a little recursion. After, write the recursive code for this simple recursive example. This part is much more difficult, and takes some practice and understanding. However, afterwards it becomes much more easy to write and implement recursive code. For one part of the lab last week, we had to write code to search through a binary tree without stopping at unnecessary nodes. As mentioned above, this meant we had to use the properties and ordering conditions of a binary tree already explained. This code was also recursive and was easier to understand and visualize than to write and implement. However, once the most basic example was thought of and code written for, the rest flowed naturally.  

No comments:

Post a Comment