我如何解决这个Python问题(trie数据结构实现)? [英] How do I fix this Python problem(trie data structure implemetion)?

查看:82
本文介绍了我如何解决这个Python问题(trie数据结构实现)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Node:
	def __init__(self,lable=None, data=None):
		self.lable = lable
		self.data = data
		self.children = dict()
	def addChild(self, key, data = None):
		if not isinstance(key, Node):
			self.children[key] = Node(key, data)
		else:
			self.children[key.lable] = key
			
	def __getitem__(self,key):
		return self.children[key]
		
class Trie:
	def __init__(self):
		self.head = Node()
		self.bottom = Node()
	def __getitem__(self,key):
		return self.head.children[key],self.bottom.children[key]
	def add(self, word, pair):
		current_node = self.head
		word_finished = True
		current_node_2 = self.bottom
		pair_finished = True
		
		for i in range(len(word)):
			if word[i] in current_node.children:
				current_node = current_node.children[word[i]]
			else:
				word_finished = False
				break
		if not word_finished:
			while i < len(word):
				current_node.addChild(word[i])
				current_node = current_node.children[word[i]]
				i+=1
				
		for j in range(len(pair)):
			if pair[j] in current_node_2.children:
				current_node_2 = current_node_2.children[word[j]]
			else:
				pair_finished = False
				break
		if not pair_finished:
			while j < len(pair):
				current_node_2.addChild(pair[j])
				current_node_2 = current_node_2.children[word[j]]
				j+=1
		
	def has_word(self, word):
		if word == '':
			return False
		if word ==None:
			raise ValueError('Trie.has_word require not null string')
		
		current_node = self.head
		exists = True
		for letter in word:
			if letter in current_node.children:
				current_node = current_node.children[letter]
			else:
				exists = False
				break
		if exists:
			if current_node.data == None:
				exists = False
		return exists
		
	def start_with_prefix(self,prefix):
		words = list()
		if prefix == None:
			raise ValueError('reqire not null prefix')
		top_node = self.head
		bot_node = self.bottom
		
		for letter in prefix:
			if letter in top_node.children:
				top_node = top_node.children[letter]
			else:
				return words
		if top_node == self.head:
			queue = [node for key, node in top_node.children.iteritems()]
		else:
			queue = [top_node]
		
		while queue:
			current_node = queue.pop()
			if current_node.data !=None:
				words.append(current_node.data)
			
			queue = [node for key, node in current_node.children.iteritems()]+queue
			
			
			
			
		return words
		
		
	def getData(self, word, pair):
		if not self.has_word(word):
			raise ValueError('{} not found in trie'.fromat(word))
		current_node = self.head
		for letter in word:
			current_node = current_node[letter]
			
		if not self.has_word(pair):
			raise ValueError('{} not found in trie'.format(pair))
		current_node_2 = self.bottom
		for letter in pair:
			current_node_2 = current_node_2[letter]
	
		return current_node.data,current_node_2.data
		
		
if __name__ == '__main__':
    """ Example use """
    trie = Trie()
    words = 'hello goodbye help gerald gold tea ted team to too tom stan standard money'
    for word in words.split():
        trie.add(word,word)
    print "'goodbye' in trie: ", trie.has_word('goodbye')
    print trie.start_with_prefix('g')
    print trie.start_with_prefix('to')





我尝试了什么:



请帮我解决。

错误我有:





当我想看到我输入的带有前缀的单词时,没有任何显示。

例如:我添加(go,move)

如果我输入我想看到所有带有'g'前缀的单词我必须看到go,move但它不起作用。



What I have tried:

please help me to fix it.
the error i have :


when i want to see the words that i entered with prefix there is nothing shown.
for example : i add ("go","move")
if i type that i want to see all words with 'g' prefix i must see go , move but it did not work.

推荐答案

因为 Trie 应该继承 Node ,但除非你告诉它,否则Python不知道。将您的类声明更改为:

Because Trie is supposed to inherit Node, but Python does not know that unless you tell it. Change your class statement to:
class Trie(Node):



此外,在将来发布带行号的错误消息时,请在源代码中注明要引用的行。在上面的代码中尝试倒计数到68是非常繁琐的。


Also in future, when posting error messages with line numbers, please indicate in your source code which lines are being referenced. Trying to count down to 68 in the above code is extremely tedious.


这篇关于我如何解决这个Python问题(trie数据结构实现)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆