Friday, February 27, 2015

Hi Trees!!!

Welcome back everyone! Hope you all have a great reading break and a lot of sleep :) First of all, I would like to indicate that I want my previous post about OOP being graded, and I would direct my wonderful TA (or whoever is reading this) to the OOP post here.

Okay, so let’s talk about what is new in class. We learned Trees!! In this post, I would like to summarize everything we learned about Trees, Binary Trees (BT) and Binary Search Trees (BST).
Let’s start with some terminologies.
Node: a structure, which may contain a value, a condition or represent a separate data structure (which could be a tree of its own).
Tree: a set of nodes with directed edges between some pairs of nodes.
Edge: connects a parent node to a child node
Each node has zero/more children (child nodes). A node has at most one parent.
Root node: The top most node in a tree and has no parent.
Leaf: node with no children.
Internal node: a node with one or more children
Subtree: tree formed by any node and its descendants and the edges connecting them.
Path: a sequence of nodes n1, n2…nk where here is an edge from ni to ni+1
length of a path: the number of edges on it
height of a tree: the maximum path length + 1
depth of a node: height of the whole tree – height of the subtree rooted at that node
arity/branching factor: the number of max children for any node




That is a long list!! Hopefully, these images can give you a visual representation of the data structure tree. 
















Here is the __init__method for the tree class we defined. Notice we use a list to represents the child nodes of a parent node





I would like to move on to BT and BST.
Binary Tree: a tree with branching factor 2. We call the children left child and right child.

Here is the __init__ method for a BTNode class. Notice we separate the two child nodes as left and right.




Binary Search Tree: a binary tree such that for every node, its value is greater than all the values in its left subtree and less than all the values in its right subtree.
BST is a good tool to use when performing operations like searching or sorting, because we can utilize the order established in a BST and cut operations instead of visiting every node.

Here is an example of a binary search tree.












When implementing functions dealing with BT and BST, it is important to keep in mind that every node has two children. Also, each subtree as well is a BT or BST. 
I do find this week’s lab changeling and interesting. With the help from my TA, I was able to solve most of the problems. I would like to include two examples here that I find extremely beneficial for my understanding of recursion and BST and how I was able to able to come up with a solution at the end.

1. implement list_longeset_path, which will list the data in a longest path of a tree rooted at a BTNode.
Here is how you can think of this problem:
base case: return [] when node is empty
recursion: return the value at this node + the longest path from the subtree rooted at the left child if it has a longer path than the subtree rooted at the right child or vice versa.

I was stuck because I did not think of putting the recursive calls before the return statement. Yet, it is completely legal to do so and it makes the code more clear and presentable. In fact, sometimes you have to do it in this way in order to solve a problem!


2. Let's look at an example about BST.
Implement list_between, which will take a binary search tree and return a list of its data between start and end, inclusive.

This is a perfect example that demonstrates how we can utilize the property of BST and make the algorithm efficient without visiting unnecessary nodes. 
suppose we have a BST like this, call it t,  and we call list_between(t, 5, 9). We start from the root node 8, 5<8<9, so no cutting can be made. This is because both left child and right child have the possibility to be in the range 5-9. Now let's say we go to its left children, and we find that 4<5. We now know that we do NOT need to examine the left child of 4, since anything less than 4 will also be less than 5. The same argument holds if we find a node whose value is greater than end, we do NOT need to visit its right child.
In summary, we keep looking in the left branch if node value is greater than start, we keep looking in the right branch if node value is less than end. We include the node value if it is between start and end. Putting these into codes, we have
















These are the two examples that really help with my understanding of recursion and BST. I find that writing comments or summarize ideas into English is an extremely good way to help you get started on a new problem. That is all for this week. I hope you enjoy reading this and please comment below if you have any questions or just want to have a chat ;) 





No comments:

Post a Comment