So I thought I had my find_partial working, and after re-reading the instructions, I’m a bit confused. My method works for finding something like ‘apx’ in a set with apple and ape in it, it will return those partial matches, if I ask it to find partial for ‘appl’ it will give me apple, not ape… I think I have it set up to stop once a leaf node is reached and there are no more relevant branches.
So question is, when I ask it to find partial match for ‘appl’ and, I have ‘ax’ in my tree: You want it to return ‘ax’ because it is the shortest one that matches part of the beginning of K?
Here’s my find_partial that returns first relevant node that contains branches that will hold values I’m looking for:
def find_any_partial(self, key, node=None, i=0, temp=None):
if i == len(key):
return temp
else:
if node.key == key[i]:
i += 1
return self.find_any_partial(key, node.eq, i, temp=node)
elif node.key < key[i] and node.high:
#print("go higher")
return self.find_any_partial(key, node.high, i, temp=temp)
else:
if node.low:
#print("go lower")
return self.find_any_partial(key, node.low, i, temp=temp)
return temp