Binary Search Tree vs Hash Table?

本文探讨了二叉搜索树(BinarySearchTree)与哈希表(HashTable)作为数据结构实现字典功能的优缺点。分析了两者在查找、插入操作的时间复杂度,并讨论了平衡二叉树与哈希函数选择的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://answers.yahoo.com/question/index;_ylt=A0SO8wNZTdFS3GwAXqJXNyoA;_ylu=X3oDMTEzMDc5Z2d1BHNlYwNzcgRwb3MDMQRjb2xvA2dxMQR2dGlkA1ZJUDI4M18x?qid=20090612072730AAnGAb0

Binary Search Tree vs Hash Table?
peti asked 5 years ago
Hello, 


in have implemented a dictionary, that has pair(char* key, char * definition) elements ( C++) using a Binary Search Tree. 


In the documentation i need to enumerate some of the advantages and disadvantages of using a BST over a Hash table as a representation. 


I already know that using a Hash Table would be faster but a lot more memory consuming. Any other differences? Also can someone please tell me the complexities involved in both parts? 


Thanks! ill choose a best answer ASAP so dont be shy.
Additional Details
Also , how about Linked Lists, how do they compare to the other 2 Data structures?
5 years ago


Answer
Follow Watchlist
    
 
Best AnswerVoter's Choice


absolt123 answered 5 years ago
Binary search tree has worst case seek of O(n) if elements are inserted in order. 


Hash table has worst case seek of O(n) if every element hashes to the same value. 


Assuming you have a good hash functions, a hash table will be very close to O(1) for both inserts and look-ups. 


If you balance your btree, you will end up with O(lg n) lookups. 


For the same number of elements, a btree will usually be more compact. 


However, a good hash table is notoriously hard to implement where as a good binary tree is, even balanced, is fairly straight forward. Hash functions are very subtle and finicky so picking a good one for your data set will take some work.
1 Comment
Other Answers (1)
 
Gregory W answered 5 years ago
An advantage of a binary tree is that you can print out the Dictionary in order the hash file is in no particular order.
A disadvantage of a binary tree is if you fill it from an alphabelical list - it will be unbalanced
Rate Comment
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值