Implement Trie (Prefix Tree) 两种实现方法的比较

class TrieNode {
    public TrieNode[] children = new TrieNode[26];
    public String item = "";  

    // Initialize your data structure here.
    public TrieNode() {
    }
}  

class Trie {
    private TrieNode root;  

    public Trie() {
        root = new TrieNode();
    }  

    // Inserts a word into the trie.
    public void insert(String word) {
        TrieNode node = root;
        for (char c : word.toCharArray()) {
            if (node.children[c - ‘a‘] == null) {
                node.children[c - ‘a‘] = new TrieNode();
            }
            node = node.children[c - ‘a‘];
        }
        node.item = word;
    }  

    // Returns if the word is in the trie.
    public boolean search(String word) {
        TrieNode node = root;
        for (char c : word.toCharArray()) {
            if (node.children[c - ‘a‘] == null) return false;
            node = node.children[c - ‘a‘];
        }
        return node.item.equals(word);
    }  

    // Returns if there is any word in the trie
    // that starts with the given prefix.
    public boolean startsWith(String prefix) {
        TrieNode node = root;
        for (char c : prefix.toCharArray()) {
            if (node.children[c - ‘a‘] == null) return false;
            node = node.children[c - ‘a‘];
        }
        return true;
    }
}  

class TrieNode {
    // Initialize your data structure here.
    char c;
    boolean leaf;   //leaf属性是一个字符串的结尾标志
    HashMap<Character, TrieNode> children = new HashMap<Character, TrieNode>();  

    public TrieNode(char c) {
        this.c = c;
    }  

    public TrieNode(){};
}  

class Trie {
    private TrieNode root;  

    public Trie() {
        root = new TrieNode();
    }  

    // Inserts a word into the trie.
    public void insert(String word) {
        Map<Character, TrieNode> children = root.children;   //用map构造了一个trie树
        for(int i=0; i<word.length(); i++) {
            char c = word.charAt(i);
            TrieNode t;
            if(children.containsKey(c)) {
                t = children.get(c);
            } else {
                t = new TrieNode(c);
                children.put(c, t);
            }
            children = t.children;
            if(i==word.length()-1) t.leaf=true;
        }
    }  

    // Returns if the word is in the trie.
    public boolean search(String word) {
        TrieNode t = searchNode(word);
        return t!=null && t.leaf;
    }  

    public boolean startsWith(String prefix) {
        return searchNode(prefix) != null;
    }  

    private TrieNode searchNode(String word) {
        Map<Character, TrieNode> children = root.children;
        TrieNode t = null;
        for(int i=0; i<word.length(); i++) {
            char c = word.charAt(i);
            if(!children.containsKey(c)) return null;
            t = children.get(c);
            children = t.children;
        }
        return t;
    }
}  

时间: 2024-12-14 18:49:11

Implement Trie (Prefix Tree) 两种实现方法的比较的相关文章

[LeetCode]Implement Trie(Prefix Tree),解题报告

目录 目录 概述 Trie树基本实现 定义Trie树节点 添加操作 查询word是否在Trie树中 AC完整代码 概述 Trie树,又称为字典树.单词查找树或者前缀树,是一种用于快速检索的多叉数结构.例如,英文字母的字典树是26叉数,数字的字典树是10叉树. Trie树的基本性质有三点,归纳为: 根节点不包含字符,根节点外每一个节点都只包含一个字符. 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串. 每个节点的所有子节点包含的字符串不相同. Trie树基本实现 我们通过Lee

leetcode Implement Trie (Prefix Tree)

题目连接 https://leetcode.com/problems/implement-trie-prefix-tree/ Implement Trie (Prefix Tree) Description Implement a trie with insert, search, and startsWith methods. 字典树.. class TrieNode { public: // Initialize your data structure here. bool vis; Tri

[LeetCode][JavaScript]Implement Trie (Prefix Tree)

Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. https://leetcode.com/problems/implement-trie-prefix-tree/ 实现字典树,每个节点至多有26个子孙,代表26个字母. 每个节点都有三个属性,key, isWord以及字典(哈希表,提高访问速度,也可以用数组),因为JS可以扩展实例化的对象,直接用下标访问对象,不需

LeetCode 208. Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs are consist of lowercase letters a-z. 这道题让我们实现一个重要但又有些复杂的数据结构-字典树, 又称前缀树或单词查找树,详细介绍可以参见网友董的博客,例如,一个保存了8个键的trie结构,"A", "to", "tea&quo

Implement Trie (Prefix Tree) 解答

Question Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs are consist of lowercase letters a-z. Solution A trie node should contains the character, its children and the flag that marks if it is a leaf

[leedcode 208] Implement Trie (Prefix Tree)

Trie树又被称为字典树.前缀树,是一种用于快速检索的多叉树.Tried树可以利用字符串的公共前缀来节省存储空间. 但如果系统存在大量没有公共前缀的字符串,相应的Trie树将非常消耗内存.(下图为Wiki上的Trie树示意图, https://en.wikipedia.org/wiki/Trie) 子节点用hashMap表示 isWord标记从根节点到该节点是否表示一个单词,还是另一单词的前缀. Implement a trie with insert, search, and startsWi

[Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie.insert("apple"); trie.search("apple"); // returns true trie.search("app"); // returns false trie.startsWith("app");

208. Implement Trie (Prefix Tree)

题目: Implement a trie with insert, search, and startsWith methods. 链接: http://leetcode.com/problems/implement-trie-prefix-tree/ 7/14/201760%,照着课件代码改写的. 将val cast成boolean,注意第21行需要设为false.以下代码可以不需要看,第一版虽然通过了,但是具体内容并不了解. 1 public class Trie { 2 private s

Implement Trie (Prefix Tree)

Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs are consist of lowercase letters a-z. 思路: 之前也没有接触过Trie,百科上查了一下,大概就是词源的问题,N个word有公共前缀,只是后缀不同,可以用树表示. 可以通过递归解决. 代码如下: class TrieNode { // 应该都是private的;只是