This week, we had a term test and I did not do well in the test. Especially in part B and part C. I guess the reason is that I put too much time in Assignment 2 and didn't prepare well for this coming test. Part B is mainly about Binary Tree. The function is list_even_between, that is a function return all even integers in the range given by start number and end number. I practiced this kind of functions in labs but I did't understand them fully at that time. And in part C, I forgot to check whether the data it return exists.
And because the test, we have only 1 lecture this week. It is about the insertion function and the delete function in a binary search tree. I think from now on, we have entered a new stage which require more advanced recursive thinking. Sometimes, I feel I just can't figure out what it should be, but when I see the answer, suddenly I understand how it works.
I would like to talk more about the delete function. The algorithm is like:
1. If this node is None, return that
2. If data is less than node.data, delete it from left child and
return this node
3. If data is more than node.data, delete it from right child
and return this node
4. If node with data has fewer than two children,
and you know one is None, return the other one
5. If node with data has two non-None children,
replace data with that of its largest child in the left subtree,
and delete that child, and return this node
At the beginning, I don't understand the return part in 4 and 5. After drawing some pictures, everything becomes clear. It's like if you cannot find anything on one side, then start searching the other side.
没有评论:
发表评论