@@ -0,0 +1,2345 @@
+import random%0Aimport math%0A%0A%0Aclass Heap:%0A    def __init__(self):%0A        self.data = %5B%5D%0A%0A    def insert(self, value):%0A        self.data.append(value)%0A        self.heapify_up()%0A%0A    def heapify_up(self):%0A        i = len(self.data) - 1%0A        while i %3E 0:%0A            print(self)%0A            parent = (i - 1) // 2%0A            if self.compare(i, parent):%0A                self.swap(i, parent)%0A                i = parent%0A            else:%0A                break%0A%0A    def peek(self):%0A        return self.data%5B0%5D%0A%0A    def pop(self):%0A        value = self.data.pop(0)%0A        if len(self.data):%0A            self.data.insert(0, self.data.pop())%0A        self.heapify_down()%0A        return value%0A%0A    def heapify_down(self):%0A        i = 0%0A        while i %3C len(self.data):%0A            print(self)%0A            left = 2 * i + 1%0A            right = 2 * i + 2%0A            if not self.compare(i, left) or not self.compare(i, right):%0A                to_swap = left%0A                if not self.compare(left, right):%0A                    to_swap = right%0A                self.swap(i, to_swap)%0A                i = to_swap%0A            else:%0A                break%0A%0A    def swap(self, a, b):%0A        self.data%5Ba%5D, self.data%5Bb%5D = self.data%5Bb%5D, self.data%5Ba%5D%0A%0A    def compare(self, a, b):%0A        if a %3E= len(self.data) or b %3E= len(self.data):%0A            return True%0A        return self.data%5Ba%5D %3C self.data%5Bb%5D%0A%0A    def __repr__(self):%0A        tree = ''%0A        width = math.pow(2, math.floor(math.log2(len(self.data))))%0A        current_width = 1%0A        i = 0%0A        while current_width %3C= width:%0A            offset = '  ' * int((width / current_width) - 1)%0A            spacer = '  ' * int((width / current_width * 2) - 1)%0A            row = offset + spacer.join(str(value).rjust(2) for value in self.data%5Bi:i + current_width%5D)%0A            tree += row + '%5Cn'%0A            i += current_width%0A            current_width *= 2%0A        return tree%0A%0A%0Adef main():%0A    heap = Heap()%0A    for i in range(30):%0A        value = random.randint(0, 99)%0A        print('inserting: %7B%7D'.format(value))%0A        heap.insert(value)%0A%0A    sorted_values = %5B%5D%0A    for i in range(30):%0A        print('------------------------------')%0A        value = heap.pop()%0A        print('popped: %7B%7D'.format(value))%0A        sorted_values.append(value)%0A%0A    print(sorted_values)%0A%0A%0Aif __name__ == '__main__':%0A    main()%0A

Create heap_stuff.py