Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In handling hash collisions, why use a linked list over a BST?

A linked list takes O(n) to search while a BST takes O(log n). So why use a linked list to handle collisions? The only reason I could think of is because inserting into the linked list is O(1) whereas inserting into the BST is O(log n).

like image 275
Adam Zerner Avatar asked Oct 21 '25 03:10

Adam Zerner


1 Answers

The justification of a hash table is that there should be very few items hashing to the same hash slot. For these small numbers, a linked list should actually be faster than the BST (better constant factors) and will in any case be simpler and more reliable to code. The worst case for a BST is the same as a linked list in any case, unless you want to go really over the top and use a balanced tree.

like image 155
mcdowella Avatar answered Oct 24 '25 16:10

mcdowella



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!