a maxheap implementation in python binaryheap binaryheap binaryheap insert6 binaryheap insert10 binaryheap insert15 binaryheap insert12 binaryheap pop 15 binaryheap pop 12 binaryheap getlist 10 6 lenbinaryheap 2 swap the element up temporary self heapi while i 2 0 if self heapi self heapi 2 self heapi self heapi 2 self heapi 2 temporary i 2 def insertself value int none swap the element down while self size 2 i if 2 i 1 self size biggerchild 2 i else if self heap2 i self heap2 i 1 biggerchild 2 i else biggerchild 2 i 1 temporary self heapi if self heapi self heapbiggerchild self heapi self heapbiggerchild self heapbiggerchild temporary i biggerchild def popself int length of the array return self size if name main import doctest doctest testmod create an instance of binaryheap binaryheap binaryheap binaryheap insert6 binaryheap insert10 binaryheap insert15 binaryheap insert12 pop rootmaxvalues because it is max heap printbinaryheap pop 15 printbinaryheap pop 12 get the list and size after operations printbinaryheap getlist printlenbinaryheap a max heap implementation in python binary_heap binaryheap binary_heap insert 6 binary_heap insert 10 binary_heap insert 15 binary_heap insert 12 binary_heap pop 15 binary_heap pop 12 binary_heap get_list 10 6 len binary_heap 2 swap the element up insert new element swap the element down pop the root element length of the array create an instance of binaryheap pop root max values because it is max heap 15 12 get the list and size after operations
class BinaryHeap:
    

    def __init__(self):
        self.__heap = [0]
        self.__size = 0

    def __swap_up(self, i: int) -> None:
        
        temporary = self.__heap[i]
        while i // 2 > 0:
            if self.__heap[i] > self.__heap[i // 2]:
                self.__heap[i] = self.__heap[i // 2]
                self.__heap[i // 2] = temporary
            i //= 2

    def insert(self, value: int) -> None:
        
        self.__heap.append(value)
        self.__size += 1
        self.__swap_up(self.__size)

    def __swap_down(self, i: int) -> None:
        
        while self.__size >= 2 * i:
            if 2 * i + 1 > self.__size:
                bigger_child = 2 * i
            else:
                if self.__heap[2 * i] > self.__heap[2 * i + 1]:
                    bigger_child = 2 * i
                else:
                    bigger_child = 2 * i + 1
            temporary = self.__heap[i]
            if self.__heap[i] < self.__heap[bigger_child]:
                self.__heap[i] = self.__heap[bigger_child]
                self.__heap[bigger_child] = temporary
            i = bigger_child

    def pop(self) -> int:
        
        max_value = self.__heap[1]
        self.__heap[1] = self.__heap[self.__size]
        self.__size -= 1
        self.__heap.pop()
        self.__swap_down(1)
        return max_value

    @property
    def get_list(self):
        return self.__heap[1:]

    def __len__(self):
        
        return self.__size


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    
    binary_heap = BinaryHeap()
    binary_heap.insert(6)
    binary_heap.insert(10)
    binary_heap.insert(15)
    binary_heap.insert(12)
    
    print(binary_heap.pop())  
    print(binary_heap.pop())  
    
    print(binary_heap.get_list)
    print(len(binary_heap))