Monday, 16 March 2015

Impression of week #7 - binary tree & binary search tree (week#8)

In week #7, we continued talking about tree, and it's mainly about binary tree and binary search tree.

As for binary tree, the most important property is that every node in it has at most 2 children -- the left child and the right child.


Here is the code of its init method:





And then we practiced a lot about binary search tree in the lab. 

A binary search tree is also a binary tree, but I'd like to call it a 'sorted' binary tree: for each node of BST, its value is greater than all values in its left subtree and less than all values in its right subtree, which means, any subtree of a  BST is also a BST.

The init method for binary tree and binary search tree are the same. But when we're creating a binary search tree, we place the values(nodes) in order. What we did so far is to use an insert function:

The 'sorted' property of binary search tree is very interesting. It makes lookup operations very convenient and efficient (I guess it also helps other operations a lot, but we just did lookup operation in courses and in the lab, so I will talk about it only this time).

Let's take a look of an example of binary search tree comparing with binary tree:

When we're checking whether a binary tree contains a value, we write the following code:
It uses preorder to go through every node from the root until it finds the value.

But in a binary search tree, if we want to check if it contains a value, we can take advantage of its order and check one side(left or right), ignoring the other on each node, which saves time.



Honestly, recursion was very hard for me and I didn't do very well in the lab. After looking at the solutions, I kind of know how they work, but not that sure. So I traced them in python visualizer, and then wrote down the recursive steps on paper. Now I have a better understanding.


What the function list_longest_ path does is to list the data in a longest path of node.

I gave a tree like this as an example:
It goes through the BST in postorder. FIrst of all, it goes to the root with value 8 and then go down along the left path until it gets to the node with value 2's left child (which is None), and then go to the right child of 2, which is 6, and then continue going to 6's left child and right child (which are both None). Up until now, the left_list and the right_list are empty lists. And then it goes to the node with value 6 (node_data), and assign [6] to the right_list of 2. Next, it goes to node_data in node with value 2, and add it to the right_list [6](since the length of right list is longer), getting a new list [2, 6], and assign it to the right_list of node with value 8. It follows this pattern and finally get a list [8, 2, 6], which is a list of the data of the longest node.

2 comments:

  1. Well done! Great use of graphics and imagery to get your point across. Tracing the recursion in the visualizers gives a very thorough understanding of the concept. I should do this as well. Keep up the good work!

    ReplyDelete
    Replies
    1. Thanks!

      BTW, Humair, do you know who I am?

      Delete