leetcode -- Implement Trie (Prefix Tree) -- 关于字典树,重要

https://leetcode.com/problems/implement-trie-prefix-tree/

关于字典树介绍http://www.cnblogs.com/tanky_woo/archive/2010/09/24/1833717.html

字典树中的枝干代表一个字母,所以要用dict或者list来做child_node. node只有一个bool 型变量isWord。要注意的是不是叶子节点,isWord也可以是True。看Word Search II。

方便word的插入,搜索。

看code就知道思路了。参考http://yucoding.blogspot.hk/2015/06/leetcode-question-implement-trie-prefix.html

方法1,用list来做child node

class TrieNode:
    # Initialize your data structure here.
    def __init__(self):
        self.value = 0#这个相当于isWord,就仅仅是bool型而已
        self.children = [None] * 26


class Trie:

    def __init__(self):
        self.root = TrieNode()

    # @param {string} word
    # @return {void}
    # Inserts a word into the trie.
    def insert(self, word):
        p = self.root
        for ch in word:
            idx = ord(ch) - ord('a')
            if not p.children[idx]:
                p.children[idx] = TrieNode()
            p = p.children[idx]
        p.value = 1;


    # @param {string} word
    # @return {boolean}
    # Returns if the word is in the trie.
    def search(self, word):
        p = self.root
        for ch in word:
            idx = ord(ch) - ord('a')
            if not p.children[idx]:
                return False
            p = p.children[idx]
        if p.value != 0:
            return True
        else:
            return False


    # @param {string} prefix
    # @return {boolean}
    # Returns if there is any word in the trie
    # that starts with the given prefix.
    def startsWith(self, prefix):
        p = self.root
        for ch in prefix:
            idx = ord(ch) - ord('a')
            if not p.children[idx]:
                return False
            p = p.children[idx]
        return True

方法2, 用dict做child node

参考http://bookshadow.com/weblog/2015/05/08/leetcode-implement-trie-prefix-tree/
一个枝干代表一个字母

class TrieNode:
    # Initialize your data structure here.
    def __init__(self):
        self.childs = dict()
        self.isWord = False

class Trie:

    def __init__(self):
        self.root = TrieNode()

    # @param {string} word
    # @return {void}
    # Inserts a word into the trie.
    def insert(self, word):
        node = self.root
        for letter in word:
            child = node.childs.get(letter)
            if child is None:
                child = TrieNode()
                node.childs[letter] = child
            node = child
        node.isWord = True

    # @param {string} word
    # @return {boolean}
    # Returns if the word is in the trie.
    def search(self, word):
        node = self.root
        for letter in word:
            node = node.childs.get(letter)
            if node is None:
                return False
        return node.isWord

    # @param {string} prefix
    # @return {boolean}
    # Returns if there is any word in the trie
    # that starts with the given prefix.
    def startsWith(self, prefix):
        node = self.root
        for letter in prefix:
            node = node.childs.get(letter)
            if node is None:
                return False
        return True

你可能感兴趣的:(LeetCode)