ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

295,实现 Trie (前缀树)

2021-06-14 23:02:53  阅读:149  来源: 互联网

标签:前缀 Trie public current TrieNode ws 295 root children


实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。

示例:

Trie trie = new Trie(); trie.insert("apple"); trie.search("apple");   // 返回 true trie.search("app");     // 返回 false trie.startsWith("app"); // 返回 true trie.insert("app");    trie.search("app");     // 返回 true

说明:

  • 你可以假设所有的输入都是由小写字母 a-z 构成的。

  • 保证所有输入均为非空字符串。

答案:

 1class TrieNode {
2    boolean isEnd;
3    TrieNode[] children;
4
5    public TrieNode() {
6        isEnd = true;
7        children = new TrieNode[26];
8    }
9}
10
11public class Trie {
12    private TrieNode root;
13
14    public Trie() {
15        root = new TrieNode();
16    }
17
18    public void insert(String word) {
19        TrieNode current = root;
20        for (int i = 0, L = word.length(); i < L; i++) {
21            int id = word.charAt(i) - 'a';
22            if (current.children[id] == null) {
23                current.children[id] = new TrieNode();
24                current.children[id].isEnd = false;
25            }
26            current = current.children[id];
27        }
28        current.isEnd = true;
29    }
30
31    public boolean search(String word) {
32        return search(word, 1);
33    }
34
35    public boolean startsWith(String prefix) {
36        return search(prefix, 2);
37    }
38
39    private boolean search(String str, int type) {
40        TrieNode current = root;
41        int i = -1, L = str.length();
42        while (++i < L) {
43            int id = str.charAt(i) - 'a';
44            if ((current = current.children[id]) == null) return false;
45        }
46        return type == 1 ? current.isEnd : true;
47    }
48}

解析:

如果对哈夫曼树比较熟悉的话,这题很容易理解,这题没什么难度,我们再来看另一种解法

 1class TrieNode {
2    public char val;
3    public boolean isWord;
4    public TrieNode[] children = new TrieNode[26];
5
6    public TrieNode() {
7    }
8
9    TrieNode(char c) {
10        TrieNode node = new TrieNode();
11        node.val = c;
12    }
13}
14
15public class Trie {
16    private TrieNode root;
17
18    public Trie() {
19        root = new TrieNode();
20        root.val = ' ';
21    }
22
23    public void insert(String word) {
24        TrieNode ws = root;
25        for (int i = 0; i < word.length(); i++) {
26            char c = word.charAt(i);
27            if (ws.children[c - 'a'] == null) {
28                ws.children[c - 'a'] = new TrieNode(c);
29            }
30            ws = ws.children[c - 'a'];
31        }
32        ws.isWord = true;
33    }
34
35    public boolean search(String word) {
36        TrieNode ws = root;
37        for (int i = 0; i < word.length(); i++) {
38            char c = word.charAt(i);
39            if (ws.children[c - 'a'] == null) 
40                return false;
41            ws = ws.children[c - 'a'];
42        }
43        return ws.isWord;
44    }
45
46    public boolean startsWith(String prefix) {
47        TrieNode ws = root;
48        for (int i = 0; i < prefix.length(); i++) {
49            char c = prefix.charAt(i);
50            if (ws.children[c - 'a'] == null) 
51                return false;
52            ws = ws.children[c - 'a'];
53        }
54        return true;
55    }
56}

标签:前缀,Trie,public,current,TrieNode,ws,295,root,children
来源: https://blog.51cto.com/u_4774266/2902565

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有