0

我尝试按照某些教程在 JS 中实现 trie 数据结构。http://jsbin.com/ilamic/3/edit

它有效,但有一个问题:

它仅在我同时调用 insert_key() 和 start_with_prefix() 时才有效 - 如果我注释掉 insert_key() 并仅调用 start_with_prefix() 我什么也得不到!:( 数据就消失了!我如何让它留在那儿,一旦插入?

PS:那个教程是 Python 教程——我把它改编成 JS 代码。也许,我错过了一些东西(

4

1 回答 1

0

Actually you missed a very simple thing, every time the code is executed the variable tr is reinitialized, so if you try to execute start_with_prefix() without populating it with insert_key() it will be empty. you need to save the state of your variable to reuse it later.

Here I've cloned your original example and added some logic. Hope this help.

于 2012-09-06T21:02:53.050 回答