Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Trie in Java? [duplicate]

Tags:

java

trie

Possible Duplicate:
Where do I find a standard Trie based map implementation in Java?

I want to use Trie in Java, is there an implementation I can use ? (I tried looking for one but I didn't found it).

like image 239
Belgi Avatar asked Nov 02 '11 16:11

Belgi


People also ask

What is the difference between tree and trie?

A tree is a general structure of recursive nodes. There are many types of trees. Popular ones are binary tree and balanced tree. A Trie is a kind of tree, known by many names including prefix tree, digital search tree, and retrieval tree (hence the name 'trie').

Is a trie unique?

Often this value is a unique identifier or pointer that gets you to some data related to the key (e.g., primary key of a record in a DBMS). Like a map, a trie can also be used to store only keys with no associated values.

Is trie faster than hashing?

Also, we can easily print all the words in the dictionary in alphabetic order with a trie. Therefore, if we want a full-text lookup application, the hash table is better as it has a faster lookup speed. However, if we want to return all the words that match a prefix, the trie is our solution.

Is a trie a linked list?

A trie is essentially two linked lists. A (vertical) link to it's left-most child and a (horizontal) link to it's right sibling.


1 Answers

There is no trie data structure in the core Java libraries.

This may be because tries are usually designed to store character strings, while Java data structures are more general, usually holding any Object (defining equality and a hash operation), though they are sometimes limited to Comparable objects (defining an order). There's no common abstraction for "a sequence of symbols," although CharSequence is suitable for character strings, and I suppose you could do something with Iterable for other types of symbols.

Here's another point to consider: when trying to implement a conventional trie in Java, you are quickly confronted with the fact that Java supports Unicode. To have any sort of space efficiency, you have to restrict the strings in your trie to some subset of symbols, or abandon the conventional approach of storing child nodes in an array indexed by symbol. This might be another reason why tries are not considered general-purpose enough for inclusion in the core library, and something to watch out for if you implement your own or use a third-party library.

like image 129
erickson Avatar answered Sep 21 '22 15:09

erickson