How to implement a Median-heap

后端 未结 6 1214
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 07:50

Like a Max-heap and Min-heap, I want to implement a Median-heap to keep track of the median of a given set of integers. The API should have the following three functions:

6条回答
  •  时光取名叫无心
    2020-12-04 08:28

    Isn't a perfectly balanced binary search tree (BST) a median heap? It is true that even red-black BSTs aren't always perfectly balanced, but it might be close enough for your purposes. And log(n) performance is guaranteed!

    AVL trees are more tighly balanced than red-black BSTs so they come even closer to being a true median heap.

提交回复
热议问题