由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 分别用LinkedList和HashMap构建字典树(Compact Trie)怎么做
相关主题
C++如何实现graph?能不能推荐一些关于Java算法复杂度的书?
Re: 请教一道题目Java concurrency的疑惑,难道我理解错了?
有否可以O(1)remove 一个元素的java LinkedList推荐?one more c++ question
C++ 有现成的 hashtable 库吗?C++(非VC++) 删除链表时如何对指针操作? 在线等回复!谢谢!
问几个关于hash, map, set的问题 (转载)c++古怪问题。。。。 (转载)
有什么方法可以优化hashtable?linkedList in C++
consistent hashing实际应用hashtable question
小公司的网站也要用memcached之类的cache吗?这个条件语句如何写?
相关话题的讨论汇总
话题: node话题: hashtable话题: trie话题: firstchild话题: label
进入Programming版参与讨论
1 (共1页)
S*******C
发帖数: 822
1
Input: WORD.LST
And a randomly picked 1000 words from the same WORD.LST for querying.
This is a list of > 100,000 English words. It is sorted alphabetically.
Your task is to create a Compact Trie. Each node of trie will contain,
(1) A leading character (possibly empty (for root))
(2) String Label (possibly null (for root))
(3) Boolean IsWord , indicating whether the current node represents a whole
word or not (this is helpful when one word may be a prefix of another).
(4) Node *RightSIbling
(5) Node *FirstChild
If a node stores "stock" as the label string then leading character should
be 's'
You will need a method for FindChild, which takes FirstChild and a character
c as input and searches for correct node by following RightSibling pointers
until it finds the node with c as its leading character.
How to create a Trie.
You can use repeated insertions: you'll need following insert method...
Input Trie T (as Node *root) , and a String newWord ;
First run a find method on newWord until it fails : it could fail in two
ways
(i) It fails in the middle of some label -- in this case split this nodes
label into two parts: make two child nodes of this node, one having the rest
of the label string, along with original node's FirstChild pointer, and
second child gets the remaining part of the original word as a new node.
Organize these two children as a sorted linked list. And make its leftmost
node as FirstChild pointer of original node.
(ii) it fails to find appropriate child while searching for leading
character, then simply insert a new Node at this point in the child list,
and put the remaining part of the word as label for this node.
----------------------------------------------------------------------------
---
Now the Hashing part, speed up FindChild operation by using a HashTable,
Use a chaining based global Hashtable. Use the number of lines N in WORD.LST
as the size of Hashtable[N].
HashEntryNode will have following fields:
{Node *parent,
char c
Node *child
HashEntryNode *next
}
The Hashtable is an array of HashEntryNode* Hashtable[N] .
Hash function will take two values as an input Node *parent and char c and
calculate hash value between 0.. N-1.
1 (共1页)
进入Programming版参与讨论
相关主题
这个条件语句如何写?问几个关于hash, map, set的问题 (转载)
c+= 怎么实现 hashtable 的?有什么方法可以优化hashtable?
[合集] 讨论一道很简单的题...consistent hashing实际应用
构建一个快速查询字典(数据结构题)?小公司的网站也要用memcached之类的cache吗?
C++如何实现graph?能不能推荐一些关于Java算法复杂度的书?
Re: 请教一道题目Java concurrency的疑惑,难道我理解错了?
有否可以O(1)remove 一个元素的java LinkedList推荐?one more c++ question
C++ 有现成的 hashtable 库吗?C++(非VC++) 删除链表时如何对指针操作? 在线等回复!谢谢!
相关话题的讨论汇总
话题: node话题: hashtable话题: trie话题: firstchild话题: label