Reproduced http://blog.csdn.net/beiyetengqing/article/details/7856113
It has been a long time to focus on the trie structure. Trie has an interesting purpose, that is, automatic prompt. In addition, trie is also required for a recent interview. Therefore, we will summarize the data structure here.
Trie, also known as word search tree or key tree, is a tree structure. A typical application is to count and sort a large number of strings (but not limited to strings), so it is often used by the search engine system for text word frequency statistics. It has the following advantages: minimizes unnecessary string comparisons and improves query efficiency than hash tables.
It has three basic properties:
- The root node does not contain characters. Each node except the root node only contains one character.
- From the root node to a node, the character passing through the path is connected to the corresponding string of the node.
- All subnodes of each node contain different characters.
The following figure shows the expression of trie. Each side represents a character. If it ends, it is represented by an asterisk. In this trie structure, we have the following strings, such as do, dork, and dorm, but there is no BA or sen in trie, because at the end of a, and N, there is no ending symbol (asterisk ).
With such a data structure, we can use it to store a dictionary. Is it very convenient to query and change the dictionary to see if there are corresponding words? We can also make smart prompts. We store words that have been searched by users in trie. When users enter a word, we can automatically prompt. For example, when users enter Ba, we will automatically prompt bat and baiI.
Now we will discuss the implementation of trie.
First, we define an abstract trie, which stores a node. There are two operations in this class: insert and query. The specific implementation is later.
Node class implementation
[Java]View plaincopy
- Class node {
- Char content; // the character in the node
- Boolean isend; // whether the end Of the words
- Int count; // the number of words sharing this character
- Using list <node> childlist; // The Child List
- Public node (char c ){
- Childlist = new rule list <node> ();
- Isend = false;
- Content = C;
- Count = 0;
- }
- Public node subnode (char c ){
- If (childlist! = NULL ){
- For (node eachchild: childlist ){
- If (eachchild. content = c ){
- Return eachchild;
- }
- }
- }
- Return NULL;
- }
- }
Now let's look at the specific implementation of this trie class.
[Java]View plaincopy
- Public class trie {
- Private node root;
- Public trie (){
- Root = new node ('');
- }
- Public void insert (string word ){
- If (search (Word) = true) return;
- Node current = root;
- For (INT I = 0; I <word. Length (); I ++ ){
- Node child = current. subnode (word. charat (I ));
- If (child! = NULL ){
- Current = child;
- } Else {
- Current. childlist. Add (new node (word. charat (I )));
- Current = current. subnode (word. charat (I ));
- }
- Current. Count ++;
- }
- // Set isend to indicate end of the word
- Current. isend = true;
- }
- Public Boolean search (string word ){
- Node current = root;
- For (INT I = 0; I <word. Length (); I ++ ){
- If (current. subnode (word. charat (I) = NULL)
- Return false;
- Else
- Current = current. subnode (word. charat (I ));
- }
- /*
- * This means that a string exists, but make sure its
- * A Word by checking its 'isend' flag
- */
- If (current. isend = true) return true;
- Else return false;
- }
- Public void deleteword (string word ){
- If (search (Word) = false) return;
- Node current = root;
- For (char C: Word. tochararray ()){
- Node child = current. subnode (C );
- If (child. Count = 1 ){
- Current. childlist. Remove (child );
- Return;
- } Else {
- Child. Count --;
- Current = child;
- }
- }
- Current. isend = false;
- }
- Public static void main (string [] ARGs ){
- Trie trie = new trie ();
- Trie. insert ("ball ");
- Trie. insert ("bils ");
- Trie. insert ("Sense ");
- // Testing Deletion
- System. Out. println (trie. Search ("bils "));
- System. Out. println (trie. Search ("ba "));
- Trie. deleteword ("bils ");
- System. Out. println (trie. Search ("bils "));
- System. Out. println (trie. Search ("ball "));
- }
- }
Time Complexity Analysis:
For insert, if the length of the string to be inserted is K and each pair of characters is queried, We can query up to 26 times (up to 26 letters) in the child into list. Therefore, complexity: O (26 * K) = O (k ). for search, the complexity is the same.
Code from: http://www.technicalypto.com/2010/04/trie-in-java.html