简体   繁体   English

python针对二叉树检查值,空输出

[英]python check value against binary tree, empty output

Been stuck on this problem now for a long time and its time to get some help. 长期以来一直在这个问题上陷入困境,并且有时间寻求帮助。 Almost everything works. 几乎一切正常。 It read the files and input the data and so on.. The program dont give me any output and something is wrong with the last if statments.. The three last rows is 它读取文件并输入数据,依此类推。。该程序没有给我任何输出,如果if语句,最后一个出现问题。.最后三行是

root1.put(word1)
if root.exists(word1):
    print(word1, end = " ")

It puts in the value in the tree. 它将值放入树中。 and then it will check against the first tree if the value "word1" is already in that tree if that is true it will print out the word, if not go on. 然后它将检查第一棵树,如果该树中已经存在值“ word1”,则为true,它将打印出该词,如果不继续。 This program just give me an empty output. 这个程序只是给我一个空的输出。 Is there any chance someone can see the problem? 有人可以看到问题吗? The other class file looks almost the same just without the 1's to all variables/para.. 对于所有变量/参数,另一个类文件看起来几乎相同,只是没有1。

Class file (root1) 类文件(root1)

class BintreeEN:
    def __init__(self, data1):
        self.left1 = None 
        self.right1 = None 
        self.data1 = data1

    def put(self, data1):
        if data1 < self.data1:
            if self.left1 is None:
                self.left1 = BintreeEN(data1)
            else:
                self.left1.put(data1)
        else:
            if self.right1 is None:
                self.right1 = BintreeEN(data1)
            else:
                self.right1.put(data1)

    def write(self):
        if self.left1: 
            self.left1.write()
        print(self.data1) 
        if self.right1: 
            self.right1.write()

    def exists(self, data1):
        if data1 < self.data1:
            if self.left1 is None:
                return None, None
            return self.left1.exists(data1)
        elif data1 > self.data1:
            if self.right1 is None:
                return None, None
            return self.right1.exists(data1)
        else:
            return self.data1

Program file 程序文件

#first tree
root = Bintree("root")
with open("word3.txt", "r", encoding = "utf-8") as file:
    for row in file:
        word = row.strip()
        checklist = root.exists(word)
        if checklist == word:
            pass
        else:
            root.put(word)
#second tree
root1 = BintreeEN("root1")
with open('engelska.txt','r', encoding = "utf-8") as f:
    for row in f:
        onerow = row.split()
        for rowz in onerow:
            word1 = rowz.strip()
            #HERE IT something thats wrong...
            if root1.exists(word1):
                pass
            else:
                root1.put(word1)
                if root.exists(word1): #Check if value is in the first tree
                    print(word1, end = " ")

Your exists() method returns a True value, always . 您的exists()方法始终返回True值。 Your .put() is never called, as the first if is always true instead: 您的.put()永远不会被调用,因为第一个if始终为true:

if root1.exists(word1):
    pass

Your .exists() method returns (None, None) in case the value does not exist in the tree, and a non-empty tuple is always True: 如果树中不存在该值,并且非空元组始终为 True (None, None)您的.exists()方法将返回(None, None)

>>> if (None, None):
...     print 'A tuple is considered True if not empty'
... 
A tuple is considered True if not empty

Return simply just None instead, not a tuple: 返回None而不是元组:

def exists(self, data1):
    if data1 < self.data1:
        if self.left1 is None:
            return None
        return self.left1.exists(data1)
    elif data1 > self.data1:
        if self.right1 is None:
            return None
        return self.right1.exists(data1)
    else:
        return self.data1

or, by using the short-circuiting nature of and and the fact that return exits the function immediately: 或者,通过使用和的短路特性and return的事实立即退出函数:

def exists(self, data1):
    if data1 < self.data1:
        return self.left1 and self.left1.exists(data1)
    if data1 > self.data1:
        return self.right1 and self.right1.exists(data1)
    return self.data1

With this change your binary tree works fine: 通过此更改,您的二叉树可以正常工作:

>>> tree = BintreeEN('foo')
>>> tree.exists('foo')
'foo'
>>> tree.exists('bar')
>>> tree.put('bar')
>>> tree.exists('bar')
'bar'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM