@@ -0,0 +1,2226 @@
+from __future__ import absolute_import%0Afrom __future__ import division%0Afrom __future__ import print_function%0A%0Aimport numpy as np%0A%0A%0Adef parent(i):%0A    return i // 2%0A%0Adef left(i):%0A    return 2 * i%0A%0Adef right(i):%0A    return 2 * i + 1%0A%0A%0Aclass MaxPriorityQueue(object):%0A    %22%22%22Max Priority Queue.%22%22%22%0A    def __init__(self):%0A        self.heap_ls = %5B0%5D%0A        self.heap_size = 0%0A%0A    def show(self):%0A        print(self.heap_ls)%0A%0A    def max_heapify(self, i):%0A        l = left(i)%0A        r = right(i)%0A        if l %3C= self.heap_size and self.heap_ls%5Bl%5D %3E self.heap_ls%5Bi%5D:%0A            max_i = l%0A        else:%0A            max_i = i%0A        if r %3C= self.heap_size and self.heap_ls%5Br%5D %3E self.heap_ls%5Bmax_i%5D:%0A            max_i = r%0A        if max_i != i:%0A            self.heap_ls%5Bi%5D, self.heap_ls%5Bmax_i%5D = (%0A                self.heap_ls%5Bmax_i%5D, self.heap_ls%5Bi%5D)%0A            self.max_heapify(max_i)%0A%0A    def find_max(self):%0A        return self.heap_ls%5B1%5D%0A%0A    def extract_max(self):%0A        if self.heap_size %3C 1:%0A            raise ValueError('Heap underflow.')%0A        maximum = self.heap_ls%5B1%5D%0A        self.heap_ls%5B1%5D = self.heap_ls.pop()%0A        self.heap_size -= 1%0A        self.max_heapify(1)%0A        return maximum%0A%0A    def increase_key(self, i, key):%0A        if key %3C self.heap_ls%5Bi%5D:%0A            raise ValueError('New key is smaller than current key.')%0A        self.heap_ls%5Bi%5D = key%0A        while i %3E 1 and self.heap_ls%5Bparent(i)%5D %3C self.heap_ls%5Bi%5D:%0A            self.heap_ls%5Bi%5D, self.heap_ls%5Bparent(i)%5D = (%0A                self.heap_ls%5Bparent(i)%5D, self.heap_ls%5Bi%5D)%0A            i = parent(i)%0A%0A    def insert(self, key):%0A        self.heap_size += 1%0A        self.heap_ls.append(-np.inf)%0A        self.increase_key(self.heap_size, key)%0A%0A%0Adef main():%0A    max_pq = MaxPriorityQueue()%0A%0A    print('Insert sequentially 5, 7, 3, 1')%0A    max_pq.insert(5)%0A    max_pq.insert(7)%0A    max_pq.insert(3)%0A    max_pq.insert(1)%0A    max_pq.show()%0A%0A    print('Increase key 1 at position 4 to 6.')%0A    max_pq.increase_key(4, 6)%0A    max_pq.show()%0A%0A    print('Find max:')%0A    print(max_pq.find_max())%0A%0A    print('Extract max:')%0A    _max = max_pq.extract_max()%0A    print(_max)%0A    print('The remaining:')%0A    max_pq.show()%0A%0Aif __name__ == '__main__':%0A    main()%0A

Add ds_max_priority_queue_tuple.py