In week 7, we talked about binary tree and binary search tree.
Binary tree is a tree whose nodes have at most two children.Binary search tree is a binary tree in which the value of left child is less than the value of node and the value of node is less than the value of right child.
Prof Diane drew two pictures which are quite useful for us to understand the concepts in her class and I drew them again using computer and post here.
The left picture represents binary tree and the right represents BST.

In lecture we also talked a very important thing which we should keep in mind. That is :"__init__" in class "binary_tree" is different from class "tree".
We set "left==None" as a default. This means if a node don't has a left child then its "left" will still exist and its value will be None.
Although binary tree and binary search tree were not hard to understand, I still met a question in this week's lab!
The head of the question is list_longest_path.
The head of the question is list_longest_path.
I got confused about how to get a whole path! The only thing came to my mind is that I could write a recursion to create a list holding all nodes. But I didn't know how to get a particular path! Even worse, I didn't finish all the questions in my lab time so I didn't have a chance to ask my TA about it.
Then I posted a question on Pizza! To my surprise, my prof answered it quickly!
According to her ,my should take advantage of some properties of binary tree. That was : "binary trees have only two children." so we can check them one by one. After some test, I got it.
Here is the solution which is posted on course website for this function.




No comments:
Post a Comment