Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a hash table using a BST?

Another common implementation(besides linked-list) for a hash table is to use a BST as the underlying data structure.
I've searched the web and SO but can't find the answer. How do I implement a Hashtable using a Binary Search Tree? 's answer just like wrapping the BST into a hash table, I don't think that means implementing hash table using a BST.

Please show me the codes for put() and get() method.

like image 226
Bin Avatar asked Apr 17 '26 01:04

Bin


1 Answers

The answer is that you simply can't!

The insert/find time in a BST is O(log n) while in HashTable it should be O(1)

UPDATE:
Now after I looked at the book...
Bin you missed what Gayle was referring to - the original question was:

Design and implement a hash table which uses chaining (linked lists) to handle collisions

then at the end of the answer it says

Another common implementation(besides linked-list) for a hash table is to use a BST as the underlying data structure.

It refers to the same thing as the original question: the use of BST is only when collisions occur, which means that the buckets part will be implemented as BST/List not the hash-map itself!

like image 111
Nir Alfasi Avatar answered Apr 19 '26 14:04

Nir Alfasi