字典树-Trie

概念

字典树,Trie (发音为 "try") 或前缀树是一种树数据结构,用于检索字符串数据集中的键。这一高效的数据结构有多种应用,如:自动补全、拼写检查、IP路由(最长前缀匹配)

特征

1. 数据结构为:Trie成员变量(Trie的指针数组、isEndOfWord结尾标志位)

2. 子节点最多有26个

插入

key为要插入的单词:

1、遍历key中的所有字母

a. 待补充

查询

待补充

基本实现--插入、查询

  1 package tree;
  2 
  3 // Java implementation of search and insert operations
  4 // on Trie
  5 public class Trie {
  6 
  7     // Alphabet size (# of symbols)
  8     static final int ALPHABET_SIZE = 26;
  9 
 10     // trie node
 11     static class TrieNode
 12     {
 13         TrieNode[] children = new TrieNode[ALPHABET_SIZE];
 14 
 15         // isEndOfWord is true if the node represents
 16         // end of a word
 17         boolean isEndOfWord;
 18 
 19         TrieNode(){
 20             isEndOfWord = false;
 21             for (int i = 0; i < ALPHABET_SIZE; i++)
 22                 children[i] = null;
 23         }
 24     };
 25 
 26     static TrieNode root;
 27 
 28     // If not present, inserts key into trie
 29     // If the key is prefix of trie node,
 30     // just marks leaf node
 31     static void insert(String key)
 32     {
 33         int level;
 34         int length = key.length();
 35         int index;
 36 
 37         TrieNode pCrawl = root;
 38 
 39         for (level = 0; level < length; level++)
 40         {
 41             index = key.charAt(level) - 'a';
 42             if (pCrawl.children[index] == null)
 43                 pCrawl.children[index] = new TrieNode();
 44 
 45             pCrawl = pCrawl.children[index];
 46         }
 47 
 48         // mark last node as leaf
 49         pCrawl.isEndOfWord = true;
 50     }
 51 
 52     // Returns true if key presents in trie, else false
 53     static boolean search(String key)
 54     {
 55         int level;
 56         int length = key.length();
 57         int index;
 58         TrieNode pCrawl = root;
 59 
 60         for (level = 0; level < length; level++)
 61         {
 62             index = key.charAt(level) - 'a';
 63 
 64             if (pCrawl.children[index] == null)
 65                 return false;
 66 
 67             pCrawl = pCrawl.children[index];
 68         }
 69 
 70         return (pCrawl != null && pCrawl.isEndOfWord);
 71     }
 72 
 73     // Driver
 74     public static void main(String args[])
 75     {
 76         // Input keys (use only 'a' through 'z' and lower case)
 77         String keys[] = {"the", "a", "there", "answer", "any",
 78                 "by", "bye", "their"};
 79 
 80         String output[] = {"Not present in trie", "Present in trie"};
 81 
 82 
 83         root = new TrieNode();
 84 
 85         // Construct trie
 86         int i;
 87         for (i = 0; i < keys.length ; i++)
 88             insert(keys[i]);
 89 
 90         // Search for different keys
 91         if(search("the") == true)
 92             System.out.println("the --- " + output[1]);
 93         else System.out.println("the --- " + output[0]);
 94 
 95         if(search("these") == true)
 96             System.out.println("these --- " + output[1]);
 97         else System.out.println("these --- " + output[0]);
 98 
 99         if(search("their") == true)
100             System.out.println("their --- " + output[1]);
101         else System.out.println("their --- " + output[0]);
102 
103         if(search("thaw") == true)
104             System.out.println("thaw --- " + output[1]);
105         else System.out.println("thaw --- " + output[0]);
106 
107     }
108 }
109 // This code is contributed by Sumit Ghosh

参考资料:

1. https://leetcode-cn.com/problems/implement-trie-prefix-tree/solution/shi-xian-trie-qian-zhui-shu-by-leetcode/

2. https://www.geeksforgeeks.org/trie-insert-and-search/

猜你喜欢

转载自www.cnblogs.com/harry1989/p/12071313.html