如何更改此前缀trie以返回包含类似前缀的所有单词

时间:2016-04-30 01:24:31

标签: python prefix trie

嗨,这是我的前缀trie代码的一部分,我试图让它返回的不仅仅是前缀,更多的解释在底部:

function xyz(b)
{
    var c = b;

    for(var i = 0; i < c.length; i++)
    {
        // do something with c items...
    }
}

我试图了解如何让class TrieNode: def __init__(self): self.isString = False self.children = {} def insertString(word, root): currentNode = root for char in word: if char not in currentNode.children: currentNode.children[char] = TrieNode() currentNode = currentNode.children[char] currentNode.isString = True def findStrings(prefix, node, results): if node.isString: results.append(prefix) for char in node.children: findStrings(prefix + char, node.children[char], results) def longestPrefix(word, root): currentNode = root currentPrefix = '' for char in word: if char not in currentNode.children: break else: currentNode = currentNode.children[char] currentPrefix += char strings = [] findStrings(currentPrefix, currentNode, strings) return strings pass # Discussion: Is it dangerous to assume that findStrings actually found a string? # Hint: There is an edge case that will break this wordList = ['aydt', 'coombs', 'schuhmacher', 'claypoole', 'exhume', 'forehands', 'carin', 'plaits', 'alfonsin', 'hometowns', 'pedestals', 'emad', 'hourly', 'purchaser', 'spogli', 'combativeness', 'henningsen', 'luedke', 'duchin', 'koglin', 'teason', 'bumpings', 'substantially', 'lamendola', 'cecola', 'henze', 'tutti', 'dills', 'satirical', 'jetted', 'intertwine', 'predict', 'breezes', 'cyclist', 'ancillary', 'schaumburg', 'viewer', "bay's", 'emissions', 'kincheloe', 'trees', 'vipperman', 'exhale', 'ornamental', 'repeated', 'pedestal', 'pedesta', 'pedest'] root = TrieNode() for word in wordList: insertString(word, root) allWords = [] findStrings('', root, allWords) print(allWords) inputWord = 'co' print(longestPrefix(inputWord, root)) inputWord = 'pedestals' print(longestPrefix(inputWord, root)) 返回'基座','基座','pedesta','pedest'而不仅仅是基座。我的代码中缺少什么?

2 个答案:

答案 0 :(得分:0)

  

我试图了解如何获得打印(longestPrefix('pedestals',   root))返回'pedestals','pedestal','pedesta','pedest'而不是   只是基座。

由于基座不是前缀,考虑到代码的逻辑,这没有意义 - 我本来希望你想知道为什么print(longestPrefix('pedest', root))没有返回那些四个结果。我在下面重新编写了代码,将所有函数转换为方法,因为每个函数都将您定义的对象作为参数:

class TrieNode:
    def __init__(self):
        self.isString = False
        self.children = {}

    def insertString(self, word):
        for char in word:
            if char not in self.children:
                self.children[char] = TrieNode()
            self = self.children[char]
        self.isString = True

    def findStrings(self, prefix):
        results = []
        if self.isString:
            results.append(prefix)

        for char in self.children:
            results.extend((self.children[char]).findStrings(prefix + char))

        return results

    def longestPrefix(self, word):
        currentPrefix = ''

        for char in word:
            if char not in self.children:
                break
            else:
                self = self.children[char]
                currentPrefix += char

        return self.findStrings(currentPrefix)

wordList = [
    'aydt', 'coombs', 'schuhmacher', 'claypoole', 'exhume', 'forehands', 'carin', 'plaits', 'alfonsin',
    'hometowns', 'pedestals', 'emad', 'hourly', 'purchaser', 'spogli', 'combativeness', 'henningsen', 'luedke',
    'duchin', 'koglin', 'teason', 'bumpings', 'substantially', 'lamendola', 'cecola', 'henze', 'tutti', 'dills',
    'satirical', 'jetted', 'intertwine', 'predict', 'breezes', 'cyclist', 'ancillary', 'schaumburg', 'viewer',
    "bay's", 'emissions', 'kincheloe', 'trees', 'vipperman', 'exhale', 'ornamental', 'repeated', 'pedestal',
    'pedesta', 'pedest'
]

root = TrieNode()

for word in wordList:
    root.insertString(word)

allWords = root.findStrings('')
print(allWords)

inputWord = 'co'
print(root.longestPrefix(inputWord))

inputWord = 'pedest'
print(root.longestPrefix(inputWord))

最后两个打印语句输出:

['coombs', 'combativeness']
['pedest', 'pedesta', 'pedestal', 'pedestals']

答案 1 :(得分:0)

wordList = ['aydt', 'coombs', 'schuhmacher', 'claypoole', 'exhume', 'forehands', 'carin', 'plaits', 'alfonsin',
            'hometowns', 'pedestals', 'emad', 'hourly', 'purchaser', 'spogli', 'combativeness', 'henningsen', 'luedke',
            'duchin', 'koglin', 'teason', 'bumpings', 'substantially', 'lamendola', 'cecola', 'henze', 'tutti', 'dills',
            'satirical', 'jetted', 'intertwine', 'predict', 'breezes', 'cyclist', 'ancillary', 'schaumburg', 'viewer',
            "bay's", 'emissions', 'kincheloe', 'trees', 'vipperman', 'exhale', 'ornamental', 'repeated', 'pedestal',
            'pedesta', 'pedest']

def findsubstring(fullstring):
    for word in wordList:
        if word in fullstring:
            print (word)

findsubstring("pedestals")

输出:

pedestals
pedestal
pedesta
pedest