3

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.

3

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

11 Comments

Yes, I agree with you, I read this in a book named <Cracking the Coding Interview>, the words are : Another common implementation for a hash table is to use a BST as the underlying data structure. Retrieving an element will no longer be O(1), but it prevents us from creating an unnecessarily large array to hold items. So, @alfasin how do you think about it ?
I have the 4th edition of the book and couldn't find such a question, can you direct me to the chapter/page ?
mine is the 5th edition, Chapter 8 Object-Oriented Design --> question 10
Not there in the 4th addition, anyways, this book has the answers too ;)
@alfasin. Thank you :) I meant the no of items in the same bucket incase of hash collision. My bad. Should have referred it as M.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.