简体   繁体   English

(Python)从二叉树交替输入?

[英](Python) Alternating inputs from a binary tree?

So I've just begun the meat and potatoes of my degree and I'm learning Python.所以我刚刚开始我的学位课程,我正在学习 Python。 One of our assignments this week was to use a binary tree and add guests, allowing the tree to alternate left and right, then print the names of left and right aisle guests.本周我们的一项任务是使用二叉树并添加客人,让树左右交替,然后打印左右过道客人的姓名。

Our professor gave us some sample code to work with, and I made a few changes to have it do what I wanted to do with it.我们的教授给了我们一些示例代码,我做了一些更改,让它做我想做的事情。 Here's what I have:这是我所拥有的:

guestnumber = 0
class Node:
    def __init__(self):
        self.left = None
        self.right = None
        self.data = list()

def guestadd(root, Guest):
    if Guest <= root.data[0]:
        if root.left == None:
            root.left = Node()
            root.left.data.append(Guest)
        else:
            guestadd(root.left, Guest)
    else:
        if Guest >= root.data[0]:
            if root.right == None:
                root.right = Node()
                root.right.data.append(Guest)
            else:
                guestadd(root.right, Guest)

def printlist(root):
    if root == None:
        return
    print(root.data)
    printlist(root.left)
    printlist(root.right)

print("Enter guest names for seating arrangements. (Max = 50)")
guestnumber = int(input("How many guests are attending?"))
root = Node()
root.data.append("Guest")
for i in range (0,guestnumber):
    guestadd (root, input("Name:"))

print("Left Aisle:")
printlist(root.left)
print("Right Aisle:")
printlist(root.right)

At first glance, everything works as intended, but I noticed I was given inconsistent results where it didn't split the names evenly.乍一看,一切都按预期工作,但我注意到我得到了不一致的结果,因为它没有均匀地分割名称。 After sleeping on it, I did some more troubleshooting and discovered that, no matter what order I put the data in, it would always put 0-9, AG, and a handful of special characters on the left node, and HZ, az, and another handful of special characters on the right side.睡了之后,我又做了一些故障排除,发现不管我把数据放在什么顺序,它总是会在左边的节点上放 0-9、AG 和少数特殊字符,还有 HZ、az、右侧还有一些特殊字符。

I've already handed in what I have, along with several screenshots of my discoveries, so I'm sure I'll be given full credit, given that's it's an introductory course.我已经提交了我所拥有的以及我的发现的几张屏幕截图,所以我相信我会得到充分的信任,因为这是一门入门课程。 But it's bugging me that I can't get get it to do what I want consistently.但是让我烦恼的是我无法让它始终如一地做我想做的事。

My best guess is that it has something to do with where the characters lie on an ASCII chart.我最好的猜测是它与字符在 ASCII 图表上的位置有关。 Everything from 0-71 was on the left, and 72-127 on the right. 0-71 的所有东西都在左边,72-127 都在右边。 How would I go about getting my program to treat the strings as just that and not thinking of it in ASCII?我将如何让我的程序将字符串视为这样,而不是在 ASCII 中考虑它?

NOTE: If I was given this task to separate guests, I wouldn't use a tree.注意:如果给我这个任务来分隔客人,我不会使用树。 I'd probably use something that just adds names to an list, then use a [::2] and [1::2] slice to alternate sides.我可能会使用一些只是将名称添加到列表中的东西,然后使用 [::2] 和 [1::2] 切片来交替边。 However, since the assignment was to learn how to use a binary tree, I wanted to stay with the constraints of the lesson然而,由于作业是学习如何使用二叉树,我想留在课程的约束下

You have nothing in your code about alternating left and right.您的代码中没有关于左右交替的内容。 You've built a binary search tree with "Guest" at the root.您已经构建了一个以“Guest”为根的二叉搜索树。 Anyone whose name comes before "Guest" in Unicode-lexicographic order goes on the left, and anyone whose name comes after "Guest" in Unicode-lexicographic order goes on the right.名字在 Unicode 字典顺序中“Guest”之前的任何人都在左边,而名字在 Unicode 字典顺序中“Guest”之后的任何人都在右边。

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

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