Advantages of Binary Search Trees over Hash Tables

前端 未结 18 1133
醉酒成梦
醉酒成梦 2020-11-29 15:02

What are the advantages of binary search trees over hash tables?

Hash tables can look up any element in Theta(1) time and it is just as easy to add an element....but

18条回答
  •  心在旅途
    2020-11-29 15:32

    Binary search trees can be faster when used with string keys. Especially when strings are long.

    Binary search trees using comparisons for less/greater which are fast for strings (when they are not equal). So a BST can quickly answer when a string is not found. When it's found it will need to do only one full comparison.

    In a hash table. You need to calculate the hash of the string and this means you need to go through all bytes at least once to compute the hash. Then again, when a matching entry is found.

提交回复
热议问题