擅长:python、mysql、java
<p>python中已经实现了一个<a href="https://docs.python.org/3/library/heapq.html" rel="nofollow noreferrer">max/min heap</a>:</p>
<pre class="lang-py prettyprint-override"><code>In [1]: import heapq
...:
...: h = []
...: heapq.heappush(h, 1)
...: heapq.heappush(h, 10)
...: heapq.heappush(h, 3)
...: heapq.heappush(h, 2)
In [2]: h
Out[2]: [1, 2, 3, 10]
In [3]: h[-2:]
Out[3]: [3, 10]
In [4]: h[::-1]
Out[4]: [10, 3, 2, 1]
</code></pre>
<p>如果你真的想实现你自己的版本,也许你可以看看<a href="https://codereview.stackexchange.com/questions/197040/min-max-heap-implementation-in-python">this discussion</a>。还有一个关于实现的问题。还有一个关于复杂性的问题</p>