Priority Queue under the hood implemented using python

This article is to make sure when you use heap library in the real interview, the interviewer ask you details about it, you won’t fail.

PQ is implemented using binary heap tree.

Binary heap tree is

  1. It’s a complete binary tree.
  2. Could be max heap or min heap. The parent and child follow the relationship in figure.
  3. the tree is represented by array.
parent-child relationship

There’re mainly two methods:

1.Insert using heaplify up.

2. Delete

Both take O(log n) running time.

helper function