binary_heap.py
def insert(self, item, key):
        """
        Inserts the item with the given key.

        >>> heap = BinaryHeap()
        >>> heap.insert("cat", 4)
        >>> heap.insert("dog", 1)
        >>> heap.insert("pig", 2)
        >>> heap.min() == ("dog", 1)
        True
        >>> heap.insert("bear", 0)
        >>> heap.min() == ("bear", 0)
        True
        """

        index = len(self.nodes) # invariant: will be the index of the new item
        self.nodes.append((item, key))

        while index > 0:
            parent = self._parent(index) # get parent index
            if self.nodes[parent][1] > key:
                self.nodes[parent], self.nodes[index] = \
                  self.nodes[index], self.nodes[parent] # multiline expression
            index = parent

        Inserts the item with the given key.

        >>> heap = BinaryHeap()
        >>> heap.insert("cat", 4)
        >>> heap.insert("dog", 1)
        >>> heap.insert("pig", 2)
        >>> heap.min() == ("dog", 1)
        True
        >>> heap.insert("bear", 0)
        >>> heap.min() == ("bear", 0)
        True
        
Inserts the item with the given key.
Inserts the item with the given key .
def insert(self, item, key):
        index = len(self.nodes) 
        self.nodes.append((item, key))
        while index > 0:
            parent = self._parent(index) 
            if self.nodes[parent][1] > key:
                self.nodes[parent], self.nodes[index] = \
                  self.nodes[index], self.nodes[parent] 
            index = parent
def insert ( self , item , key ) : index = len ( self . nodes ) self . nodes . append ( ( item , key ) ) while index > 0 : parent = self . _parent ( index ) if self . nodes [ parent ] [ 1 ] > key : self . nodes [ parent ] , self . nodes [ index ] = self . nodes [ index ] , self . nodes [ parent ] index = parent
Inserts the item with the given key.
Inserts the item with the given key .
"""
        Inserts the item with the given key.

        >>> heap = BinaryHeap()
        >>> heap.insert("cat", 4)
        >>> heap.insert("dog", 1)
        >>> heap.insert("pig", 2)
        >>> heap.min() == ("dog", 1)
        True
        >>> heap.insert("bear", 0)
        >>> heap.min() == ("bear", 0)
        True
        """ # invariant: will be the index of the new item # get parent index # multiline expression
self
item
key