5/40
Part 2 of 8 · The heapLearn and do
Push: add at the end, then swap up while smaller than the parent
With the tree stored in a list, a push is a short loop. Put the new item at the end of the list. Then, while it's smaller than its parent at (i - 1) // 2, swap the two and move up to the parent's index. Stop at the root, or as soon as the item isn't smaller. Push 10 onto h and print it:
h = [7, 12, 18, 30, 21, 45, 60]
push(h, 10)
print(h)
push(h, 10) on [7, 12, 18, 30, 21, 45, 60]. At which index does 10 end up?
Pick one to see what it prints.
Pick an answer above to see the rest