performancedata-structureshashmaphashtableavl-tree

When is an AVL tree better than a hash table?


More specifically, are there any operations that can be performed more efficiently if using an AVL tree rather than a hash table?


Solution

  • I generally prefer AVL trees to hash tables. I know that the expected-time O(1) complexity of hash tables beats the guaranteed-time O(log n) complexity of AVL trees, but in practice constant factors make the two data structures generally competitive, and there are no niggling worries about some unexpected data that evokes bad behavior. Also, I often find that sometime during the maintenance life of a program, in a situation not foreseen when the initial choice of a hash table seemed right, that I need the data in sorted order, so I end up rewriting the program to use an AVL tree instead of a hash table; do that enough times, and you learn that you may as well just start with AVL trees.

    If your keys are strings, ternary search tries offer a reasonable alternative to AVL trees or hash tables.