为什么我的二进制树在不同的字节弦上行为不同?

发布于 2025-01-24 08:54:01 字数 1746 浏览 0 评论 0原文

我一直在用Python练习递归,目前正在尝试停止递归到单个字节,而要停止以某个字节大小。在此示例中,我选择2个,因此,在我的代码中,如果要产生的两个潜在的孩子小于2,则不会反复出现,并且只会返回当前节点。它可以与第一个字节字符串一起使用,但接下来的两个失败。为什么会发生这种情况?如何解决?

第1 b的正确输出:停止以3尺寸的递归/创造儿童,因为下一代儿童的孩子至少比1个孩子小。 2nd B的尺寸2

b'\x00\x01\x00\x02\x00\x03'
b'\x00\x01\x00'
b'\x02\x00\x03'

不正确输出:似乎正在递归直到单个字节

b'L_]ju\x87\xd4\x14j\x1b> \xc52'
b'L_]ju\x87\xd4'
b'L_]'
b'ju\x87\xd4'
b'ju'
b'\x87\xd4'
b'\x14j\x1b> \xc52'
b'\x14j\x1b'
b'> \xc52'
b'> '
b'\xc52'
from random import randbytes


class Node:
    def __init__(self, value):
        self.value = value
        self.children = []
        self.parent = None
        self.bytesize = len(value)
  
    def make_children(self, child):
        child.parent = self
        self.children.append(child)

    def print_tree(self):
        print(self.value)
        if len(self.children) > 0: # leaf node case
            for child in self.children:
                child.print_tree()

def build_tree(value_list):
    root = Node(value_list)
    #if len(value_list) == 1:
    if len(value_list) / 2 < 2: # MODIFY TO STOP RECURSING IF SIZE OF CHILDREN WILL BE BELOW 2
        return root
        
    mid_point = len(value_list) // 2
    left_half = value_list[:mid_point]
    right_half = value_list[mid_point:]

    child1 = build_tree(left_half)
    root.make_children(child1)

    child2 = build_tree(right_half)
    root.make_children(child2)

    return root


if __name__ == '__main__':
    #list1 = [12, 7, 8, 15, 9]
    b = b'\x00\x01\x00\x02\x00\x03'
    #b = b'\x4c\x5f\x5d\x6a\x75\x87\xd4\x14\x6a\x1b\x3e\x20\xc5\x32'
    #b = randbytes(6)
    file = build_tree(b)
    file.print_tree()
    print(len(b))

I have been practicing recursion with python and currently am attempting to stop recursing all the way down to single bytes and instead stop at a certain byte size. In this example I choose 2, so in my code if a either of the potential children to be spawned is less than 2, it won't recurse and will just return the current node. It works fine with the first byte string, but fails with the next two. Why is this happening and how can I fix it?

Correct output for 1st b: stops recursing/creating children at size 3, because next generation of children have at least 1 child smaller than
size 2

b'\x00\x01\x00\x02\x00\x03'
b'\x00\x01\x00'
b'\x02\x00\x03'

Incorrect output for 2nd b: Appears to be recursing until single bytes

b'L_]ju\x87\xd4\x14j\x1b> \xc52'
b'L_]ju\x87\xd4'
b'L_]'
b'ju\x87\xd4'
b'ju'
b'\x87\xd4'
b'\x14j\x1b> \xc52'
b'\x14j\x1b'
b'> \xc52'
b'> '
b'\xc52'
from random import randbytes


class Node:
    def __init__(self, value):
        self.value = value
        self.children = []
        self.parent = None
        self.bytesize = len(value)
  
    def make_children(self, child):
        child.parent = self
        self.children.append(child)

    def print_tree(self):
        print(self.value)
        if len(self.children) > 0: # leaf node case
            for child in self.children:
                child.print_tree()

def build_tree(value_list):
    root = Node(value_list)
    #if len(value_list) == 1:
    if len(value_list) / 2 < 2: # MODIFY TO STOP RECURSING IF SIZE OF CHILDREN WILL BE BELOW 2
        return root
        
    mid_point = len(value_list) // 2
    left_half = value_list[:mid_point]
    right_half = value_list[mid_point:]

    child1 = build_tree(left_half)
    root.make_children(child1)

    child2 = build_tree(right_half)
    root.make_children(child2)

    return root


if __name__ == '__main__':
    #list1 = [12, 7, 8, 15, 9]
    b = b'\x00\x01\x00\x02\x00\x03'
    #b = b'\x4c\x5f\x5d\x6a\x75\x87\xd4\x14\x6a\x1b\x3e\x20\xc5\x32'
    #b = randbytes(6)
    file = build_tree(b)
    file.print_tree()
    print(len(b))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清旖 2025-01-31 08:54:01

您的代码实际上是按预期工作的。您提到的两个字符字符串都有2个字节,而不是1个字符。

这是一种显示可能使其更清楚的字节词的方法:

def print_string(s):
    print(' '.join(map('{:#2x}'.format, s)))

print_string(b'> ')
# 0x3e 0x20

print_string(b'\xc52')
# 0xc5 0x32

Your code is actually working as intended. The two byte strings you mention both have 2 bytes, not 1.

Here is one way to display a bytestring that might make it more clear:

def print_string(s):
    print(' '.join(map('{:#2x}'.format, s)))

print_string(b'> ')
# 0x3e 0x20

print_string(b'\xc52')
# 0xc5 0x32
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文