Comments on: Programming Interview Questions 26: Trim Binary Search Tree /2012/01/17/programming-interview-questions-26-trim-binary-search-tree/?utm_source=rss&utm_medium=rss&utm_campaign=programming-interview-questions-26-trim-binary-search-tree Information Retrieval and Machine Learning Thu, 16 Feb 2012 16:19:09 +0000 hourly 1 http://wordpress.org/?v=3.3 By: Kowshik /2012/01/17/programming-interview-questions-26-trim-binary-search-tree/#comment-1519 Kowshik Thu, 19 Jan 2012 10:05:22 +0000 /?p=918#comment-1519 Sorry, please ignore. It works fine! Great question! Sorry, please ignore. It works fine!
Great question!

]]>
By: Kowshik /2012/01/17/programming-interview-questions-26-trim-binary-search-tree/#comment-1518 Kowshik Thu, 19 Jan 2012 10:01:49 +0000 /?p=918#comment-1518 It looks to me like there are null checks missing (or None checks in Python?). Please see my comment inline below: def trimBST(tree, minVal, maxVal): if not tree: return tree.left=trimBST(tree.left, minVal, maxVal) tree.right=trimBST(tree.right, minVal, maxVal) ########## # Shouldn't there be a check here on tree.left == None and tree.right == None # before the comparisons below? This happens when leaf nodes are checked and # the first line returns None viz: # # if not tree: # return ########## if minVal<=tree.val<=maxVal: return tree if tree.valmaxVal: return tree.left It looks to me like there are null checks missing (or None checks in Python?).
Please see my comment inline below:

def trimBST(tree, minVal, maxVal):
if not tree:
return
tree.left=trimBST(tree.left, minVal, maxVal)
tree.right=trimBST(tree.right, minVal, maxVal)
##########
# Shouldn’t there be a check here on tree.left == None and tree.right == None
# before the comparisons below? This happens when leaf nodes are checked and
# the first line returns None viz:
#
# if not tree:
# return
##########
if minVal<=tree.val<=maxVal:
return tree
if tree.valmaxVal:
return tree.left

]]>
By: Arden /2012/01/17/programming-interview-questions-26-trim-binary-search-tree/#comment-1498 Arden Tue, 17 Jan 2012 23:17:03 +0000 /?p=918#comment-1498 But note that we still have to destroy all that subtree, otherwise a memory leak will occur. So we can't simply cut the link between the node and its child. In python code I don't explicitly free the nodes, I just remove references to them and then the garbage collector performs the deletion itself. But I will update the article mentioning this case to avoid further confusion. Thanks for the comment, this is important to note. But note that we still have to destroy all that subtree, otherwise a memory leak will occur. So we can’t simply cut the link between the node and its child. In python code I don’t explicitly free the nodes, I just remove references to them and then the garbage collector performs the deletion itself. But I will update the article mentioning this case to avoid further confusion. Thanks for the comment, this is important to note.

]]>
By: Sachin /2012/01/17/programming-interview-questions-26-trim-binary-search-tree/#comment-1493 Sachin Tue, 17 Jan 2012 21:26:43 +0000 /?p=918#comment-1493 If we do the condition checking before the recursive calls, we can avoid one unnecessary recursive call for the cases where tree.val > max and tree.val < min. When tree.val > max, we need to trim only the left subtree and vice versa. If we do the condition checking before the recursive calls, we can avoid one unnecessary recursive call for the cases where tree.val > max and tree.val < min. When tree.val > max, we need to trim only the left subtree and vice versa.

]]>