def __contains__(self, item):
"""(BST, object -> Boolean
Check to see if obect is contained in BST
"""
#traverse the BST
parent, current = None, self.root
while current is not None:
if item == current.item:
return True
elif item < current.item:
parent, current = current, current.left
else: # item > current.item
parent, current = current, current.right
return False
I'll just talk about my technique for solving the first portion, I set a while loop that basically traverses the BST and compares the values with the item until it either finds it or terminates at the bottom of the list. Looking at the code it seems saving parent was unnecessary. I could have just set current to current.left or current.right dependently? I wonder why I found it necessary to create the parent class? Maybe I thought I needed it at some point also I'm sure I could have compacted this code even more so. That's something I'll sit down an look at.
No comments:
Post a Comment