#!/usr/bin/env python
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division


class Binheap(object):
    ''' Create an empty heap. '''
    def __init__(self, binlist=[]):
        self.binlist = binlist

    def push(self, value):
        ''' Add a value to the heap. '''
        self.binlist.append(value)
        self.__promote(len(self.binlist)-1)
        return

    def __parent(self, index):
        ''' Find the parents of a bin. '''
        return [(index-1)//2, self.binlist[(index-1)//2]]

    def __children(self, index):
        ''' Find the children of a bin. '''
        children = []
        try:
            child1 = (2 * index + 1, self.binlist[2*index+1])
        except IndexError:
            pass
        else:
            children.append(child1)

        try:
            child2 = (2*index+2, self.binlist[2*index+2])
        except IndexError:
            pass
        else:
            children.append(child2)

        return children

    def __swap(self, index1, index2):
        ''' Swap two bins. '''
        self.binlist[index1], self.binlist[index2] = self.binlist[index2], self.binlist[index1]
        return

    def pop(self):
        ''' Pop the top of the heap. '''
        last_index = len(self.binlist)-1
        self.__swap(0, last_index)
        top = self.binlist.pop()
        self.__demote()
        return top

    def __promote(self, index):
        ''' Promote a bin. '''
        child = self.binlist[index]
        parent = self.__parent(index)
        if parent[0] >= 0:
            if parent[1] < child:
                self.__swap(parent[0], index)
                self.__promote(parent[0])
            else:
                return

    def __demote(self, index=0):
        children = self.__children(index)
        ''' Demote a bin. '''
        if len(children):
            victor_index = self.__battle_children(index, children)

            if victor_index:
                self.__swap(victor_index, index)
                self.__demote(victor_index)
        else:
            return

    def __battle_children(self, index, children):
        ''' Compare children bins. '''
        if len(children) == 2:

            if children[0][1] >= children[1][1]:
                return children[0][0]
            else:
                return children[1][0]

        elif len(children) == 1:
            return children[0][0]
        else:
            return None

@@ -612,37 +612,61 @@
 ildren of a bin.
+ Build list of children.
  '''%0A
-
         children
@@ -1590,16 +1590,80 @@
 %5D %3E= 0:%0A
+            # If is true only when parent index is non-negative%0A
         
@@ -2293,17 +2293,50 @@
 ) == 2:%0A
+            # Handle two children
 %0A
-
         
@@ -2504,16 +2504,47 @@
 ) == 1:%0A
+            # Handle one child%0A
         
@@ -2561,32 +2561,32 @@
  children%5B0%5D%5B0%5D%0A
-
         else:%0A  
@@ -2575,32 +2575,65 @@
 %5D%0A        else:%0A
+            # Handle no children%0A
             retu

add comments.