简单Trie树

有一个txt文件,每一行是一个单词,构建一棵字典树

txt文件:

简陋的实现,未优化:

class node:
    def __init__(self, eachChar):
        self.val = eachChar
        self.count = 0
        self.nextNode = dict()


rootNode = node('#')
for line in open('words.txt'):
    tmpList = list(line.strip())
    curNode = rootNode
    newWord = 0
    for i in range(len(tmpList)):
        if tmpList[i] in curNode.nextNode:
            curNode.nextNode[tmpList[i]].count += 1
            curNode = curNode.nextNode[tmpList[i]]
        else:
            newWord = 1
            newNode = node(tmpList[i])
            newNode.count = 1
            curNode.nextNode[tmpList[i]] = newNode
            curNode = curNode.nextNode[tmpList[i]]
    if not newWord:
        print("Word already exsit:", line.strip())


# 层次遍历
queueList = []
nodeList = []
nodeList.append(rootNode)
while nodeList:
    queueList = []
    for i in range(len(nodeList)):
        tmpNode = nodeList.pop(0)
        for key, value in tmpNode.nextNode.items():
            queueList.append(key)
            nodeList.append(value)
    if queueList:
        print(queueList)


# 查找
while 1:
    word = input("input the word:")
    tmpList = list(word.strip())
    curNode = rootNode
    newWord = 0
    for i in range(len(tmpList)):
        if tmpList[i] in curNode.nextNode:
            curNode = curNode.nextNode[tmpList[i]]
        else:
            print(word, 0)
            break
    if curNode.val == tmpList[-1]:
        print(word, curNode.count)

运行:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这是一段基于 Python 的 trie 的代码: ``` class TrieNode: def __init__(self): self.children = {} self.is_word = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.is_word = True def search(self, word): node = self.root for char in word: if char not in node.children: return False node = node.children[char] return node.is_word def starts_with(self, prefix): node = self.root for char in prefix: if char not in node.children: return False node = node.children[char] return True ``` 这个 trie 的实现包含了三个操作:插入单词,搜索单词以及搜索前缀。 ### 回答2: Trie,也称为字典或前缀,是一种特殊的数据结构,通常用于快速检索和存储字符串集合。下面是一个用Python编写的Trie的基本代码: ```python class TrieNode: def __init__(self): self.children = {} self.is_end_of_word = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): current_node = self.root for char in word: if char not in current_node.children: current_node.children[char] = TrieNode() current_node = current_node.children[char] current_node.is_end_of_word = True def search(self, word): current_node = self.root for char in word: if char not in current_node.children: return False current_node = current_node.children[char] return current_node.is_end_of_word def starts_with(self, prefix): current_node = self.root for char in prefix: if char not in current_node.children: return False current_node = current_node.children[char] return True ``` 上述代码中,`TrieNode`类表示Trie的节点。每个节点包含一个子节点字典和一个布尔值`is_end_of_word`,用于标记是否是一个单词的结尾。 `Trie`类包含了Trie的基本操作。其中`insert`方法用于向Trie中插入一个单词,`search`方法用于查找一个单词是否存在,`starts_with`方法用于检查一个前缀是否存在。 这段代码实现了一个简单Trie,可以用于处理一些与字符串相关的问题,如单词的插入、查找和前缀匹配等操作。需要注意的是,上述代码只是Trie的基本实现,仍有许多优化和扩展的空间,如节点压缩、前缀搜索等。 ### 回答3: Trie,也被称为字典或前缀,是一种常用的数据结构,用于存储和搜索字符串集合。下面是一个简单Trie代码示例: ```python class TrieNode: def __init__(self): self.children = {} # 子节点集合 self.is_word = False # 标记是否为一个完整的单词 class Trie: def __init__(self): self.root = TrieNode() # 初始化根节点 def insert(self, word): node = self.root # 从根节点开始 for char in word: if char not in node.children: node.children[char] = TrieNode() # 如果字符不在子节点集合中,则添加一个新节点 node = node.children[char] # 继续处理下一个字符 node.is_word = True # 标记最后一个节点为一个完整的单词 def search(self, word): node = self.root for char in word: if char not in node.children: return False # 如果字符在任何节点中都不存在,则单词不存在 node = node.children[char] return node.is_word # 判断最后一个节点是否为一个完整的单词 def startsWith(self, prefix): node = self.root for char in prefix: if char not in node.children: return False # 如果字符在任何节点中都不存在,则前缀不存在 node = node.children[char] return True # 返回True,表示存在以该前缀开头的单词 ``` 这段代码定义了两个类:`TrieNode`表示一个Trie的节点,包含子节点集合和标记是否为一个完整的单词;`Trie`表示Trie,其中的方法包括`insert`插入一个单词,`search`搜索是否存在一个单词,以及`startsWith`判断是否存在以某个前缀开头的单词。 这个Trie代码示例可以用于构建和查询字符串集合。通过不断向Trie插入单词,可以将它们按字符顺序分解成状结构,通过遍历节点来搜索和判断单词是否存在。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值