Redian新闻
>
关于trie和binary search tree的疑问。
avatar
关于trie和binary search tree的疑问。# JobHunting - 待字闺中
r*o
1
我对trie一直不是很懂,
如果要在n个单词中查某个单词(长度为m个字母)的话,
为什么不能把这n个单词组成一个binary search tree呢?这样平均复杂度是O(lgn)。
而用trie查的话复杂度是O(m)。
这样lgn这个问题可能很弱,欢迎拍砖。
avatar
X*n
2
但实际上 m总是有限的, 但n可以无数多, 比如说dictionary
所以一般意义上m远小logn

【在 r****o 的大作中提到】
: 我对trie一直不是很懂,
: 如果要在n个单词中查某个单词(长度为m个字母)的话,
: 为什么不能把这n个单词组成一个binary search tree呢?这样平均复杂度是O(lgn)。
: 而用trie查的话复杂度是O(m)。
: 这样lgn: 这个问题可能很弱,欢迎拍砖。

avatar
a*d
3
in binary search tree, each comparison compares entire key.
But for trie, at each node it only compares one bit.
For long key, such as strings, it makes a big difference.
In other word, lg(n) of m character comparison for BST.
O(m) comparison for trie.

【在 r****o 的大作中提到】
: 我对trie一直不是很懂,
: 如果要在n个单词中查某个单词(长度为m个字母)的话,
: 为什么不能把这n个单词组成一个binary search tree呢?这样平均复杂度是O(lgn)。
: 而用trie查的话复杂度是O(m)。
: 这样lgn: 这个问题可能很弱,欢迎拍砖。

avatar
r*o
4
谢谢,我没搞明白为什么是lg(n) of m character comparison for BST.
我觉得对于BST就是lg(n)啊.

【在 a*d 的大作中提到】
: in binary search tree, each comparison compares entire key.
: But for trie, at each node it only compares one bit.
: For long key, such as strings, it makes a big difference.
: In other word, lg(n) of m character comparison for BST.
: O(m) comparison for trie.

avatar
n*0
5
请问怎么把n个单词组成bst?

【在 r****o 的大作中提到】
: 我对trie一直不是很懂,
: 如果要在n个单词中查某个单词(长度为m个字母)的话,
: 为什么不能把这n个单词组成一个binary search tree呢?这样平均复杂度是O(lgn)。
: 而用trie查的话复杂度是O(m)。
: 这样lgn: 这个问题可能很弱,欢迎拍砖。

avatar
f*5
6
lookup for BST is O(lgn)
but pay attention now u want to compare a word each time,
you need to compare the target word with the word in current node
u need to compare each letter of the word...

【在 r****o 的大作中提到】
: 谢谢,我没搞明白为什么是lg(n) of m character comparison for BST.
: 我觉得对于BST就是lg(n)啊.

avatar
f*5
7
list to BST with O(n)
please refer to DSW algorithm

【在 n*****0 的大作中提到】
: 请问怎么把n个单词组成bst?
avatar
r*o
8
Thanks a lot!

【在 f*********5 的大作中提到】
: lookup for BST is O(lgn)
: but pay attention now u want to compare a word each time,
: you need to compare the target word with the word in current node
: u need to compare each letter of the word...

avatar
n*0
9
如果单词可以放在bst的internal node和leaf node的话
可以根据每个单词的字母排列为node创建一个unique的index
lookup的时候比较node的index就可以了,所以整个过程只需要O(lgn) ,假设树是
balance的
而tire的lookup需要O(m)
所以关键还是单词长度m和单词个数n的关系。
不知道理解的对不对

【在 f*********5 的大作中提到】
: list to BST with O(n)
: please refer to DSW algorithm

avatar
b*r
10
this makes sense

【在 a*d 的大作中提到】
: in binary search tree, each comparison compares entire key.
: But for trie, at each node it only compares one bit.
: For long key, such as strings, it makes a big difference.
: In other word, lg(n) of m character comparison for BST.
: O(m) comparison for trie.

avatar
f*5
11
大概是这样吧

【在 n*****0 的大作中提到】
: 如果单词可以放在bst的internal node和leaf node的话
: 可以根据每个单词的字母排列为node创建一个unique的index
: lookup的时候比较node的index就可以了,所以整个过程只需要O(lgn) ,假设树是
: balance的
: 而tire的lookup需要O(m)
: 所以关键还是单词长度m和单词个数n的关系。
: 不知道理解的对不对

avatar
b*h
12
如果这个unique的index可以创建,那O(1)不就可以找到了(直接index到一个hash
table里),还要bst干嘛?

【在 n*****0 的大作中提到】
: 如果单词可以放在bst的internal node和leaf node的话
: 可以根据每个单词的字母排列为node创建一个unique的index
: lookup的时候比较node的index就可以了,所以整个过程只需要O(lgn) ,假设树是
: balance的
: 而tire的lookup需要O(m)
: 所以关键还是单词长度m和单词个数n的关系。
: 不知道理解的对不对

avatar
a*d
13
In BST, it is not always exactly m character comparison, however, the deeper
you go down the tree, more likely you will need to compare more characters.
(strings are more similar)

【在 f*********5 的大作中提到】
: lookup for BST is O(lgn)
: but pay attention now u want to compare a word each time,
: you need to compare the target word with the word in current node
: u need to compare each letter of the word...

avatar
n*0
14
嗯 用unique的index,用hash table就可以查询了,O(1)就行了。我想的有点多了。。。
不过unique的index还是要handle overflow的问题。

【在 b********h 的大作中提到】
: 如果这个unique的index可以创建,那O(1)不就可以找到了(直接index到一个hash
: table里),还要bst干嘛?

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。