binary_heap.py
def popmin(self):
        """
        Pop and return the item/key pair with minimum key.
        Assumes the heap is not empty.

        Returns the pair as a tuple.

        >>> heap = BinaryHeap()
        >>> heap.insert('A', 4)
        >>> heap.insert('B', 5)
        >>> heap.insert('C', 3)
        >>> heap.popmin()
        ('C', 3)
        >>> heap.popmin()
        ('A', 4)
        >>> heap.insert('D', 1)
        >>> heap.popmin()
        ('D', 1)
        >>> heap.popmin()
        ('B', 5)
        >>> len(heap)
        0
        """

        min_item = self.min()

        if len(self) == 1:
            # it would still work without this, but
            # let's add it for good style
            self.nodes.pop()
            return min_item

        # move the last item to the root
        self.nodes[0] = self.nodes[-1]
        self.nodes.pop()

        index = 0

        while True:
            lc, rc = self._lchild(index), self._rchild(index)
            if lc >= len(self):
                return min_item

            # get the index of the child with minimum key
            if rc >= len(self) or self.nodes[lc][1] <= self.nodes[rc][1]:
                min_child = lc
            else:
                min_child = rc

            # check if there really is a heap property violation
            # if not, quit
            if self.nodes[index][1] <= self.nodes[min_child][1]:
                return min_item

            # swap the current vertex with the min_child vertex
            self.nodes[index], self.nodes[min_child] = \
              self.nodes[min_child], self.nodes[index]

            index = min_child

        Pop and return the item/key pair with minimum key.
        Assumes the heap is not empty.

        Returns the pair as a tuple.

        >>> heap = BinaryHeap()
        >>> heap.insert('A', 4)
        >>> heap.insert('B', 5)
        >>> heap.insert('C', 3)
        >>> heap.popmin()
        ('C', 3)
        >>> heap.popmin()
        ('A', 4)
        >>> heap.insert('D', 1)
        >>> heap.popmin()
        ('D', 1)
        >>> heap.popmin()
        ('B', 5)
        >>> len(heap)
        0
        
Pop and return the item/key pair with minimum key.
Assumes the heap is not empty.

Returns the pair as a tuple.


Pop and return the item / key pair with minimum key . Assumes the heap is not empty . Returns the pair as a tuple .
def popmin(self):
        min_item = self.min()
        if len(self) == 1:
            self.nodes.pop()
            return min_item
        self.nodes[0] = self.nodes[-1]
        self.nodes.pop()
        index = 0
        while True:
            lc, rc = self._lchild(index), self._rchild(index)
            if lc >= len(self):
                return min_item
            if rc >= len(self) or self.nodes[lc][1] <= self.nodes[rc][1]:
                min_child = lc
            else:
                min_child = rc
            if self.nodes[index][1] <= self.nodes[min_child][1]:
                return min_item
            self.nodes[index], self.nodes[min_child] = \
              self.nodes[min_child], self.nodes[index]
            index = min_child
def popmin ( self ) : min_item = self . min ( ) if len ( self ) == 1 : self . nodes . pop ( ) return min_item self . nodes [ 0 ] = self . nodes [ - 1 ] self . nodes . pop ( ) index = 0 while True : lc , rc = self . _lchild ( index ) , self . _rchild ( index ) if lc >= len ( self ) : return min_item if rc >= len ( self ) or self . nodes [ lc ] [ 1 ] <= self . nodes [ rc ] [ 1 ] : min_child = lc else : min_child = rc if self . nodes [ index ] [ 1 ] <= self . nodes [ min_child ] [ 1 ] : return min_item self . nodes [ index ] , self . nodes [ min_child ] = self . nodes [ min_child ] , self . nodes [ index ] index = min_child
Pop and return the item/key pair with minimum key.
Pop and return the item / key pair with minimum key .
"""
        Pop and return the item/key pair with minimum key.
        Assumes the heap is not empty.

        Returns the pair as a tuple.

        >>> heap = BinaryHeap()
        >>> heap.insert('A', 4)
        >>> heap.insert('B', 5)
        >>> heap.insert('C', 3)
        >>> heap.popmin()
        ('C', 3)
        >>> heap.popmin()
        ('A', 4)
        >>> heap.insert('D', 1)
        >>> heap.popmin()
        ('D', 1)
        >>> heap.popmin()
        ('B', 5)
        >>> len(heap)
        0
        """ # it would still work without this, but # let's add it for good style # move the last item to the root # get the index of the child with minimum key # check if there really is a heap property violation # if not, quit # swap the current vertex with the min_child vertex
self