What's the difference of hash table and binary tree?

本文探讨了哈希表与二叉树在数据查找效率、内存使用及应用场景上的区别,包括哈希表的O(1)查找时间和可能增加的内存使用,以及二叉树的O(logN)查找时间和较低的内存使用。讨论了哈希表和二叉树在处理特定类型搜索(如完全或部分匹配)时的优缺点。

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

http://bytes.com/topic/c/answers/597897-whats-difference-hash-table-binary-tree

What's the difference of hash table and binary tree?

fdmfdmfdm@gmail.com
P: n/a
This might not be the best place to post this topic, but I assume most
of the experts in C shall know this.

This is an interview question. My answer is:

hash table gives you O(1) searching but according to your hash
function you might take more memory than binary tree. On the contrary,
binary tree gives you O(logN) searching but less memory.

Am I right?

Feb 6 '07 # 1


Ben Pfaff
P: n/a
"fdmfdmfdm@gmail.com" <fdmfdmfdm@gmail.comwrites:
hash table gives you O(1) searching but according to your hash
function you might take more memory than binary tree. On the contrary,
binary tree gives you O(logN) searching but less memory.
The memory used by many implementations of hash tables and binary
search trees is fixed for a given number of elements. That is,
in many implementations, the hash function has no effect on an
N-element hash table's memory usage, and the particular content
of an N-element binary search tree has no effect on the BTS's
memory usage.

I'd guess that, in fact, it's easier to optimize the memory usage
of a hash table than of a binary search tree, especially if
you're willing to let the hash table slow down a little (while
remaining O(1) average time). But I haven't made a study of it.
--
"It would be a much better example of undefined behavior
if the behavior were undefined."
--Michael Rubenstein
Feb 6 '07 # 2

Christopher Layne
P: n/a
fdmfdmfdm@gmail.com wrote:
hash table gives you O(1) searching but according to your hash
function you might take more memory than binary tree. On the contrary,
binary tree gives you O(logN) searching but less memory.

Am I right?
One large difference is that ability to preserve some fundamental kind of
order with a tree and have a traversal reflect this.

Table of last names and you want to search them. Hash or binary tree are both
fine. Now what if you've got a partial last name you want to search - which
method do you think will be most efficient?
Feb 6 '07 # 3

user923005
P: n/a
On Feb 5, 10:16 pm, "fdmfdm...@gmail.com" <fdmfdm...@gmail.comwrote:
This might not be the best place to post this topic, but I assume most
of the experts in C shall know this.

This is an interview question. My answer is:

hash table gives you O(1) searching but according to your hash
function you might take more memory than binary tree. On the contrary,
binary tree gives you O(logN) searching but less memory.

Am I right?
The most important difference (besides hash tables being faster)
between hash tables and btrees is that hash tables only find on
equality searches.
Btrees can do range searches with things like <, >, <=, >=, between,
etc.

Your post is better aimed at a group like news:comp.programming

Feb 6 '07 # 4

CBFalconer
P: n/a
Christopher Layne wrote:
fdmfdmfdm@gmail.com wrote:
>hash table gives you O(1) searching but according to your hash
function you might take more memory than binary tree. On the
contrary, binary tree gives you O(logN) searching but less memory.

Am I right?

One large difference is that ability to preserve some fundamental
kind of order with a tree and have a traversal reflect this.

Table of last names and you want to search them. Hash or binary
tree are both fine. Now what if you've got a partial last name
you want to search - which method do you think will be most
efficient?
The binary tree requires considerably more care to avoid a worst
case O(N) operation, since it is easily fed a sorted list on input.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 6 '07 # 5

Lane Straatman
P: n/a

"CBFalconer" <cbfalconer@yahoo.comwrote in message
news:45C83963.1C6AD06B@yahoo.com...
Christopher Layne wrote:
> fdmfdmfdm@gmail.com wrote:
>>hash table gives you O(1) searching but according to your hash
function you might take more memory than binary tree. On the
contrary, binary tree gives you O(logN) searching but less memory.

Am I right?

One large difference is that ability to preserve some fundamental
kind of order with a tree and have a traversal reflect this.

Table of last names and you want to search them. Hash or binary
tree are both fine. Now what if you've got a partial last name
you want to search - which method do you think will be most
efficient?

The binary tree requires considerably more care to avoid a worst
case O(N) operation, since it is easily fed a sorted list on input.
Why does not every sort of size, say, greater than fifty, permute the input
randomly from the get-go? There isn't anything theoretically difficult in
doing so. LS
Feb 6 '07 # 6

user923005
P: n/a
On Feb 6, 1:56 am, "Lane Straatman" <inva...@invalid.netwrote:
"CBFalconer" <cbfalco...@yahoo.comwrote in message

news:45C83963.1C6AD06B@yahoo.com...
Christopher Layne wrote:
fdmfdm...@gmail.com wrote:
>hash table gives you O(1) searching but according to your hash
function you might take more memory than binary tree. On the
contrary, binary tree gives you O(logN) searching but less memory.
>Am I right?
One large difference is that ability to preserve some fundamental
kind of order with a tree and have a traversal reflect this.
Table of last names and you want to search them. Hash or binary
tree are both fine. Now what if you've got a partial last name
you want to search - which method do you think will be most
efficient?
The binary tree requires considerably more care to avoid a worst
case O(N) operation, since it is easily fed a sorted list on input.

Why does not every sort of size, say, greater than fifty, permute the input
randomly from the get-go? There isn't anything theoretically difficult in
doing so. LS
Skiplists do that. Some trees are self-balancing (AVL, Red-Black,
Weak Heaps...). A pure binary tree that does not self-balance is a
rarity in common usage where sorted distributions are likely to occur
anyway.

Feb 6 '07 # 7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值