In [4]:
class HeapArray:

    def __init__(self):
        self._data = []
        self._size = 0

    def __str__(self):
        return ' '.join([str(i) for i in self._data]) + " Size = "+str(self._size) #try to understand what??

    def is_empty(self):
        return self._size == 0

    def insert(self,e):
        self._data.append(e)
        # Now we need to adjust
        index = self._size
        while index > 0 and self._data[index] < self._data[(index-1)//2]:
            temp = self._data[index]
            self._data[index] = self._data[(index-1)//2]
            self._data[(index-1)//2] = temp
            index = (index - 1)//2
        self._size = self._size + 1
        return True

    def delete_min(self):
        ??? next time
In [2]:
x = 5
y = 7

temp = y
y = x
x = temp
print(x,y)
7 5
In [5]:
h = HeapArray()
In [6]:
h.insert(10)
h.insert(2)
h.insert(4)
h.insert(8)
h.insert(16)
h.insert(1)
Out[6]:
True
In [7]:
print(h)
1 8 2 10 16 4 Size = 6
In [9]:
h2 = HeapArray()
h2.insert(2)
h2.insert(10)
h2.insert(8)
h2.insert(4)
h2.insert(1)
h2.insert(16)
print(h2)
1 2 8 10 4 16 Size = 6
In [ ]: