A Tree class, my $0.02 contribution to the python community.

Antoon Pardon apardon at forel.vub.ac.be
Thu Oct 13 03:56:11 EDT 2005


Op 2005-10-12, George Sakkis schreef <gsakkis at rutgers.edu>:
> "Antoon Pardon" <apardon at forel.vub.ac.be> wrote:
>> Comments are welcome:
>>
>>   http://www.pardon-sleeuwaegen.be/antoon/avltree.html
>
> How about adding two shortcut methods, nextkey(k) and prevkey(k), to return the next and previous
> key respectively ? For instance nextkey would be equivalent to (untested):

I'll file this as: I'll probably never need it, so I'm going to resist
the temptation to add them now. If i find out I'm wrong, I can still do
so later.

> def nextkey(self, key):
>     iter = self[key:]
>     first = iter.next()
>     if key not in self: return first
>     else: return iter.next()

I think the if statement can be replaced by:

      if not self.cmp(key, first) == 0: return first

> Also for consistency, nextvalue(k), prevvalue(k), nextitem(k), previtem(k) would be reasonable
> additions.
>
> And a question: what does step do if the keys are not integers since you restrict step to be integer
> ?

It skips keys/items/values.

>>> radio = [
...        'alfa', 'bravo', 'charlie', 'delta', 'echo', 'foxtrot', 'golf', 'hotel', 'india',
...        'juliet', 'kilo', 'lima', 'mike', 'november', 'oscar', 'papa', 'quebec', 'romeo',
...        'sierra', 'tango', 'uniform', 'victor', 'whiskey', 'x-ray', 'yankee', 'zulu' ]
>>> 
>>> letters = 'abcdefghijklmnopqrstuvwxyz'
>>> from avltree import Tree
>>> t=Tree(zip(radio,letters))
>>> t.keys('choco',None,3)
['delta', 'golf', 'juliet', 'mike', 'papa', 'sierra', 'victor', 'yankee']
>>> t.values('bureau',None,4)
['c', 'g', 'k', 'o', 's', 'w']


What would you have in mind if step would have been a string here?

-- 
Antoon Pardon



More information about the Python-list mailing list