0

I am working on a heap structure.

Whenever I added 10 or more nodes to the tree, for some reason the data switches from a node to a None type.

def _FindLast(self,root,index,node_count):

    if root is None or index>= node_count:
        # print(f"this is the temp in none {type(root)}")
        return None
    if (2*index+1 == node_count-1):
        # print("return left")
        print(f"this is the type from left {type(root.left)}")
        temp = root.left
        root.left = None
        # print(f"this is the temp type{type(temp)}")
        return temp
    if (2*index+2 == node_count-1):
        # print("return right")
        print(f"this is the type from right {type(root.right)}")
        temp = root.right
        root.right = None
        return temp
    left_results = self._FindLast(root.left,2*index+1, node_count)
    # print(f"the type from {type(left_results)}")
    if (left_results is not None):
        # print("yes")
        # print(type(left_results))
        return left_results
    return self._FindLast(root.right,2*index+2,node_count)

I really don't get why I have added the method above.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • 3
    What is the function supposed to do? Why does it *change* the tree by setting either `.left` or `.right` to `None` when the name if the function is `_findLast`? One would not expect a "finding" function to change anything to the data structure... Why does your tree have nodes? A binary heap is best implemented in a list -- which is where the given index will do a lookup... – trincot Jul 23 '21 at 07:14
  • 3
    You seem to mix up the generic binary tree data structure with the specific, list-based, heap data structure. Without more context (what is this function supposed to do?), example input, and code that actually calls this function, it is impossible to answer this question. – trincot Jul 23 '21 at 09:23

0 Answers0