来自集合的Python双端队列不是顺序确定的;重复执行会产生不同的结果 - python

from collections import deque

class Node(object):
    def __init__(self,val):
        self.value = val
        self.right = None
        self.left = None

class Btree:
    def __init__(self):
        self.root = None

    def btree_print(self):
        if self.root:
            print("Values of btree are")
            q = deque()
            q.append(self.root)
            q.append(None)

            while q:
                parent = q.popleft()
                if parent is None:
                    print("End of  a Level")
                    if q:
                        q.append(None)
                    continue

                print("Value of btree Elm:", parent.value)

                for child in {parent.left, parent.right}:
                    if child:
                        q.append(child)

        else:
            print("Btree Empty")

    def btree_insert_helper(self, val):
        q = deque()
        q.append(self.root)

        while q:
            node = q.popleft()
            for child in {node.left, node.right}:
                if child:
                    q.append(child)
                elif node.left:
                    node.right = Node(val)
                    return
                else:
                    node.left = Node(val)
                    return

    def btree_insert(self, val):
        if self.root:
            self.btree_insert_helper(val)
        else:
            self.root = Node(val)

btree = Btree()
btree.btree_insert(1)
btree.btree_insert(2)
btree.btree_insert(3)
btree.btree_insert(4)
btree.btree_insert(5)
btree.btree_insert(6)
btree.btree_insert(7)

btree.btree_print()

在上面的程序中,如果我多次执行它,将会给我带来不同的结果。我看到2个不同的订单。无论逻辑如何,结果都应始终与其单线程程序相同。
我不确定为什么要更改订单。

预期结果,订单1:

Values of btree are
Value of btree Elm: 1
End of  a Level
Value of btree Elm: 2
Value of btree Elm: 3
End of  a Level
Value of btree Elm: 4
Value of btree Elm: 5
Value of btree Elm: 6
Value of btree Elm: 7
End of  a Level

没想到,但有时程序会给出以下结果(4、5翻转为5、4),Order2:

Values of btree are
Value of btree Elm: 1
End of  a Level
Value of btree Elm: 2
Value of btree Elm: 3
End of  a Level
Value of btree Elm: 5
Value of btree Elm: 4
Value of btree Elm: 6
Value of btree Elm: 7
End of  a Level

任何帮助将不胜感激

python大神给出的解决方案

二叉树本身是一致的。您的打印顺序不一致的原因来自以下行:

for child in {parent.left, parent.right}:

set{parent.left, parent.right}上进行迭代。集合没有顺序,因此您可以从左向右或从右向左进行迭代。

更改为:

for child in (parent.left, parent.right):

将使其始终如一地打印。

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

如何打印浮点数的全精度[Python] - python

我编写了以下函数,其中传递了x,y的值:def check(x, y): print(type(x)) print(type(y)) print(x) print(y) if x == y: print "Yes" 现在当我打电话check(1.00000000000000001, 1.0000000000000002)它正在打印:<…

Python:检查新文件是否在文件夹中[重复] - python

This question already has answers here: How do I watch a file for changes? (23个答案) 3年前关闭。 我是python的新手,但是我尝试创建一个自动化过程,其中我的代码将侦听目录中的新文件条目。例如,某人可以手动将zip文件复制到一个特定的文件夹中,并且我希望我的代码能够在文件完全…