Sunday, March 15, 2015

impressions of week 8

In week 8, I learned how to mutate binary trees. To insert nodes into a binary tree, the algorithm uses recursion. It needs to check whether if the tree is there, because if it is None, then I can just create a BTNode of that particular value to be inserted. This is the base case for recursion. If the tree is there, then I need to compare whether the value to be inserted is less than or greater than the root node. If it is greater than the root node, I can just keep calling the insert function on node.right to insert the value into its appropriate position. Likewise, if the value is less than the root node, call insert on node.left to insert the value. And if the value is the same as the root node, then there is nothing to do, because the tree already has that value. 
I also learned the algorithm of deleting a node. It is very confusing for me to understand, because nowhere do I see in the code that 'deletes' a node. 
As seen on the left, the code for delete does not have any operators like 'del'. I was confused and so I asked my friends after class and understood that the delete function 'deletes' a node by making the original memory address of the value that a node contains lost. This way, that node is deleted. 
However, I still do not really understand what does the else statement do for the delete function, so I plan to seek help in the week to come.

1 comment:

  1. Nicely organized and very straight to the point. Great work and I am impressed by your explanation on BTNode. Good job and good luck with your final exam.

    ReplyDelete