문제

I'd like to store some data associated to words composed of simple ascii letters (a-Z) The goal is to retrieve very quickly the data associated to a word in a future parsing.

I though about the following structure:

struct Foo {
  Foo *letter[26];
  void *data;
};

Thus, it is possible to go down through the "letter tree" while parsing a word in a string and get the associated data.

"foo" => ['f' node] -> ['o' node] -> ['o' node]

The problem is the size of the entire tree if I got many words.

Is there a way to reduce the size of the tree without losing performances?

도움이 되었습니까?

해결책

What you're describeing is called trie. A compact radix tree is more compact.

다른 팁

Is there a reason you're using a tree instead of storing words in a hash table? These will tend to use a bit more memory, but will give you near constant time performance for table lookups.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top