Microsoft Interview Question
Software Engineer / DevelopersA BST may be used when the search needs to be done for a range of values. Since we know that the nodes of the left subtree hold values less than the root and that of the right hold values greater than the root, we can find the nodes containing a range of values easily.
Using a hash when looking for a range of values will need us to search for every value within that range. If most of the values dont exist then we are doing a lot of unnecessary searches. This doesnt happen for a BST.
If list has such numbers whose hash function evaluates to same hash key then inserting/retrieving such numbers is time consuming as compared to BST.
Binary search tree: allows various operations on a set of
- algooz April 27, 2008elements (create, find, find_min, find_max, insert,
delete)
If tree is “balanced” (expected depth of any node is O(logn)) the
average running time of all of the above operations is O(logn).
Worst case complexity is O(n).
Hash tables: support only a subset of the operations allowed by a search tree.
Operations that require ordering info among the elements, such as
find_min, and find_max, are not supported
Insertion, delete, and find: supported in constant
time on average.
More detailed:
* Hash tables have faster average lookup and insertion time (O(1)), while some kinds of binary search tree have faster worst-case lookup and insertion time (O(log n) instead of O(n)). Hash tables have seen extensive use in real time systems, but trees can be useful in high-security real time systems where untrusted users may deliberately supply information that triggers worst-case performance in a hash table, although careful design can remove that issue. Hash tables shine in very large arrays, where O(1) performance is important. Skip lists have worst-case operation time of O(n), but average-case of O(log n), with much less insertion and deletion overhead than balanced binary trees.
* Hash tables can have more compact storage for small value types, especially when the values are bits.
* There are simple persistent versions of balanced binary trees, which are especially prominent in functional languages.
* Building a hash table requires a reasonable hash function for the key type, which can be difficult to write well, while balanced binary trees and skip lists only require a total ordering on the keys. On the other hand, with hash tables the data may be cyclically or partially ordered without any problems.
* Balanced binary trees and skip lists preserve ordering — allowing one to efficiently iterate over the keys in order or to efficiently locate an association whose key is nearest to a given value. Hash tables do not preserve ordering and therefore cannot perform these operations as efficiently.
* Balanced binary trees can be easily adapted to efficiently assign a single value to a large ordered range of keys, or to count the number of keys in an ordered range.
This link might be useful for more insight
en.wikipedia.org/wiki/Associative_array