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 worst-case performance O(log N), which is quite good
-
A well-designed hash table has typical performance O(1), which is excellent; but worst-case is O(N), which is bad
-
Search trees require that keys be well-ordered: 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