Hashtables vs. balanced search trees

Hashtables and balanced search trees can both be used in applications that need fast insert and find

What are advantages and disadvantages of each?

Balanced search trees guarantee worstcase performance O(log N), which is quite good

A welldesigned hash table has typical performance O(1), which is excellent; but worstcase is O(N), which is bad

Search trees require that keys be wellordered: For any keys K1, K2, either K1<K2, K1==K2, or K1> K2

So, there needs to be an ordering function that can be applied to the keys

Hashtables only require that keys be testable for equality, and that a hash function be computable for them

So, there need to be an equality test function and a hash function that can be applied to the keys
CONTENTS PREVIOUS NEXT