how to convert code that uses cmp to python3

Ian Kelly ian.g.kelly at gmail.com
Fri Apr 8 13:05:43 EDT 2016


On Fri, Apr 8, 2016 at 10:33 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Ian Kelly <ian.g.kelly at gmail.com>:
>
>> That's fine for those operations and probably insert, but how do you
>> search an AVL tree for a specific key without also using __eq__?
>
> Not needed:
>
> ========================================================================
> if key < node.key:
>     look_right()
> elif node.key < key:
>     look_left()
> else:
>     found_it()
> ========================================================================

That makes me a little nervous since it assumes that the keys are
totally ordered and could return an incorrect node if they aren't.
Granted, the keys *should* be totally ordered if the data structure is
being used properly, but an explicit equality check ensures that the
worst that could happen is the node simply isn't found despite being
present.

More to the contextual point, this is still doing two comparisons,
even if both of them are less than, so it doesn't really solve the
OP's issue.



More information about the Python-list mailing list