插入Trie实现功能无法正常工作

时间:2017-02-20 05:33:17

标签: python trie

我在Python中实现Trie数据结构时遇到了一些问题。 以下是我的代码,我的" addWord"功能无法正常工作。 具体来说,例如,如果我想添加单词" sam"进入Trie,我应该期望根节点链接到一个孩子" s",它链接到一个孩子" a",然后链接到" m",最后链接到结束标志" *"。但是,我在这里的功能导致这个单词中的所有字母都是root的子句,而且这个单词中的所有字母都是root的每个子节点的子节点,这些字母永远存在。这里的关键是在整个Trie中,所有节点都有数据" s"实际上是指同一个对象,对于所有三个字母和结束符号都是相同的。我真的无法弄清楚我的功能如何设法做到这一点"惊人的"事情。我对该功能的测试也发布在下面。谁能帮我看看有什么不对? (似乎这一行:current = current.children[index]有问题,但我不知道为什么)

# implementation of Trie data structure
# a trie, or digital tree, is a kind of search tree
# used to store characters
# each node stores a character instead of number

# implementing class of Trie nodes
class Node:
    def __init__(self, data = "*", children = []):
        self.data = data
        # children list can be implemented instead
        # with a binary search tree
        # note that members of children list are nodes
        self.children = children
    def isWord(self):
        for items in self.children:
            if items.data == "*":
                # if * is one of the children
                return True
        return False
    def hasChild(self, data):
        print("checking", self.data, "has child", data)
        for index in range(len(self.children)):
            if self.children[index].data == data:
                return index
        return -1
    def addChild(self, data):
        new_node = Node(data)
        self.children.append(new_node)

# implementing class of Trie tree
class Trie(object):
    def __init__(self, root = Node("*")):
        self.root = root
    def addWord(self, data):
        current = self.root
        while True:
            print()
            for items in self.root.children:
                print(items.data)
            print()
            if len(data) == 0:
                # entire word finish adding
                # current node point to
                # last character of the word
                # append end sign to current node
                current.children.append(Node("*"))
                print("append end sign to", current.data)
                return
            if current.hasChild(data[0]) != -1:
                print(current.data, "does have", data[0])
                # next character in existing Trie
                # check remainder of the word
                index = current.hasChild(data[0])
                data = data[1:]
                print("word truncated to", data)
                current = current.children[index]
                print("current point to", current.data)
            else:
                print(current.data, "does not have", data[0])
                # add next character to this node
                current.addChild(data[0])
                # check remainder of the word
                data = data[1:]
                print("word truncated to", data)
                current = current.children[-1]
                print("current point to last element", current.data)
        # End of method

console: test against my implementation

0 个答案:

没有答案
相关问题