from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

class BinaryMinHeap(object):
    """Binary Min Heap class."""
    def __init__(self):
        # Put single zero as the 1st element, so that 
        # integer division can be used in later methods.
        self.heap_ls = [0]
        self.current_size = 0

    def _percolate_up(self, i):
        while i // 2 > 0:
            if self.heap_ls[i] < self.heap_ls[i // 2]:
                tmp = self.heap_ls[i // 2]
                self.heap_ls[i // 2] = self.heap_ls[i]
                self.heap_ls[i] = tmp
            i = i // 2

    def insert(self, new_node):
        self.heap_ls.append(new_node)
        self.current_size += 1
        self._percolate_up(self.current_size)

    def _get_min_child(self, i):
        if (i * 2 + 1) > self.current_size:
            return i * 2
        else:
            if self.heap_ls[i * 2] < self.heap_ls[i * 2 + 1]:
                return i * 2
            else:
                return i * 2 + 1

    def _percolate_down(self, i):
        while (i * 2) <= self.current_size:
            min_child = self._get_min_child(i)
            if self.heap_ls[i] > self.heap_ls[min_child]:
                tmp = self.heap_ls[i]
                self.heap_ls[i] = self.heap_ls[min_child]
                self.heap_ls[min_child] = tmp
            else:
                pass
            i = min_child

    def find_min(self):
        return self.heap_ls[1]
    
    def delete_min(self):
        val_del = self.heap_ls[1]
        self.heap_ls[1] = self.heap_ls[self.current_size]
        self.current_size -= 1
        self.heap_ls.pop()
        self._percolate_down(1)
        return val_del

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

    def size(self):
        return self.current_size

    def __contains__(self, node):
        return node in self.heap_ls

    def build_heap(self, a_list):
        self.current_size = len(a_list)
        self.heap_ls = [0] + a_list[:]
        i = len(a_list) // 2
        while i > 0:
            self._percolate_down(i)
            i -= 1


class BinaryMaxHeap(object):
    """BinaryMaxHeap class."""
    def __init__(self):
        pass
        

def main():
    bh_min = BinaryMinHeap()
    bh_min.insert(5)
    print('Heap: {}'.format(bh_min.heap_ls))    
    bh_min.insert(9)
    print('Heap: {}'.format(bh_min.heap_ls))
    bh_min.insert(11)
    print('Heap: {}'.format(bh_min.heap_ls))
    bh_min.insert(14)
    print('Heap: {}'.format(bh_min.heap_ls))
    bh_min.insert(18)
    print('Heap: {}'.format(bh_min.heap_ls))
    bh_min.insert(19)
    print('Heap: {}'.format(bh_min.heap_ls))
    bh_min.insert(21)
    print('Heap: {}'.format(bh_min.heap_ls))
    bh_min.insert(33)
    print('Heap: {}'.format(bh_min.heap_ls))
    bh_min.insert(17)
    print('Heap: {}'.format(bh_min.heap_ls))
    bh_min.insert(27)
    print('Heap: {}'.format(bh_min.heap_ls))

    print('Find min: {}'.format(bh_min.find_min()))
    print('Is empty? {}'.format(bh_min.is_empty()))
    print('Size? {}'.format(bh_min.size()))

    bh_min.insert(7)
    print('Heap: {}'.format(bh_min.heap_ls))

    print(bh_min.delete_min())
    print('Heap: {}'.format(bh_min.heap_ls))

    bh_min = BinaryMinHeap()
    bh_min.build_heap([9, 6, 5, 2, 3])
    print('Heap: {}'.format(bh_min.heap_ls))

    print('9 in bh_min: {}'.format(9 in bh_min))
    print('1 in bh_min: {}'.format(1 in bh_min))


if __name__ == '__main__':
    main()


@@ -2302,36 +2302,40 @@
 t(5)%0A    print('
-Heap
+Insert 5
 : %7B%7D'.format(bh_
@@ -2376,36 +2376,40 @@
 t(9)%0A    print('
-Heap
+Insert 9
 : %7B%7D'.format(bh_
@@ -2447,36 +2447,41 @@
 (11)%0A    print('
-Heap
+Insert 11
 : %7B%7D'.format(bh_
@@ -2519,36 +2519,41 @@
 (14)%0A    print('
-Heap
+Insert 14
 : %7B%7D'.format(bh_
@@ -2591,36 +2591,41 @@
 (18)%0A    print('
-Heap
+Insert 18
 : %7B%7D'.format(bh_
@@ -2663,36 +2663,41 @@
 (19)%0A    print('
-Heap
+Insert 19
 : %7B%7D'.format(bh_
@@ -2735,36 +2735,41 @@
 (21)%0A    print('
-Heap
+Insert 21
 : %7B%7D'.format(bh_
@@ -2807,36 +2807,41 @@
 (33)%0A    print('
-Heap
+Insert 33
 : %7B%7D'.format(bh_
@@ -2879,37 +2879,41 @@
 (17)%0A    print('
-Heap:
+Insert 17
  %7B%7D'.format(bh_m
@@ -2950,36 +2950,41 @@
 (27)%0A    print('
-Heap
+Insert 17
 : %7B%7D'.format(bh_
@@ -3171,36 +3171,40 @@
 t(7)%0A    print('
-Heap
+Insert 7
 : %7B%7D'.format(bh_
@@ -3228,16 +3228,33 @@
   print(
+'Min: %7B%7D'.format(
 bh_min.d
@@ -3265,16 +3265,17 @@
 e_min())
+)
 %0A    pri
@@ -3270,36 +3270,42 @@
 ()))%0A    print('
-Heap
+Delete min
 : %7B%7D'.format(bh_
@@ -3398,17 +3398,23 @@
  print('
-H
+Build h
 eap: %7B%7D'

Revise main() with print() text