size = len(numbers)
tree = [None] * size * 3
def build_tree(numbers, root, node, element):
if element != 0 and element != size:
if tree[node] < numbers[element]:
if tree[node + 2] is None:
tree[root] = numbers[element]
tree[node + 2] = root
build_tree(numbers, root + 3, 0, element + 1)
else:
build_tree(numbers, root, tree[node + 2], element)
elif tree[node] > numbers[element]:
if tree[node + 1] is None:
tree[root] = numbers[element]
tree[node + 1] = root
build_tree(numbers, root + 3, 0, element + 1)
else:
build_tree(numbers, root, tree[node + 1], element)
elif element == 0:
tree[root] = numbers[element]
build_tree(numbers, root + 3, 0, element + 1)
build_tree(numbers, 0, 0, 0)
I wrote this function for an assignment I had and it works as intended, but only for up to about 300 integers. After that it crashes and I haven't been able to figure out why. Same thing happens for my C implementation of the very same code.
The function builds a binary search tree, the values are taken from the numbers array.
tree[0] > value of node
tree[1] > index of right child
tree[2] > index of left child
to give an example,
tree[tree[2]]
would give us the value of left child of tree[0].
Here's the result:
Process finished with exit code -1073741571 (0xC00000FD)
User contributions licensed under CC BY-SA 3.0