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.

Community
  • 1
  • 1
Bin
  • 980
  • 2
  • 12
  • 24
  • What do you mean by that? Using buckets with a BST for collision resolution instead of a linked list? Also, "show me the codes" is instantly off-topic here. You're expected to show what you've tried, where you're stuck, what you've researched, etc. –  Sep 21 '13 at 09:57
  • Included in java 8: [Improve the performance of java.util.HashMap under high hash-collision conditions by using balanced trees rather than linked lists to store map entries. Implement the same improvement in the LinkedHashMap class.](http://openjdk.java.net/jeps/180) – Ritesh Dec 20 '15 at 21:28
  • I have added an answer here, hope this helps: http://stackoverflow.com/questions/17279805/how-do-i-implement-a-hashtable-using-a-binary-search-tree – Prabhash Rathore Feb 13 '16 at 15:15

1 Answers1

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!

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • 1
    Yes, I agree with you, I read this in a book named , 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 ? – Bin Sep 21 '13 at 10:35
  • I have the 4th edition of the book and couldn't find such a question, can you direct me to the chapter/page ? – Nir Alfasi Sep 21 '13 at 15:01
  • mine is the 5th edition, Chapter 8 Object-Oriented Design --> question 10 – Bin Sep 23 '13 at 08:32
  • Not there in the 4th addition, anyways, this book has the answers too ;) – Nir Alfasi Sep 23 '13 at 14:22
  • thanks, but these sentences are in the answer to question 8.10 :D – Bin Sep 24 '13 at 10:35
  • @Bin if you'll quote the answer from the book people here might be able to explain it – Nir Alfasi Sep 24 '13 at 14:53
  • In 6th edition she has said "we can also iterate through the keys in order", if BST is for collision resolution only, this line doesn't make sense. Right?@alfasin,@Bin – Koushik Balaji Venkatesan Sep 20 '16 at 05:32
  • @KoushikBalajiVenkatesan no, it has nothing to do with iterating the keys in-order. remember, we're talking about the implementation of collisions (traversing the buckets that have multiple keys) not iterating the keys of the whole map! – Nir Alfasi Sep 20 '16 at 08:02
  • Not replace the underlying ARRAY with BST but instead use BST in case of collision rather than the traditional LinkedList. This way in case of collision you will reach the bucket location and then search the BST to find the item in O(logN). In traditional hashMap its a linkedList with search operation consuming O(N). – Akh Oct 14 '17 at 17:48
  • @AKh pay close attention to what you refer as `N` :) – Nir Alfasi Oct 14 '17 at 19:04
  • 1
    @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. – Akh Oct 16 '17 at 02:52