Home > other >  Why does my binary tree behave differently given different byte strings?
Why does my binary tree behave differently given different byte strings?

Time:04-28

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))

CodePudding user response:

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'\xc52')
# 0x3e 0x20

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